API CHANGE Removed obsolete Translatable::table_exists()

ENHANCEMENT Made Translatable constructor arguments optional, as by default all database fields are marked translatable
MINOR More unit tests for Translatable

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@70118 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-01-13 22:28:24 +00:00
parent 696934582f
commit 455ee11b5a
3 changed files with 70 additions and 26 deletions

View File

@ -98,17 +98,6 @@ class Translatable extends DataObjectDecorator {
$record = $this->owner->toMap();
return (isset($record["Lang"])) ? $record["Lang"] : Translatable::default_lang();
}
/**
* Checks if a table given table exists in the db
*
* @param mixed $table Table name
* @return boolean Returns true if $table exists.
*/
static function table_exists($table) {
if (!self::$tableList) self::$tableList = DB::tableList();
return isset(self::$tableList[strtolower($table)]);
}
/**
* Choose the language the site is currently on.
@ -320,8 +309,9 @@ class Translatable extends DataObjectDecorator {
/**
* Construct a new Translatable object.
* @var array $translatableFields The different fields of the object that can be translated.
* This is currently not implemented, all fields are marked translatable (see {@link setOwner()}).
*/
function __construct($translatableFields) {
function __construct($translatableFields = null) {
parent::__construct();
// @todo Disabled selection of translatable fields - we're setting all fields as translatable in setOwner()
@ -360,7 +350,7 @@ class Translatable extends DataObjectDecorator {
function extraStatics() {
if(!Translatable::is_enabled()) return;
if(get_class($this->owner) == ClassInfo::baseDataClass(get_class($this->owner))) {
return array(
"db" => array(

View File

@ -20,10 +20,11 @@ class TranslatableTest extends FunctionalTest {
Translatable::enable();
Translatable::set_default_lang("en");
// needs to recreate the database schema with *_lang tables
// needs to recreate the database schema with language properties
self::kill_temp_db();
// refresh the decorated statics - different fields in $db with Translatable enabled
singleton('SiteTree')->loadExtraStatics();
singleton('TranslatableDataObject')->loadExtraStatics();
$dbname = self::create_temp_db();
DB::set_alternative_database_name($dbname);
@ -41,6 +42,39 @@ class TranslatableTest extends FunctionalTest {
parent::tearDown();
}
function testSiteTreeHierarchyTranslation() {
//$parentPage = $this->objFromFixture();
}
function testTranslatablePropertiesOnDataObject() {
$origObj = $this->objFromFixture('TranslatableDataObject', 'testobject_en');
$translatedObj = $origObj->createTranslation('fr');
$translatedObj->TranslatableProperty = 'Fr';
$translatedObj->TranslatableDecoratedProperty = 'Fr';
$translatedObj->write();
$this->assertEquals(
$origObj->TranslatableProperty,
'En',
'Creating a translation doesnt affect database field on original object'
);
$this->assertEquals(
$origObj->TranslatableDecoratedProperty,
'En',
'Creating a translation doesnt affect decorated database field on original object'
);
$this->assertEquals(
$translatedObj->TranslatableProperty,
'Fr',
'Translated object saves database field independently of original object'
);
$this->assertEquals(
$translatedObj->TranslatableDecoratedProperty,
'Fr',
'Translated object saves decorated database field independently of original object'
);
}
function testCreateTranslationOnSiteTree() {
$origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $origPage->createTranslation('de');
@ -74,9 +108,9 @@ class TranslatableTest extends FunctionalTest {
function testGetTranslationOnSiteTree() {
$origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $this->objFromFixture('Page', 'testpage_fr');
$translatedPage = $origPage->createTranslation('fr');
$getTranslationPage = $origPage->getTranslation('fr');
$this->assertNotNull($getTranslationPage);
$this->assertEquals($getTranslationPage->ID, $translatedPage->ID);
}
@ -103,7 +137,6 @@ class TranslatableTest extends FunctionalTest {
'af',
'de',
//'en', // default language is not included
'fr', // already in the fixtures
),
'Language codes are returned specifically for the queried page through getTranslatedLangs()'
);
@ -117,12 +150,11 @@ class TranslatableTest extends FunctionalTest {
'even if translations for other pages exist in the database'
);
}
function testTranslationCanHaveSameURLSegment() {
$origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $origPage->createTranslation('de');
$translatedPage->URLSegment = 'testpage';
$translatedPage->publish('Stage', 'Live');
$this->assertEquals($origPage->URLSegment, $translatedPage->URLSegment);
}
@ -143,7 +175,7 @@ class TranslatableTest extends FunctionalTest {
);
// then in "translation mode"
$pageTranslated = $this->objFromFixture('Page', 'testpage_fr');
$pageTranslated = $pageOrigLang->createTranslation('fr');
$fields = $pageTranslated->getCMSFields();
$this->assertType(
'TextField',
@ -159,4 +191,27 @@ class TranslatableTest extends FunctionalTest {
}
}
class TranslatableDataObject extends DataObject implements TestOnly {
static $extensions = array(
"Translatable",
);
static $db = array(
'TranslatableProperty' => 'Text'
);
}
class TranslatableDataObjectDecorator extends DataObjectDecorator implements TestOnly {
function extraStatics() {
return array(
'db' => array(
'TranslatableDecoratedProperty' => 'Text'
)
);
}
}
DataObject::add_extension('TranslatableDataObject', 'TranslatableDataObjectDecorator');
?>

View File

@ -3,11 +3,6 @@ Page:
Title: Home
URLSegment: testpage
Lang: en
testpage_fr:
Title: Maison
URLSegment: testpage
Lang: fr
OriginalID: =>Page.testpage_en
parent:
Title: Parent
URLSegment: parent
@ -18,4 +13,8 @@ Page:
grandchild:
Title: Grantchild
URLSegment: grandchild
Parent: =>Page.child
Parent: =>Page.child
TranslatableDataObject:
testobject_en:
TranslatableProperty: En
TranslatableDecoratedProperty: En