From 0c4b2f81576433988a3173a62c7fea8d49dc1385 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 12 Jun 2013 12:32:42 +0200 Subject: [PATCH] API SiteTree->validURLSegment() prioritizes extension votes Tri-state, use NULL to ignore the extension result --- code/model/SiteTree.php | 18 +++++++++--------- tests/model/SiteTreeTest.php | 23 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/code/model/SiteTree.php b/code/model/SiteTree.php index 440cb1f4..05b8155a 100644 --- a/code/model/SiteTree.php +++ b/code/model/SiteTree.php @@ -1588,20 +1588,20 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid } } + $votes = array_filter( + (array)$this->extend('augmentValidURLSegment'), + function($v) {return !is_null($v);} + ); + if($votes) { + return min($votes); + } + $existingPage = DataObject::get_one( 'SiteTree', "\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter" ); - if ($existingPage) { - return false; - } - - $votes = $this->extend('augmentValidURLSegment'); - if($votes) { - return min($votes); - } - return true; + return !($existingPage); } /** diff --git a/tests/model/SiteTreeTest.php b/tests/model/SiteTreeTest.php index 863e290b..f26c94b6 100644 --- a/tests/model/SiteTreeTest.php +++ b/tests/model/SiteTreeTest.php @@ -4,8 +4,9 @@ * @subpackage tests */ class SiteTreeTest extends SapphireTest { - protected static $fixture_file = 'SiteTreeTest.yml'; + protected static $fixture_file = 'SiteTreeTest.yml'; + protected $illegalExtensions = array( 'SiteTree' => array('SiteTreeSubsites') ); @@ -700,6 +701,18 @@ class SiteTreeTest extends SapphireTest { $this->assertTrue($sitetree->validURLSegment(), 'Valid URLSegment values are allowed'); } + public function testURLSegmentPrioritizesExtensionVotes() { + $sitetree = new SiteTree(); + $sitetree->URLSegment = 'unique-segment'; + $this->assertTrue($sitetree->validURLSegment()); + + SiteTree::add_extension('SiteTreeTest_Extension'); + $sitetree = new SiteTree(); + $sitetree->URLSegment = 'unique-segment'; + $this->assertFalse($sitetree->validURLSegment()); + SiteTree::remove_extension('SiteTreeTest_Extension'); + } + public function testURLSegmentMultiByte() { $origAllow = Config::inst()->get('URLSegmentFilter', 'default_allow_multibyte'); Config::inst()->update('URLSegmentFilter', 'default_allow_multibyte', true); @@ -960,3 +973,11 @@ class SiteTreeTest_StageStatusInherit extends SiteTree implements TestOnly { return $flags; } } + +class SiteTreeTest_Extension extends DataExtension implements TestOnly { + + public function augmentValidURLSegment() { + return false; + } + +} \ No newline at end of file