From f65a16481856c7079c090323c9120da19b2fe831 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Thu, 9 Mar 2017 18:17:31 +1300 Subject: [PATCH 1/2] Update SearchForm Fix RedirectorPageTest --- code/Search/SearchForm.php | 85 ++++++++++++++---------------- tests/model/RedirectorPageTest.php | 20 +++---- tests/search/SearchFormTest.php | 75 +++++++++++++++----------- 3 files changed, 95 insertions(+), 85 deletions(-) diff --git a/code/Search/SearchForm.php b/code/Search/SearchForm.php index 1fdaf24a..49ce0187 100644 --- a/code/Search/SearchForm.php +++ b/code/Search/SearchForm.php @@ -2,8 +2,10 @@ namespace SilverStripe\CMS\Search; +use BadMethodCallException; +use SilverStripe\Assets\File; use SilverStripe\CMS\Model\SiteTree; -use SilverStripe\Control\Controller; +use SilverStripe\Control\RequestHandler; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; @@ -26,19 +28,22 @@ use Translatable; */ 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. + * + * @var int */ protected $pageLength = 10; /** * Classes to search + * + * @var array */ protected $classesToSearch = array( - "SilverStripe\\CMS\\Model\\SiteTree", - "SilverStripe\\Assets\\File" + SiteTree::class, + File::class ); private static $casting = array( @@ -46,15 +51,20 @@ class SearchForm extends Form ); /** - * - * @param Controller $controller + * @skipUpgrade + * @param RequestHandler $controller * @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 * if fields are added to the form. * @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) { $fields = new FieldList( new TextField('Search', _t('SearchForm.SEARCH', 'Search')) @@ -69,7 +79,7 @@ class SearchForm extends Form if (!$actions) { $actions = new FieldList( - new FormAction("getResults", _t('SearchForm.GO', 'Go')) + new FormAction("results", _t('SearchForm.GO', 'Go')) ); } @@ -88,13 +98,12 @@ class SearchForm extends Form */ public function classesToSearch($classes) { - $supportedClasses = array('SilverStripe\\CMS\\Model\\SiteTree', 'SilverStripe\\Assets\\File'); + $supportedClasses = array(SiteTree::class, File::class); $illegalClasses = array_diff($classes, $supportedClasses); if ($illegalClasses) { - user_error( + throw new BadMethodCallException( "SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses) - . "'. At this stage, only File and SiteTree are allowed", - E_USER_WARNING + . "'. At this stage, only File and SiteTree are allowed" ); } $legalClasses = array_intersect($classes, $supportedClasses); @@ -112,34 +121,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()}. * - * @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 */ - 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 - if (!isset($data) || !is_array($data)) { - $data = $_REQUEST; - } + // Get request data from request handler + $request = $this->getRequestHandler()->getRequest(); // set language (if present) + $locale = null; + $origLocale = null; if (class_exists('Translatable')) { - if (SiteTree::singleton()->hasExtension('Translatable') && isset($data['searchlocale'])) { - if ($data['searchlocale'] == "ALL") { + $locale = $request->requestVar('searchlocale'); + if (SiteTree::singleton()->hasExtension('Translatable') && $locale) { + if ($locale === "ALL") { Translatable::disable_locale_filter(); } else { $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', ' return " +" . $matches[2] . " +" . $matches[4] . " "; @@ -155,10 +163,8 @@ class SearchForm extends Form $keywords = $this->addStarsToKeywords($keywords); - if (!$pageLength) { - $pageLength = $this->pageLength; - } - $start = isset($_GET['start']) ? (int)$_GET['start'] : 0; + $pageLength = $this->getPageLength(); + $start = $request->requestVar('start') ?: 0; if (strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false) { $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", true); @@ -177,8 +183,8 @@ class SearchForm extends Form // reset locale if (class_exists('Translatable')) { - if (SiteTree::singleton()->hasExtension('Translatable') && isset($data['searchlocale'])) { - if ($data['searchlocale'] == "ALL") { + if (SiteTree::singleton()->hasExtension('Translatable') && $locale) { + if ($locale == "ALL") { Translatable::enable_locale_filter(); } else { 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. * - * @param array $data * @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 - 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; + return $this->getRequestHandler()->getRequest()->requestVar('Search'); } /** diff --git a/tests/model/RedirectorPageTest.php b/tests/model/RedirectorPageTest.php index 96959637..d440c150 100644 --- a/tests/model/RedirectorPageTest.php +++ b/tests/model/RedirectorPageTest.php @@ -22,15 +22,15 @@ class RedirectorPageTest extends FunctionalTest public function testGoodRedirectors() { /* 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(Director::baseURL() . "redirection-dest/", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodinternal')->redirectionLink()); - $this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodinternal')->Link()); + $this->assertEquals("http://www.google.com", $this->objFromFixture(RedirectorPage::class, 'goodexternal')->Link()); + $this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture(RedirectorPage::class, 'goodinternal')->redirectionLink()); + $this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture(RedirectorPage::class, 'goodinternal')->Link()); } public function testEmptyRedirectors() { /* 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()); /* An error message will be shown if you visit it */ @@ -38,7 +38,7 @@ class RedirectorPageTest extends FunctionalTest $this->assertContains('message-setupWithoutRedirect', $content); /* 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()); $content = $this->get(Director::makeRelative($page2->Link()))->getBody(); $this->assertContains('message-setupWithoutRedirect', $content); @@ -47,14 +47,14 @@ class RedirectorPageTest extends FunctionalTest public function testReflexiveAndTransitiveInternalRedirectors() { /* 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()); $content = $this->get(Director::makeRelative($page->Link()))->getBody(); $this->assertContains('message-setupWithoutRedirect', $content); /* 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 */ - $page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'transitive'); + $page = $this->objFromFixture(RedirectorPage::class, 'transitive'); $this->assertEquals(Director::baseURL() . 'good-internal/', $page->Link()); $this->autoFollowRedirection = false; @@ -64,7 +64,7 @@ class RedirectorPageTest extends FunctionalTest 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'); $page->write(); $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() { - $page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodinternal'); + $page = $this->objFromFixture(RedirectorPage::class, 'goodinternal'); RedirectorPageController::add_extension('RedirectorPageTest_RedirectExtension'); $response = $this->get($page->regularLink()); $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'); } diff --git a/tests/search/SearchFormTest.php b/tests/search/SearchFormTest.php index 667860dc..596cf8f8 100644 --- a/tests/search/SearchFormTest.php +++ b/tests/search/SearchFormTest.php @@ -1,5 +1,7 @@ array('SiteTreeSubsites', 'Translatable') + SiteTree::class => array('SiteTreeSubsites', 'Translatable') ); protected $mockController; @@ -52,7 +55,7 @@ class ZZZSearchFormTest extends FunctionalTest { parent::setUp(); - $holderPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'searchformholder'); + $holderPage = $this->objFromFixture(SiteTree::class, 'searchformholder'); $this->mockController = new ContentController($holderPage); $this->waitUntilIndexingFinished(); @@ -64,7 +67,7 @@ class ZZZSearchFormTest extends FunctionalTest protected function checkFulltextSupport() { $conn = DB::get_conn(); - if (class_exists('SilverStripe\\MSSQL\\MSSQLDatabase') && $conn instanceof MSSQLDatabase) { + if (class_exists(MSSQLDatabase::class) && $conn instanceof MSSQLDatabase) { $supports = $conn->fullTextEnabled(); } else { $supports = true; @@ -75,31 +78,37 @@ class ZZZSearchFormTest extends FunctionalTest return $supports; } + /** + * @skipUpgrade + */ public function testSearchFormTemplateCanBeChanged() { if (!$this->checkFulltextSupport()) { return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $sf = new SearchForm($this->mockController); $sf->setTemplate('BlankPage'); $this->assertContains( - '', + '', $sf->forTemplate() ); } + /** + * @skipUpgrade + */ public function testPublishedPagesMatchedByTitle() { if (!$this->checkFulltextSupport()) { return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $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); $this->waitUntilIndexingFinished(); @@ -111,15 +120,18 @@ class ZZZSearchFormTest extends FunctionalTest ); } + /** + * @skipUpgrade + */ public function testDoubleQuotesPublishedPagesMatchedByTitle() { if (!$this->checkFulltextSupport()) { return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $sf = new SearchForm($this->mockController); - $publishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicPublishedPage'); + $publishedPage = $this->objFromFixture(SiteTree::class, 'publicPublishedPage'); $publishedPage->Title = "finding butterflies"; $publishedPage->write(); $publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); @@ -133,16 +145,19 @@ class ZZZSearchFormTest extends FunctionalTest ); } + /** + * @skipUpgrade + */ public function testUnpublishedPagesNotIncluded() { if (!$this->checkFulltextSupport()) { return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $sf = new SearchForm($this->mockController); $results = $sf->getResults(null, array('Search'=>'publicUnpublishedPage')); - $unpublishedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'publicUnpublishedPage'); + $unpublishedPage = $this->objFromFixture(SiteTree::class, 'publicUnpublishedPage'); $this->assertNotContains( $unpublishedPage->ID, $results->column('ID'), @@ -156,9 +171,9 @@ class ZZZSearchFormTest extends FunctionalTest return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $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); $results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers')); $this->assertNotContains( @@ -167,7 +182,7 @@ class ZZZSearchFormTest extends FunctionalTest '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(); $results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers')); $this->assertContains( @@ -184,9 +199,9 @@ class ZZZSearchFormTest extends FunctionalTest return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $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); $results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); $this->assertNotContains( @@ -195,7 +210,7 @@ class ZZZSearchFormTest extends FunctionalTest '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(); $results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); $this->assertNotContains( @@ -205,7 +220,7 @@ class ZZZSearchFormTest extends FunctionalTest ); $member->logOut(); - $member = $this->objFromFixture('SilverStripe\\Security\\Member', 'websiteuser'); + $member = $this->objFromFixture(Member::class, 'websiteuser'); $member->logIn(); $results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); $this->assertContains( @@ -218,12 +233,12 @@ class ZZZSearchFormTest extends FunctionalTest public function testInheritedRestrictedPagesNotIncluded() { - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $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); - $page = $this->objFromFixture('SilverStripe\\CMS\\Model\\SiteTree', 'inheritRestrictedView'); + $page = $this->objFromFixture(SiteTree::class, 'inheritRestrictedView'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $results = $sf->getResults(null, array('Search'=>'inheritRestrictedView')); $this->assertNotContains( @@ -232,7 +247,7 @@ class ZZZSearchFormTest extends FunctionalTest '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(); $results = $sf->getResults(null, array('Search'=>'inheritRestrictedView')); $this->assertContains( @@ -249,9 +264,9 @@ class ZZZSearchFormTest extends FunctionalTest return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $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')); $this->assertNotContains( $page->ID, @@ -266,11 +281,11 @@ class ZZZSearchFormTest extends FunctionalTest return; } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $sf = new SearchForm($this->mockController); - $dontShowInSearchFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'dontShowInSearchFile'); + $dontShowInSearchFile = $this->objFromFixture(File::class, 'dontShowInSearchFile'); $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); $results = $sf->getResults(null, array('Search'=>'dontShowInSearchFile')); @@ -294,13 +309,13 @@ class ZZZSearchFormTest extends FunctionalTest 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"); } - $sf = new SearchForm($this->mockController, 'SilverStripe\\CMS\\Search\\SearchForm'); + $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); $results = $sf->getResults(null, array('Search'=>'Brötchen')); From c7d99479768ea301dddeaf4ee60a635b3db784db Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 13 Mar 2017 11:11:56 +1300 Subject: [PATCH 2/2] Update SearchFormTest --- code/Search/SearchForm.php | 12 +++--- tests/search/SearchFormTest.php | 69 +++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 23 deletions(-) diff --git a/code/Search/SearchForm.php b/code/Search/SearchForm.php index 49ce0187..1e0e1b4f 100644 --- a/code/Search/SearchForm.php +++ b/code/Search/SearchForm.php @@ -64,7 +64,6 @@ class SearchForm extends Form FieldList $fields = null, FieldList $actions = null ) { - if (!$fields) { $fields = new FieldList( new TextField('Search', _t('SearchForm.SEARCH', 'Search')) @@ -166,11 +165,12 @@ class SearchForm extends Form $pageLength = $this->getPageLength(); $start = $request->requestVar('start') ?: 0; - if (strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false) { - $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", true); - } else { - $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength); - } + $booleanSearch = + strpos($keywords, '"') !== false || + strpos($keywords, '+') !== false || + strpos($keywords, '-') !== false || + strpos($keywords, '*') !== false; + $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", $booleanSearch); // filter by permission if ($results) { diff --git a/tests/search/SearchFormTest.php b/tests/search/SearchFormTest.php index 596cf8f8..466ec874 100644 --- a/tests/search/SearchFormTest.php +++ b/tests/search/SearchFormTest.php @@ -1,7 +1,10 @@ array('SiteTreeSubsites', 'Translatable') ); + /** + * @var ContentController + */ protected $mockController; public function waitUntilIndexingFinished() @@ -45,6 +51,7 @@ class ZZZSearchFormTest extends FunctionalTest // 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(); + Config::modify(); FulltextSearchable::enable(); self::create_temp_db(); $this->resetDBSchema(true); @@ -55,8 +62,9 @@ class ZZZSearchFormTest extends FunctionalTest { parent::setUp(); + /** @var Page $holderPage */ $holderPage = $this->objFromFixture(SiteTree::class, 'searchformholder'); - $this->mockController = new ContentController($holderPage); + $this->mockController = ModelAsController::controller_for($holderPage); $this->waitUntilIndexingFinished(); } @@ -106,13 +114,16 @@ class ZZZSearchFormTest extends FunctionalTest return; } + $request = new HTTPRequest('GET', 'search', ['Search'=>'publicPublishedPage']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $publishedPage = $this->objFromFixture(SiteTree::class, 'publicPublishedPage'); $publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $this->waitUntilIndexingFinished(); - $results = $sf->getResults(null, array('Search'=>'publicPublishedPage')); + + $results = $sf->getResults(); $this->assertContains( $publishedPage->ID, $results->column('ID'), @@ -129,6 +140,8 @@ class ZZZSearchFormTest extends FunctionalTest return; } + $request = new HTTPRequest('GET', 'search', ['Search'=>'"finding butterflies"']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $publishedPage = $this->objFromFixture(SiteTree::class, 'publicPublishedPage'); @@ -137,7 +150,7 @@ class ZZZSearchFormTest extends FunctionalTest $publishedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); $this->waitUntilIndexingFinished(); - $results = $sf->getResults(null, array('Search'=>'"finding butterflies"')); + $results = $sf->getResults(); $this->assertContains( $publishedPage->ID, $results->column('ID'), @@ -154,9 +167,11 @@ class ZZZSearchFormTest extends FunctionalTest return; } + $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(SiteTree::class, 'publicUnpublishedPage'); $this->assertNotContains( $unpublishedPage->ID, @@ -171,11 +186,13 @@ class ZZZSearchFormTest extends FunctionalTest return; } + $request = new HTTPRequest('GET', 'search', ['Search'=>'restrictedViewLoggedInUsers']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $page = $this->objFromFixture(SiteTree::class, 'restrictedViewLoggedInUsers'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers')); + $results = $sf->getResults(); $this->assertNotContains( $page->ID, $results->column('ID'), @@ -184,7 +201,7 @@ class ZZZSearchFormTest extends FunctionalTest $member = $this->objFromFixture(Member::class, 'randomuser'); $member->logIn(); - $results = $sf->getResults(null, array('Search'=>'restrictedViewLoggedInUsers')); + $results = $sf->getResults(); $this->assertContains( $page->ID, $results->column('ID'), @@ -199,11 +216,13 @@ class ZZZSearchFormTest extends FunctionalTest return; } + $request = new HTTPRequest('GET', 'search', ['Search'=>'restrictedViewOnlyWebsiteUsers']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $page = $this->objFromFixture(SiteTree::class, 'restrictedViewOnlyWebsiteUsers'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); + $results = $sf->getResults(); $this->assertNotContains( $page->ID, $results->column('ID'), @@ -212,7 +231,7 @@ class ZZZSearchFormTest extends FunctionalTest $member = $this->objFromFixture(Member::class, 'randomuser'); $member->logIn(); - $results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); + $results = $sf->getResults(); $this->assertNotContains( $page->ID, $results->column('ID'), @@ -222,7 +241,7 @@ class ZZZSearchFormTest extends FunctionalTest $member = $this->objFromFixture(Member::class, 'websiteuser'); $member->logIn(); - $results = $sf->getResults(null, array('Search'=>'restrictedViewOnlyWebsiteUsers')); + $results = $sf->getResults(); $this->assertContains( $page->ID, $results->column('ID'), @@ -233,6 +252,8 @@ class ZZZSearchFormTest extends FunctionalTest public function testInheritedRestrictedPagesNotIncluded() { + $request = new HTTPRequest('GET', 'search', ['Search'=>'inheritRestrictedView']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $parent = $this->objFromFixture(SiteTree::class, 'restrictedViewLoggedInUsers'); @@ -240,7 +261,7 @@ class ZZZSearchFormTest extends FunctionalTest $page = $this->objFromFixture(SiteTree::class, 'inheritRestrictedView'); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $results = $sf->getResults(null, array('Search'=>'inheritRestrictedView')); + $results = $sf->getResults(); $this->assertNotContains( $page->ID, $results->column('ID'), @@ -249,7 +270,7 @@ class ZZZSearchFormTest extends FunctionalTest $member = $this->objFromFixture(Member::class, 'websiteuser'); $member->logIn(); - $results = $sf->getResults(null, array('Search'=>'inheritRestrictedView')); + $results = $sf->getResults(); $this->assertContains( $page->ID, $results->column('ID'), @@ -264,14 +285,16 @@ class ZZZSearchFormTest extends FunctionalTest return; } + $request = new HTTPRequest('GET', 'search', ['Search'=>'dontShowInSearchPage']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $page = $this->objFromFixture(SiteTree::class, 'dontShowInSearchPage'); - $results = $sf->getResults(null, array('Search'=>'dontShowInSearchPage')); + $results = $sf->getResults(); $this->assertNotContains( $page->ID, $results->column('ID'), - 'Page with "Show in Search" disabled doesnt show' + 'Page with "Show in Search" disabled does not show' ); } @@ -281,6 +304,8 @@ class ZZZSearchFormTest extends FunctionalTest return; } + $request = new HTTPRequest('GET', 'search', ['Search'=>'dontShowInSearchFile']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $dontShowInSearchFile = $this->objFromFixture(File::class, 'dontShowInSearchFile'); @@ -288,14 +313,18 @@ class ZZZSearchFormTest extends FunctionalTest $showInSearchFile = $this->objFromFixture(File::class, 'showInSearchFile'); $showInSearchFile->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $results = $sf->getResults(null, array('Search'=>'dontShowInSearchFile')); + $results = $sf->getResults(); $this->assertNotContains( $dontShowInSearchFile->ID, $results->column('ID'), '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( $showInSearchFile->ID, $results->column('ID'), @@ -313,19 +342,25 @@ class ZZZSearchFormTest extends FunctionalTest $this->markTestSkipped("PostgreSQLDatabase doesn't support entity-encoded searches"); } + $request = new HTTPRequest('GET', 'search', ['Search'=>'Brötchen']); + $this->mockController->setRequest($request); $sf = new SearchForm($this->mockController); $pageWithSpecialChars = $this->objFromFixture(SiteTree::class, 'pageWithSpecialChars'); $pageWithSpecialChars->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $results = $sf->getResults(null, array('Search'=>'Brötchen')); + $results = $sf->getResults(); $this->assertContains( $pageWithSpecialChars->ID, $results->column('ID'), '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( $pageWithSpecialChars->ID, $results->column('ID'),