Merge pull request #1758 from open-sausages/pulls/4.0/form-refactor

Update SearchForm
This commit is contained in:
Chris Joe 2017-03-13 15:22:53 +13:00 committed by GitHub
commit 319ce1861e
3 changed files with 152 additions and 107 deletions

View File

@ -2,8 +2,10 @@
namespace SilverStripe\CMS\Search; namespace SilverStripe\CMS\Search;
use BadMethodCallException;
use SilverStripe\Assets\File;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller; use SilverStripe\Control\RequestHandler;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction; use SilverStripe\Forms\FormAction;
@ -26,19 +28,22 @@ use Translatable;
*/ */
class SearchForm extends Form class SearchForm extends Form
{ {
/** /**
* @var int $pageLength How many results are shown per page. * How many results are shown per page.
* Relies on pagination being implemented in the search results template. * Relies on pagination being implemented in the search results template.
*
* @var int
*/ */
protected $pageLength = 10; protected $pageLength = 10;
/** /**
* Classes to search * Classes to search
*
* @var array
*/ */
protected $classesToSearch = array( protected $classesToSearch = array(
"SilverStripe\\CMS\\Model\\SiteTree", SiteTree::class,
"SilverStripe\\Assets\\File" File::class
); );
private static $casting = array( private static $casting = array(
@ -46,15 +51,19 @@ class SearchForm extends Form
); );
/** /**
* * @skipUpgrade
* @param Controller $controller * @param RequestHandler $controller
* @param string $name The name of the form (used in URL addressing) * @param string $name The name of the form (used in URL addressing)
* @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized * @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized
* if fields are added to the form. * if fields are added to the form.
* @param FieldList $actions Optional, defaults to a single field named "Go". * @param FieldList $actions Optional, defaults to a single field named "Go".
*/ */
public function __construct($controller, $name, $fields = null, $actions = null) public function __construct(
{ RequestHandler $controller = null,
$name = 'SearchForm',
FieldList $fields = null,
FieldList $actions = null
) {
if (!$fields) { if (!$fields) {
$fields = new FieldList( $fields = new FieldList(
new TextField('Search', _t('SearchForm.SEARCH', 'Search')) new TextField('Search', _t('SearchForm.SEARCH', 'Search'))
@ -69,7 +78,7 @@ class SearchForm extends Form
if (!$actions) { if (!$actions) {
$actions = new FieldList( $actions = new FieldList(
new FormAction("getResults", _t('SearchForm.GO', 'Go')) new FormAction("results", _t('SearchForm.GO', 'Go'))
); );
} }
@ -88,13 +97,12 @@ class SearchForm extends Form
*/ */
public function classesToSearch($classes) public function classesToSearch($classes)
{ {
$supportedClasses = array('SilverStripe\\CMS\\Model\\SiteTree', 'SilverStripe\\Assets\\File'); $supportedClasses = array(SiteTree::class, File::class);
$illegalClasses = array_diff($classes, $supportedClasses); $illegalClasses = array_diff($classes, $supportedClasses);
if ($illegalClasses) { if ($illegalClasses) {
user_error( throw new BadMethodCallException(
"SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses) "SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses)
. "'. At this stage, only File and SiteTree are allowed", . "'. At this stage, only File and SiteTree are allowed"
E_USER_WARNING
); );
} }
$legalClasses = array_intersect($classes, $supportedClasses); $legalClasses = array_intersect($classes, $supportedClasses);
@ -112,34 +120,33 @@ class SearchForm extends Form
} }
/** /**
* Return dataObjectSet of the results using $_REQUEST to get info from form. * Return dataObjectSet of the results using current request to get info from form.
* Wraps around {@link searchEngine()}. * Wraps around {@link searchEngine()}.
* *
* @param int $pageLength DEPRECATED 2.3 Use SearchForm->pageLength
* @param array $data Request data as an associative array. Should contain at least a key 'Search' with all searched keywords.
* @return SS_List * @return SS_List
*/ */
public function getResults($pageLength = null, $data = null) public function getResults()
{ {
// legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.org tutorials // Get request data from request handler
if (!isset($data) || !is_array($data)) { $request = $this->getRequestHandler()->getRequest();
$data = $_REQUEST;
}
// set language (if present) // set language (if present)
$locale = null;
$origLocale = null;
if (class_exists('Translatable')) { if (class_exists('Translatable')) {
if (SiteTree::singleton()->hasExtension('Translatable') && isset($data['searchlocale'])) { $locale = $request->requestVar('searchlocale');
if ($data['searchlocale'] == "ALL") { if (SiteTree::singleton()->hasExtension('Translatable') && $locale) {
if ($locale === "ALL") {
Translatable::disable_locale_filter(); Translatable::disable_locale_filter();
} else { } else {
$origLocale = Translatable::get_current_locale(); $origLocale = Translatable::get_current_locale();
Translatable::set_current_locale($data['searchlocale']); Translatable::set_current_locale($locale);
} }
} }
} }
$keywords = $data['Search']; $keywords = $request->requestVar('Search');
$andProcessor = create_function('$matches', ' $andProcessor = create_function('$matches', '
return " +" . $matches[2] . " +" . $matches[4] . " "; return " +" . $matches[2] . " +" . $matches[4] . " ";
@ -155,16 +162,15 @@ class SearchForm extends Form
$keywords = $this->addStarsToKeywords($keywords); $keywords = $this->addStarsToKeywords($keywords);
if (!$pageLength) { $pageLength = $this->getPageLength();
$pageLength = $this->pageLength; $start = $request->requestVar('start') ?: 0;
}
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
if (strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false) { $booleanSearch =
$results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", true); strpos($keywords, '"') !== false ||
} else { strpos($keywords, '+') !== false ||
$results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength); strpos($keywords, '-') !== false ||
} strpos($keywords, '*') !== false;
$results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", $booleanSearch);
// filter by permission // filter by permission
if ($results) { if ($results) {
@ -177,8 +183,8 @@ class SearchForm extends Form
// reset locale // reset locale
if (class_exists('Translatable')) { if (class_exists('Translatable')) {
if (SiteTree::singleton()->hasExtension('Translatable') && isset($data['searchlocale'])) { if (SiteTree::singleton()->hasExtension('Translatable') && $locale) {
if ($data['searchlocale'] == "ALL") { if ($locale == "ALL") {
Translatable::enable_locale_filter(); Translatable::enable_locale_filter();
} else { } else {
Translatable::set_current_locale($origLocale); Translatable::set_current_locale($origLocale);
@ -216,22 +222,11 @@ class SearchForm extends Form
/** /**
* Get the search query for display in a "You searched for ..." sentence. * Get the search query for display in a "You searched for ..." sentence.
* *
* @param array $data
* @return string * @return string
*/ */
public function getSearchQuery($data = null) public function getSearchQuery()
{ {
// legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.org tutorials return $this->getRequestHandler()->getRequest()->requestVar('Search');
if (!isset($data)) {
$data = $_REQUEST;
}
// The form could be rendered without the search being done, so check for that.
if (isset($data['Search'])) {
return $data['Search'];
}
return null;
} }
/** /**

View File

@ -22,15 +22,15 @@ class RedirectorPageTest extends FunctionalTest
public function testGoodRedirectors() public function testGoodRedirectors()
{ {
/* For good redirectors, the final destination URL will be returned */ /* For good redirectors, the final destination URL will be returned */
$this->assertEquals("http://www.google.com", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodexternal')->Link()); $this->assertEquals("http://www.google.com", $this->objFromFixture(RedirectorPage::class, 'goodexternal')->Link());
$this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodinternal')->redirectionLink()); $this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture(RedirectorPage::class, 'goodinternal')->redirectionLink());
$this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodinternal')->Link()); $this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture(RedirectorPage::class, 'goodinternal')->Link());
} }
public function testEmptyRedirectors() public function testEmptyRedirectors()
{ {
/* If a redirector page is misconfigured, then its link method will just return the usual URLSegment-generated value */ /* If a redirector page is misconfigured, then its link method will just return the usual URLSegment-generated value */
$page1 = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'badexternal'); $page1 = $this->objFromFixture(RedirectorPage::class, 'badexternal');
$this->assertEquals(Director::baseURL() . 'bad-external/', $page1->Link()); $this->assertEquals(Director::baseURL() . 'bad-external/', $page1->Link());
/* An error message will be shown if you visit it */ /* An error message will be shown if you visit it */
@ -38,7 +38,7 @@ class RedirectorPageTest extends FunctionalTest
$this->assertContains('message-setupWithoutRedirect', $content); $this->assertContains('message-setupWithoutRedirect', $content);
/* This also applies for internal links */ /* This also applies for internal links */
$page2 = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'badinternal'); $page2 = $this->objFromFixture(RedirectorPage::class, 'badinternal');
$this->assertEquals(Director::baseURL() . 'bad-internal/', $page2->Link()); $this->assertEquals(Director::baseURL() . 'bad-internal/', $page2->Link());
$content = $this->get(Director::makeRelative($page2->Link()))->getBody(); $content = $this->get(Director::makeRelative($page2->Link()))->getBody();
$this->assertContains('message-setupWithoutRedirect', $content); $this->assertContains('message-setupWithoutRedirect', $content);
@ -47,14 +47,14 @@ class RedirectorPageTest extends FunctionalTest
public function testReflexiveAndTransitiveInternalRedirectors() public function testReflexiveAndTransitiveInternalRedirectors()
{ {
/* Reflexive redirectors are those that point to themselves. They should behave the same as an empty redirector */ /* Reflexive redirectors are those that point to themselves. They should behave the same as an empty redirector */
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'reflexive'); $page = $this->objFromFixture(RedirectorPage::class, 'reflexive');
$this->assertEquals(Director::baseURL() . 'reflexive/', $page->Link()); $this->assertEquals(Director::baseURL() . 'reflexive/', $page->Link());
$content = $this->get(Director::makeRelative($page->Link()))->getBody(); $content = $this->get(Director::makeRelative($page->Link()))->getBody();
$this->assertContains('message-setupWithoutRedirect', $content); $this->assertContains('message-setupWithoutRedirect', $content);
/* Transitive redirectors are those that point to another redirector page. They should send people to the URLSegment /* Transitive redirectors are those that point to another redirector page. They should send people to the URLSegment
* of the destination page - the middle-stop, so to speak. That should redirect to the final destination */ * of the destination page - the middle-stop, so to speak. That should redirect to the final destination */
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'transitive'); $page = $this->objFromFixture(RedirectorPage::class, 'transitive');
$this->assertEquals(Director::baseURL() . 'good-internal/', $page->Link()); $this->assertEquals(Director::baseURL() . 'good-internal/', $page->Link());
$this->autoFollowRedirection = false; $this->autoFollowRedirection = false;
@ -64,7 +64,7 @@ class RedirectorPageTest extends FunctionalTest
public function testExternalURLGetsPrefixIfNotSet() public function testExternalURLGetsPrefixIfNotSet()
{ {
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'externalnoprefix'); $page = $this->objFromFixture(RedirectorPage::class, 'externalnoprefix');
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite has prefixed with http'); $this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite has prefixed with http');
$page->write(); $page->write();
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!'); $this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!');
@ -90,12 +90,12 @@ class RedirectorPageTest extends FunctionalTest
*/ */
public function testRedirectRespectsFinishedResponse() public function testRedirectRespectsFinishedResponse()
{ {
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodinternal'); $page = $this->objFromFixture(RedirectorPage::class, 'goodinternal');
RedirectorPageController::add_extension('RedirectorPageTest_RedirectExtension'); RedirectorPageController::add_extension('RedirectorPageTest_RedirectExtension');
$response = $this->get($page->regularLink()); $response = $this->get($page->regularLink());
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('/foo', $response->getHeader('Location')); $this->assertEquals('http://www.mysite.com/foo', $response->getHeader('Location'));
RedirectorPageController::remove_extension('RedirectorPageTest_RedirectExtension'); RedirectorPageController::remove_extension('RedirectorPageTest_RedirectExtension');
} }

View File

@ -1,5 +1,10 @@
<?php <?php
use SilverStripe\Assets\File;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\Versioning\Versioned; use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\MSSQL\MSSQLDatabase; use SilverStripe\MSSQL\MSSQLDatabase;
@ -8,6 +13,7 @@ use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Search\SearchForm; use SilverStripe\CMS\Search\SearchForm;
use SilverStripe\ORM\Search\FulltextSearchable; use SilverStripe\ORM\Search\FulltextSearchable;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Security\Member;
/** /**
* @package cms * @package cms
@ -24,9 +30,12 @@ class ZZZSearchFormTest extends FunctionalTest
protected static $fixture_file = 'SearchFormTest.yml'; protected static $fixture_file = 'SearchFormTest.yml';
protected $illegalExtensions = array( protected $illegalExtensions = array(
'SilverStripe\\CMS\\Model\\SiteTree' => array('SiteTreeSubsites', 'Translatable') SiteTree::class => array('SiteTreeSubsites', 'Translatable')
); );
/**
* @var ContentController
*/
protected $mockController; protected $mockController;
public function waitUntilIndexingFinished() public function waitUntilIndexingFinished()
@ -42,6 +51,7 @@ class ZZZSearchFormTest extends FunctionalTest
// HACK Postgres doesn't refresh TSearch indexes when the schema changes after CREATE TABLE // HACK Postgres doesn't refresh TSearch indexes when the schema changes after CREATE TABLE
// MySQL will need a different table type // MySQL will need a different table type
self::kill_temp_db(); self::kill_temp_db();
Config::modify();
FulltextSearchable::enable(); FulltextSearchable::enable();
self::create_temp_db(); self::create_temp_db();
$this->resetDBSchema(true); $this->resetDBSchema(true);
@ -52,8 +62,9 @@ class ZZZSearchFormTest extends FunctionalTest
{ {
parent::setUp(); parent::setUp();
$holderPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'searchformholder'); /** @var Page $holderPage */
$this->mockController = new ContentController($holderPage); $holderPage = $this->objFromFixture(SiteTree::class, 'searchformholder');
$this->mockController = ModelAsController::controller_for($holderPage);
$this->waitUntilIndexingFinished(); $this->waitUntilIndexingFinished();
} }
@ -64,7 +75,7 @@ class ZZZSearchFormTest extends FunctionalTest
protected function checkFulltextSupport() protected function checkFulltextSupport()
{ {
$conn = DB::get_conn(); $conn = DB::get_conn();
if (class_exists('SilverStripe\\MSSQL\\MSSQLDatabase') && $conn instanceof MSSQLDatabase) { if (class_exists(MSSQLDatabase::class) && $conn instanceof MSSQLDatabase) {
$supports = $conn->fullTextEnabled(); $supports = $conn->fullTextEnabled();
} else { } else {
$supports = true; $supports = true;
@ -75,35 +86,44 @@ class ZZZSearchFormTest extends FunctionalTest
return $supports; return $supports;
} }
/**
* @skipUpgrade
*/
public function testSearchFormTemplateCanBeChanged() public function testSearchFormTemplateCanBeChanged()
{ {
if (!$this->checkFulltextSupport()) { if (!$this->checkFulltextSupport()) {
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $sf = new SearchForm($this->mockController);
$sf->setTemplate('BlankPage'); $sf->setTemplate('BlankPage');
$this->assertContains( $this->assertContains(
'<body class="SearchForm Form RequestHandler BlankPage">', '<body class="SearchForm Form BlankPage">',
$sf->forTemplate() $sf->forTemplate()
); );
} }
/**
* @skipUpgrade
*/
public function testPublishedPagesMatchedByTitle() public function testPublishedPagesMatchedByTitle()
{ {
if (!$this->checkFulltextSupport()) { if (!$this->checkFulltextSupport()) {
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'publicPublishedPage']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$publishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicPublishedPage'); $publishedPage = $this->objFromFixture(SiteTree::class, 'publicPublishedPage');
$publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$this->waitUntilIndexingFinished(); $this->waitUntilIndexingFinished();
$results = $sf->getResults(null, array('Search'=>'publicPublishedPage'));
$results = $sf->getResults();
$this->assertContains( $this->assertContains(
$publishedPage->ID, $publishedPage->ID,
$results->column('ID'), $results->column('ID'),
@ -111,21 +131,26 @@ class ZZZSearchFormTest extends FunctionalTest
); );
} }
/**
* @skipUpgrade
*/
public function testDoubleQuotesPublishedPagesMatchedByTitle() public function testDoubleQuotesPublishedPagesMatchedByTitle()
{ {
if (!$this->checkFulltextSupport()) { if (!$this->checkFulltextSupport()) {
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'"finding butterflies"']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$publishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicPublishedPage'); $publishedPage = $this->objFromFixture(SiteTree::class, 'publicPublishedPage');
$publishedPage->Title = "finding butterflies"; $publishedPage->Title = "finding butterflies";
$publishedPage->write(); $publishedPage->write();
$publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$this->waitUntilIndexingFinished(); $this->waitUntilIndexingFinished();
$results = $sf->getResults(null, array('Search'=>'"finding butterflies"')); $results = $sf->getResults();
$this->assertContains( $this->assertContains(
$publishedPage->ID, $publishedPage->ID,
$results->column('ID'), $results->column('ID'),
@ -133,16 +158,21 @@ class ZZZSearchFormTest extends FunctionalTest
); );
} }
/**
* @skipUpgrade
*/
public function testUnpublishedPagesNotIncluded() public function testUnpublishedPagesNotIncluded()
{ {
if (!$this->checkFulltextSupport()) { if (!$this->checkFulltextSupport()) {
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'publicUnpublishedPage']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$results = $sf->getResults(null, array('Search'=>'publicUnpublishedPage')); $results = $sf->getResults();
$unpublishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicUnpublishedPage'); $unpublishedPage = $this->objFromFixture(SiteTree::class, 'publicUnpublishedPage');
$this->assertNotContains( $this->assertNotContains(
$unpublishedPage->ID, $unpublishedPage->ID,
$results->column('ID'), $results->column('ID'),
@ -156,20 +186,22 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'restrictedViewLoggedInUsers']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'restrictedViewLoggedInUsers'); $page = $this->objFromFixture(SiteTree::class, 'restrictedViewLoggedInUsers');
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers')); $results = $sf->getResults();
$this->assertNotContains( $this->assertNotContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
'Page with "Restrict to logged in users" doesnt show without valid login' 'Page with "Restrict to logged in users" doesnt show without valid login'
); );
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'randomuser'); $member = $this->objFromFixture(Member::class, 'randomuser');
$member->logIn(); $member->logIn();
$results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers')); $results = $sf->getResults();
$this->assertContains( $this->assertContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
@ -184,20 +216,22 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'restrictedViewOnlyWebsiteUsers']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'restrictedViewOnlyWebsiteUsers'); $page = $this->objFromFixture(SiteTree::class, 'restrictedViewOnlyWebsiteUsers');
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); $results = $sf->getResults();
$this->assertNotContains( $this->assertNotContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
'Page with "Restrict to these users" doesnt show without valid login' 'Page with "Restrict to these users" doesnt show without valid login'
); );
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'randomuser'); $member = $this->objFromFixture(Member::class, 'randomuser');
$member->logIn(); $member->logIn();
$results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); $results = $sf->getResults();
$this->assertNotContains( $this->assertNotContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
@ -205,9 +239,9 @@ class ZZZSearchFormTest extends FunctionalTest
); );
$member->logOut(); $member->logOut();
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'websiteuser'); $member = $this->objFromFixture(Member::class, 'websiteuser');
$member->logIn(); $member->logIn();
$results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); $results = $sf->getResults();
$this->assertContains( $this->assertContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
@ -218,23 +252,25 @@ class ZZZSearchFormTest extends FunctionalTest
public function testInheritedRestrictedPagesNotIncluded() public function testInheritedRestrictedPagesNotIncluded()
{ {
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'inheritRestrictedView']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$parent = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'restrictedViewLoggedInUsers'); $parent = $this->objFromFixture(SiteTree::class, 'restrictedViewLoggedInUsers');
$parent->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $parent->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'inheritRestrictedView'); $page = $this->objFromFixture(SiteTree::class, 'inheritRestrictedView');
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$results = $sf->getResults(null, array('Search'=>'inheritRestrictedView')); $results = $sf->getResults();
$this->assertNotContains( $this->assertNotContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
'Page inheriting "Restrict to loggedin users" doesnt show without valid login' 'Page inheriting "Restrict to loggedin users" doesnt show without valid login'
); );
$member = $this->objFromFixture('SilverStripe\\Security\\Member', 'websiteuser'); $member = $this->objFromFixture(Member::class, 'websiteuser');
$member->logIn(); $member->logIn();
$results = $sf->getResults(null, array('Search'=>'inheritRestrictedView')); $results = $sf->getResults();
$this->assertContains( $this->assertContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
@ -249,14 +285,16 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'dontShowInSearchPage']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'dontShowInSearchPage'); $page = $this->objFromFixture(SiteTree::class, 'dontShowInSearchPage');
$results = $sf->getResults(null, array('Search'=>'dontShowInSearchPage')); $results = $sf->getResults();
$this->assertNotContains( $this->assertNotContains(
$page->ID, $page->ID,
$results->column('ID'), $results->column('ID'),
'Page with "Show in Search" disabled doesnt show' 'Page with "Show in Search" disabled does not show'
); );
} }
@ -266,21 +304,27 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'dontShowInSearchFile']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$dontShowInSearchFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'dontShowInSearchFile'); $dontShowInSearchFile = $this->objFromFixture(File::class, 'dontShowInSearchFile');
$dontShowInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $dontShowInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$showInSearchFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'showInSearchFile'); $showInSearchFile = $this->objFromFixture(File::class, 'showInSearchFile');
$showInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $showInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$results = $sf->getResults(null, array('Search'=>'dontShowInSearchFile')); $results = $sf->getResults();
$this->assertNotContains( $this->assertNotContains(
$dontShowInSearchFile->ID, $dontShowInSearchFile->ID,
$results->column('ID'), $results->column('ID'),
'File with "Show in Search" disabled doesnt show' 'File with "Show in Search" disabled doesnt show'
); );
$results = $sf->getResults(null, array('Search'=>'showInSearchFile')); // Check ShowInSearch=1 can be found
$request = new HTTPRequest('GET', 'search', ['Search'=>'showInSearchFile']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$results = $sf->getResults();
$this->assertContains( $this->assertContains(
$showInSearchFile->ID, $showInSearchFile->ID,
$results->column('ID'), $results->column('ID'),
@ -294,23 +338,29 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
if (class_exists('SilverStripe\\PostgreSQL\\PostgreSQLDatabase') && DB::get_conn() instanceof PostgreSQLDatabase) { if (class_exists(PostgreSQLDatabase::class) && DB::get_conn() instanceof PostgreSQLDatabase) {
$this->markTestSkipped("PostgreSQLDatabase doesn't support entity-encoded searches"); $this->markTestSkipped("PostgreSQLDatabase doesn't support entity-encoded searches");
} }
$sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); $request = new HTTPRequest('GET', 'search', ['Search'=>'Brötchen']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$pageWithSpecialChars = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'pageWithSpecialChars'); $pageWithSpecialChars = $this->objFromFixture(SiteTree::class, 'pageWithSpecialChars');
$pageWithSpecialChars->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $pageWithSpecialChars->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
$results = $sf->getResults(null, array('Search'=>'Brötchen')); $results = $sf->getResults();
$this->assertContains( $this->assertContains(
$pageWithSpecialChars->ID, $pageWithSpecialChars->ID,
$results->column('ID'), $results->column('ID'),
'Published pages with umlauts in title are found' 'Published pages with umlauts in title are found'
); );
$results = $sf->getResults(null, array('Search'=>'Bäcker')); // Check another word
$request = new HTTPRequest('GET', 'search', ['Search'=>'Bäcker']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController);
$results = $sf->getResults();
$this->assertContains( $this->assertContains(
$pageWithSpecialChars->ID, $pageWithSpecialChars->ID,
$results->column('ID'), $results->column('ID'),