Permalink
Browse files

FIX support disabled locale filter for delayed querying

  • Loading branch information...
1 parent 929beb2 commit cde0f57c431563e47a8bc3f18769ce2b5ad86a4b @jthomerson jthomerson committed May 31, 2013
Showing with 53 additions and 3 deletions.
  1. +4 −1 _config/extensions.yml
  2. +11 −2 code/model/Translatable.php
  3. +38 −0 tests/unit/TranslatableTest.php
@@ -12,4 +12,7 @@ LeftAndMain:
['TranslatableCMSMainExtension']
HtmlEditorField_Toolbar:
extensions:
- ['TranslatableEditorToolbarExtension']
+ ['TranslatableEditorToolbarExtension']
+DataQuery:
+ extensions:
+ ['Translatable']
@@ -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 @@ static function add_to_class($class, $extensionClass, $args = null) {
*
* 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 @@ function augmentSQL(SQLQuery &$query) {
$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 @@ function augmentSQL(SQLQuery &$query) {
$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 @@ protected function baseTransform($nonEditableField, $originalField) {
return $nonEditableField_holder;
}
-}
+}
@@ -40,6 +40,44 @@ function 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() {
$origPage = $this->objFromFixture('Page', 'testpage_en');

0 comments on commit cde0f57

Please sign in to comment.