diff --git a/code/model/Translatable.php b/code/model/Translatable.php index 62f8c94..2e83234 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..ec25d5b 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,41 @@ 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 +1145,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