diff --git a/src/Pages/SubsitesVirtualPage.php b/src/Pages/SubsitesVirtualPage.php index d5ab24e0..d36cb4de 100644 --- a/src/Pages/SubsitesVirtualPage.php +++ b/src/Pages/SubsitesVirtualPage.php @@ -222,15 +222,13 @@ public function validURLSegment() { $isValid = parent::validURLSegment(); - $filters = [ - 'URLSegment' => $this->URLSegment, - ]; - // Veto the validation rules if its false. In this case, some logic // needs to be duplicated from parent to find out the exact reason the validation failed. if (!$isValid) { - // Exclude the current page from the filter - $filters['ID:not'] = $this->ID; + $filters = [ + 'URLSegment' => $this->URLSegment, + 'ID:not' => $this->ID, + ]; if (Config::inst()->get(SiteTree::class, 'nested_urls')) { $filters['ParentID'] = $this->ParentID ?: 0; diff --git a/tests/php/SubsitesVirtualPageTest.php b/tests/php/SubsitesVirtualPageTest.php index 99e96462..527927f3 100644 --- a/tests/php/SubsitesVirtualPageTest.php +++ b/tests/php/SubsitesVirtualPageTest.php @@ -311,4 +311,41 @@ protected function fixVersionNumberCache($page) Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Live', [$p->ID]); } } + + public function testValidURLSegmentWithUniquePageAndNestedURLs() + { + SiteTree::config()->set('nested_urls', true); + + $newPage = new SubsitesVirtualPage(); + $newPage->Title = 'My new page'; + $newPage->URLSegment = 'my-new-page'; + + $this->assertTrue($newPage->validURLSegment()); + } + + public function testValidURLSegmentWithExistingPageInSubsite() + { + $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); + Subsite::changeSubsite($subsite1->ID); + + SiteTree::config()->set('nested_urls', false); + + $similarContactUsPage = new SubsitesVirtualPage(); + $similarContactUsPage->Title = 'Similar to Contact Us in Subsite 1'; + $similarContactUsPage->URLSegment = 'contact-us'; + + $this->assertFalse($similarContactUsPage->validURLSegment()); + } + + public function testValidURLSegmentWithExistingPageInAnotherSubsite() + { + $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); + Subsite::changeSubsite($subsite1->ID); + + $similarStaffPage = new SubsitesVirtualPage(); + $similarStaffPage->Title = 'Similar to Staff page in main site'; + $similarStaffPage->URLSegment = 'staff'; + + $this->assertFalse($similarStaffPage->validURLSegment()); + } }