silverstripe-cms/tests/search/SearchFormTest.php

281 lines
9.2 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\DB;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\MSSQL\MSSQLDatabase;
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
/**
* @package cms
* @subpackage testing
*
* @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
*
* Because this manipulates the test database in severe ways, I've renamed the test to force it to run last...
*/
class ZZZSearchFormTest extends FunctionalTest {
2016-03-08 21:50:55 +01:00
protected static $fixture_file = 'SearchFormTest.yml';
protected $illegalExtensions = array(
'SiteTree' => array('SiteTreeSubsites', 'Translatable')
);
protected $mockController;
2016-03-08 21:50:55 +01:00
public function waitUntilIndexingFinished() {
$schema = DB::get_schema();
if (method_exists($schema, 'waitUntilIndexingFinished')) $schema->waitUntilIndexingFinished();
}
2016-03-08 21:50:55 +01:00
public function setUpOnce() {
// HACK Postgres doesn't refresh TSearch indexes when the schema changes after CREATE TABLE
// MySQL will need a different table type
self::kill_temp_db();
FulltextSearchable::enable();
self::create_temp_db();
$this->resetDBSchema(true);
parent::setUpOnce();
}
2016-03-08 21:50:55 +01:00
public function setUp() {
parent::setUp();
2016-03-08 21:50:55 +01:00
$holderPage = $this->objFromFixture('SiteTree', 'searchformholder');
$this->mockController = new ContentController($holderPage);
2016-03-08 21:50:55 +01:00
$this->waitUntilIndexingFinished();
}
2016-03-08 21:50:55 +01:00
/**
* @return Boolean
*/
protected function checkFulltextSupport() {
$conn = DB::get_conn();
if(class_exists('SilverStripe\\MSSQL\\MSSQLDatabase') && $conn instanceof MSSQLDatabase) {
$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
public function testSearchFormTemplateCanBeChanged() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$sf->setTemplate('BlankPage');
2016-03-08 21:50:55 +01:00
$this->assertContains(
'<body class="SearchForm Form RequestHandler BlankPage">',
$sf->forTemplate()
);
}
2016-03-08 21:50:55 +01:00
public function testPublishedPagesMatchedByTitle() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$publishedPage = $this->objFromFixture('SiteTree', 'publicPublishedPage');
2016-04-01 05:17:37 +02:00
$publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
2016-03-08 21:50:55 +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
public function testDoubleQuotesPublishedPagesMatchedByTitle() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
$publishedPage = $this->objFromFixture('SiteTree', 'publicPublishedPage');
$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
$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
public function testUnpublishedPagesNotIncluded() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$results = $sf->getResults(null, array('Search'=>'publicUnpublishedPage'));
$unpublishedPage = $this->objFromFixture('SiteTree', 'publicUnpublishedPage');
$this->assertNotContains(
$unpublishedPage->ID,
$results->column('ID'),
'Unpublished pages are not found by searchform'
);
}
2016-03-08 21:50:55 +01:00
public function testPagesRestrictedToLoggedinUsersNotIncluded() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$page = $this->objFromFixture('SiteTree', 'restrictedViewLoggedInUsers');
2016-04-01 05:17:37 +02:00
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$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
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'randomuser');
$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();
}
public function testPagesRestrictedToSpecificGroupNotIncluded() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$page = $this->objFromFixture('SiteTree', 'restrictedViewOnlyWebsiteUsers');
2016-04-01 05:17:37 +02:00
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$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
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'randomuser');
$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
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'websiteuser');
$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
public function testInheritedRestrictedPagesNotIncluded() {
$sf = new SearchForm($this->mockController, 'SearchForm');
$parent = $this->objFromFixture('SiteTree', 'restrictedViewLoggedInUsers');
2016-04-01 05:17:37 +02:00
$parent->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
2016-03-08 21:50:55 +01:00
$page = $this->objFromFixture('SiteTree', 'inheritRestrictedView');
2016-04-01 05:17:37 +02:00
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$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
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'websiteuser');
$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
public function testDisabledShowInSearchFlagNotIncludedForSiteTree() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$page = $this->objFromFixture('SiteTree', 'dontShowInSearchPage');
$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
public function testDisabledShowInSearchFlagNotIncludedForFiles() {
if(!$this->checkFulltextSupport()) return;
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$dontShowInSearchFile = $this->objFromFixture('File', 'dontShowInSearchFile');
2016-04-01 05:17:37 +02:00
$dontShowInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$showInSearchFile = $this->objFromFixture('File', 'showInSearchFile');
2016-04-01 05:17:37 +02:00
$showInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$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
$results = $sf->getResults(null, array('Search'=>'showInSearchFile'));
$this->assertContains(
$showInSearchFile->ID,
$results->column('ID'),
'File with "Show in Search" enabled can be found'
);
}
public function testSearchTitleAndContentWithSpecialCharacters() {
if(!$this->checkFulltextSupport()) return;
if(class_exists('SilverStripe\\PostgreSQL\\PostgreSQLDatabase') && DB::get_conn() instanceof PostgreSQLDatabase) {
$this->markTestSkipped("PostgreSQLDatabase doesn't support entity-encoded searches");
}
$sf = new SearchForm($this->mockController, 'SearchForm');
2016-03-08 21:50:55 +01:00
$pageWithSpecialChars = $this->objFromFixture('SiteTree', 'pageWithSpecialChars');
2016-04-01 05:17:37 +02:00
$pageWithSpecialChars->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
2016-03-08 21:50:55 +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
$results = $sf->getResults(null, array('Search'=>'Bäcker'));
$this->assertContains(
$pageWithSpecialChars->ID,
$results->column('ID'),
'Published pages with htmlencoded umlauts in content are found'
);
}
}