From a008f928c6c203ac4c779823a047b7fa8978487d Mon Sep 17 00:00:00 2001 From: Jeremy Thomerson Date: Wed, 31 Jul 2013 18:21:44 +0000 Subject: [PATCH] FIX: get_one_by_locale didn't work when filter disabled --- code/model/Translatable.php | 6 +++- tests/unit/TranslatableTest.php | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/code/model/Translatable.php b/code/model/Translatable.php index 9d71f52..e4128ab 100755 --- a/code/model/Translatable.php +++ b/code/model/Translatable.php @@ -322,7 +322,11 @@ class Translatable extends DataExtension implements PermissionProvider { $orig = Translatable::get_current_locale(); Translatable::set_current_locale($locale); - $do = $class::get()->where($filter)->sort($orderby)->First(); + $do = $class::get() + ->where($filter) + ->where(sprintf('"Locale" = \'%s\'', Convert::raw2sql($locale))) + ->sort($orderby) + ->First(); Translatable::set_current_locale($orig); return $do; } diff --git a/tests/unit/TranslatableTest.php b/tests/unit/TranslatableTest.php index 70b6e6b..9f765cf 100755 --- a/tests/unit/TranslatableTest.php +++ b/tests/unit/TranslatableTest.php @@ -10,6 +10,7 @@ class TranslatableTest extends FunctionalTest { protected $extraDataObjects = array( 'TranslatableTest_DataObject', + 'TranslatableTest_OneByLocaleDataObject', 'TranslatableTest_Page', ); @@ -17,6 +18,7 @@ class TranslatableTest extends FunctionalTest { 'SiteTree' => array('Translatable'), 'SiteConfig' => array('Translatable'), 'TranslatableTest_DataObject' => array('Translatable'), + 'TranslatableTest_OneByLocaleDataObject' => array('Translatable'), ); private $origLocale; @@ -47,6 +49,59 @@ class TranslatableTest extends FunctionalTest { return $this->assertEquals($expected, $actual, $message); } + function testGetOneByLocale() { + Translatable::disable_locale_filter(); + $this->assertEquals( + 0, + TranslatableTest_OneByLocaleDataObject::get()->count(), + 'should not be any test objects yet' + ); + Translatable::enable_locale_filter(); + + $obj = new TranslatableTest_OneByLocaleDataObject(); + $obj->TranslatableProperty = 'test - en'; + $obj->write(); + + Translatable::disable_locale_filter(); + $this->assertEquals( + 1, + TranslatableTest_OneByLocaleDataObject::get()->count(), + 'should not be any test objects yet' + ); + Translatable::enable_locale_filter(); + + $found = Translatable::get_one_by_locale('TranslatableTest_OneByLocaleDataObject', $obj->Locale); + $this->assertNotNull($found, 'should have found one for ' . $obj->Locale); + $this->assertEquals($obj->ID, $found->ID); + + $translated = $obj->createTranslation('de_DE'); + $translated->write(); + + Translatable::disable_locale_filter(); + $this->assertEquals( + 2, + TranslatableTest_OneByLocaleDataObject::get()->count(), + 'should not be any test objects yet' + ); + Translatable::enable_locale_filter(); + + $found = Translatable::get_one_by_locale( + 'TranslatableTest_OneByLocaleDataObject', + $translated->Locale + ); + $this->assertNotNull($found, 'should have found one for ' . $translated->Locale); + $this->assertEquals($translated->ID, $found->ID); + + // test again to make sure that get_one_by_locale works when locale filter disabled + Translatable::disable_locale_filter(); + $found = Translatable::get_one_by_locale( + 'TranslatableTest_OneByLocaleDataObject', + $translated->Locale + ); + $this->assertEquals($translated->ID, $found->ID); + Translatable::enable_locale_filter(); + } + function testLocaleFilteringEnabledAndDisabled() { $this->assertTrue(Translatable::locale_filter_enabled()); @@ -1108,6 +1163,12 @@ class TranslatableTest extends FunctionalTest { } } +class TranslatableTest_OneByLocaleDataObject extends DataObject implements TestOnly { + private static $db = array( + 'TranslatableProperty' => 'Text' + ); +} + class TranslatableTest_DataObject extends DataObject implements TestOnly { // add_extension() used to add decorator at end of file