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

@ -13,3 +13,6 @@ LeftAndMain:
HtmlEditorField_Toolbar: HtmlEditorField_Toolbar:
extensions: extensions:
['TranslatableEditorToolbarExtension'] ['TranslatableEditorToolbarExtension']
DataQuery:
extensions:
['Translatable']

View File

@ -163,6 +163,8 @@
*/ */
class Translatable extends DataExtension implements PermissionProvider { class Translatable extends DataExtension implements PermissionProvider {
const QUERY_LOCALE_FILTER_ENABLED = 'Translatable.LocaleFilterEnabled';
/** /**
* The 'default' language. * The 'default' language.
* @var string * @var string
@ -560,7 +562,7 @@ class Translatable extends DataExtension implements PermissionProvider {
* *
* Use {@link disable_locale_filter()} to temporarily disable this "auto-filtering". * 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, // 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 // limit the current call to its locale. This fixes a lot of problems
// with other extensions like Versioned // with other extensions like Versioned
@ -575,6 +577,8 @@ class Translatable extends DataExtension implements PermissionProvider {
$locale $locale
// unless the filter has been temporarily disabled // unless the filter has been temporarily disabled
&& self::locale_filter_enabled() && 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 // DataObject::get_by_id() should work independently of language
&& !$query->filtersOnID() && !$query->filtersOnID()
// the query contains this table // the query contains this table
@ -590,6 +594,11 @@ class Translatable extends DataExtension implements PermissionProvider {
} }
} }
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 * Create <table>_translation database table to enable
* tracking of "translation groups" in which each related * tracking of "translation groups" in which each related

View File

@ -41,6 +41,44 @@ class TranslatableTest extends FunctionalTest {
parent::tearDown(); 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() { function testLocaleGetParamRedirectsToTranslation() {
$origPage = $this->objFromFixture('Page', 'testpage_en'); $origPage = $this->objFromFixture('Page', 'testpage_en');
$origPage->publish('Stage', 'Live'); $origPage->publish('Stage', 'Live');