silverstripe-cms/tests/controller/CMSSiteTreeFilterTest.php

194 lines
6.9 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\Versioning\Versioned;
2016-07-22 01:32:32 +02:00
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_Search;
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_ChangedPages;
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_DeletedPages;
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_StatusDraftPages;
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_StatusRemovedFromDraftPages;
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_StatusDeletedPages;
class CMSSiteTreeFilterTest extends SapphireTest {
protected static $fixture_file = 'CMSSiteTreeFilterTest.yml';
2016-03-08 21:50:55 +01:00
public function testSearchFilterEmpty() {
$page1 = $this->objFromFixture('Page', 'page1');
$page2 = $this->objFromFixture('Page', 'page2');
2016-03-08 21:50:55 +01:00
$f = new CMSSiteTreeFilter_Search();
$results = $f->pagesIncluded();
2016-03-08 21:50:55 +01:00
$this->assertTrue($f->isPageIncluded($page1));
$this->assertTrue($f->isPageIncluded($page2));
}
2016-03-08 21:50:55 +01:00
public function testSearchFilterByTitle() {
$page1 = $this->objFromFixture('Page', 'page1');
$page2 = $this->objFromFixture('Page', 'page2');
2016-03-08 21:50:55 +01:00
$f = new CMSSiteTreeFilter_Search(array('Title' => 'Page 1'));
$results = $f->pagesIncluded();
2016-03-08 21:50:55 +01:00
$this->assertTrue($f->isPageIncluded($page1));
$this->assertFalse($f->isPageIncluded($page2));
$this->assertEquals(1, count($results));
$this->assertEquals(
array('ID' => $page1->ID, 'ParentID' => 0),
$results[0]
);
}
2016-03-08 21:50:55 +01:00
public function testIncludesParentsForNestedMatches() {
$parent = $this->objFromFixture('Page', 'page3');
$child = $this->objFromFixture('Page', 'page3b');
2016-03-08 21:50:55 +01:00
$f = new CMSSiteTreeFilter_Search(array('Title' => 'Page 3b'));
$results = $f->pagesIncluded();
2016-03-08 21:50:55 +01:00
$this->assertTrue($f->isPageIncluded($parent));
$this->assertTrue($f->isPageIncluded($child));
$this->assertEquals(1, count($results));
$this->assertEquals(
array('ID' => $child->ID, 'ParentID' => $parent->ID),
$results[0]
);
}
2016-03-08 21:50:55 +01:00
public function testChangedPagesFilter() {
$unchangedPage = $this->objFromFixture('Page', 'page1');
2016-04-01 05:17:37 +02:00
$unchangedPage->publishRecursive();
2016-03-08 21:50:55 +01:00
$changedPage = $this->objFromFixture('Page', 'page2');
$changedPage->Title = 'Original';
2016-04-01 05:17:37 +02:00
$changedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$changedPage->Title = 'Changed';
$changedPage->write();
2016-03-08 21:50:55 +01:00
// Check that only changed pages are returned
$f = new CMSSiteTreeFilter_ChangedPages(array('Term' => 'Changed'));
$results = $f->pagesIncluded();
2016-03-08 21:50:55 +01:00
$this->assertTrue($f->isPageIncluded($changedPage));
$this->assertFalse($f->isPageIncluded($unchangedPage));
$this->assertEquals(1, count($results));
$this->assertEquals(
array('ID' => $changedPage->ID, 'ParentID' => 0),
$results[0]
);
2016-03-08 21:50:55 +01:00
// Check that only changed pages are returned
$f = new CMSSiteTreeFilter_ChangedPages(array('Term' => 'No Matches'));
$results = $f->pagesIncluded();
$this->assertEquals(0, count($results));
// If we roll back to an earlier version than what's on the published site, we should still show the changed
$changedPage->Title = 'Changed 2';
2016-04-01 05:17:37 +02:00
$changedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$changedPage->doRollbackTo(1);
$f = new CMSSiteTreeFilter_ChangedPages(array('Term' => 'Changed'));
$results = $f->pagesIncluded();
$this->assertEquals(1, count($results));
$this->assertEquals(array('ID' => $changedPage->ID, 'ParentID' => 0), $results[0]);
}
2016-03-08 21:50:55 +01:00
public function testDeletedPagesFilter() {
$deletedPage = $this->objFromFixture('Page', 'page2');
2016-04-01 05:17:37 +02:00
$deletedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$deletedPageID = $deletedPage->ID;
$deletedPage->delete();
$deletedPage = Versioned::get_one_by_stage(
2016-07-22 01:32:32 +02:00
'SilverStripe\\CMS\\Model\\SiteTree',
'Live',
array('"SiteTree_Live"."ID"' => $deletedPageID)
);
$f = new CMSSiteTreeFilter_DeletedPages(array('Term' => 'Page'));
$this->assertTrue($f->isPageIncluded($deletedPage));
2016-03-08 21:50:55 +01:00
// Check that only changed pages are returned
$f = new CMSSiteTreeFilter_DeletedPages(array('Term' => 'No Matches'));
$this->assertFalse($f->isPageIncluded($deletedPage));
}
2016-03-08 21:50:55 +01:00
public function testStatusDraftPagesFilter() {
$draftPage = $this->objFromFixture('Page', 'page4');
$draftPage = Versioned::get_one_by_stage(
2016-07-22 01:32:32 +02:00
'SilverStripe\\CMS\\Model\\SiteTree',
'Stage',
sprintf('"SiteTree"."ID" = %d', $draftPage->ID)
);
// Check draft page is shown
$f = new CMSSiteTreeFilter_StatusDraftPages(array('Term' => 'Page'));
$this->assertTrue($f->isPageIncluded($draftPage));
2016-03-08 21:50:55 +01:00
// Check filter respects parameters
$f = new CMSSiteTreeFilter_StatusDraftPages(array('Term' => 'No Match'));
$this->assertEmpty($f->isPageIncluded($draftPage));
2016-03-08 21:50:55 +01:00
// Ensures empty array returned if no data to show
$f = new CMSSiteTreeFilter_StatusDraftPages();
$draftPage->delete();
2016-03-08 21:50:55 +01:00
$this->assertEmpty($f->isPageIncluded($draftPage));
}
public function testDateFromToLastSameDate() {
$draftPage = $this->objFromFixture('Page', 'page4');
// Grab the date
$date = substr($draftPage->LastEdited, 0, 10);
// Filter with that date
$filter = New CMSSiteTreeFilter_Search(array(
'LastEditedFrom' => $date,
'LastEditedTo' => $date
));
$this->assertTrue($filter->isPageIncluded($draftPage), 'Using the same date for from and to should show find that page');
}
2016-03-08 21:50:55 +01:00
public function testStatusRemovedFromDraftFilter() {
$removedDraftPage = $this->objFromFixture('Page', 'page6');
2016-04-01 05:17:37 +02:00
$removedDraftPage->publishRecursive();
$removedDraftPage->deleteFromStage('Stage');
$removedDraftPage = Versioned::get_one_by_stage(
2016-07-22 01:32:32 +02:00
'SilverStripe\\CMS\\Model\\SiteTree',
'Live',
sprintf('"SiteTree"."ID" = %d', $removedDraftPage->ID)
);
// Check live-only page is included
$f = new CMSSiteTreeFilter_StatusRemovedFromDraftPages(array('LastEditedFrom' => '2000-01-01 00:00'));
$this->assertTrue($f->isPageIncluded($removedDraftPage));
// Check filter is respected
$f = new CMSSiteTreeFilter_StatusRemovedFromDraftPages(array('LastEditedTo' => '1999-01-01 00:00'));
$this->assertEmpty($f->isPageIncluded($removedDraftPage));
2016-03-08 21:50:55 +01:00
// Ensures empty array returned if no data to show
$f = new CMSSiteTreeFilter_StatusRemovedFromDraftPages();
$removedDraftPage->delete();
$this->assertEmpty($f->isPageIncluded($removedDraftPage));
}
2016-03-08 21:50:55 +01:00
public function testStatusDeletedFilter() {
$deletedPage = $this->objFromFixture('Page', 'page7');
2016-04-01 05:17:37 +02:00
$deletedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$deletedPageID = $deletedPage->ID;
2016-03-08 21:50:55 +01:00
// Can't use straight $blah->delete() as that blows it away completely and test fails
$deletedPage->deleteFromStage(Versioned::LIVE);
$deletedPage->deleteFromStage(Versioned::DRAFT);
2016-07-22 01:32:32 +02:00
$checkParentExists = Versioned::get_latest_version('SilverStripe\\CMS\\Model\\SiteTree', $deletedPageID);
// Check deleted page is included
$f = new CMSSiteTreeFilter_StatusDeletedPages(array('Title' => 'Page'));
$this->assertTrue($f->isPageIncluded($checkParentExists));
2016-03-08 21:50:55 +01:00
// Check filter is respected
$f = new CMSSiteTreeFilter_StatusDeletedPages(array('Title' => 'Bobby'));
$this->assertFalse($f->isPageIncluded($checkParentExists));
}
}