Skip to content

Commit

Permalink
Adding test to show that hasMany associations can also be translated
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 26, 2014
1 parent 5b733cc commit 8501c6e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 5 additions & 1 deletion tests/Fixture/TranslateFixture.php
Expand Up @@ -69,6 +69,10 @@ class TranslateFixture extends TestFixture {
array('locale' => 'deu', 'model' => 'Articles', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titel #3'),
array('locale' => 'deu', 'model' => 'Articles', 'foreign_key' => 3, 'field' => 'body', 'content' => 'Inhalt #3'),
array('locale' => 'cze', 'model' => 'Articles', 'foreign_key' => 3, 'field' => 'title', 'content' => 'Titulek #3'),
array('locale' => 'cze', 'model' => 'Articles', 'foreign_key' => 3, 'field' => 'body', 'content' => 'Obsah #3')
array('locale' => 'cze', 'model' => 'Articles', 'foreign_key' => 3, 'field' => 'body', 'content' => 'Obsah #3'),
array('locale' => 'eng', 'model' => 'Comments', 'foreign_key' => 1, 'field' => 'comment', 'content' => 'Comment #1'),
array('locale' => 'eng', 'model' => 'Comments', 'foreign_key' => 2, 'field' => 'comment', 'content' => 'Comment #2'),
array('locale' => 'eng', 'model' => 'Comments', 'foreign_key' => 3, 'field' => 'comment', 'content' => 'Comment #3'),
array('locale' => 'eng', 'model' => 'Comments', 'foreign_key' => 4, 'field' => 'comment', 'content' => 'Comment #4'),
);
}
33 changes: 32 additions & 1 deletion tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\Model\Behavior;

use Cake\Collection\Collection;
use Cake\Event\Event;
use Cake\Model\Behavior\TranslateBehavior;
use Cake\ORM\Entity;
Expand All @@ -32,7 +33,8 @@ class TranslateBehaviorTest extends TestCase {
*/
public $fixtures = [
'core.translate',
'core.article'
'core.article',
'core.comment'
];

public function tearDown() {
Expand Down Expand Up @@ -277,4 +279,33 @@ public function testFindTranslationsWithFieldOverriding() {
$this->assertEquals($expected, $grouped->toArray());
}

/**
* Tests that fields can be overriden in a hasMany association
*
* @return void
*/
public function testFindSingleLocaleHasMany() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$table->hasMany('Comments');
$comments = $table->hasMany('Comments')->target();
$comments->addBehavior('Translate', ['fields' => ['comment']]);

$table->locale('eng');
$comments->locale('eng');

$results = $table->find()->contain(['Comments' => function($q) {
return $q->select(['id', 'comment', 'article_id']);
}]);

$list = new Collection($results->first()->comments);
$expected = [
1 => 'Comment #1',
2 => 'Comment #2',
3 => 'Comment #3',
4 => 'Comment #4'
];
$this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
}

}

0 comments on commit 8501c6e

Please sign in to comment.