diff --git a/src/ORM/Behavior/TranslateBehavior.php b/src/ORM/Behavior/TranslateBehavior.php index 573ffa4dc15..59315d8ea00 100644 --- a/src/ORM/Behavior/TranslateBehavior.php +++ b/src/ORM/Behavior/TranslateBehavior.php @@ -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; } diff --git a/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php b/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php index 4c4b012fa2a..ae2a7e5ea37 100644 --- a/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php +++ b/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php @@ -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.