TranslateBehaviorTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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\Collection\Collection;
  17. use Cake\Event\Event;
  18. use Cake\Model\Behavior\TranslateBehavior;
  19. use Cake\ORM\Entity;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Translate behavior test case
  24. */
  25. class TranslateBehaviorTest extends TestCase {
  26. /**
  27. * fixtures
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.translate',
  33. 'core.article',
  34. 'core.comment'
  35. ];
  36. public function tearDown() {
  37. parent::tearDown();
  38. TableRegistry::clear();
  39. }
  40. /**
  41. * Tests that fields from a translated model are overriden
  42. *
  43. * @return void
  44. */
  45. public function testFindSingleLocale() {
  46. $table = TableRegistry::get('Articles');
  47. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  48. $table->locale('eng');
  49. $results = $table->find()->combine('title', 'body', 'id')->toArray();
  50. $expected = [
  51. 1 => ['Title #1' => 'Content #1'],
  52. 2 => ['Title #2' => 'Content #2'],
  53. 3 => ['Title #3' => 'Content #3'],
  54. ];
  55. $this->assertSame($expected, $results);
  56. }
  57. /**
  58. * Tests that overriding fields with the translate behavior works when
  59. * using conditions and that all other columns are preserved
  60. *
  61. * @return void
  62. */
  63. public function testFindSingleLocaleWithConditions() {
  64. $table = TableRegistry::get('Articles');
  65. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  66. $table->locale('eng');
  67. $results = $table->find()
  68. ->where(['Articles.id' => 2])
  69. ->all();
  70. $this->assertCount(1, $results);
  71. $row = $results->first();
  72. $expected = [
  73. 'id' => 2,
  74. 'title' => 'Title #2',
  75. 'body' => 'Content #2',
  76. 'author_id' => 3,
  77. 'published' => 'Y',
  78. '_locale' => 'eng'
  79. ];
  80. $this->assertEquals($expected, $row->toArray());
  81. }
  82. /**
  83. * Tests that translating fields work when other formatters are used
  84. *
  85. * @return void
  86. */
  87. public function testFindList() {
  88. $table = TableRegistry::get('Articles');
  89. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  90. $table->locale('eng');
  91. $results = $table->find('list')->toArray();
  92. $expected = [1 => 'Title #1', 2 => 'Title #2', 3 => 'Title #3'];
  93. $this->assertSame($expected, $results);
  94. }
  95. /**
  96. * Tests that the query count return the correct results
  97. *
  98. * @return void
  99. */
  100. public function testFindCount() {
  101. $table = TableRegistry::get('Articles');
  102. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  103. $table->locale('eng');
  104. $this->assertEquals(3, $table->find()->count());
  105. }
  106. /**
  107. * Tests that it is possible to get all translated fields at once
  108. *
  109. * @return void
  110. */
  111. public function testFindTranslations() {
  112. $table = TableRegistry::get('Articles');
  113. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  114. $results = $table->find('translations');
  115. $expected = [
  116. [
  117. 'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'locale' => 'eng'],
  118. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  119. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  120. ],
  121. [
  122. 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
  123. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  124. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  125. ],
  126. [
  127. 'eng' => ['title' => 'Title #3', 'body' => 'Content #3', 'locale' => 'eng'],
  128. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  129. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  130. ]
  131. ];
  132. $translations = $results->map(function($row) {
  133. $translations = $row->get('_translations');
  134. if (!$translations) {
  135. return [];
  136. }
  137. return array_map(function($t) {
  138. return $t->toArray();
  139. }, $translations);
  140. });
  141. $this->assertEquals($expected, $translations->toArray());
  142. $expected = [
  143. 1 => ['First Article' => 'First Article Body'],
  144. 2 => ['Second Article' => 'Second Article Body'],
  145. 3 => ['Third Article' => 'Third Article Body']
  146. ];
  147. $grouped = $results->combine('title', 'body', 'id');
  148. $this->assertEquals($expected, $grouped->toArray());
  149. }
  150. /**
  151. * Tests that it is possible to request just a few translations
  152. *
  153. * @return void
  154. */
  155. public function testFindFilteredTranslations() {
  156. $table = TableRegistry::get('Articles');
  157. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  158. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  159. $expected = [
  160. [
  161. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  162. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  163. ],
  164. [
  165. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  166. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  167. ],
  168. [
  169. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  170. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  171. ]
  172. ];
  173. $translations = $results->map(function($row) {
  174. $translations = $row->get('_translations');
  175. if (!$translations) {
  176. return [];
  177. }
  178. return array_map(function($t) {
  179. return $t->toArray();
  180. }, $translations);
  181. });
  182. $this->assertEquals($expected, $translations->toArray());
  183. $expected = [
  184. 1 => ['First Article' => 'First Article Body'],
  185. 2 => ['Second Article' => 'Second Article Body'],
  186. 3 => ['Third Article' => 'Third Article Body']
  187. ];
  188. $grouped = $results->combine('title', 'body', 'id');
  189. $this->assertEquals($expected, $grouped->toArray());
  190. }
  191. /**
  192. * Tests that it is possible to combine find('list') and find('translations')
  193. *
  194. * @return void
  195. */
  196. public function testFindTranslationsList() {
  197. $table = TableRegistry::get('Articles');
  198. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  199. $results = $table
  200. ->find('list', [
  201. 'idField' => 'title',
  202. 'valueField' => '_translations.deu.title',
  203. 'groupField' => 'id'
  204. ])
  205. ->find('translations', ['locales' => ['deu']]);
  206. $expected = [
  207. 1 => ['First Article' => 'Titel #1'],
  208. 2 => ['Second Article' => 'Titel #2'],
  209. 3 => ['Third Article' => 'Titel #3']
  210. ];
  211. $this->assertEquals($expected, $results->toArray());
  212. }
  213. /**
  214. * Tests that you can both override fields and find all translations
  215. *
  216. * @return void
  217. */
  218. public function testFindTranslationsWithFieldOverriding() {
  219. $table = TableRegistry::get('Articles');
  220. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  221. $table->locale('cze');
  222. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  223. $expected = [
  224. [
  225. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  226. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  227. ],
  228. [
  229. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  230. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  231. ],
  232. [
  233. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  234. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  235. ]
  236. ];
  237. $translations = $results->map(function($row) {
  238. $translations = $row->get('_translations');
  239. if (!$translations) {
  240. return [];
  241. }
  242. return array_map(function($t) {
  243. return $t->toArray();
  244. }, $translations);
  245. });
  246. $this->assertEquals($expected, $translations->toArray());
  247. $expected = [
  248. 1 => ['Titulek #1' => 'Obsah #1'],
  249. 2 => ['Titulek #2' => 'Obsah #2'],
  250. 3 => ['Titulek #3' => 'Obsah #3']
  251. ];
  252. $grouped = $results->combine('title', 'body', 'id');
  253. $this->assertEquals($expected, $grouped->toArray());
  254. }
  255. /**
  256. * Tests that fields can be overriden in a hasMany association
  257. *
  258. * @return void
  259. */
  260. public function testFindSingleLocaleHasMany() {
  261. $table = TableRegistry::get('Articles');
  262. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  263. $table->hasMany('Comments');
  264. $comments = $table->hasMany('Comments')->target();
  265. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  266. $table->locale('eng');
  267. $comments->locale('eng');
  268. $results = $table->find()->contain(['Comments' => function($q) {
  269. return $q->select(['id', 'comment', 'article_id']);
  270. }]);
  271. $list = new Collection($results->first()->comments);
  272. $expected = [
  273. 1 => 'Comment #1',
  274. 2 => 'Comment #2',
  275. 3 => 'Comment #3',
  276. 4 => 'Comment #4'
  277. ];
  278. $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
  279. }
  280. public function testTranslationsHasMany() {
  281. $table = TableRegistry::get('Articles');
  282. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  283. $table->hasMany('Comments');
  284. $comments = $table->hasMany('Comments')->target();
  285. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  286. $results = $table->find('translations')->contain(['Comments' => function($q) {
  287. return $q->find('translations')->select(['id', 'comment', 'article_id']);
  288. }]);
  289. $article = $results->first();
  290. debug(json_encode($article, JSON_PRETTY_PRINT));
  291. }
  292. }