FEATURE Enabled specifying a language through a hidden field in SearchForm which limits the search to pages in this language (incl. unit tests)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@70327 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-01-19 02:18:41 +00:00
parent 7057b56b2c
commit 4c4926d2b9
3 changed files with 109 additions and 0 deletions

View File

@ -2,6 +2,11 @@
/**
* Standard basic search form which conducts a fulltext search on all {@link SiteTree}
* objects.
*
* If multilingual content is enabled through the {@link Translatable} extension,
* only pages the currently set language on the holder for this searchform are found.
* The language is set through a hidden field in the form, which is prepoluated
* with {@link Translatable::current_lang()} when then form is constructed.
*
* @see Use ModelController and SearchContext for a more generic search implementation based around DataObject
* @package sapphire
@ -44,6 +49,10 @@ class SearchForm extends Form {
));
}
if(Translatable::is_enabled()) {
$fields->push(new HiddenField('lang', 'lang', Translatable::current_lang()));
}
if(!$actions) {
$actions = new FieldSet(
new FormAction("getResults", _t('SearchForm.GO', 'Go'))
@ -75,6 +84,11 @@ class SearchForm extends Form {
public function getResults($pageLength = null, $data = null){
// legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.com tutorials
if(!isset($data)) $data = $_REQUEST;
// set language (if present)
if(Translatable::is_enabled() && isset($data['lang'])) {
Translatable::set_reading_lang($data['lang']);
}
$keywords = $data['Search'];

View File

@ -0,0 +1,88 @@
<?php
/**
* @package sapphire
* @subpackage testing
*/
class TranslatableSearchFormTest extends FunctionalTest {
static $fixture_file = 'sapphire/tests/search/TranslatableSearchFormTest.yml';
protected $mockController;
protected $recreateTempDb = true;
function setUp() {
$this->origTranslatableSettings['enabled'] = Translatable::is_enabled();
$this->origTranslatableSettings['default_lang'] = Translatable::default_lang();
Translatable::enable();
Translatable::set_default_lang("en");
// needs to recreate the database schema with language properties
self::kill_temp_db();
// refresh the decorated statics - different fields in $db with Translatable enabled
singleton('SiteTree')->loadExtraStatics();
singleton('TranslatableTest_DataObject')->loadExtraStatics();
$dbname = self::create_temp_db();
DB::set_alternative_database_name($dbname);
parent::setUp();
$holderPage = $this->objFromFixture('SiteTree', 'searchformholder');
$this->mockController = new ContentController($holderPage);
}
function tearDown() {
if(!$this->origTranslatableSettings['enabled']) Translatable::disable();
Translatable::set_default_lang($this->origTranslatableSettings['default_lang']);
self::kill_temp_db();
self::create_temp_db();
parent::tearDown();
}
function testPublishedPagesMatchedByTitleInDefaultLanguage() {
$sf = new SearchForm($this->mockController, 'SearchForm');
$publishedPage = $this->objFromFixture('SiteTree', 'publishedPage');
$publishedPage->publish('Stage', 'Live');
$translatedPublishedPage = $publishedPage->createTranslation('de');
$translatedPublishedPage->Title = 'translatedPublishedPage';
$translatedPublishedPage->Content = 'German content';
$translatedPublishedPage->write();
$translatedPublishedPage->publish('Stage', 'Live');
// Translatable::set_reading_lang() can't be used because the context
// from the holder is not present here - we set the language explicitly
// through a pseudo GET variable in getResults()
$lang = 'en';
$results = $sf->getResults(null, array('Search'=>'content', 'lang'=>$lang));
$this->assertContains(
$publishedPage->ID,
$results->column('ID'),
'Published pages are found by searchform in default language'
);
$this->assertNotContains(
$translatedPublishedPage->ID,
$results->column('ID'),
'Published pages in another language are not found when searching in default language'
);
$lang = 'de';
$results = $sf->getResults(null, array('Search'=>'content', 'lang'=>$lang));
$this->assertNotContains(
$publishedPage->ID,
$results->column('ID'),
'Published pages in default language are not found when searching in another language'
);
$this->assertContains(
(string)$translatedPublishedPage->ID,
$results->column('ID'),
'Published pages in another language are found when searching in this language'
);
}
}
?>

View File

@ -0,0 +1,7 @@
SiteTree:
searchformholder:
URLSegment: searchformholder
Title: searchformholder
publishedPage:
Title: publishedPage
Content: English content