Update SearchForm

Fix RedirectorPageTest
This commit is contained in:
Damian Mooyman 2017-03-09 18:17:31 +13:00
parent 38c7cf710a
commit f65a164818
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
3 changed files with 95 additions and 85 deletions

View File

@ -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');
}
/**

View File

@ -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');
}

View File

@ -1,5 +1,7 @@
<?php
use SilverStripe\Assets\File;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\MSSQL\MSSQLDatabase;
@ -8,6 +10,7 @@ use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Search\SearchForm;
use SilverStripe\ORM\Search\FulltextSearchable;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Security\Member;
/**
* @package cms
@ -24,7 +27,7 @@ class ZZZSearchFormTest extends FunctionalTest
protected static $fixture_file = 'SearchFormTest.yml';
protected $illegalExtensions = array(
'SilverStripe\\CMS\\Model\\SiteTree' => 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(
'<body class="SearchForm Form RequestHandler BlankPage">',
'<body class="SearchForm Form BlankPage">',
$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'));