diff --git a/src/Model/Location.php b/src/Model/Location.php index 4142b2fe1..4ace5c0d7 100644 --- a/src/Model/Location.php +++ b/src/Model/Location.php @@ -235,6 +235,7 @@ public function getAdmins() { $locationAdminIds = []; } } + $locationAdminIds[] = get_post_field( 'post_author', $locationId ); return array_unique( array_map('intval', diff --git a/src/Model/Timeframe.php b/src/Model/Timeframe.php index bbc4bf9d1..865988fa0 100644 --- a/src/Model/Timeframe.php +++ b/src/Model/Timeframe.php @@ -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) ); } diff --git a/tests/php/Model/ItemTest.php b/tests/php/Model/ItemTest.php index fc3bd49f9..6bb9694c4 100644 --- a/tests/php/Model/ItemTest.php +++ b/tests/php/Model/ItemTest.php @@ -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()); } diff --git a/tests/php/Model/LocationTest.php b/tests/php/Model/LocationTest.php index 14dfee426..8af28c5ad 100644 --- a/tests/php/Model/LocationTest.php +++ b/tests/php/Model/LocationTest.php @@ -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()); } /** diff --git a/tests/php/Model/TimeframeTest.php b/tests/php/Model/TimeframeTest.php index 2bec842bc..035cd2d31 100644 --- a/tests/php/Model/TimeframeTest.php +++ b/tests/php/Model/TimeframeTest.php @@ -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(); diff --git a/tests/php/Wordpress/CustomPostTypeTest.php b/tests/php/Wordpress/CustomPostTypeTest.php index 68c344ed1..2c236e682 100644 --- a/tests/php/Wordpress/CustomPostTypeTest.php +++ b/tests/php/Wordpress/CustomPostTypeTest.php @@ -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; @@ -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;