From d8748ff5f9354b07046f060c3de1b5fc2d6df1a4 Mon Sep 17 00:00:00 2001 From: "Nicolaas @ Tappy @ Sunny Side Up" Date: Sun, 15 Sep 2024 17:06:28 +1200 Subject: [PATCH 1/3] FIX: return right order for getClassDropdown method --- code/Model/SiteTree.php | 19 ++++++------------- tests/php/Model/SiteTreeTest.php | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index f2229621..e3bb8508 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -2744,8 +2744,6 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi protected function getClassDropdown() { $classes = SiteTree::page_type_classes(); - $currentClass = null; - $result = []; foreach ($classes as $class) { $instance = singleton($class); @@ -2771,20 +2769,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi } } - $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 7eb44ec1..5bb77b2c 100644 --- a/tests/php/Model/SiteTreeTest.php +++ b/tests/php/Model/SiteTreeTest.php @@ -1280,7 +1280,7 @@ class SiteTreeTest extends SapphireTest */ public function testAllowedChildren($className, $expected, $assertionMessage) { - $class = new $className; + $class = new $className(); $this->assertEquals($expected, $class->allowedChildren(), $assertionMessage); } @@ -1359,6 +1359,9 @@ class SiteTreeTest extends SapphireTest ); } + /** + * @return void + */ public function testClassDropdown() { $sitetree = new SiteTree(); @@ -1380,7 +1383,18 @@ class SiteTreeTest extends SapphireTest $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); } From 4b3e1f09bf224743f55d7dca1675dabf687e51d2 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:06:39 +1200 Subject: [PATCH 2/3] ENH Don't use deprecated method (#3006) --- code/BatchActions/CMSBatchAction_Archive.php | 2 +- code/BatchActions/CMSBatchAction_Restore.php | 2 +- code/Controllers/CMSMain.php | 2 +- code/GraphQL/LinkablePlugin.php | 2 +- code/GraphQL/Resolver.php | 2 +- code/Model/SiteTree.php | 2 +- code/Model/SiteTreeExtension.php | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/code/BatchActions/CMSBatchAction_Archive.php b/code/BatchActions/CMSBatchAction_Archive.php index c0f8412c..7afd03b4 100644 --- a/code/BatchActions/CMSBatchAction_Archive.php +++ b/code/BatchActions/CMSBatchAction_Archive.php @@ -29,6 +29,6 @@ class CMSBatchAction_Archive extends CMSBatchAction public function applicablePages($ids) { // canArchive() is deprecated, not $this->applicablePagesHelper() - return Deprecation::withNoReplacement(fn() => $this->applicablePagesHelper($ids, 'canArchive')); + return Deprecation::withSuppressedNotice(fn() => $this->applicablePagesHelper($ids, 'canArchive')); } } diff --git a/code/BatchActions/CMSBatchAction_Restore.php b/code/BatchActions/CMSBatchAction_Restore.php index d9699c6c..38a08f68 100644 --- a/code/BatchActions/CMSBatchAction_Restore.php +++ b/code/BatchActions/CMSBatchAction_Restore.php @@ -20,7 +20,7 @@ class CMSBatchAction_Restore extends CMSBatchAction { public function __construct() { - Deprecation::withNoReplacement(function () { + Deprecation::withSuppressedNotice(function () { Deprecation::notice('5.3.0', 'Will be removed without equivalent functionality to replace it', Deprecation::SCOPE_CLASS); }); } diff --git a/code/Controllers/CMSMain.php b/code/Controllers/CMSMain.php index 4c559bf7..474f8cc6 100644 --- a/code/Controllers/CMSMain.php +++ b/code/Controllers/CMSMain.php @@ -1998,7 +1998,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if (!$record || !$record->exists()) { throw new HTTPResponse_Exception("Bad record ID #$id", 404); } - $canArchive = Deprecation::withNoReplacement(fn() => $record->canArchive()); + $canArchive = Deprecation::withSuppressedNotice(fn() => $record->canArchive()); if (!$canArchive) { return Security::permissionFailure(); } diff --git a/code/GraphQL/LinkablePlugin.php b/code/GraphQL/LinkablePlugin.php index 798cb579..4f650576 100644 --- a/code/GraphQL/LinkablePlugin.php +++ b/code/GraphQL/LinkablePlugin.php @@ -49,7 +49,7 @@ class LinkablePlugin implements ModelQueryPlugin public function __construct() { - Deprecation::withNoReplacement(function () { + Deprecation::withSuppressedNotice(function () { Deprecation::notice('5.3.0', 'Will be moved to the silverstripe/graphql module', Deprecation::SCOPE_CLASS); }); } diff --git a/code/GraphQL/Resolver.php b/code/GraphQL/Resolver.php index 19c4ff5c..29571b47 100644 --- a/code/GraphQL/Resolver.php +++ b/code/GraphQL/Resolver.php @@ -13,7 +13,7 @@ class Resolver { public function __construct() { - Deprecation::withNoReplacement(function () { + Deprecation::withSuppressedNotice(function () { Deprecation::notice('5.3.0', 'Will be moved to the silverstripe/graphql module', Deprecation::SCOPE_CLASS); }); } diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index e3bb8508..8dcfea5a 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -2578,7 +2578,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi // If a page is on any stage it can be archived if (($isOnDraft || $isPublished)) { - $canArchive = Deprecation::withNoReplacement(fn() => $this->canArchive()); + $canArchive = Deprecation::withSuppressedNotice(fn() => $this->canArchive()); if ($canArchive) { $title = $isPublished ? _t('SilverStripe\\CMS\\Controllers\\CMSMain.UNPUBLISH_AND_ARCHIVE', 'Unpublish and archive') diff --git a/code/Model/SiteTreeExtension.php b/code/Model/SiteTreeExtension.php index 6348a283..49a63122 100644 --- a/code/Model/SiteTreeExtension.php +++ b/code/Model/SiteTreeExtension.php @@ -18,10 +18,10 @@ abstract class SiteTreeExtension extends DataExtension { public function __construct() { - // Wrapping with Deprecation::withNoReplacement() to avoid triggering deprecation notices + // Wrapping with Deprecation::withSuppressedNotice() to avoid triggering deprecation notices // as we are unable to update existing subclasses of this class until a new major // unless we add in the pointless empty methods that are in this class - Deprecation::withNoReplacement(function () { + Deprecation::withSuppressedNotice(function () { $class = Extension::class; Deprecation::notice('5.3.0', "Subclass $class instead", Deprecation::SCOPE_CLASS); }); From e2ca1cb77da7c0ca80d6ee891bee423e937c4206 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 23 Sep 2024 15:15:02 +1200 Subject: [PATCH 3/3] DEP Increase minimum version of silverstripe/framework --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b274b107..3299e698 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "php": "^8.1", "silverstripe/admin": "^2.2", "silverstripe/campaign-admin": "^2", - "silverstripe/framework": "^5.2", + "silverstripe/framework": "^5.4", "silverstripe/reports": "^5", "silverstripe/siteconfig": "^5", "silverstripe/versioned": "^2",