TranslateBehaviorTest.php 52 KB

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