Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix additional translation records being created for the default locale.
The default locale should be saved onto the host table and not into the
translations tables. By returning early when both the default locale and
bundled translations are present we can fix the regression introduced in #9640

Refs #9688
  • Loading branch information
markstory committed Nov 2, 2016
1 parent 1a1263e commit 2660312
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -283,7 +283,10 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o
$fields = array_keys($values);
$noFields = empty($fields);

if ($noFields && $noBundled) {
// If there are no fields and no bundled translations, or both fields
// in the default locale and bundled translations we can
// skip the remaining logic as its not necessary.
if ($noFields && $noBundled || ($fields && $bundled)) {
return;
}

Expand Down
60 changes: 60 additions & 0 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -1257,6 +1257,66 @@ public function testSaveExistingRecordWithTranslatesField()
$this->assertEquals('Description #1', $results['eng']['description']);
}

/**
* Tests that default locale saves ok.
*
* @return void
*/
public function testSaveDefaultLocale()
{
$table = TableRegistry::get('Articles');
$table->hasMany('Comments');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);

$article = $table->get(1);
$data = [
'title' => 'New title',
'body' => 'New body',
];
$article = $table->patchEntity($article, $data);
$table->save($article);
$this->assertNull($article->get('_i18n'));

$article = $table->get(1);
$this->assertEquals('New title', $article->get('title'));
$this->assertEquals('New body', $article->get('body'));
}

/**
* Tests that translations are added to the whitelist of associations to be
* saved
*
* @return void
*/
public function testSaveTranslationDefaultLocale()
{
$table = TableRegistry::get('Articles');
$table->hasMany('Comments');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);

$article = $table->get(1);
$data = [
'title' => 'New title',
'body' => 'New body',
'_translations' => [
'es' => [
'title' => 'ES title',
'body' => 'ES body'
]
]
];
$article = $table->patchEntity($article, $data);
$table->save($article);
$this->assertNull($article->get('_i18n'));

$article = $table->find('translations')->where(['id' => 1])->first();
$this->assertEquals('New title', $article->get('title'));
$this->assertEquals('New body', $article->get('body'));

$this->assertEquals('ES title', $article->_translations['es']->title);
$this->assertEquals('ES body', $article->_translations['es']->body);
}

/**
* Test that no properties are enabled when the translations
* option is off.
Expand Down

0 comments on commit 2660312

Please sign in to comment.