mirror of
https://github.com/silverstripe/silverstripe-translatable
synced 2024-10-22 11:05:59 +02:00
FIX: get_one_by_locale didn't work when filter disabled
This commit is contained in:
parent
69fe99735b
commit
a008f928c6
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user