2011-03-22 10:00:53 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-06-23 01:51:20 +02:00
|
|
|
use SilverStripe\MSSQL\MSSQLDatabase;
|
|
|
|
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Controllers\ContentController;
|
|
|
|
use SilverStripe\CMS\Search\SearchForm;
|
|
|
|
|
2016-06-23 01:51:20 +02:00
|
|
|
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
/**
|
2011-03-22 10:47:26 +01:00
|
|
|
* @package cms
|
2011-03-22 10:00:53 +01:00
|
|
|
* @subpackage testing
|
2016-01-06 00:42:07 +01:00
|
|
|
*
|
2011-03-22 10:00:53 +01:00
|
|
|
* @todo Fix unpublished pages check in testPublishedPagesMatchedByTitle()
|
|
|
|
* @todo All tests run on unpublished pages at the moment, due to the searchform not distinguishing between them
|
2016-01-06 00:42:07 +01:00
|
|
|
*
|
2011-11-07 23:27:25 +01:00
|
|
|
* Because this manipulates the test database in severe ways, I've renamed the test to force it to run last...
|
2011-03-22 10:00:53 +01:00
|
|
|
*/
|
2011-11-07 23:27:25 +01:00
|
|
|
class ZZZSearchFormTest extends FunctionalTest {
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $fixture_file = 'SearchFormTest.yml';
|
2014-01-15 23:36:25 +01:00
|
|
|
|
|
|
|
protected $illegalExtensions = array(
|
2016-08-10 06:08:39 +02:00
|
|
|
'SilverStripe\\CMS\\Model\\SiteTree' => array('SiteTreeSubsites', 'Translatable')
|
2014-01-15 23:36:25 +01:00
|
|
|
);
|
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
protected $mockController;
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function waitUntilIndexingFinished() {
|
2013-06-21 00:45:33 +02:00
|
|
|
$schema = DB::get_schema();
|
|
|
|
if (method_exists($schema, 'waitUntilIndexingFinished')) $schema->waitUntilIndexingFinished();
|
2011-03-22 10:00:53 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setUpOnce() {
|
2011-03-22 10:00:53 +01:00
|
|
|
// HACK Postgres doesn't refresh TSearch indexes when the schema changes after CREATE TABLE
|
2011-11-07 23:27:25 +01:00
|
|
|
// MySQL will need a different table type
|
|
|
|
self::kill_temp_db();
|
2011-10-31 00:20:56 +01:00
|
|
|
FulltextSearchable::enable();
|
2011-11-07 23:27:25 +01:00
|
|
|
self::create_temp_db();
|
|
|
|
$this->resetDBSchema(true);
|
2011-03-22 10:00:53 +01:00
|
|
|
parent::setUpOnce();
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setUp() {
|
2011-03-22 10:00:53 +01:00
|
|
|
parent::setUp();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$holderPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'searchformholder');
|
2011-03-22 10:00:53 +01:00
|
|
|
$this->mockController = new ContentController($holderPage);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
$this->waitUntilIndexingFinished();
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-12-11 15:18:47 +01:00
|
|
|
/**
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
protected function checkFulltextSupport() {
|
2013-06-21 00:45:33 +02:00
|
|
|
$conn = DB::get_conn();
|
2016-06-23 01:51:20 +02:00
|
|
|
if(class_exists('SilverStripe\\MSSQL\\MSSQLDatabase') && $conn instanceof MSSQLDatabase) {
|
2012-12-11 15:18:47 +01:00
|
|
|
$supports = $conn->fullTextEnabled();
|
|
|
|
} else {
|
|
|
|
$supports = true;
|
|
|
|
}
|
|
|
|
if(!$supports) $this->markTestSkipped('Fulltext not supported by DB driver or setup');
|
|
|
|
return $supports;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2014-10-03 06:08:47 +02:00
|
|
|
public function testSearchFormTemplateCanBeChanged() {
|
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2014-10-03 06:08:47 +02:00
|
|
|
$sf->setTemplate('BlankPage');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2014-10-03 06:08:47 +02:00
|
|
|
$this->assertContains(
|
|
|
|
'<body class="SearchForm Form RequestHandler BlankPage">',
|
|
|
|
$sf->forTemplate()
|
|
|
|
);
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testPublishedPagesMatchedByTitle() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$publishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicPublishedPage');
|
2016-04-01 05:17:37 +02:00
|
|
|
$publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
$this->waitUntilIndexingFinished();
|
|
|
|
$results = $sf->getResults(null, array('Search'=>'publicPublishedPage'));
|
|
|
|
$this->assertContains(
|
|
|
|
$publishedPage->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Published pages are found by searchform'
|
|
|
|
);
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testDoubleQuotesPublishedPagesMatchedByTitle() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2011-03-22 10:00:53 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$publishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicPublishedPage');
|
2011-03-22 10:00:53 +01:00
|
|
|
$publishedPage->Title = "finding butterflies";
|
|
|
|
$publishedPage->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
$this->waitUntilIndexingFinished();
|
|
|
|
$results = $sf->getResults(null, array('Search'=>'"finding butterflies"'));
|
|
|
|
$this->assertContains(
|
|
|
|
$publishedPage->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Published pages are found by searchform'
|
|
|
|
);
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testUnpublishedPagesNotIncluded() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'publicUnpublishedPage'));
|
2016-07-22 01:32:32 +02:00
|
|
|
$unpublishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicUnpublishedPage');
|
2011-03-22 10:00:53 +01:00
|
|
|
$this->assertNotContains(
|
|
|
|
$unpublishedPage->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Unpublished pages are not found by searchform'
|
|
|
|
);
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testPagesRestrictedToLoggedinUsersNotIncluded() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'restrictedViewLoggedInUsers');
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2011-03-22 10:00:53 +01:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers'));
|
|
|
|
$this->assertNotContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page with "Restrict to logged in users" doesnt show without valid login'
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-06-23 01:51:20 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'randomuser');
|
2011-03-22 10:00:53 +01:00
|
|
|
$member->logIn();
|
|
|
|
$results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers'));
|
|
|
|
$this->assertContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page with "Restrict to logged in users" shows if login is present'
|
|
|
|
);
|
|
|
|
$member->logOut();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testPagesRestrictedToSpecificGroupNotIncluded() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'restrictedViewOnlyWebsiteUsers');
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2011-03-22 10:00:53 +01:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers'));
|
|
|
|
$this->assertNotContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page with "Restrict to these users" doesnt show without valid login'
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-06-23 01:51:20 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'randomuser');
|
2011-03-22 10:00:53 +01:00
|
|
|
$member->logIn();
|
|
|
|
$results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers'));
|
|
|
|
$this->assertNotContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page with "Restrict to these users" doesnt show if logged in user is not in the right group'
|
|
|
|
);
|
|
|
|
$member->logOut();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-06-23 01:51:20 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'websiteuser');
|
2011-03-22 10:00:53 +01:00
|
|
|
$member->logIn();
|
|
|
|
$results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers'));
|
|
|
|
$this->assertContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page with "Restrict to these users" shows if user in this group is logged in'
|
|
|
|
);
|
|
|
|
$member->logOut();
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2013-07-04 04:54:13 +02:00
|
|
|
public function testInheritedRestrictedPagesNotIncluded() {
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2013-07-04 04:54:13 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$parent = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'restrictedViewLoggedInUsers');
|
2016-04-01 05:17:37 +02:00
|
|
|
$parent->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'inheritRestrictedView');
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2011-03-22 10:00:53 +01:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'inheritRestrictedView'));
|
|
|
|
$this->assertNotContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page inheriting "Restrict to loggedin users" doesnt show without valid login'
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-06-23 01:51:20 +02:00
|
|
|
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'websiteuser');
|
2011-03-22 10:00:53 +01:00
|
|
|
$member->logIn();
|
|
|
|
$results = $sf->getResults(null, array('Search'=>'inheritRestrictedView'));
|
|
|
|
$this->assertContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page inheriting "Restrict to loggedin users" shows if user in this group is logged in'
|
|
|
|
);
|
|
|
|
$member->logOut();
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testDisabledShowInSearchFlagNotIncludedForSiteTree() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'dontShowInSearchPage');
|
2011-03-22 10:00:53 +01:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'dontShowInSearchPage'));
|
|
|
|
$this->assertNotContains(
|
|
|
|
$page->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Page with "Show in Search" disabled doesnt show'
|
|
|
|
);
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testDisabledShowInSearchFlagNotIncludedForFiles() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-09-15 15:59:18 +02:00
|
|
|
$dontShowInSearchFile = $this->objFromFixture('File', 'dontShowInSearchFile');
|
2016-04-01 05:17:37 +02:00
|
|
|
$dontShowInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2011-09-15 15:59:18 +02:00
|
|
|
$showInSearchFile = $this->objFromFixture('File', 'showInSearchFile');
|
2016-04-01 05:17:37 +02:00
|
|
|
$showInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-01-26 06:38:42 +01:00
|
|
|
|
2011-09-15 15:59:18 +02:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'dontShowInSearchFile'));
|
|
|
|
$this->assertNotContains(
|
|
|
|
$dontShowInSearchFile->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'File with "Show in Search" disabled doesnt show'
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-09-15 15:59:18 +02:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'showInSearchFile'));
|
|
|
|
$this->assertContains(
|
|
|
|
$showInSearchFile->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'File with "Show in Search" enabled can be found'
|
|
|
|
);
|
|
|
|
}
|
2011-03-22 10:00:53 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testSearchTitleAndContentWithSpecialCharacters() {
|
2012-12-11 15:18:47 +01:00
|
|
|
if(!$this->checkFulltextSupport()) return;
|
|
|
|
|
2016-06-23 01:51:20 +02:00
|
|
|
if(class_exists('SilverStripe\\PostgreSQL\\PostgreSQLDatabase') && DB::get_conn() instanceof PostgreSQLDatabase) {
|
2013-08-20 16:56:24 +02:00
|
|
|
$this->markTestSkipped("PostgreSQLDatabase doesn't support entity-encoded searches");
|
|
|
|
}
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$pageWithSpecialChars = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'pageWithSpecialChars');
|
2016-04-01 05:17:37 +02:00
|
|
|
$pageWithSpecialChars->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'Brötchen'));
|
|
|
|
$this->assertContains(
|
|
|
|
$pageWithSpecialChars->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Published pages with umlauts in title are found'
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-22 10:00:53 +01:00
|
|
|
$results = $sf->getResults(null, array('Search'=>'Bäcker'));
|
|
|
|
$this->assertContains(
|
|
|
|
$pageWithSpecialChars->ID,
|
|
|
|
$results->column('ID'),
|
|
|
|
'Published pages with htmlencoded umlauts in content are found'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|