TranslateBehaviorTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Collection\Collection;
  17. use Cake\Event\Event;
  18. use Cake\I18n\I18n;
  19. use Cake\Model\Behavior\TranslateBehavior;
  20. use Cake\Model\Behavior\Translate\TranslateTrait;
  21. use Cake\ORM\Entity;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Stub entity class
  26. */
  27. class Article extends Entity {
  28. use TranslateTrait;
  29. }
  30. /**
  31. * Translate behavior test case
  32. */
  33. class TranslateBehaviorTest extends TestCase {
  34. /**
  35. * fixtures
  36. *
  37. * @var array
  38. */
  39. public $fixtures = [
  40. 'core.translates',
  41. 'core.articles',
  42. 'core.comments',
  43. 'core.authors'
  44. ];
  45. public function tearDown() {
  46. parent::tearDown();
  47. I18n::locale(I18n::defaultLocale());
  48. TableRegistry::clear();
  49. }
  50. /**
  51. * Returns an array with all the translations found for a set of records
  52. *
  53. * @param array|\Traversable $data
  54. * @return Collection
  55. */
  56. protected function _extractTranslations($data) {
  57. return (new Collection($data))->map(function ($row) {
  58. $translations = $row->get('_translations');
  59. if (!$translations) {
  60. return [];
  61. }
  62. return array_map(function ($t) {
  63. return $t->toArray();
  64. }, $translations);
  65. });
  66. }
  67. /**
  68. * Tests that fields from a translated model are overriden
  69. *
  70. * @return void
  71. */
  72. public function testFindSingleLocale() {
  73. $table = TableRegistry::get('Articles');
  74. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  75. $table->locale('eng');
  76. $results = $table->find()->combine('title', 'body', 'id')->toArray();
  77. $expected = [
  78. 1 => ['Title #1' => 'Content #1'],
  79. 2 => ['Title #2' => 'Content #2'],
  80. 3 => ['Title #3' => 'Content #3'],
  81. ];
  82. $this->assertSame($expected, $results);
  83. }
  84. /**
  85. * Tests that fields from a translated model use the I18n class locale
  86. * and that it propogates to associated models
  87. *
  88. * @return void
  89. */
  90. public function testFindSingleLocaleAssociatedEnv() {
  91. I18n::locale('eng');
  92. $table = TableRegistry::get('Articles');
  93. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  94. $table->hasMany('Comments');
  95. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  96. $results = $table->find()
  97. ->select(['id', 'title', 'body'])
  98. ->contain(['Comments' => ['fields' => ['article_id', 'comment']]])
  99. ->hydrate(false)
  100. ->toArray();
  101. $expected = [
  102. [
  103. 'id' => 1,
  104. 'title' => 'Title #1',
  105. 'body' => 'Content #1',
  106. 'comments' => [
  107. ['article_id' => 1, 'comment' => 'Comment #1', '_locale' => 'eng'],
  108. ['article_id' => 1, 'comment' => 'Comment #2', '_locale' => 'eng'],
  109. ['article_id' => 1, 'comment' => 'Comment #3', '_locale' => 'eng'],
  110. ['article_id' => 1, 'comment' => 'Comment #4', '_locale' => 'eng']
  111. ],
  112. '_locale' => 'eng'
  113. ],
  114. [
  115. 'id' => 2,
  116. 'title' => 'Title #2',
  117. 'body' => 'Content #2',
  118. 'comments' => [
  119. ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'eng'],
  120. ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'eng']
  121. ],
  122. '_locale' => 'eng'
  123. ],
  124. [
  125. 'id' => 3,
  126. 'title' => 'Title #3',
  127. 'body' => 'Content #3',
  128. 'comments' => [],
  129. '_locale' => 'eng'
  130. ]
  131. ];
  132. $this->assertSame($expected, $results);
  133. I18n::locale('spa');
  134. $results = $table->find()
  135. ->select(['id', 'title', 'body'])
  136. ->contain([
  137. 'Comments' => [
  138. 'fields' => ['article_id', 'comment'],
  139. 'sort' => ['Comments.id' => 'ASC']
  140. ]
  141. ])
  142. ->hydrate(false)
  143. ->toArray();
  144. $expected = [
  145. [
  146. 'id' => 1,
  147. 'title' => 'First Article',
  148. 'body' => 'First Article Body',
  149. 'comments' => [
  150. ['article_id' => 1, 'comment' => 'First Comment for First Article', '_locale' => 'spa'],
  151. ['article_id' => 1, 'comment' => 'Second Comment for First Article', '_locale' => 'spa'],
  152. ['article_id' => 1, 'comment' => 'Third Comment for First Article', '_locale' => 'spa'],
  153. ['article_id' => 1, 'comment' => 'Comentario #4', '_locale' => 'spa']
  154. ],
  155. '_locale' => 'spa'
  156. ],
  157. [
  158. 'id' => 2,
  159. 'title' => 'Second Article',
  160. 'body' => 'Second Article Body',
  161. 'comments' => [
  162. ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'spa'],
  163. ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'spa']
  164. ],
  165. '_locale' => 'spa'
  166. ],
  167. [
  168. 'id' => 3,
  169. 'title' => 'Third Article',
  170. 'body' => 'Third Article Body',
  171. 'comments' => [],
  172. '_locale' => 'spa'
  173. ]
  174. ];
  175. $this->assertSame($expected, $results);
  176. }
  177. /**
  178. * Tests that fields from a translated model are not overriden if translation
  179. * is null
  180. *
  181. * @return void
  182. */
  183. public function testFindSingleLocaleWithNullTranslation() {
  184. $table = TableRegistry::get('Comments');
  185. $table->addBehavior('Translate', ['fields' => ['comment']]);
  186. $table->locale('spa');
  187. $results = $table->find()
  188. ->where(['Comments.id' => 6])
  189. ->combine('id', 'comment')->toArray();
  190. $expected = [6 => 'Second Comment for Second Article'];
  191. $this->assertSame($expected, $results);
  192. }
  193. /**
  194. * Tests that overriding fields with the translate behavior works when
  195. * using conditions and that all other columns are preserved
  196. *
  197. * @return void
  198. */
  199. public function testFindSingleLocaleWithConditions() {
  200. $table = TableRegistry::get('Articles');
  201. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  202. $table->locale('eng');
  203. $results = $table->find()
  204. ->where(['Articles.id' => 2])
  205. ->all();
  206. $this->assertCount(1, $results);
  207. $row = $results->first();
  208. $expected = [
  209. 'id' => 2,
  210. 'title' => 'Title #2',
  211. 'body' => 'Content #2',
  212. 'author_id' => 3,
  213. 'published' => 'Y',
  214. '_locale' => 'eng'
  215. ];
  216. $this->assertEquals($expected, $row->toArray());
  217. }
  218. /**
  219. * Tests that translating fields work when other formatters are used
  220. *
  221. * @return void
  222. */
  223. public function testFindList() {
  224. $table = TableRegistry::get('Articles');
  225. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  226. $table->locale('eng');
  227. $results = $table->find('list')->toArray();
  228. $expected = [1 => 'Title #1', 2 => 'Title #2', 3 => 'Title #3'];
  229. $this->assertSame($expected, $results);
  230. }
  231. /**
  232. * Tests that the query count return the correct results
  233. *
  234. * @return void
  235. */
  236. public function testFindCount() {
  237. $table = TableRegistry::get('Articles');
  238. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  239. $table->locale('eng');
  240. $this->assertEquals(3, $table->find()->count());
  241. }
  242. /**
  243. * Tests that it is possible to get all translated fields at once
  244. *
  245. * @return void
  246. */
  247. public function testFindTranslations() {
  248. $table = TableRegistry::get('Articles');
  249. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  250. $results = $table->find('translations');
  251. $expected = [
  252. [
  253. 'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'locale' => 'eng'],
  254. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  255. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  256. ],
  257. [
  258. 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
  259. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  260. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  261. ],
  262. [
  263. 'eng' => ['title' => 'Title #3', 'body' => 'Content #3', 'locale' => 'eng'],
  264. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  265. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  266. ]
  267. ];
  268. $translations = $this->_extractTranslations($results);
  269. $this->assertEquals($expected, $translations->toArray());
  270. $expected = [
  271. 1 => ['First Article' => 'First Article Body'],
  272. 2 => ['Second Article' => 'Second Article Body'],
  273. 3 => ['Third Article' => 'Third Article Body']
  274. ];
  275. $grouped = $results->combine('title', 'body', 'id');
  276. $this->assertEquals($expected, $grouped->toArray());
  277. }
  278. /**
  279. * Tests that it is possible to request just a few translations
  280. *
  281. * @return void
  282. */
  283. public function testFindFilteredTranslations() {
  284. $table = TableRegistry::get('Articles');
  285. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  286. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  287. $expected = [
  288. [
  289. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  290. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  291. ],
  292. [
  293. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  294. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  295. ],
  296. [
  297. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  298. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  299. ]
  300. ];
  301. $translations = $this->_extractTranslations($results);
  302. $this->assertEquals($expected, $translations->toArray());
  303. $expected = [
  304. 1 => ['First Article' => 'First Article Body'],
  305. 2 => ['Second Article' => 'Second Article Body'],
  306. 3 => ['Third Article' => 'Third Article Body']
  307. ];
  308. $grouped = $results->combine('title', 'body', 'id');
  309. $this->assertEquals($expected, $grouped->toArray());
  310. }
  311. /**
  312. * Tests that it is possible to combine find('list') and find('translations')
  313. *
  314. * @return void
  315. */
  316. public function testFindTranslationsList() {
  317. $table = TableRegistry::get('Articles');
  318. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  319. $results = $table
  320. ->find('list', [
  321. 'idField' => 'title',
  322. 'valueField' => '_translations.deu.title',
  323. 'groupField' => 'id'
  324. ])
  325. ->find('translations', ['locales' => ['deu']]);
  326. $expected = [
  327. 1 => ['First Article' => 'Titel #1'],
  328. 2 => ['Second Article' => 'Titel #2'],
  329. 3 => ['Third Article' => 'Titel #3']
  330. ];
  331. $this->assertEquals($expected, $results->toArray());
  332. }
  333. /**
  334. * Tests that you can both override fields and find all translations
  335. *
  336. * @return void
  337. */
  338. public function testFindTranslationsWithFieldOverriding() {
  339. $table = TableRegistry::get('Articles');
  340. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  341. $table->locale('cze');
  342. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  343. $expected = [
  344. [
  345. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  346. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  347. ],
  348. [
  349. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  350. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  351. ],
  352. [
  353. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  354. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  355. ]
  356. ];
  357. $translations = $this->_extractTranslations($results);
  358. $this->assertEquals($expected, $translations->toArray());
  359. $expected = [
  360. 1 => ['Titulek #1' => 'Obsah #1'],
  361. 2 => ['Titulek #2' => 'Obsah #2'],
  362. 3 => ['Titulek #3' => 'Obsah #3']
  363. ];
  364. $grouped = $results->combine('title', 'body', 'id');
  365. $this->assertEquals($expected, $grouped->toArray());
  366. }
  367. /**
  368. * Tests that fields can be overriden in a hasMany association
  369. *
  370. * @return void
  371. */
  372. public function testFindSingleLocaleHasMany() {
  373. $table = TableRegistry::get('Articles');
  374. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  375. $table->hasMany('Comments');
  376. $comments = $table->hasMany('Comments')->target();
  377. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  378. $table->locale('eng');
  379. $comments->locale('eng');
  380. $results = $table->find()->contain(['Comments' => function ($q) {
  381. return $q->select(['id', 'comment', 'article_id']);
  382. }]);
  383. $list = new Collection($results->first()->comments);
  384. $expected = [
  385. 1 => 'Comment #1',
  386. 2 => 'Comment #2',
  387. 3 => 'Comment #3',
  388. 4 => 'Comment #4'
  389. ];
  390. $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
  391. }
  392. /**
  393. * Test that it is possible to bring translations from hasMany relations
  394. *
  395. * @return void
  396. */
  397. public function testTranslationsHasMany() {
  398. $table = TableRegistry::get('Articles');
  399. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  400. $table->hasMany('Comments');
  401. $comments = $table->hasMany('Comments')->target();
  402. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  403. $results = $table->find('translations')->contain([
  404. 'Comments' => function ($q) {
  405. return $q->find('translations')->select(['id', 'comment', 'article_id']);
  406. }
  407. ]);
  408. $comments = $results->first()->comments;
  409. $expected = [
  410. [
  411. 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng']
  412. ],
  413. [
  414. 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng']
  415. ],
  416. [
  417. 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng']
  418. ],
  419. [
  420. 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
  421. 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']
  422. ]
  423. ];
  424. $translations = $this->_extractTranslations($comments);
  425. $this->assertEquals($expected, $translations->toArray());
  426. }
  427. /**
  428. * Tests that it is possible to both override fields with a translation and
  429. * also find separately other translations
  430. *
  431. * @return void
  432. */
  433. public function testTranslationsHasManyWithOverride() {
  434. $table = TableRegistry::get('Articles');
  435. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  436. $table->hasMany('Comments');
  437. $comments = $table->hasMany('Comments')->target();
  438. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  439. $table->locale('cze');
  440. $comments->locale('eng');
  441. $results = $table->find('translations')->contain([
  442. 'Comments' => function ($q) {
  443. return $q->find('translations')->select(['id', 'comment', 'article_id']);
  444. }
  445. ]);
  446. $comments = $results->first()->comments;
  447. $expected = [
  448. 1 => 'Comment #1',
  449. 2 => 'Comment #2',
  450. 3 => 'Comment #3',
  451. 4 => 'Comment #4'
  452. ];
  453. $list = new Collection($comments);
  454. $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
  455. $expected = [
  456. [
  457. 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng']
  458. ],
  459. [
  460. 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng']
  461. ],
  462. [
  463. 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng']
  464. ],
  465. [
  466. 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
  467. 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']
  468. ]
  469. ];
  470. $translations = $this->_extractTranslations($comments);
  471. $this->assertEquals($expected, $translations->toArray());
  472. $this->assertEquals('Titulek #1', $results->first()->title);
  473. $this->assertEquals('Obsah #1', $results->first()->body);
  474. }
  475. /**
  476. * Tests that it is possible to translate belongsTo associations
  477. *
  478. * @return void
  479. */
  480. public function testFindSingleLocaleBelongsto() {
  481. $table = TableRegistry::get('Articles');
  482. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  483. $authors = $table->belongsTo('Authors')->target();
  484. $authors->addBehavior('Translate', ['fields' => ['name']]);
  485. $table->locale('eng');
  486. $authors->locale('eng');
  487. $results = $table->find()
  488. ->select(['title', 'body'])
  489. ->order(['title' => 'asc'])
  490. ->contain(['Authors' => function ($q) {
  491. return $q->select(['id', 'name']);
  492. }]);
  493. $expected = [
  494. [
  495. 'title' => 'Title #1',
  496. 'body' => 'Content #1',
  497. 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
  498. '_locale' => 'eng'
  499. ],
  500. [
  501. 'title' => 'Title #2',
  502. 'body' => 'Content #2',
  503. 'author' => ['id' => 3, 'name' => 'larry', '_locale' => 'eng'],
  504. '_locale' => 'eng'
  505. ],
  506. [
  507. 'title' => 'Title #3',
  508. 'body' => 'Content #3',
  509. 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
  510. '_locale' => 'eng'
  511. ]
  512. ];
  513. $results = array_map(function ($r) {
  514. return $r->toArray();
  515. }, $results->toArray());
  516. $this->assertEquals($expected, $results);
  517. }
  518. /**
  519. * Tests that updating an existing record translations work
  520. *
  521. * @return void
  522. */
  523. public function testUpdateSingleLocale() {
  524. $table = TableRegistry::get('Articles');
  525. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  526. $table->locale('eng');
  527. $article = $table->find()->first();
  528. $this->assertEquals(1, $article->get('id'));
  529. $article->set('title', 'New translated article');
  530. $table->save($article);
  531. $this->assertNull($article->get('_i18n'));
  532. $article = $table->find()->first();
  533. $this->assertEquals(1, $article->get('id'));
  534. $this->assertEquals('New translated article', $article->get('title'));
  535. $this->assertEquals('Content #1', $article->get('body'));
  536. $table->locale(false);
  537. $article = $table->find()->first();
  538. $this->assertEquals(1, $article->get('id'));
  539. $this->assertEquals('First Article', $article->get('title'));
  540. $table->locale('eng');
  541. $article->set('title', 'Wow, such translated article');
  542. $article->set('body', 'A translated body');
  543. $table->save($article);
  544. $this->assertNull($article->get('_i18n'));
  545. $article = $table->find()->first();
  546. $this->assertEquals(1, $article->get('id'));
  547. $this->assertEquals('Wow, such translated article', $article->get('title'));
  548. $this->assertEquals('A translated body', $article->get('body'));
  549. }
  550. /**
  551. * Tests adding new translation to a record
  552. *
  553. * @return void
  554. */
  555. public function testInsertNewTranslations() {
  556. $table = TableRegistry::get('Articles');
  557. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  558. $table->locale('fra');
  559. $article = $table->find()->first();
  560. $this->assertEquals(1, $article->get('id'));
  561. $article->set('title', 'Le titre');
  562. $table->save($article);
  563. $this->assertEquals('fra', $article->get('_locale'));
  564. $article = $table->find()->first();
  565. $this->assertEquals(1, $article->get('id'));
  566. $this->assertEquals('Le titre', $article->get('title'));
  567. $this->assertEquals('First Article Body', $article->get('body'));
  568. $article->set('title', 'Un autre titre');
  569. $article->set('body', 'Le contenu');
  570. $table->save($article);
  571. $this->assertNull($article->get('_i18n'));
  572. $article = $table->find()->first();
  573. $this->assertEquals('Un autre titre', $article->get('title'));
  574. $this->assertEquals('Le contenu', $article->get('body'));
  575. }
  576. /**
  577. * Tests that it is possible to use the _locale property to specify the language
  578. * to use for saving an entity
  579. *
  580. * @return void
  581. */
  582. public function testUpdateTranslationWithLocaleInEntity() {
  583. $table = TableRegistry::get('Articles');
  584. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  585. $article = $table->find()->first();
  586. $this->assertEquals(1, $article->get('id'));
  587. $article->set('_locale', 'fra');
  588. $article->set('title', 'Le titre');
  589. $table->save($article);
  590. $this->assertNull($article->get('_i18n'));
  591. $article = $table->find()->first();
  592. $this->assertEquals(1, $article->get('id'));
  593. $this->assertEquals('First Article', $article->get('title'));
  594. $this->assertEquals('First Article Body', $article->get('body'));
  595. $table->locale('fra');
  596. $article = $table->find()->first();
  597. $this->assertEquals(1, $article->get('id'));
  598. $this->assertEquals('Le titre', $article->get('title'));
  599. $this->assertEquals('First Article Body', $article->get('body'));
  600. }
  601. /**
  602. * Tests that translations are added to the whitelist of associations to be
  603. * saved
  604. *
  605. * @return void
  606. */
  607. public function testSaveTranslationWithAssociationWhitelist() {
  608. $table = TableRegistry::get('Articles');
  609. $table->hasMany('Comments');
  610. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  611. $table->locale('fra');
  612. $article = $table->find()->first();
  613. $this->assertEquals(1, $article->get('id'));
  614. $article->set('title', 'Le titre');
  615. $table->save($article, ['associated' => ['Comments']]);
  616. $this->assertNull($article->get('_i18n'));
  617. $article = $table->find()->first();
  618. $this->assertEquals('Le titre', $article->get('title'));
  619. }
  620. /**
  621. * Tests that after deleting a translated entity, all translations are also removed
  622. *
  623. * @return void
  624. */
  625. public function testDelete() {
  626. $table = TableRegistry::get('Articles');
  627. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  628. $article = $table->find()->first();
  629. $this->assertTrue($table->delete($article));
  630. $translations = TableRegistry::get('I18n')->find()
  631. ->where(['model' => 'Articles', 'foreign_key' => $article->id])
  632. ->count();
  633. $this->assertEquals(0, $translations);
  634. }
  635. /**
  636. * Tests saving multiple translations at once when the translations already
  637. * exist in the database
  638. *
  639. * @return void
  640. */
  641. public function testSaveMultipleTranslations() {
  642. $table = TableRegistry::get('Articles');
  643. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  644. $article = $results = $table->find('translations')->first();
  645. $translations = $article->get('_translations');
  646. $translations['deu']->set('title', 'Another title');
  647. $translations['eng']->set('body', 'Another body');
  648. $article->set('_translations', $translations);
  649. $table->save($article);
  650. $this->assertNull($article->get('_i18n'));
  651. $article = $results = $table->find('translations')->first();
  652. $translations = $article->get('_translations');
  653. $this->assertEquals('Another title', $translations['deu']->get('title'));
  654. $this->assertEquals('Another body', $translations['eng']->get('body'));
  655. }
  656. /**
  657. * Tests saving multiple existing translations and adding new ones
  658. *
  659. * @return void
  660. */
  661. public function testSaveMultipleNewTranslations() {
  662. $table = TableRegistry::get('Articles');
  663. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  664. $article = $results = $table->find('translations')->first();
  665. $translations = $article->get('_translations');
  666. $translations['deu']->set('title', 'Another title');
  667. $translations['eng']->set('body', 'Another body');
  668. $translations['spa'] = new Entity(['title' => 'Titulo']);
  669. $translations['fre'] = new Entity(['title' => 'Titre']);
  670. $article->set('_translations', $translations);
  671. $table->save($article);
  672. $this->assertNull($article->get('_i18n'));
  673. $article = $results = $table->find('translations')->first();
  674. $translations = $article->get('_translations');
  675. $this->assertEquals('Another title', $translations['deu']->get('title'));
  676. $this->assertEquals('Another body', $translations['eng']->get('body'));
  677. $this->assertEquals('Titulo', $translations['spa']->get('title'));
  678. $this->assertEquals('Titre', $translations['fre']->get('title'));
  679. }
  680. /**
  681. * Tests that iterating a resultset twice when using the translations finder
  682. * will not cause any errors nor information loss
  683. *
  684. * @return void
  685. */
  686. public function testUseCountInFindTranslations() {
  687. $table = TableRegistry::get('Articles');
  688. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  689. $articles = $results = $table->find('translations');
  690. $all = $articles->all();
  691. $this->assertCount(3, $all);
  692. $article = $all->first();
  693. $this->assertNotEmpty($article->get('_translations'));
  694. }
  695. /**
  696. * Tests that multiple translations saved when having a default locale
  697. * are correclty saved
  698. *
  699. * @return void
  700. */
  701. public function testSavingWithNonDefaultLocale() {
  702. $table = TableRegistry::get('Articles');
  703. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  704. $table->entityClass(__NAMESPACE__ . '\Article');
  705. I18n::locale('fra');
  706. $translations = [
  707. 'fra' => ['title' => 'Un article'],
  708. 'spa' => ['title' => 'Un artículo']
  709. ];
  710. $article = $table->get(1);
  711. foreach ($translations as $lang => $data) {
  712. $article->translation($lang)->set($data, ['guard' => false]);
  713. }
  714. $table->save($article);
  715. $article = $table->find('translations')->where(['Articles.id' => 1])->first();
  716. $this->assertEquals('Un article', $article->translation('fra')->title);
  717. $this->assertEquals('Un artículo', $article->translation('spa')->title);
  718. }
  719. /**
  720. * Tests that translation queries are added to union queries as well.
  721. *
  722. * @return void
  723. */
  724. public function testTranslationWithUnionQuery() {
  725. $table = TableRegistry::get('Comments');
  726. $table->addBehavior('Translate', ['fields' => ['comment']]);
  727. $table->locale('spa');
  728. $query = $table->find()->where(['Comments.id' => 6]);
  729. $query2 = $table->find()->where(['Comments.id' => 5]);
  730. $query->union($query2);
  731. $results = $query->sortBy('id')->toArray();
  732. $this->assertCount(2, $results);
  733. $this->assertEquals('First Comment for Second Article', $results[0]->comment);
  734. $this->assertEquals('Second Comment for Second Article', $results[1]->comment);
  735. }
  736. /**
  737. * Tests the use of `model` config option.
  738. *
  739. * @return void
  740. */
  741. public function testChangingModelFieldValue() {
  742. $table = TableRegistry::get('Articles');
  743. $table->hasMany('OtherComments', ['className' => 'Comments']);
  744. $table->OtherComments->addBehavior('Translate', ['fields' => ['comment'], 'model' => 'Comments']);
  745. $items = $table->OtherComments->associations();
  746. $association = $items->getByProperty('comment_translation');
  747. $this->assertNotEmpty($association, 'Translation association not found');
  748. $found = false;
  749. foreach ($association->conditions() as $key => $value) {
  750. if (strpos($key, 'comment_translation.model') !== false) {
  751. $found = true;
  752. $this->assertEquals('Comments', $value);
  753. break;
  754. }
  755. }
  756. $this->assertTrue($found, '`model` field condition on a Translation association was not found');
  757. }
  758. }