Merge pull request #3004 from sunnysideup/patch-21

FIX: put current page at top of ClassName dropdown (was broken)
This commit is contained in:
Guy Sartorelli 2024-09-18 09:30:05 +12:00 committed by GitHub
commit 24bb95d3bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 15 deletions

View File

@ -2744,8 +2744,6 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
protected function getClassDropdown() protected function getClassDropdown()
{ {
$classes = SiteTree::page_type_classes(); $classes = SiteTree::page_type_classes();
$currentClass = null;
$result = []; $result = [];
foreach ($classes as $class) { foreach ($classes as $class) {
$instance = singleton($class); $instance = singleton($class);
@ -2771,20 +2769,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
} }
} }
$pageTypeName = $instance->i18n_singular_name(); $result[$class] = $instance->i18n_singular_name();
$currentClass = $class;
$result[$class] = $pageTypeName;
} }
// sort alphabetically, and put current on top // Sort alphabetically, and put current on top
asort($result); asort($result);
if ($currentClass) { if (isset($result[$this->ClassName])) {
$currentPageTypeName = $result[$currentClass]; $currentPageTypeName = $result[$this->ClassName];
unset($result[$currentClass]); unset($result[$this->ClassName]);
$result = array_reverse($result ?? []); $result = [$this->ClassName => $currentPageTypeName] + $result;
$result[$currentClass] = $currentPageTypeName;
$result = array_reverse($result ?? []);
} }
return $result; return $result;

View File

@ -1280,7 +1280,7 @@ class SiteTreeTest extends SapphireTest
*/ */
public function testAllowedChildren($className, $expected, $assertionMessage) public function testAllowedChildren($className, $expected, $assertionMessage)
{ {
$class = new $className; $class = new $className();
$this->assertEquals($expected, $class->allowedChildren(), $assertionMessage); $this->assertEquals($expected, $class->allowedChildren(), $assertionMessage);
} }
@ -1359,6 +1359,9 @@ class SiteTreeTest extends SapphireTest
); );
} }
/**
* @return void
*/
public function testClassDropdown() public function testClassDropdown()
{ {
$sitetree = new SiteTree(); $sitetree = new SiteTree();
@ -1380,7 +1383,18 @@ class SiteTreeTest extends SapphireTest
$this->assertArrayNotHasKey(SiteTreeTest_NotRoot::class, $method->invoke($rootPage)); $this->assertArrayNotHasKey(SiteTreeTest_NotRoot::class, $method->invoke($rootPage));
$this->assertArrayHasKey(SiteTreeTest_NotRoot::class, $method->invoke($nonRootPage)); $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); Security::setCurrentUser(null);
} }