TranslateBehaviorTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Event\Event;
  17. use Cake\Model\Behavior\TranslateBehavior;
  18. use Cake\ORM\Entity;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Translate behavior test case
  23. */
  24. class TranslateBehaviorTest extends TestCase {
  25. /**
  26. * fixtures
  27. *
  28. * @var array
  29. */
  30. public $fixtures = [
  31. 'core.translate',
  32. 'core.article'
  33. ];
  34. public function tearDown() {
  35. parent::tearDown();
  36. TableRegistry::clear();
  37. }
  38. /**
  39. * Tests that fields from a translated model are overriden
  40. *
  41. * @return void
  42. */
  43. public function testFindSingleLocale() {
  44. $table = TableRegistry::get('Articles');
  45. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  46. $table->locale('eng');
  47. $results = $table->find()->combine('title', 'body', 'id')->toArray();
  48. $expected = [
  49. 1 => ['Title #1' => 'Content #1'],
  50. 2 => ['Title #2' => 'Content #2'],
  51. 3 => ['Title #3' => 'Content #3'],
  52. ];
  53. $this->assertSame($expected, $results);
  54. }
  55. /**
  56. * Tests that overriding fields with the translate behavior works when
  57. * using conditions and that all other columns are preserved
  58. *
  59. * @return void
  60. */
  61. public function testFindSingleLocaleWithConditions() {
  62. $table = TableRegistry::get('Articles');
  63. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  64. $table->locale('eng');
  65. $results = $table->find()
  66. ->where(['Articles.id' => 2])
  67. ->all();
  68. $this->assertCount(1, $results);
  69. $row = $results->first();
  70. $expected = [
  71. 'id' => 2,
  72. 'title' => 'Title #2',
  73. 'body' => 'Content #2',
  74. 'author_id' => 3,
  75. 'published' => 'Y',
  76. '_locale' => 'eng'
  77. ];
  78. $this->assertEquals($expected, $row->toArray());
  79. }
  80. /**
  81. * Tests that translating fields work when other formatters are used
  82. *
  83. * @return void
  84. */
  85. public function testFindList() {
  86. $table = TableRegistry::get('Articles');
  87. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  88. $table->locale('eng');
  89. $results = $table->find('list')->toArray();
  90. $expected = [1 => 'Title #1', 2 => 'Title #2', 3 => 'Title #3'];
  91. $this->assertSame($expected, $results);
  92. }
  93. /**
  94. * Tests that the query count return the correct results
  95. *
  96. * @return void
  97. */
  98. public function testFindCount() {
  99. $table = TableRegistry::get('Articles');
  100. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  101. $table->locale('eng');
  102. $this->assertEquals(3, $table->find()->count());
  103. }
  104. /**
  105. * Tests that it is possible to get all translated fields at once
  106. *
  107. * @return void
  108. */
  109. public function testFindTranslations() {
  110. $table = TableRegistry::get('Articles');
  111. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  112. $results = $table->find('translations');
  113. $expected = [
  114. [
  115. 'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'locale' => 'eng'],
  116. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  117. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  118. ],
  119. [
  120. 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
  121. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  122. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  123. ],
  124. [
  125. 'eng' => ['title' => 'Title #3', 'body' => 'Content #3', 'locale' => 'eng'],
  126. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  127. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  128. ]
  129. ];
  130. $translations = $results->map(function($row) {
  131. $translations = $row->get('_translations');
  132. if (!$translations) {
  133. return [];
  134. }
  135. return array_map(function($t) {
  136. return $t->toArray();
  137. }, $translations);
  138. });
  139. $this->assertEquals($expected, $translations->toArray());
  140. $expected = [
  141. 1 => ['First Article' => 'First Article Body'],
  142. 2 => ['Second Article' => 'Second Article Body'],
  143. 3 => ['Third Article' => 'Third Article Body']
  144. ];
  145. $grouped = $results->combine('title', 'body', 'id');
  146. $this->assertEquals($expected, $grouped->toArray());
  147. }
  148. /**
  149. * Tests that it is possible to request just a few translations
  150. *
  151. * @return void
  152. */
  153. public function testFindFilteredTranslations() {
  154. $table = TableRegistry::get('Articles');
  155. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  156. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  157. $expected = [
  158. [
  159. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  160. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  161. ],
  162. [
  163. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  164. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  165. ],
  166. [
  167. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  168. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  169. ]
  170. ];
  171. $translations = $results->map(function($row) {
  172. $translations = $row->get('_translations');
  173. if (!$translations) {
  174. return [];
  175. }
  176. return array_map(function($t) {
  177. return $t->toArray();
  178. }, $translations);
  179. });
  180. $this->assertEquals($expected, $translations->toArray());
  181. $expected = [
  182. 1 => ['First Article' => 'First Article Body'],
  183. 2 => ['Second Article' => 'Second Article Body'],
  184. 3 => ['Third Article' => 'Third Article Body']
  185. ];
  186. $grouped = $results->combine('title', 'body', 'id');
  187. $this->assertEquals($expected, $grouped->toArray());
  188. }
  189. /**
  190. * Tests that it is possible to combine find('list') and find('translations')
  191. *
  192. * @return void
  193. */
  194. public function testFindTranslationsList() {
  195. $table = TableRegistry::get('Articles');
  196. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  197. $results = $table
  198. ->find('list', [
  199. 'idField' => 'title',
  200. 'valueField' => '_translations.deu.title',
  201. 'groupField' => 'id'
  202. ])
  203. ->find('translations', ['locales' => ['deu']]);
  204. $expected = [
  205. 1 => ['First Article' => 'Titel #1'],
  206. 2 => ['Second Article' => 'Titel #2'],
  207. 3 => ['Third Article' => 'Titel #3']
  208. ];
  209. $this->assertEquals($expected, $results->toArray());
  210. }
  211. }