Skip to content
Permalink
Browse files

Adding test to show that hasMany associations can also be translated

  • Loading branch information...
lorenzo committed Jan 26, 2014
1 parent 5b733cc commit 8501c6e3fd9618ac5a31ac777f261b0335e422b8
Showing with 37 additions and 2 deletions.
  1. +5 −1 tests/Fixture/TranslateFixture.php
  2. +32 −1 tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
@@ -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'),
);
}
@@ -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;
@@ -32,7 +33,8 @@ class TranslateBehaviorTest extends TestCase {
*/
public $fixtures = [
'core.translate',
'core.article'
'core.article',
'core.comment'
];
public function tearDown() {
@@ -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.
You can’t perform that action at this time.