FIX: get_one_by_locale didn't work when filter disabled

This commit is contained in:
Jeremy Thomerson 2013-07-31 18:21:44 +00:00 committed by Ingo Schommer
parent 69fe99735b
commit a008f928c6
2 changed files with 66 additions and 1 deletions

View File

@ -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;
}

View File

@ -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