TranslateBehaviorTest.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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\ORM\Behavior;
  16. use Cake\Collection\Collection;
  17. use Cake\I18n\I18n;
  18. use Cake\ORM\Behavior\Translate\TranslateTrait;
  19. use Cake\ORM\Entity;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\Validation\Validator;
  23. /**
  24. * Stub entity class
  25. */
  26. class Article extends Entity
  27. {
  28. use TranslateTrait;
  29. }
  30. /**
  31. * Translate behavior test case
  32. */
  33. class TranslateBehaviorTest extends TestCase
  34. {
  35. /**
  36. * fixtures
  37. *
  38. * @var array
  39. */
  40. public $fixtures = [
  41. 'core.articles',
  42. 'core.authors',
  43. 'core.special_tags',
  44. 'core.tags',
  45. 'core.comments',
  46. 'core.translates'
  47. ];
  48. public function tearDown()
  49. {
  50. parent::tearDown();
  51. I18n::locale(I18n::defaultLocale());
  52. TableRegistry::clear();
  53. }
  54. /**
  55. * Returns an array with all the translations found for a set of records
  56. *
  57. * @param array|\Traversable $data
  58. * @return Collection
  59. */
  60. protected function _extractTranslations($data)
  61. {
  62. return (new Collection($data))->map(function ($row) {
  63. $translations = $row->get('_translations');
  64. if (!$translations) {
  65. return [];
  66. }
  67. return array_map(function ($t) {
  68. return $t->toArray();
  69. }, $translations);
  70. });
  71. }
  72. /**
  73. * Tests that custom translation tables are respected
  74. *
  75. * @return void
  76. */
  77. public function testCustomTranslationTable()
  78. {
  79. $table = TableRegistry::get('Articles');
  80. $table->addBehavior('Translate', [
  81. 'translationTable' => '\TestApp\Model\Table\I18nTable',
  82. 'fields' => ['title', 'body']
  83. ]);
  84. $items = $table->associations();
  85. $i18n = $items->getByProperty('_i18n');
  86. $this->assertEquals('\TestApp\Model\Table\I18nTable', $i18n->name());
  87. $this->assertInstanceOf('TestApp\Model\Table\I18nTable', $i18n->target());
  88. $this->assertEquals('test_custom_i18n_datasource', $i18n->target()->connection()->configName());
  89. $this->assertEquals('custom_i18n_table', $i18n->target()->table());
  90. }
  91. /**
  92. * Tests that the strategy can be changed for i18n
  93. *
  94. * @return void
  95. */
  96. public function testStrategy()
  97. {
  98. $table = TableRegistry::get('Articles');
  99. $table->addBehavior('Translate', [
  100. 'strategy' => 'select',
  101. 'fields' => ['title', 'body']
  102. ]);
  103. $items = $table->associations();
  104. $i18n = $items->getByProperty('_i18n');
  105. $this->assertEquals('select', $i18n->strategy());
  106. }
  107. /**
  108. * Tests that fields from a translated model are overridden
  109. *
  110. * @return void
  111. */
  112. public function testFindSingleLocale()
  113. {
  114. $table = TableRegistry::get('Articles');
  115. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  116. $table->locale('eng');
  117. $results = $table->find()->combine('title', 'body', 'id')->toArray();
  118. $expected = [
  119. 1 => ['Title #1' => 'Content #1'],
  120. 2 => ['Title #2' => 'Content #2'],
  121. 3 => ['Title #3' => 'Content #3'],
  122. ];
  123. $this->assertSame($expected, $results);
  124. }
  125. /**
  126. * Test that iterating in a formatResults() does not drop data.
  127. *
  128. * @return void
  129. */
  130. public function testFindTranslationsFormatResultsIteration()
  131. {
  132. $table = TableRegistry::get('Articles');
  133. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  134. $table->locale('eng');
  135. $results = $table->find('translations')
  136. ->limit(1)
  137. ->formatResults(function ($results) {
  138. foreach ($results as $res) {
  139. $res->first = 'val';
  140. }
  141. foreach ($results as $res) {
  142. $res->second = 'loop';
  143. }
  144. return $results;
  145. })
  146. ->toArray();
  147. $this->assertCount(1, $results);
  148. $this->assertSame('Title #1', $results[0]->title);
  149. $this->assertSame('val', $results[0]->first);
  150. $this->assertSame('loop', $results[0]->second);
  151. $this->assertNotEmpty($results[0]->_translations);
  152. }
  153. /**
  154. * Tests that fields from a translated model use the I18n class locale
  155. * and that it propogates to associated models
  156. *
  157. * @return void
  158. */
  159. public function testFindSingleLocaleAssociatedEnv()
  160. {
  161. I18n::locale('eng');
  162. $table = TableRegistry::get('Articles');
  163. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  164. $table->hasMany('Comments');
  165. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  166. $results = $table->find()
  167. ->select(['id', 'title', 'body'])
  168. ->contain(['Comments' => ['fields' => ['article_id', 'comment']]])
  169. ->hydrate(false)
  170. ->toArray();
  171. $expected = [
  172. [
  173. 'id' => 1,
  174. 'title' => 'Title #1',
  175. 'body' => 'Content #1',
  176. 'comments' => [
  177. ['article_id' => 1, 'comment' => 'Comment #1', '_locale' => 'eng'],
  178. ['article_id' => 1, 'comment' => 'Comment #2', '_locale' => 'eng'],
  179. ['article_id' => 1, 'comment' => 'Comment #3', '_locale' => 'eng'],
  180. ['article_id' => 1, 'comment' => 'Comment #4', '_locale' => 'eng']
  181. ],
  182. '_locale' => 'eng'
  183. ],
  184. [
  185. 'id' => 2,
  186. 'title' => 'Title #2',
  187. 'body' => 'Content #2',
  188. 'comments' => [
  189. ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'eng'],
  190. ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'eng']
  191. ],
  192. '_locale' => 'eng'
  193. ],
  194. [
  195. 'id' => 3,
  196. 'title' => 'Title #3',
  197. 'body' => 'Content #3',
  198. 'comments' => [],
  199. '_locale' => 'eng'
  200. ]
  201. ];
  202. $this->assertSame($expected, $results);
  203. I18n::locale('spa');
  204. $results = $table->find()
  205. ->select(['id', 'title', 'body'])
  206. ->contain([
  207. 'Comments' => [
  208. 'fields' => ['article_id', 'comment'],
  209. 'sort' => ['Comments.id' => 'ASC']
  210. ]
  211. ])
  212. ->hydrate(false)
  213. ->toArray();
  214. $expected = [
  215. [
  216. 'id' => 1,
  217. 'title' => 'First Article',
  218. 'body' => 'Contenido #1',
  219. 'comments' => [
  220. ['article_id' => 1, 'comment' => 'First Comment for First Article', '_locale' => 'spa'],
  221. ['article_id' => 1, 'comment' => 'Second Comment for First Article', '_locale' => 'spa'],
  222. ['article_id' => 1, 'comment' => 'Third Comment for First Article', '_locale' => 'spa'],
  223. ['article_id' => 1, 'comment' => 'Comentario #4', '_locale' => 'spa']
  224. ],
  225. '_locale' => 'spa'
  226. ],
  227. [
  228. 'id' => 2,
  229. 'title' => 'Second Article',
  230. 'body' => 'Second Article Body',
  231. 'comments' => [
  232. ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'spa'],
  233. ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'spa']
  234. ],
  235. '_locale' => 'spa'
  236. ],
  237. [
  238. 'id' => 3,
  239. 'title' => 'Third Article',
  240. 'body' => 'Third Article Body',
  241. 'comments' => [],
  242. '_locale' => 'spa'
  243. ]
  244. ];
  245. $this->assertSame($expected, $results);
  246. }
  247. /**
  248. * Tests that fields from a translated model are not overridden if translation
  249. * is null
  250. *
  251. * @return void
  252. */
  253. public function testFindSingleLocaleWithNullTranslation()
  254. {
  255. $table = TableRegistry::get('Comments');
  256. $table->addBehavior('Translate', ['fields' => ['comment']]);
  257. $table->locale('spa');
  258. $results = $table->find()
  259. ->where(['Comments.id' => 6])
  260. ->combine('id', 'comment')->toArray();
  261. $expected = [6 => 'Second Comment for Second Article'];
  262. $this->assertSame($expected, $results);
  263. }
  264. /**
  265. * Tests that overriding fields with the translate behavior works when
  266. * using conditions and that all other columns are preserved
  267. *
  268. * @return void
  269. */
  270. public function testFindSingleLocaleWithConditions()
  271. {
  272. $table = TableRegistry::get('Articles');
  273. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  274. $table->locale('eng');
  275. $results = $table->find()
  276. ->where(['Articles.id' => 2])
  277. ->all();
  278. $this->assertCount(1, $results);
  279. $row = $results->first();
  280. $expected = [
  281. 'id' => 2,
  282. 'title' => 'Title #2',
  283. 'body' => 'Content #2',
  284. 'author_id' => 3,
  285. 'published' => 'Y',
  286. '_locale' => 'eng'
  287. ];
  288. $this->assertEquals($expected, $row->toArray());
  289. }
  290. /**
  291. * Tests that translating fields work when other formatters are used
  292. *
  293. * @return void
  294. */
  295. public function testFindList()
  296. {
  297. $table = TableRegistry::get('Articles');
  298. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  299. $table->locale('eng');
  300. $results = $table->find('list')->toArray();
  301. $expected = [1 => 'Title #1', 2 => 'Title #2', 3 => 'Title #3'];
  302. $this->assertSame($expected, $results);
  303. }
  304. /**
  305. * Tests that the query count return the correct results
  306. *
  307. * @return void
  308. */
  309. public function testFindCount()
  310. {
  311. $table = TableRegistry::get('Articles');
  312. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  313. $table->locale('eng');
  314. $this->assertEquals(3, $table->find()->count());
  315. }
  316. /**
  317. * Tests that it is possible to get all translated fields at once
  318. *
  319. * @return void
  320. */
  321. public function testFindTranslations()
  322. {
  323. $table = TableRegistry::get('Articles');
  324. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  325. $results = $table->find('translations');
  326. $expected = [
  327. [
  328. 'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'description' => 'Description #1', 'locale' => 'eng'],
  329. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  330. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze'],
  331. 'spa' => ['body' => 'Contenido #1', 'locale' => 'spa', 'description' => '']
  332. ],
  333. [
  334. 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
  335. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  336. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  337. ],
  338. [
  339. 'eng' => ['title' => 'Title #3', 'body' => 'Content #3', 'locale' => 'eng'],
  340. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  341. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  342. ]
  343. ];
  344. $translations = $this->_extractTranslations($results);
  345. $this->assertEquals($expected, $translations->toArray());
  346. $expected = [
  347. 1 => ['First Article' => 'First Article Body'],
  348. 2 => ['Second Article' => 'Second Article Body'],
  349. 3 => ['Third Article' => 'Third Article Body']
  350. ];
  351. $grouped = $results->combine('title', 'body', 'id');
  352. $this->assertEquals($expected, $grouped->toArray());
  353. }
  354. /**
  355. * Tests that it is possible to request just a few translations
  356. *
  357. * @return void
  358. */
  359. public function testFindFilteredTranslations()
  360. {
  361. $table = TableRegistry::get('Articles');
  362. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  363. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  364. $expected = [
  365. [
  366. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  367. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  368. ],
  369. [
  370. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  371. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  372. ],
  373. [
  374. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  375. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  376. ]
  377. ];
  378. $translations = $this->_extractTranslations($results);
  379. $this->assertEquals($expected, $translations->toArray());
  380. $expected = [
  381. 1 => ['First Article' => 'First Article Body'],
  382. 2 => ['Second Article' => 'Second Article Body'],
  383. 3 => ['Third Article' => 'Third Article Body']
  384. ];
  385. $grouped = $results->combine('title', 'body', 'id');
  386. $this->assertEquals($expected, $grouped->toArray());
  387. }
  388. /**
  389. * Tests that it is possible to combine find('list') and find('translations')
  390. *
  391. * @return void
  392. */
  393. public function testFindTranslationsList()
  394. {
  395. $table = TableRegistry::get('Articles');
  396. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  397. $results = $table
  398. ->find('list', [
  399. 'keyField' => 'title',
  400. 'valueField' => '_translations.deu.title',
  401. 'groupField' => 'id'
  402. ])
  403. ->find('translations', ['locales' => ['deu']]);
  404. $expected = [
  405. 1 => ['First Article' => 'Titel #1'],
  406. 2 => ['Second Article' => 'Titel #2'],
  407. 3 => ['Third Article' => 'Titel #3']
  408. ];
  409. $this->assertEquals($expected, $results->toArray());
  410. }
  411. /**
  412. * Tests that you can both override fields and find all translations
  413. *
  414. * @return void
  415. */
  416. public function testFindTranslationsWithFieldOverriding()
  417. {
  418. $table = TableRegistry::get('Articles');
  419. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  420. $table->locale('cze');
  421. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  422. $expected = [
  423. [
  424. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  425. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
  426. ],
  427. [
  428. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  429. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
  430. ],
  431. [
  432. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  433. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
  434. ]
  435. ];
  436. $translations = $this->_extractTranslations($results);
  437. $this->assertEquals($expected, $translations->toArray());
  438. $expected = [
  439. 1 => ['Titulek #1' => 'Obsah #1'],
  440. 2 => ['Titulek #2' => 'Obsah #2'],
  441. 3 => ['Titulek #3' => 'Obsah #3']
  442. ];
  443. $grouped = $results->combine('title', 'body', 'id');
  444. $this->assertEquals($expected, $grouped->toArray());
  445. }
  446. /**
  447. * Tests that fields can be overridden in a hasMany association
  448. *
  449. * @return void
  450. */
  451. public function testFindSingleLocaleHasMany()
  452. {
  453. $table = TableRegistry::get('Articles');
  454. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  455. $table->hasMany('Comments');
  456. $comments = $table->hasMany('Comments')->target();
  457. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  458. $table->locale('eng');
  459. $comments->locale('eng');
  460. $results = $table->find()->contain(['Comments' => function ($q) {
  461. return $q->select(['id', 'comment', 'article_id']);
  462. }]);
  463. $list = new Collection($results->first()->comments);
  464. $expected = [
  465. 1 => 'Comment #1',
  466. 2 => 'Comment #2',
  467. 3 => 'Comment #3',
  468. 4 => 'Comment #4'
  469. ];
  470. $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
  471. }
  472. /**
  473. * Test that it is possible to bring translations from hasMany relations
  474. *
  475. * @return void
  476. */
  477. public function testTranslationsHasMany()
  478. {
  479. $table = TableRegistry::get('Articles');
  480. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  481. $table->hasMany('Comments');
  482. $comments = $table->hasMany('Comments')->target();
  483. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  484. $results = $table->find('translations')->contain([
  485. 'Comments' => function ($q) {
  486. return $q->find('translations')->select(['id', 'comment', 'article_id']);
  487. }
  488. ]);
  489. $comments = $results->first()->comments;
  490. $expected = [
  491. [
  492. 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng']
  493. ],
  494. [
  495. 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng']
  496. ],
  497. [
  498. 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng']
  499. ],
  500. [
  501. 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
  502. 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']
  503. ]
  504. ];
  505. $translations = $this->_extractTranslations($comments);
  506. $this->assertEquals($expected, $translations->toArray());
  507. }
  508. /**
  509. * Tests that it is possible to both override fields with a translation and
  510. * also find separately other translations
  511. *
  512. * @return void
  513. */
  514. public function testTranslationsHasManyWithOverride()
  515. {
  516. $table = TableRegistry::get('Articles');
  517. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  518. $table->hasMany('Comments');
  519. $comments = $table->hasMany('Comments')->target();
  520. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  521. $table->locale('cze');
  522. $comments->locale('eng');
  523. $results = $table->find('translations')->contain([
  524. 'Comments' => function ($q) {
  525. return $q->find('translations')->select(['id', 'comment', 'article_id']);
  526. }
  527. ]);
  528. $comments = $results->first()->comments;
  529. $expected = [
  530. 1 => 'Comment #1',
  531. 2 => 'Comment #2',
  532. 3 => 'Comment #3',
  533. 4 => 'Comment #4'
  534. ];
  535. $list = new Collection($comments);
  536. $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
  537. $expected = [
  538. [
  539. 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng']
  540. ],
  541. [
  542. 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng']
  543. ],
  544. [
  545. 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng']
  546. ],
  547. [
  548. 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
  549. 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']
  550. ]
  551. ];
  552. $translations = $this->_extractTranslations($comments);
  553. $this->assertEquals($expected, $translations->toArray());
  554. $this->assertEquals('Titulek #1', $results->first()->title);
  555. $this->assertEquals('Obsah #1', $results->first()->body);
  556. }
  557. /**
  558. * Tests that it is possible to translate belongsTo associations
  559. *
  560. * @return void
  561. */
  562. public function testFindSingleLocaleBelongsto()
  563. {
  564. $table = TableRegistry::get('Articles');
  565. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  566. $authors = $table->belongsTo('Authors')->target();
  567. $authors->addBehavior('Translate', ['fields' => ['name']]);
  568. $table->locale('eng');
  569. $authors->locale('eng');
  570. $results = $table->find()
  571. ->select(['title', 'body'])
  572. ->order(['title' => 'asc'])
  573. ->contain(['Authors' => function ($q) {
  574. return $q->select(['id', 'name']);
  575. }]);
  576. $expected = [
  577. [
  578. 'title' => 'Title #1',
  579. 'body' => 'Content #1',
  580. 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
  581. '_locale' => 'eng'
  582. ],
  583. [
  584. 'title' => 'Title #2',
  585. 'body' => 'Content #2',
  586. 'author' => ['id' => 3, 'name' => 'larry', '_locale' => 'eng'],
  587. '_locale' => 'eng'
  588. ],
  589. [
  590. 'title' => 'Title #3',
  591. 'body' => 'Content #3',
  592. 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
  593. '_locale' => 'eng'
  594. ]
  595. ];
  596. $results = array_map(function ($r) {
  597. return $r->toArray();
  598. }, $results->toArray());
  599. $this->assertEquals($expected, $results);
  600. }
  601. /**
  602. * Tests that it is possible to translate belongsToMany associations
  603. *
  604. * @return void
  605. */
  606. public function testFindSingleLocaleBelongsToMany()
  607. {
  608. $table = TableRegistry::get('Articles');
  609. $specialTags = TableRegistry::get('SpecialTags');
  610. $specialTags->addBehavior('Translate', ['fields' => ['extra_info']]);
  611. $table->belongsToMany('Tags', [
  612. 'through' => $specialTags
  613. ]);
  614. $specialTags->locale('eng');
  615. $result = $table->get(2, ['contain' => 'Tags']);
  616. $this->assertNotEmpty($result);
  617. $this->assertNotEmpty($result->tags);
  618. $this->assertEquals('Translated Info', $result->tags[0]->special_tags[0]->extra_info);
  619. }
  620. /**
  621. * Tests that updating an existing record translations work
  622. *
  623. * @return void
  624. */
  625. public function testUpdateSingleLocale()
  626. {
  627. $table = TableRegistry::get('Articles');
  628. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  629. $table->locale('eng');
  630. $article = $table->find()->first();
  631. $this->assertEquals(1, $article->get('id'));
  632. $article->set('title', 'New translated article');
  633. $table->save($article);
  634. $this->assertNull($article->get('_i18n'));
  635. $article = $table->find()->first();
  636. $this->assertEquals(1, $article->get('id'));
  637. $this->assertEquals('New translated article', $article->get('title'));
  638. $this->assertEquals('Content #1', $article->get('body'));
  639. $table->locale(false);
  640. $article = $table->find()->first();
  641. $this->assertEquals(1, $article->get('id'));
  642. $this->assertEquals('First Article', $article->get('title'));
  643. $table->locale('eng');
  644. $article->set('title', 'Wow, such translated article');
  645. $article->set('body', 'A translated body');
  646. $table->save($article);
  647. $this->assertNull($article->get('_i18n'));
  648. $article = $table->find()->first();
  649. $this->assertEquals(1, $article->get('id'));
  650. $this->assertEquals('Wow, such translated article', $article->get('title'));
  651. $this->assertEquals('A translated body', $article->get('body'));
  652. }
  653. /**
  654. * Tests adding new translation to a record
  655. *
  656. * @return void
  657. */
  658. public function testInsertNewTranslations()
  659. {
  660. $table = TableRegistry::get('Articles');
  661. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  662. $table->locale('fra');
  663. $article = $table->find()->first();
  664. $this->assertEquals(1, $article->get('id'));
  665. $article->set('title', 'Le titre');
  666. $table->save($article);
  667. $this->assertEquals('fra', $article->get('_locale'));
  668. $article = $table->find()->first();
  669. $this->assertEquals(1, $article->get('id'));
  670. $this->assertEquals('Le titre', $article->get('title'));
  671. $this->assertEquals('First Article Body', $article->get('body'));
  672. $article->set('title', 'Un autre titre');
  673. $article->set('body', 'Le contenu');
  674. $table->save($article);
  675. $this->assertNull($article->get('_i18n'));
  676. $article = $table->find()->first();
  677. $this->assertEquals('Un autre titre', $article->get('title'));
  678. $this->assertEquals('Le contenu', $article->get('body'));
  679. }
  680. /**
  681. * Tests that it is possible to use the _locale property to specify the language
  682. * to use for saving an entity
  683. *
  684. * @return void
  685. */
  686. public function testUpdateTranslationWithLocaleInEntity()
  687. {
  688. $table = TableRegistry::get('Articles');
  689. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  690. $article = $table->find()->first();
  691. $this->assertEquals(1, $article->get('id'));
  692. $article->set('_locale', 'fra');
  693. $article->set('title', 'Le titre');
  694. $table->save($article);
  695. $this->assertNull($article->get('_i18n'));
  696. $article = $table->find()->first();
  697. $this->assertEquals(1, $article->get('id'));
  698. $this->assertEquals('First Article', $article->get('title'));
  699. $this->assertEquals('First Article Body', $article->get('body'));
  700. $table->locale('fra');
  701. $article = $table->find()->first();
  702. $this->assertEquals(1, $article->get('id'));
  703. $this->assertEquals('Le titre', $article->get('title'));
  704. $this->assertEquals('First Article Body', $article->get('body'));
  705. }
  706. /**
  707. * Tests that translations are added to the whitelist of associations to be
  708. * saved
  709. *
  710. * @return void
  711. */
  712. public function testSaveTranslationWithAssociationWhitelist()
  713. {
  714. $table = TableRegistry::get('Articles');
  715. $table->hasMany('Comments');
  716. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  717. $table->locale('fra');
  718. $article = $table->find()->first();
  719. $this->assertEquals(1, $article->get('id'));
  720. $article->set('title', 'Le titre');
  721. $table->save($article, ['associated' => ['Comments']]);
  722. $this->assertNull($article->get('_i18n'));
  723. $article = $table->find()->first();
  724. $this->assertEquals('Le titre', $article->get('title'));
  725. }
  726. /**
  727. * Tests that after deleting a translated entity, all translations are also removed
  728. *
  729. * @return void
  730. */
  731. public function testDelete()
  732. {
  733. $table = TableRegistry::get('Articles');
  734. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  735. $article = $table->find()->first();
  736. $this->assertTrue($table->delete($article));
  737. $translations = TableRegistry::get('I18n')->find()
  738. ->where(['model' => 'Articles', 'foreign_key' => $article->id])
  739. ->count();
  740. $this->assertEquals(0, $translations);
  741. }
  742. /**
  743. * Tests saving multiple translations at once when the translations already
  744. * exist in the database
  745. *
  746. * @return void
  747. */
  748. public function testSaveMultipleTranslations()
  749. {
  750. $table = TableRegistry::get('Articles');
  751. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  752. $article = $results = $table->find('translations')->first();
  753. $translations = $article->get('_translations');
  754. $translations['deu']->set('title', 'Another title');
  755. $translations['eng']->set('body', 'Another body');
  756. $article->set('_translations', $translations);
  757. $table->save($article);
  758. $this->assertNull($article->get('_i18n'));
  759. $article = $results = $table->find('translations')->first();
  760. $translations = $article->get('_translations');
  761. $this->assertEquals('Another title', $translations['deu']->get('title'));
  762. $this->assertEquals('Another body', $translations['eng']->get('body'));
  763. }
  764. /**
  765. * Tests saving multiple existing translations and adding new ones
  766. *
  767. * @return void
  768. */
  769. public function testSaveMultipleNewTranslations()
  770. {
  771. $table = TableRegistry::get('Articles');
  772. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  773. $article = $results = $table->find('translations')->first();
  774. $translations = $article->get('_translations');
  775. $translations['deu']->set('title', 'Another title');
  776. $translations['eng']->set('body', 'Another body');
  777. $translations['spa'] = new Entity(['title' => 'Titulo']);
  778. $translations['fre'] = new Entity(['title' => 'Titre']);
  779. $article->set('_translations', $translations);
  780. $table->save($article);
  781. $this->assertNull($article->get('_i18n'));
  782. $article = $results = $table->find('translations')->first();
  783. $translations = $article->get('_translations');
  784. $this->assertEquals('Another title', $translations['deu']->get('title'));
  785. $this->assertEquals('Another body', $translations['eng']->get('body'));
  786. $this->assertEquals('Titulo', $translations['spa']->get('title'));
  787. $this->assertEquals('Titre', $translations['fre']->get('title'));
  788. }
  789. /**
  790. * Tests that iterating a resultset twice when using the translations finder
  791. * will not cause any errors nor information loss
  792. *
  793. * @return void
  794. */
  795. public function testUseCountInFindTranslations()
  796. {
  797. $table = TableRegistry::get('Articles');
  798. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  799. $articles = $results = $table->find('translations');
  800. $all = $articles->all();
  801. $this->assertCount(3, $all);
  802. $article = $all->first();
  803. $this->assertNotEmpty($article->get('_translations'));
  804. }
  805. /**
  806. * Tests that multiple translations saved when having a default locale
  807. * are correctly saved
  808. *
  809. * @return void
  810. */
  811. public function testSavingWithNonDefaultLocale()
  812. {
  813. $table = TableRegistry::get('Articles');
  814. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  815. $table->entityClass(__NAMESPACE__ . '\Article');
  816. I18n::locale('fra');
  817. $translations = [
  818. 'fra' => ['title' => 'Un article'],
  819. 'spa' => ['title' => 'Un artículo']
  820. ];
  821. $article = $table->get(1);
  822. foreach ($translations as $lang => $data) {
  823. $article->translation($lang)->set($data, ['guard' => false]);
  824. }
  825. $table->save($article);
  826. $article = $table->find('translations')->where(['Articles.id' => 1])->first();
  827. $this->assertEquals('Un article', $article->translation('fra')->title);
  828. $this->assertEquals('Un artículo', $article->translation('spa')->title);
  829. }
  830. /**
  831. * Tests that translation queries are added to union queries as well.
  832. *
  833. * @return void
  834. */
  835. public function testTranslationWithUnionQuery()
  836. {
  837. $table = TableRegistry::get('Comments');
  838. $table->addBehavior('Translate', ['fields' => ['comment']]);
  839. $table->locale('spa');
  840. $query = $table->find()->where(['Comments.id' => 6]);
  841. $query2 = $table->find()->where(['Comments.id' => 5]);
  842. $query->union($query2);
  843. $results = $query->sortBy('id', SORT_ASC)->toList();
  844. $this->assertCount(2, $results);
  845. $this->assertEquals('First Comment for Second Article', $results[0]->comment);
  846. $this->assertEquals('Second Comment for Second Article', $results[1]->comment);
  847. }
  848. /**
  849. * Tests the use of `referenceName` config option.
  850. *
  851. * @return void
  852. */
  853. public function testAutoReferenceName()
  854. {
  855. $table = TableRegistry::get('Articles');
  856. $table->hasMany('OtherComments', ['className' => 'Comments']);
  857. $table->OtherComments->addBehavior(
  858. 'Translate',
  859. ['fields' => ['comment']]
  860. );
  861. $items = $table->OtherComments->associations();
  862. $association = $items->getByProperty('comment_translation');
  863. $this->assertNotEmpty($association, 'Translation association not found');
  864. $found = false;
  865. foreach ($association->conditions() as $key => $value) {
  866. if (strpos($key, 'comment_translation.model') !== false) {
  867. $found = true;
  868. $this->assertEquals('Comments', $value);
  869. break;
  870. }
  871. }
  872. $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
  873. }
  874. /**
  875. * Tests the use of unconventional `referenceName` config option.
  876. *
  877. * @return void
  878. */
  879. public function testChangingReferenceName()
  880. {
  881. $table = TableRegistry::get('Articles');
  882. $table->alias('FavoritePost');
  883. $table->addBehavior(
  884. 'Translate',
  885. ['fields' => ['body'], 'referenceName' => 'Posts']
  886. );
  887. $items = $table->associations();
  888. $association = $items->getByProperty('body_translation');
  889. $this->assertNotEmpty($association, 'Translation association not found');
  890. $found = false;
  891. foreach ($association->conditions() as $key => $value) {
  892. if (strpos($key, 'body_translation.model') !== false) {
  893. $found = true;
  894. $this->assertEquals('Posts', $value);
  895. break;
  896. }
  897. }
  898. $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
  899. }
  900. /**
  901. * Tests that onlyTranslated will remove records from the result set
  902. * if they are not fully translated
  903. *
  904. * @return void
  905. */
  906. public function testFilterUntranslated()
  907. {
  908. $table = TableRegistry::get('Articles');
  909. $table->addBehavior('Translate', [
  910. 'fields' => ['title', 'body'],
  911. 'onlyTranslated' => true
  912. ]);
  913. $table->locale('eng');
  914. $results = $table->find()->where(['Articles.id' => 1])->all();
  915. $this->assertCount(1, $results);
  916. $table->locale('fr');
  917. $results = $table->find()->where(['Articles.id' => 1])->all();
  918. $this->assertCount(0, $results);
  919. }
  920. /**
  921. * Tests that records not translated in the current locale will not be
  922. * present in the results for the translations finder, and also proves
  923. * that this can be overridden.
  924. *
  925. * @return void
  926. */
  927. public function testFilterUntranslatedWithFinder()
  928. {
  929. $table = TableRegistry::get('Comments');
  930. $table->addBehavior('Translate', [
  931. 'fields' => ['comment'],
  932. 'onlyTranslated' => true
  933. ]);
  934. $table->locale('eng');
  935. $results = $table->find('translations')->all();
  936. $this->assertCount(4, $results);
  937. $table->locale('spa');
  938. $results = $table->find('translations')->all();
  939. $this->assertCount(1, $results);
  940. $table->locale('spa');
  941. $results = $table->find('translations', ['filterByCurrentLocale' => false])->all();
  942. $this->assertCount(6, $results);
  943. $table->locale('spa');
  944. $results = $table->find('translations')->all();
  945. $this->assertCount(1, $results);
  946. }
  947. /**
  948. * Tests that allowEmptyTranslations takes effect
  949. *
  950. * @return void
  951. */
  952. public function testEmptyTranslations()
  953. {
  954. $table = TableRegistry::get('Articles');
  955. $table->addBehavior('Translate', [
  956. 'fields' => ['title', 'body', 'description'],
  957. 'allowEmptyTranslations' => false,
  958. ]);
  959. $table->locale('spa');
  960. $result = $table->find()->first();
  961. $this->assertNull($result->description);
  962. }
  963. /**
  964. * Test save with clean translate fields
  965. *
  966. * @return void
  967. */
  968. public function testSaveWithCleanFields()
  969. {
  970. $table = TableRegistry::get('Articles');
  971. $table->addBehavior('Translate', ['fields' => ['title']]);
  972. $table->entityClass(__NAMESPACE__ . '\Article');
  973. I18n::locale('fra');
  974. $article = $table->get(1);
  975. $article->set('body', 'New Body');
  976. $table->save($article);
  977. $result = $table->get(1);
  978. $this->assertEquals('New Body', $result->body);
  979. $this->assertSame($article->title, $result->title);
  980. }
  981. /**
  982. * Test save new entity with _translations field
  983. *
  984. * @return void
  985. */
  986. public function testSaveNewRecordWithTranslatesField()
  987. {
  988. $table = TableRegistry::get('Articles');
  989. $table->addBehavior('Translate', [
  990. 'fields' => ['title'],
  991. 'validator' => (new \Cake\Validation\Validator)->add('title', 'notBlank', ['rule' => 'notBlank'])
  992. ]);
  993. $table->entityClass(__NAMESPACE__ . '\Article');
  994. $data = [
  995. 'author_id' => 1,
  996. 'published' => 'N',
  997. '_translations' => [
  998. 'en' => [
  999. 'title' => 'Title EN',
  1000. 'body' => 'Body EN'
  1001. ],
  1002. 'es' => [
  1003. 'title' => 'Title ES'
  1004. ]
  1005. ]
  1006. ];
  1007. $article = $table->patchEntity($table->newEntity(), $data);
  1008. $result = $table->save($article);
  1009. $this->assertNotFalse($result);
  1010. $expected = [
  1011. [
  1012. 'en' => [
  1013. 'title' => 'Title EN',
  1014. 'locale' => 'en'
  1015. ],
  1016. 'es' => [
  1017. 'title' => 'Title ES',
  1018. 'locale' => 'es'
  1019. ]
  1020. ]
  1021. ];
  1022. $result = $table->find('translations')->where(['id' => $result->id]);
  1023. $this->assertEquals($expected, $this->_extractTranslations($result)->toArray());
  1024. }
  1025. /**
  1026. * Test update entity with _translations field.
  1027. *
  1028. * @return void
  1029. */
  1030. public function testSaveExistingRecordWithTranslatesField()
  1031. {
  1032. $table = TableRegistry::get('Articles');
  1033. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1034. $table->entityClass(__NAMESPACE__ . '\Article');
  1035. $data = [
  1036. 'author_id' => 1,
  1037. 'published' => 'Y',
  1038. '_translations' => [
  1039. 'eng' => [
  1040. 'title' => 'First Article1',
  1041. 'body' => 'First Article content has been updated'
  1042. ],
  1043. 'spa' => [
  1044. 'title' => 'Mi nuevo titulo',
  1045. 'body' => 'Contenido Actualizado'
  1046. ]
  1047. ]
  1048. ];
  1049. $article = $table->find()->first();
  1050. $article = $table->patchEntity($article, $data);
  1051. $this->assertNotFalse($table->save($article));
  1052. $results = $this->_extractTranslations(
  1053. $table->find('translations')->where(['id' => 1])
  1054. )->first();
  1055. $this->assertEquals('Mi nuevo titulo', $results['spa']['title']);
  1056. $this->assertEquals('Contenido Actualizado', $results['spa']['body']);
  1057. $this->assertEquals('First Article1', $results['eng']['title']);
  1058. $this->assertEquals('Description #1', $results['eng']['description']);
  1059. }
  1060. /**
  1061. * Test that no properties are enabled when the translations
  1062. * option is off.
  1063. *
  1064. * @return void
  1065. */
  1066. public function testBuildMarshalMapTranslationsOff()
  1067. {
  1068. $table = TableRegistry::get('Articles');
  1069. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1070. $marshaller = $table->marshaller();
  1071. $translate = $table->behaviors()->get('Translate');
  1072. $result = $translate->buildMarshalMap($marshaller, [], ['translations' => false]);
  1073. $this->assertSame([], $result);
  1074. }
  1075. /**
  1076. * Test building a marshal map with translations on.
  1077. *
  1078. * @return void
  1079. */
  1080. public function testBuildMarshalMapTranslationsOn()
  1081. {
  1082. $table = TableRegistry::get('Articles');
  1083. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1084. $marshaller = $table->marshaller();
  1085. $translate = $table->behaviors()->get('Translate');
  1086. $result = $translate->buildMarshalMap($marshaller, [], ['translations' => true]);
  1087. $this->assertArrayHasKey('_translations', $result);
  1088. $this->assertInstanceOf('Closure', $result['_translations']);
  1089. $result = $translate->buildMarshalMap($marshaller, [], []);
  1090. $this->assertArrayHasKey('_translations', $result);
  1091. $this->assertInstanceOf('Closure', $result['_translations']);
  1092. }
  1093. /**
  1094. * Test marshalling non-array data
  1095. *
  1096. * @return void
  1097. */
  1098. public function testBuildMarshalMapNonArrayData()
  1099. {
  1100. $table = TableRegistry::get('Articles');
  1101. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1102. $translate = $table->behaviors()->get('Translate');
  1103. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1104. $entity = $table->newEntity();
  1105. $result = $map['_translations']('garbage', $entity);
  1106. $this->assertNull($result, 'Non-array should not error out.');
  1107. $this->assertEmpty($entity->errors());
  1108. $this->assertEmpty($entity->get('_translations'));
  1109. }
  1110. /**
  1111. * Test buildMarshalMap() builds new entities.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testBuildMarshalMapBuildEntities()
  1116. {
  1117. $table = TableRegistry::get('Articles');
  1118. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1119. $translate = $table->behaviors()->get('Translate');
  1120. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1121. $entity = $table->newEntity();
  1122. $data = [
  1123. 'en' => [
  1124. 'title' => 'English Title',
  1125. 'body' => 'English Content'
  1126. ],
  1127. 'es' => [
  1128. 'title' => 'Titulo Español',
  1129. 'body' => 'Contenido Español'
  1130. ]
  1131. ];
  1132. $result = $map['_translations']($data, $entity);
  1133. $this->assertEmpty($entity->errors(), 'No validation errors.');
  1134. $this->assertCount(2, $result);
  1135. $this->assertArrayHasKey('en', $result);
  1136. $this->assertArrayHasKey('es', $result);
  1137. $this->assertEquals('English Title', $result['en']->title);
  1138. $this->assertEquals('Titulo Español', $result['es']->title);
  1139. }
  1140. /**
  1141. * Test that validation errors are added to the original entity.
  1142. *
  1143. * @return void
  1144. */
  1145. public function testBuildMarshalMapBuildEntitiesValidationErrors()
  1146. {
  1147. $table = TableRegistry::get('Articles');
  1148. $table->addBehavior('Translate', [
  1149. 'fields' => ['title', 'body'],
  1150. 'validator' => 'custom'
  1151. ]);
  1152. $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
  1153. $table->validator('custom', $validator);
  1154. $translate = $table->behaviors()->get('Translate');
  1155. $entity = $table->newEntity();
  1156. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1157. $data = [
  1158. 'en' => [
  1159. 'title' => 'English Title',
  1160. 'body' => 'English Content'
  1161. ],
  1162. 'es' => [
  1163. 'title' => '',
  1164. 'body' => 'Contenido Español'
  1165. ]
  1166. ];
  1167. $result = $map['_translations']($data, $entity);
  1168. $this->assertNotEmpty($entity->errors(), 'Needs validation errors.');
  1169. $expected = [
  1170. 'title' => [
  1171. '_empty' => 'This field cannot be left empty'
  1172. ]
  1173. ];
  1174. $this->assertEquals($expected, $entity->errors('es'));
  1175. $this->assertEquals('English Title', $result['en']->title);
  1176. $this->assertEquals('', $result['es']->title);
  1177. }
  1178. /**
  1179. * Test that marshalling updates existing translation entities.
  1180. *
  1181. * @return void
  1182. */
  1183. public function testBuildMarshalMapUpdateExistingEntities()
  1184. {
  1185. $table = TableRegistry::get('Articles');
  1186. $table->addBehavior('Translate', [
  1187. 'fields' => ['title', 'body'],
  1188. ]);
  1189. $translate = $table->behaviors()->get('Translate');
  1190. $entity = $table->newEntity();
  1191. $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1192. $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1193. $entity->set('_translations', [
  1194. 'es' => $es,
  1195. 'en' => $en,
  1196. ]);
  1197. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1198. $data = [
  1199. 'en' => [
  1200. 'title' => 'English Title',
  1201. ],
  1202. 'es' => [
  1203. 'title' => 'Spanish Title',
  1204. ]
  1205. ];
  1206. $result = $map['_translations']($data, $entity);
  1207. $this->assertEmpty($entity->errors(), 'No validation errors.');
  1208. $this->assertSame($en, $result['en']);
  1209. $this->assertSame($es, $result['es']);
  1210. $this->assertSame($en, $entity->get('_translations')['en']);
  1211. $this->assertSame($es, $entity->get('_translations')['es']);
  1212. $this->assertEquals('English Title', $result['en']->title);
  1213. $this->assertEquals('Spanish Title', $result['es']->title);
  1214. $this->assertEquals('Old body', $result['en']->body);
  1215. $this->assertEquals('Old body', $result['es']->body);
  1216. }
  1217. /**
  1218. * Test that updating translation records works with validations.
  1219. *
  1220. * @return void
  1221. */
  1222. public function testBuildMarshalMapUpdateEntitiesValidationErrors()
  1223. {
  1224. $table = TableRegistry::get('Articles');
  1225. $table->addBehavior('Translate', [
  1226. 'fields' => ['title', 'body'],
  1227. 'validator' => 'custom'
  1228. ]);
  1229. $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
  1230. $table->validator('custom', $validator);
  1231. $translate = $table->behaviors()->get('Translate');
  1232. $entity = $table->newEntity();
  1233. $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1234. $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1235. $entity->set('_translations', [
  1236. 'es' => $es,
  1237. 'en' => $en,
  1238. ]);
  1239. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1240. $data = [
  1241. 'en' => [
  1242. 'title' => 'English Title',
  1243. 'body' => 'English Content'
  1244. ],
  1245. 'es' => [
  1246. 'title' => '',
  1247. 'body' => 'Contenido Español'
  1248. ]
  1249. ];
  1250. $result = $map['_translations']($data, $entity);
  1251. $this->assertNotEmpty($entity->errors(), 'Needs validation errors.');
  1252. $expected = [
  1253. 'title' => [
  1254. '_empty' => 'This field cannot be left empty'
  1255. ]
  1256. ];
  1257. $this->assertEquals($expected, $entity->errors('es'));
  1258. }
  1259. }