Skip to content

Commit

Permalink
Add tests and move logic into the if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed May 31, 2019
1 parent 1f51fcd commit 900d04d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Pages/SubsitesVirtualPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions tests/php/SubsitesVirtualPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 900d04d

Please sign in to comment.