diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 846a60b330..2ee8e6b1d5 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -2747,8 +2747,6 @@ public function isNew() protected function getClassDropdown() { $classes = SiteTree::page_type_classes(); - $currentClass = null; - $result = []; foreach ($classes as $class) { $instance = singleton($class); @@ -2774,20 +2772,15 @@ protected function getClassDropdown() } } - $pageTypeName = $instance->i18n_singular_name(); - - $currentClass = $class; - $result[$class] = $pageTypeName; + $result[$class] = $instance->i18n_singular_name(); } - // sort alphabetically, and put current on top + // Sort alphabetically, and put current on top asort($result); - if ($currentClass) { - $currentPageTypeName = $result[$currentClass]; - unset($result[$currentClass]); - $result = array_reverse($result ?? []); - $result[$currentClass] = $currentPageTypeName; - $result = array_reverse($result ?? []); + if (isset($result[$this->ClassName])) { + $currentPageTypeName = $result[$this->ClassName]; + unset($result[$this->ClassName]); + $result = [$this->ClassName => $currentPageTypeName] + $result; } return $result; diff --git a/tests/php/Model/SiteTreeTest.php b/tests/php/Model/SiteTreeTest.php index e3eec3454d..29476014fb 100644 --- a/tests/php/Model/SiteTreeTest.php +++ b/tests/php/Model/SiteTreeTest.php @@ -1272,7 +1272,7 @@ public function testAllowedChildrenContainsCoreSubclassesButNotHiddenClass() #[DataProvider('allowedChildrenProvider')] public function testAllowedChildren($className, $expected, $assertionMessage) { - $class = new $className; + $class = new $className(); $this->assertEquals($expected, $class->allowedChildren(), $assertionMessage); } @@ -1351,6 +1351,9 @@ public function testAllowedChildrenValidation() ); } + /** + * @return void + */ public function testClassDropdown() { $sitetree = new SiteTree(); @@ -1372,7 +1375,18 @@ public function testClassDropdown() $this->assertArrayNotHasKey(SiteTreeTest_NotRoot::class, $method->invoke($rootPage)); $this->assertArrayHasKey(SiteTreeTest_NotRoot::class, $method->invoke($nonRootPage)); - + foreach ([SiteTreeTest_ClassA::class, SiteTreeTest_ClassB::class] as $className) { + $otherPage = new $className(); + $otherPage->write(); + $result = $method->invoke(object: $otherPage); + $this->assertEquals(array_key_first($result), $className); + // remove the first element as this is not alphabetical + array_shift($result); + // create a sorted array + $resultSorted = $result; + asort($resultSorted); + $this->assertEquals($result, $resultSorted); + } Security::setCurrentUser(null); }