Add tests and move logic into the if statement

This commit is contained in:
Robbie Averill 2019-05-31 16:32:28 +12:00
parent 1f51fcd909
commit 900d04d94a
2 changed files with 41 additions and 6 deletions

View File

@ -222,15 +222,13 @@ class SubsitesVirtualPage extends VirtualPage
{ {
$isValid = parent::validURLSegment(); $isValid = parent::validURLSegment();
$filters = [
'URLSegment' => $this->URLSegment,
];
// Veto the validation rules if its false. In this case, some logic // 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. // needs to be duplicated from parent to find out the exact reason the validation failed.
if (!$isValid) { if (!$isValid) {
// Exclude the current page from the filter $filters = [
$filters['ID:not'] = $this->ID; 'URLSegment' => $this->URLSegment,
'ID:not' => $this->ID,
];
if (Config::inst()->get(SiteTree::class, 'nested_urls')) { if (Config::inst()->get(SiteTree::class, 'nested_urls')) {
$filters['ParentID'] = $this->ParentID ?: 0; $filters['ParentID'] = $this->ParentID ?: 0;

View File

@ -311,4 +311,41 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
Versioned::prepopulate_versionnumber_cache(SiteTree::class, 'Live', [$p->ID]); 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());
}
} }