Permalink
Browse files

Adding more tests to find('translations')

  • Loading branch information...
1 parent 4deb658 commit b075bfbac99ac1230cfb2daedc7da9706ae8ebab @lorenzo lorenzo committed Jan 25, 2014
@@ -128,7 +128,7 @@ public function findTranslations($query, $options) {
}])
->formatResults(function($results) {
return $this->_groupTranslations($results);
- });
+ }, $query::PREPEND);
}
protected function _rowMapper($results, $locale) {
@@ -114,6 +114,11 @@ public function testFindCount() {
$this->assertEquals(3, $table->find()->count());
}
+/**
+ * Tests that it is possible to get all translated fields at once
+ *
+ * @return void
+ */
public function testFindTranslations() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
@@ -146,6 +151,84 @@ public function testFindTranslations() {
}, $translations);
});
$this->assertEquals($expected, $translations->toArray());
+
+ $expected = [
+ 1 => ['First Article' => 'First Article Body'],
+ 2 => ['Second Article' => 'Second Article Body'],
+ 3 => ['Third Article' => 'Third Article Body']
+ ];
+
+ $grouped = $results->combine('title', 'body', 'id');
+ $this->assertEquals($expected, $grouped->toArray());
+ }
+
+/**
+ * Tests that it is possible to request just a few translations
+ *
+ * @return void
+ */
+ public function testFindFilteredTranslations() {
+ $table = TableRegistry::get('Articles');
+ $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+ $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
+ $expected = [
+ [
+ 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
+ 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
+ ],
+ [
+ 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
+ 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
+ ],
+ [
+ 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
+ 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
+ ]
+ ];
+
+ $translations = $results->map(function($row) {
+ $translations = $row->get('_translations');
+ if (!$translations) {
+ return [];
+ }
+ return array_map(function($t) {
+ return $t->toArray();
+ }, $translations);
+ });
+ $this->assertEquals($expected, $translations->toArray());
+
+ $expected = [
+ 1 => ['First Article' => 'First Article Body'],
+ 2 => ['Second Article' => 'Second Article Body'],
+ 3 => ['Third Article' => 'Third Article Body']
+ ];
+
+ $grouped = $results->combine('title', 'body', 'id');
+ $this->assertEquals($expected, $grouped->toArray());
+ }
+
+/**
+ * Tests that it is possible to combine find('list') and find('translations')
+ *
+ * @return void
+ */
+ public function testFindTranslationsList() {
+ $table = TableRegistry::get('Articles');
+ $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+ $results = $table
+ ->find('list', [
+ 'idField' => 'title',
+ 'valueField' => '_translations.deu.title',
+ 'groupField' => 'id'
+ ])
+ ->find('translations', ['locales' => ['deu']]);
+
+ $expected = [
+ 1 => ['First Article' => 'Titel #1'],
+ 2 => ['Second Article' => 'Titel #2'],
+ 3 => ['Third Article' => 'Titel #3']
+ ];
+ $this->assertEquals($expected, $results->toArray());
}
}

0 comments on commit b075bfb

Please sign in to comment.