mirror of
https://github.com/silverstripe/silverstripe-subsites
synced 2024-10-22 11:05:55 +02:00
Add tests and move logic into the if statement
This commit is contained in:
parent
1f51fcd909
commit
900d04d94a
@ -222,15 +222,13 @@ class SubsitesVirtualPage extends VirtualPage
|
||||
{
|
||||
$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;
|
||||
|
@ -311,4 +311,41 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user