Skip to content

Commit

Permalink
Completing test and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 26, 2014
1 parent b0dc5e6 commit 878b587
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
1 change: 1 addition & 0 deletions tests/Fixture/TranslateFixture.php
Expand Up @@ -74,5 +74,6 @@ class TranslateFixture extends TestFixture {
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'),
array('locale' => 'spa', 'model' => 'Comments', 'foreign_key' => 4, 'field' => 'comment', 'content' => 'Comentario #4'),
);
}
75 changes: 44 additions & 31 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -42,6 +42,24 @@ public function tearDown() {
TableRegistry::clear();
}


/**
* Returns an array with all the translations found for a set of records
*
* @return Collection
*/
protected function _extractTranslations($data) {
return (new Collection($data))->map(function($row) {
$translations = $row->get('_translations');
if (!$translations) {
return [];
}
return array_map(function($t) {
return $t->toArray();
}, $translations);
});
}

/**
* Tests that fields from a translated model are overriden
*
Expand Down Expand Up @@ -143,17 +161,8 @@ public function testFindTranslations() {
]
];

$translations = $results->map(function($row) {
$translations = $row->get('_translations');
if (!$translations) {
return [];
}
return array_map(function($t) {
return $t->toArray();
}, $translations);
});
$translations = $this->_extractTranslations($results);
$this->assertEquals($expected, $translations->toArray());

$expected = [
1 => ['First Article' => 'First Article Body'],
2 => ['Second Article' => 'Second Article Body'],
Expand Down Expand Up @@ -188,15 +197,7 @@ public function testFindFilteredTranslations() {
]
];

$translations = $results->map(function($row) {
$translations = $row->get('_translations');
if (!$translations) {
return [];
}
return array_map(function($t) {
return $t->toArray();
}, $translations);
});
$translations = $this->_extractTranslations($results);
$this->assertEquals($expected, $translations->toArray());

$expected = [
Expand Down Expand Up @@ -258,15 +259,7 @@ public function testFindTranslationsWithFieldOverriding() {
]
];

$translations = $results->map(function($row) {
$translations = $row->get('_translations');
if (!$translations) {
return [];
}
return array_map(function($t) {
return $t->toArray();
}, $translations);
});
$translations = $this->_extractTranslations($results);
$this->assertEquals($expected, $translations->toArray());

$expected = [
Expand Down Expand Up @@ -308,6 +301,11 @@ public function testFindSingleLocaleHasMany() {
$this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
}

/**
* Test that it is possible to bring translations from hasMany relations
*
* @return void
*/
public function testTranslationsHasMany() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
Expand All @@ -317,14 +315,29 @@ public function testTranslationsHasMany() {

$results = $table->find('translations')->contain([
'Comments' => function($q) {
debug(\Cake\Utility\Debugger::trace());
return $q->find('translations')->select(['id', 'comment', 'article_id']);
}
]);

$comments = $results->first()->comments;
$expected = [
[
'eng' => ['comment' => 'Comment #1', 'locale' => 'eng']
],
[
'eng' => ['comment' => 'Comment #2', 'locale' => 'eng']
],
[
'eng' => ['comment' => 'Comment #3', 'locale' => 'eng']
],
[
'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']
]
];

$article = $results->first();
debug(json_encode($article, JSON_PRETTY_PRINT));
$translations = $this->_extractTranslations($comments);
$this->assertEquals($expected, $translations->toArray());
}

}

0 comments on commit 878b587

Please sign in to comment.