Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bugfix/issue-1472' into release/…
Browse files Browse the repository at this point in the history
…flotte-prerelease

# Conflicts:
#	tests/php/Wordpress/CustomPostTypeTest.php
  • Loading branch information
hansmorb committed Jan 21, 2024
2 parents a888dd4 + dd3a85a commit 29b30c3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Model/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public function getAdmins() {
$locationAdminIds = [];
}
}
$locationAdminIds[] = get_post_field( 'post_author', $locationId );

return array_unique(
array_map('intval',
Expand Down
10 changes: 10 additions & 0 deletions src/Model/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,16 @@ public function getAdmins(): array {
$itemAdminIds = $item->getAdmins();
}

if ( empty($locationAdminIds) && empty($itemAdminIds) ) {
return [];
}
if ( empty($locationAdminIds) ) {
return $itemAdminIds;
}
if ( empty($itemAdminIds) ) {
return $locationAdminIds;
}

return array_unique( array_merge ($locationAdminIds,$itemAdminIds) );
}

Expand Down
12 changes: 8 additions & 4 deletions tests/php/Model/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ public function testGetBookableTimeframesByLocation() {
*/

public function testGetAdmins() {
// Case: No admins
//$this->assertEquals([], $this->itemModel->getAdmins()); - Currently this function includes the post author
$this->assertEquals([self::USER_ID], $this->itemModel->getAdmins());

$userArray[] = $this->subscriberId;
//Case: CB Manager as admin
$this->createCBManager();
$adminItemModel = new Item(
$this->createItem("Testitem2",'publish', $userArray)
$this->createItem("Testitem2",'publish', [$this->cbManagerUserID])
);
//$this->assertEquals($userArray, $adminItemModel->getAdmins()); - This should work when postAuthor is not appended anymore
$this->assertContains($this->subscriberId, $adminItemModel->getAdmins());
//$this->assertEquals([$this->cbManagerUserID], $adminItemModel->getAdmins()); - Currently this function includes the post author
$this->assertEquals([$this->cbManagerUserID, self::USER_ID], $adminItemModel->getAdmins());
}


Expand Down
13 changes: 9 additions & 4 deletions tests/php/Model/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ public function testGetBookableTimeframesByItem() {
}

public function testGetAdmins() {
$userArray[] = $this->subscriberId;
// Case: No admins
//$this->assertEquals([], $this->locationModel->getAdmins()); - Currently this function includes the post author
$this->assertEquals([self::USER_ID], $this->locationModel->getAdmins());

//Case: CB Manager as admin
$this->createCBManager();
$adminLocationModel = new Location(
$this->createLocation("TestLocation2",'publish', $userArray)
$this->createLocation("TestLocation2",'publish', [$this->cbManagerUserID])
);
//$this->assertEquals($userArray, $adminItemModel->getAdmins()); - This should work when postAuthor is not appended anymore
$this->assertContains($this->subscriberId, $adminLocationModel->getAdmins());
//$this->assertEquals([$this->cbManagerUserID], $adminLocationModel->getAdmins()); - Currently this function includes the post author
$this->assertEquals([$this->cbManagerUserID, self::USER_ID], $adminLocationModel->getAdmins());
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/php/Model/TimeframeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,31 @@ public function testGetGridSize() {
$this->assertEquals( 1, $hourlyBookable->getGridSize() );
}

public function testGetAdmins() {
//Case 1: no admins set
//$this->assertEquals( [], $this->firstTimeframe->getAdmins() ); - The author is currently always included as admin
$this->assertEquals( [self::USER_ID], $this->firstTimeframe->getAdmins() );

//Case 2: Item admin set
$this->createCBManager();
$managedItem = $this->createItem("Managed Item", 'publish', [$this->cbManagerUserID]);
$unmanagedLocation = $this->createLocation("Unmanaged Location", 'publish');
$timeframe = new Timeframe( $this->createBookableTimeFrameIncludingCurrentDay($unmanagedLocation, $managedItem) );
$this->assertEqualsCanonicalizing( [$this->cbManagerUserID, self::USER_ID], $timeframe->getAdmins() );

//Case 3: Location admin set
$managedLocation = $this->createLocation("Managed Location", 'publish', [$this->cbManagerUserID]);
$unmanagedItem = $this->createItem("Unmanaged Item", 'publish');
$timeframe = new Timeframe( $this->createBookableTimeFrameIncludingCurrentDay($managedLocation, $unmanagedItem) );
$this->assertEqualsCanonicalizing( [$this->cbManagerUserID, self::USER_ID], $timeframe->getAdmins() );

//Case 4: Both admins set
$otherManagedLocation = $this->createLocation("Other Managed Location", 'publish', [$this->cbManagerUserID]);
$otherManagedItem = $this->createItem("Other Managed Item", 'publish', [$this->cbManagerUserID]);
$timeframe = new Timeframe( $this->createBookableTimeFrameIncludingCurrentDay($otherManagedLocation, $otherManagedItem) );
$this->assertEqualsCanonicalizing( [$this->cbManagerUserID, self::USER_ID], $timeframe->getAdmins() );
}

protected function setUp() : void {

parent::setUp();
Expand Down
10 changes: 6 additions & 4 deletions tests/php/Wordpress/CustomPostTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,12 @@ protected function createBookableTimeFrameStartingInAWeek($locationId = null, $i
}

// Create Item
protected function createItem($title, $postStatus = 'publish', $admins = []) {
protected function createItem($title, $postStatus = 'publish', $admins = [], $postAuthor = self::USER_ID) {
$itemId = wp_insert_post( [
'post_title' => $title,
'post_type' => Item::$postType,
'post_status' => $postStatus
'post_status' => $postStatus,
'post_author' => $postAuthor
] );

$this->itemIds[] = $itemId;
Expand All @@ -409,11 +410,12 @@ protected function createItem($title, $postStatus = 'publish', $admins = []) {
}

// Create Location
protected function createLocation($title, $postStatus = 'publish', $admins = []) {
protected function createLocation($title, $postStatus = 'publish', $admins = [], $postAuthor = self::USER_ID) {
$locationId = wp_insert_post( [
'post_title' => $title,
'post_type' => Location::$postType,
'post_status' => $postStatus
'post_status' => $postStatus,
'post_author' => $postAuthor
] );

$this->locationIds[] = $locationId;
Expand Down

0 comments on commit 29b30c3

Please sign in to comment.