Update SearchFormTest

This commit is contained in:
Damian Mooyman 2017-03-13 11:11:56 +13:00
parent f65a164818
commit c7d9947976
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
2 changed files with 58 additions and 23 deletions

View File

@ -64,7 +64,6 @@ class SearchForm extends Form
FieldList $fields = null, FieldList $fields = null,
FieldList $actions = 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'))
@ -166,11 +165,12 @@ class SearchForm extends Form
$pageLength = $this->getPageLength(); $pageLength = $this->getPageLength();
$start = $request->requestVar('start') ?: 0; $start = $request->requestVar('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) {

View File

@ -1,7 +1,10 @@
<?php <?php
use SilverStripe\Assets\File; use SilverStripe\Assets\File;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\CMS\Model\SiteTree; 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;
@ -30,6 +33,9 @@ class ZZZSearchFormTest extends FunctionalTest
SiteTree::class => array('SiteTreeSubsites', 'Translatable') SiteTree::class => array('SiteTreeSubsites', 'Translatable')
); );
/**
* @var ContentController
*/
protected $mockController; protected $mockController;
public function waitUntilIndexingFinished() 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 // 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);
@ -55,8 +62,9 @@ class ZZZSearchFormTest extends FunctionalTest
{ {
parent::setUp(); parent::setUp();
/** @var Page $holderPage */
$holderPage = $this->objFromFixture(SiteTree::class, 'searchformholder'); $holderPage = $this->objFromFixture(SiteTree::class, 'searchformholder');
$this->mockController = new ContentController($holderPage); $this->mockController = ModelAsController::controller_for($holderPage);
$this->waitUntilIndexingFinished(); $this->waitUntilIndexingFinished();
} }
@ -106,13 +114,16 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$request = new HTTPRequest('GET', 'search', ['Search'=>'publicPublishedPage']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$publishedPage = $this->objFromFixture(SiteTree::class, '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'),
@ -129,6 +140,8 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$request = new HTTPRequest('GET', 'search', ['Search'=>'"finding butterflies"']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$publishedPage = $this->objFromFixture(SiteTree::class, 'publicPublishedPage'); $publishedPage = $this->objFromFixture(SiteTree::class, 'publicPublishedPage');
@ -137,7 +150,7 @@ class ZZZSearchFormTest extends FunctionalTest
$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'),
@ -154,9 +167,11 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$request = new HTTPRequest('GET', 'search', ['Search'=>'publicUnpublishedPage']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$results = $sf->getResults(null, array('Search'=>'publicUnpublishedPage')); $results = $sf->getResults();
$unpublishedPage = $this->objFromFixture(SiteTree::class, 'publicUnpublishedPage'); $unpublishedPage = $this->objFromFixture(SiteTree::class, 'publicUnpublishedPage');
$this->assertNotContains( $this->assertNotContains(
$unpublishedPage->ID, $unpublishedPage->ID,
@ -171,11 +186,13 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$request = new HTTPRequest('GET', 'search', ['Search'=>'restrictedViewLoggedInUsers']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$page = $this->objFromFixture(SiteTree::class, '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'),
@ -184,7 +201,7 @@ class ZZZSearchFormTest extends FunctionalTest
$member = $this->objFromFixture(Member::class, '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'),
@ -199,11 +216,13 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$request = new HTTPRequest('GET', 'search', ['Search'=>'restrictedViewOnlyWebsiteUsers']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$page = $this->objFromFixture(SiteTree::class, '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'),
@ -212,7 +231,7 @@ class ZZZSearchFormTest extends FunctionalTest
$member = $this->objFromFixture(Member::class, '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'),
@ -222,7 +241,7 @@ class ZZZSearchFormTest extends FunctionalTest
$member = $this->objFromFixture(Member::class, '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'),
@ -233,6 +252,8 @@ class ZZZSearchFormTest extends FunctionalTest
public function testInheritedRestrictedPagesNotIncluded() public function testInheritedRestrictedPagesNotIncluded()
{ {
$request = new HTTPRequest('GET', 'search', ['Search'=>'inheritRestrictedView']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$parent = $this->objFromFixture(SiteTree::class, 'restrictedViewLoggedInUsers'); $parent = $this->objFromFixture(SiteTree::class, 'restrictedViewLoggedInUsers');
@ -240,7 +261,7 @@ class ZZZSearchFormTest extends FunctionalTest
$page = $this->objFromFixture(SiteTree::class, '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'),
@ -249,7 +270,7 @@ class ZZZSearchFormTest extends FunctionalTest
$member = $this->objFromFixture(Member::class, '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'),
@ -264,14 +285,16 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$request = new HTTPRequest('GET', 'search', ['Search'=>'dontShowInSearchPage']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$page = $this->objFromFixture(SiteTree::class, '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'
); );
} }
@ -281,6 +304,8 @@ class ZZZSearchFormTest extends FunctionalTest
return; return;
} }
$request = new HTTPRequest('GET', 'search', ['Search'=>'dontShowInSearchFile']);
$this->mockController->setRequest($request);
$sf = new SearchForm($this->mockController); $sf = new SearchForm($this->mockController);
$dontShowInSearchFile = $this->objFromFixture(File::class, 'dontShowInSearchFile'); $dontShowInSearchFile = $this->objFromFixture(File::class, 'dontShowInSearchFile');
@ -288,14 +313,18 @@ class ZZZSearchFormTest extends FunctionalTest
$showInSearchFile = $this->objFromFixture(File::class, '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'),
@ -313,19 +342,25 @@ class ZZZSearchFormTest extends FunctionalTest
$this->markTestSkipped("PostgreSQLDatabase doesn't support entity-encoded searches"); $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); $sf = new SearchForm($this->mockController);
$pageWithSpecialChars = $this->objFromFixture(SiteTree::class, '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'),