FIX support disabled locale filter for delayed querying

Fixes silverstripe/silverstripe-translatable#113
This commit is contained in:
Jeremy Thomerson 2013-05-31 19:09:54 +00:00
parent 929beb216c
commit cde0f57c43
3 changed files with 53 additions and 3 deletions

View File

@ -12,4 +12,7 @@ LeftAndMain:
['TranslatableCMSMainExtension']
HtmlEditorField_Toolbar:
extensions:
['TranslatableEditorToolbarExtension']
['TranslatableEditorToolbarExtension']
DataQuery:
extensions:
['Translatable']

View File

@ -163,6 +163,8 @@
*/
class Translatable extends DataExtension implements PermissionProvider {
const QUERY_LOCALE_FILTER_ENABLED = 'Translatable.LocaleFilterEnabled';
/**
* The 'default' language.
* @var string
@ -560,7 +562,7 @@ class Translatable extends DataExtension implements PermissionProvider {
*
* Use {@link disable_locale_filter()} to temporarily disable this "auto-filtering".
*/
function augmentSQL(SQLQuery &$query) {
function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
// If the record is saved (and not a singleton), and has a locale,
// limit the current call to its locale. This fixes a lot of problems
// with other extensions like Versioned
@ -575,6 +577,8 @@ class Translatable extends DataExtension implements PermissionProvider {
$locale
// unless the filter has been temporarily disabled
&& self::locale_filter_enabled()
// or it was disabled when the DataQuery was created
&& $dataQuery->getQueryParam(self::QUERY_LOCALE_FILTER_ENABLED)
// DataObject::get_by_id() should work independently of language
&& !$query->filtersOnID()
// the query contains this table
@ -589,6 +593,11 @@ class Translatable extends DataExtension implements PermissionProvider {
$query->addWhere($qry);
}
}
function augmentDataQueryCreation(SQLQuery &$sqlQuery, DataQuery &$dataQuery) {
$enabled = self::locale_filter_enabled();
$dataQuery->setQueryParam(self::QUERY_LOCALE_FILTER_ENABLED, $enabled);
}
/**
* Create <table>_translation database table to enable
@ -1813,4 +1822,4 @@ class Translatable_Transformation extends FormTransformation {
return $nonEditableField_holder;
}
}
}

View File

@ -40,6 +40,44 @@ class TranslatableTest extends FunctionalTest {
parent::tearDown();
}
function testLocaleFilteringEnabledAndDisabled() {
$this->assertTrue(Translatable::locale_filter_enabled());
// get our base page to use for testing
$origPage = $this->objFromFixture('Page', 'testpage_en');
$origPage->MenuTitle = 'unique-key-used-in-my-query';
$origPage->write();
$origPage->publish('Stage', 'Live');
// create a translation of it so that we can see if translations are filtered
$translatedPage = $origPage->createTranslation('de_DE');
$translatedPage->MenuTitle = $origPage->MenuTitle;
$translatedPage->write();
$translatedPage->publish('Stage', 'Live');
$where = sprintf("MenuTitle = '%s'", Convert::raw2sql($origPage->MenuTitle));
// make sure that our query was filtered
$this->assertEquals(1, Page::get()->where($where)->count());
// test no filtering with disabled locale filter
Translatable::disable_locale_filter();
$this->assertEquals(2, Page::get()->where($where)->count());
Translatable::enable_locale_filter();
// make sure that our query was filtered after re-enabling the filter
$this->assertEquals(1, Page::get()->where($where)->count());
// test effectiveness of disabling locale filter with 3.x delayed querying
// see https://github.com/silverstripe/silverstripe-translatable/issues/113
Translatable::disable_locale_filter();
// create the DataList while the locale filter is disabled
$dataList = Page::get()->where($where);
Translatable::enable_locale_filter();
// but don't use it until later - after the filter is re-enabled
$this->assertEquals(2, $dataList->count());
}
function testLocaleGetParamRedirectsToTranslation() {
$origPage = $this->objFromFixture('Page', 'testpage_en');