Skip to content

Commit

Permalink
MINOR Partially merged r70306, r74986, r75027 from trunk, seems like …
Browse files Browse the repository at this point in the history
…parts have been missed in previous merges

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@76593 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
chillu authored and Sam Minnee committed Feb 2, 2011
1 parent 0de8502 commit 7e50b9e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
13 changes: 13 additions & 0 deletions core/model/Translatable.php
Expand Up @@ -970,8 +970,21 @@ function createTranslation($locale) {

$class = $this->owner->class;
$newTranslation = new $class;

// copy all fields from owner (apart from ID)
$newTranslation->update($this->owner->toMap());

// If the object has Hierarchy extension,
// check for existing translated parents and assign
// their ParentID (and overwrite any existing ParentID relations
// to parents in other language). If no parent translations exist,
// they are automatically created in onBeforeWrite()
if($newTranslation->hasField('ParentID')) {
$origParent = $this->owner->Parent();
$newTranslationParent = $origParent->getTranslation($locale);
if($newTranslationParent) $newTranslation->ParentID = $newTranslationParent->ID;
}

$newTranslation->ID = 0;
$newTranslation->Locale = $locale;
// hacky way to set an existing translation group in onAfterWrite()
Expand Down
37 changes: 21 additions & 16 deletions tests/model/TranslatableTest.php
Expand Up @@ -524,9 +524,19 @@ function testCreateTranslationTranslatesUntranslatedParents() {
$this->assertFalse($parentPage->hasTranslation('de_DE'));

$translatedGrandChildPage = $grandchildPage->createTranslation('de_DE');

$this->assertTrue($grandchildPage->hasTranslation('de_DE'));
$this->assertTrue($child1Page->hasTranslation('de_DE'));
$this->assertTrue($parentPage->hasTranslation('de_DE'));

$this->assertEquals(
$grandchildPage->getTranslation('de_DE')->Parent()->ID,
$child1Page->getTranslation('de_DE')->ID
);
$this->assertEquals(
$child1Page->getTranslation('de_DE')->Parent()->ID,
$parentPage->getTranslation('de_DE')->ID
);
}

function testHierarchyAllChildrenIncludingDeleted() {
Expand Down Expand Up @@ -592,32 +602,27 @@ function testHierarchyAllChildrenIncludingDeleted() {
SiteTree::flush_and_destroy_cache();
$parentPage = $this->objFromFixture('Page', 'parent');
$this->assertEquals(
$parentPage->AllChildrenIncludingDeleted()->column('ID'),
$translatedParentPage->AllChildrenIncludingDeleted()->column('ID'),
array(
$child2PageTranslatedID,
$child1PageTranslatedID // $child1PageTranslated was deleted from stage, so the original record doesn't have the ID set
),
"Showing AllChildrenIncludingDeleted() in translation mode with parent page in default language shows children in default language"
"Showing AllChildrenIncludingDeleted() in translation mode with parent page in translated language shows children in translated language"
);

// @todo getTranslation() doesn't switch languages for future calls, its handled statically through set_reading_locale()
// // on translated page in translation mode using getTranslation()
// SiteTree::flush_and_destroy_cache();
// $parentPage = $this->objFromFixture('Page', 'parent');
// $translatedParentPage = $parentPage->getTranslation('de_DE');
// $this->assertEquals(
// $translatedParentPage->AllChildrenIncludingDeleted()->column('ID'),
// array(
// $child2PageTranslatedID,
// $child1PageTranslatedID,
// ),
// "Showing AllChildrenIncludingDeleted() in translation mode with translated parent page shows only translated children"
// );
Translatable::set_reading_locale('de_DE');
SiteTree::flush_and_destroy_cache();
$parentPage = $this->objFromFixture('Page', 'parent');
$this->assertEquals(
$parentPage->AllChildrenIncludingDeleted()->column('ID'),
array(),
"Showing AllChildrenIncludingDeleted() in translation mode with parent page in translated language shows children in default language"
);

// reset language
Translatable::set_reading_locale('en_US');
}

function testRootUrlDefaultsToTranslatedUrlSegment() {
$origPage = $this->objFromFixture('Page', 'homepage_en');
$origPage->publish('Stage', 'Live');
Expand Down

0 comments on commit 7e50b9e

Please sign in to comment.