TranslateBehaviorTest.php 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM\Behavior;
  16. use Cake\Collection\Collection;
  17. use Cake\Datasource\EntityInterface;
  18. use Cake\I18n\I18n;
  19. use Cake\ORM\Behavior\Translate\TranslateTrait;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Locator\TableLocator;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Validation\Validator;
  24. /**
  25. * Stub entity class
  26. */
  27. class Article extends Entity
  28. {
  29. use TranslateTrait;
  30. }
  31. /**
  32. * Translate behavior test case
  33. */
  34. class TranslateBehaviorTest extends TestCase
  35. {
  36. /**
  37. * fixtures
  38. *
  39. * @var array
  40. */
  41. public $fixtures = [
  42. 'core.Articles',
  43. 'core.ArticlesTags',
  44. 'core.Authors',
  45. 'core.Groups',
  46. 'core.SpecialTags',
  47. 'core.Tags',
  48. 'core.Comments',
  49. 'core.Translates',
  50. ];
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. I18n::setLocale(I18n::getDefaultLocale());
  55. }
  56. /**
  57. * Returns an array with all the translations found for a set of records
  58. *
  59. * @param array|\Traversable $data
  60. * @return Collection
  61. */
  62. protected function _extractTranslations($data)
  63. {
  64. return (new Collection($data))->map(function (EntityInterface $row) {
  65. $translations = $row->get('_translations');
  66. if (!$translations) {
  67. return [];
  68. }
  69. return array_map(function (EntityInterface $entity) {
  70. return $entity->toArray();
  71. }, $translations);
  72. });
  73. }
  74. /**
  75. * Tests that custom translation tables are respected
  76. *
  77. * @return void
  78. */
  79. public function testCustomTranslationTable()
  80. {
  81. $table = $this->getTableLocator()->get('Articles');
  82. $table->addBehavior('Translate', [
  83. 'translationTable' => '\TestApp\Model\Table\I18nTable',
  84. 'fields' => ['title', 'body'],
  85. ]);
  86. $items = $table->associations();
  87. $i18n = $items->getByProperty('_i18n');
  88. $this->assertEquals('\TestApp\Model\Table\I18nTable', $i18n->getName());
  89. $this->assertInstanceOf('TestApp\Model\Table\I18nTable', $i18n->getTarget());
  90. $this->assertEquals('test_custom_i18n_datasource', $i18n->getTarget()->getConnection()->configName());
  91. $this->assertEquals('custom_i18n_table', $i18n->getTarget()->getTable());
  92. }
  93. /**
  94. * Tests that the strategy can be changed for i18n
  95. *
  96. * @return void
  97. */
  98. public function testStrategy()
  99. {
  100. $table = $this->getTableLocator()->get('Articles');
  101. $table->addBehavior('Translate', [
  102. 'strategy' => 'select',
  103. 'fields' => ['title', 'body'],
  104. ]);
  105. $items = $table->associations();
  106. $i18n = $items->getByProperty('_i18n');
  107. $this->assertEquals('select', $i18n->getStrategy());
  108. }
  109. /**
  110. * Tests that fields from a translated model are overridden
  111. *
  112. * @return void
  113. */
  114. public function testFindSingleLocale()
  115. {
  116. $table = $this->getTableLocator()->get('Articles');
  117. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  118. $table->setLocale('eng');
  119. $results = $table->find()->combine('title', 'body', 'id')->toArray();
  120. $expected = [
  121. 1 => ['Title #1' => 'Content #1'],
  122. 2 => ['Title #2' => 'Content #2'],
  123. 3 => ['Title #3' => 'Content #3'],
  124. ];
  125. $this->assertSame($expected, $results);
  126. }
  127. /**
  128. * Test that iterating in a formatResults() does not drop data.
  129. *
  130. * @return void
  131. */
  132. public function testFindTranslationsFormatResultsIteration()
  133. {
  134. $table = $this->getTableLocator()->get('Articles');
  135. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  136. $table->setLocale('eng');
  137. $results = $table->find('translations')
  138. ->limit(1)
  139. ->formatResults(function ($results) {
  140. foreach ($results as $res) {
  141. $res->first = 'val';
  142. }
  143. foreach ($results as $res) {
  144. $res->second = 'loop';
  145. }
  146. return $results;
  147. })
  148. ->toArray();
  149. $this->assertCount(1, $results);
  150. $this->assertSame('Title #1', $results[0]->title);
  151. $this->assertSame('val', $results[0]->first);
  152. $this->assertSame('loop', $results[0]->second);
  153. $this->assertNotEmpty($results[0]->_translations);
  154. }
  155. /**
  156. * Tests that fields from a translated model use the I18n class locale
  157. * and that it propagates to associated models
  158. *
  159. * @return void
  160. */
  161. public function testFindSingleLocaleAssociatedEnv()
  162. {
  163. I18n::setLocale('eng');
  164. $table = $this->getTableLocator()->get('Articles');
  165. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  166. $table->hasMany('Comments');
  167. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  168. $results = $table->find()
  169. ->select(['id', 'title', 'body'])
  170. ->contain(['Comments' => ['fields' => ['article_id', 'comment']]])
  171. ->enableHydration(false)
  172. ->toArray();
  173. $expected = [
  174. [
  175. 'id' => 1,
  176. 'title' => 'Title #1',
  177. 'body' => 'Content #1',
  178. 'comments' => [
  179. ['article_id' => 1, 'comment' => 'Comment #1', '_locale' => 'eng'],
  180. ['article_id' => 1, 'comment' => 'Comment #2', '_locale' => 'eng'],
  181. ['article_id' => 1, 'comment' => 'Comment #3', '_locale' => 'eng'],
  182. ['article_id' => 1, 'comment' => 'Comment #4', '_locale' => 'eng'],
  183. ],
  184. '_locale' => 'eng',
  185. ],
  186. [
  187. 'id' => 2,
  188. 'title' => 'Title #2',
  189. 'body' => 'Content #2',
  190. 'comments' => [
  191. ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'eng'],
  192. ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'eng'],
  193. ],
  194. '_locale' => 'eng',
  195. ],
  196. [
  197. 'id' => 3,
  198. 'title' => 'Title #3',
  199. 'body' => 'Content #3',
  200. 'comments' => [],
  201. '_locale' => 'eng',
  202. ],
  203. ];
  204. $this->assertSame($expected, $results);
  205. I18n::setLocale('spa');
  206. $results = $table->find()
  207. ->select(['id', 'title', 'body'])
  208. ->contain([
  209. 'Comments' => [
  210. 'fields' => ['article_id', 'comment'],
  211. 'sort' => ['Comments.id' => 'ASC'],
  212. ],
  213. ])
  214. ->enableHydration(false)
  215. ->toArray();
  216. $expected = [
  217. [
  218. 'id' => 1,
  219. 'title' => 'First Article',
  220. 'body' => 'Contenido #1',
  221. 'comments' => [
  222. ['article_id' => 1, 'comment' => 'First Comment for First Article', '_locale' => 'spa'],
  223. ['article_id' => 1, 'comment' => 'Second Comment for First Article', '_locale' => 'spa'],
  224. ['article_id' => 1, 'comment' => 'Third Comment for First Article', '_locale' => 'spa'],
  225. ['article_id' => 1, 'comment' => 'Comentario #4', '_locale' => 'spa'],
  226. ],
  227. '_locale' => 'spa',
  228. ],
  229. [
  230. 'id' => 2,
  231. 'title' => 'Second Article',
  232. 'body' => 'Second Article Body',
  233. 'comments' => [
  234. ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'spa'],
  235. ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'spa'],
  236. ],
  237. '_locale' => 'spa',
  238. ],
  239. [
  240. 'id' => 3,
  241. 'title' => 'Third Article',
  242. 'body' => 'Third Article Body',
  243. 'comments' => [],
  244. '_locale' => 'spa',
  245. ],
  246. ];
  247. $this->assertSame($expected, $results);
  248. }
  249. /**
  250. * Tests that fields from a translated model are not overridden if translation
  251. * is null
  252. *
  253. * @return void
  254. */
  255. public function testFindSingleLocaleWithNullTranslation()
  256. {
  257. $table = $this->getTableLocator()->get('Comments');
  258. $table->addBehavior('Translate', ['fields' => ['comment']]);
  259. $table->setLocale('spa');
  260. $results = $table->find()
  261. ->where(['Comments.id' => 6])
  262. ->combine('id', 'comment')->toArray();
  263. $expected = [6 => 'Second Comment for Second Article'];
  264. $this->assertSame($expected, $results);
  265. }
  266. /**
  267. * Tests that overriding fields with the translate behavior works when
  268. * using conditions and that all other columns are preserved
  269. *
  270. * @return void
  271. */
  272. public function testFindSingleLocaleWithgetConditions()
  273. {
  274. $table = $this->getTableLocator()->get('Articles');
  275. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  276. $table->setLocale('eng');
  277. $results = $table->find()
  278. ->where(['Articles.id' => 2])
  279. ->all();
  280. $this->assertCount(1, $results);
  281. $row = $results->first();
  282. $expected = [
  283. 'id' => 2,
  284. 'title' => 'Title #2',
  285. 'body' => 'Content #2',
  286. 'author_id' => 3,
  287. 'published' => 'Y',
  288. '_locale' => 'eng',
  289. ];
  290. $this->assertEquals($expected, $row->toArray());
  291. }
  292. /**
  293. * Tests the deprecated locale method.
  294. *
  295. * @group deprecated
  296. * @return void
  297. */
  298. public function testLocale()
  299. {
  300. $this->deprecated(function () {
  301. $table = $this->getTableLocator()->get('Articles');
  302. $table->addBehavior('Translate');
  303. $this->assertEquals('en_US', $table->locale());
  304. $table->locale('fr_FR');
  305. $this->assertEquals('fr_FR', $table->locale());
  306. $table->locale(false);
  307. $this->assertEquals('en_US', $table->locale());
  308. I18n::setLocale('fr_FR');
  309. $this->assertEquals('fr_FR', $table->locale());
  310. });
  311. }
  312. /**
  313. * Tests the locale setter/getter.
  314. *
  315. * @return void
  316. */
  317. public function testSetGetLocale()
  318. {
  319. $table = $this->getTableLocator()->get('Articles');
  320. $table->addBehavior('Translate');
  321. $this->assertEquals('en_US', $table->getLocale());
  322. $table->setLocale('fr_FR');
  323. $this->assertEquals('fr_FR', $table->getLocale());
  324. $table->setLocale(null);
  325. $this->assertEquals('en_US', $table->getLocale());
  326. I18n::setLocale('fr_FR');
  327. $this->assertEquals('fr_FR', $table->getLocale());
  328. }
  329. /**
  330. * Tests translationField method for translated fields.
  331. *
  332. * @return void
  333. */
  334. public function testTranslationFieldForTranslatedFields()
  335. {
  336. $table = $this->getTableLocator()->get('Articles');
  337. $table->addBehavior('Translate', [
  338. 'fields' => ['title', 'body'],
  339. 'defaultLocale' => 'en_US',
  340. ]);
  341. $expectedSameLocale = 'Articles.title';
  342. $expectedOtherLocale = 'Articles_title_translation.content';
  343. $field = $table->translationField('title');
  344. $this->assertSame($expectedSameLocale, $field);
  345. I18n::setLocale('es_ES');
  346. $field = $table->translationField('title');
  347. $this->assertSame($expectedOtherLocale, $field);
  348. I18n::setLocale('en');
  349. $field = $table->translationField('title');
  350. $this->assertSame($expectedOtherLocale, $field);
  351. $table->removeBehavior('Translate');
  352. $table->addBehavior('Translate', [
  353. 'fields' => ['title', 'body'],
  354. 'defaultLocale' => 'de_DE',
  355. ]);
  356. I18n::setLocale('de_DE');
  357. $field = $table->translationField('title');
  358. $this->assertSame($expectedSameLocale, $field);
  359. I18n::setLocale('en_US');
  360. $field = $table->translationField('title');
  361. $this->assertSame($expectedOtherLocale, $field);
  362. $table->setLocale('de_DE');
  363. $field = $table->translationField('title');
  364. $this->assertSame($expectedSameLocale, $field);
  365. $table->setLocale('es');
  366. $field = $table->translationField('title');
  367. $this->assertSame($expectedOtherLocale, $field);
  368. }
  369. /**
  370. * Tests translationField method for other fields.
  371. *
  372. * @return void
  373. */
  374. public function testTranslationFieldForOtherFields()
  375. {
  376. $table = $this->getTableLocator()->get('Articles');
  377. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  378. $expected = 'Articles.foo';
  379. $field = $table->translationField('foo');
  380. $this->assertSame($expected, $field);
  381. }
  382. /**
  383. * Tests that translating fields work when other formatters are used
  384. *
  385. * @return void
  386. */
  387. public function testFindList()
  388. {
  389. $table = $this->getTableLocator()->get('Articles');
  390. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  391. $table->setLocale('eng');
  392. $results = $table->find('list')->toArray();
  393. $expected = [1 => 'Title #1', 2 => 'Title #2', 3 => 'Title #3'];
  394. $this->assertSame($expected, $results);
  395. }
  396. /**
  397. * Tests that the query count return the correct results
  398. *
  399. * @return void
  400. */
  401. public function testFindCount()
  402. {
  403. $table = $this->getTableLocator()->get('Articles');
  404. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  405. $table->setLocale('eng');
  406. $this->assertEquals(3, $table->find()->count());
  407. }
  408. /**
  409. * Tests that it is possible to get all translated fields at once
  410. *
  411. * @return void
  412. */
  413. public function testFindTranslations()
  414. {
  415. $table = $this->getTableLocator()->get('Articles');
  416. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  417. $results = $table->find('translations');
  418. $expected = [
  419. [
  420. 'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'description' => 'Description #1', 'locale' => 'eng'],
  421. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  422. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze'],
  423. 'spa' => ['body' => 'Contenido #1', 'locale' => 'spa', 'description' => ''],
  424. ],
  425. [
  426. 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
  427. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  428. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze'],
  429. ],
  430. [
  431. 'eng' => ['title' => 'Title #3', 'body' => 'Content #3', 'locale' => 'eng'],
  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 => ['First Article' => 'First Article Body'],
  440. 2 => ['Second Article' => 'Second Article Body'],
  441. 3 => ['Third Article' => 'Third Article Body'],
  442. ];
  443. $grouped = $results->combine('title', 'body', 'id');
  444. $this->assertEquals($expected, $grouped->toArray());
  445. }
  446. /**
  447. * Tests that it is possible to request just a few translations
  448. *
  449. * @return void
  450. */
  451. public function testFindFilteredTranslations()
  452. {
  453. $table = $this->getTableLocator()->get('Articles');
  454. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  455. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  456. $expected = [
  457. [
  458. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  459. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze'],
  460. ],
  461. [
  462. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  463. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze'],
  464. ],
  465. [
  466. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  467. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze'],
  468. ],
  469. ];
  470. $translations = $this->_extractTranslations($results);
  471. $this->assertEquals($expected, $translations->toArray());
  472. $expected = [
  473. 1 => ['First Article' => 'First Article Body'],
  474. 2 => ['Second Article' => 'Second Article Body'],
  475. 3 => ['Third Article' => 'Third Article Body'],
  476. ];
  477. $grouped = $results->combine('title', 'body', 'id');
  478. $this->assertEquals($expected, $grouped->toArray());
  479. }
  480. /**
  481. * Tests that it is possible to combine find('list') and find('translations')
  482. *
  483. * @return void
  484. */
  485. public function testFindTranslationsList()
  486. {
  487. $table = $this->getTableLocator()->get('Articles');
  488. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  489. $results = $table
  490. ->find('list', [
  491. 'keyField' => 'title',
  492. 'valueField' => '_translations.deu.title',
  493. 'groupField' => 'id',
  494. ])
  495. ->find('translations', ['locales' => ['deu']]);
  496. $expected = [
  497. 1 => ['First Article' => 'Titel #1'],
  498. 2 => ['Second Article' => 'Titel #2'],
  499. 3 => ['Third Article' => 'Titel #3'],
  500. ];
  501. $this->assertEquals($expected, $results->toArray());
  502. }
  503. /**
  504. * Tests that you can both override fields and find all translations
  505. *
  506. * @return void
  507. */
  508. public function testFindTranslationsWithFieldOverriding()
  509. {
  510. $table = $this->getTableLocator()->get('Articles');
  511. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  512. $table->setLocale('cze');
  513. $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
  514. $expected = [
  515. [
  516. 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
  517. 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze'],
  518. ],
  519. [
  520. 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
  521. 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze'],
  522. ],
  523. [
  524. 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
  525. 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze'],
  526. ],
  527. ];
  528. $translations = $this->_extractTranslations($results);
  529. $this->assertEquals($expected, $translations->toArray());
  530. $expected = [
  531. 1 => ['Titulek #1' => 'Obsah #1'],
  532. 2 => ['Titulek #2' => 'Obsah #2'],
  533. 3 => ['Titulek #3' => 'Obsah #3'],
  534. ];
  535. $grouped = $results->combine('title', 'body', 'id');
  536. $this->assertEquals($expected, $grouped->toArray());
  537. }
  538. /**
  539. * Tests that fields can be overridden in a hasMany association
  540. *
  541. * @return void
  542. */
  543. public function testFindSingleLocaleHasMany()
  544. {
  545. $table = $this->getTableLocator()->get('Articles');
  546. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  547. $table->hasMany('Comments');
  548. $comments = $table->hasMany('Comments')->getTarget();
  549. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  550. $table->setLocale('eng');
  551. $comments->setLocale('eng');
  552. $results = $table->find()->contain(['Comments' => function ($q) {
  553. return $q->select(['id', 'comment', 'article_id']);
  554. }]);
  555. $list = new Collection($results->first()->comments);
  556. $expected = [
  557. 1 => 'Comment #1',
  558. 2 => 'Comment #2',
  559. 3 => 'Comment #3',
  560. 4 => 'Comment #4',
  561. ];
  562. $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
  563. }
  564. /**
  565. * Test that it is possible to bring translations from hasMany relations
  566. *
  567. * @return void
  568. */
  569. public function testTranslationsHasMany()
  570. {
  571. $table = $this->getTableLocator()->get('Articles');
  572. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  573. $table->hasMany('Comments');
  574. $comments = $table->hasMany('Comments')->getTarget();
  575. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  576. $results = $table->find('translations')->contain([
  577. 'Comments' => function ($q) {
  578. return $q->find('translations')->select(['id', 'comment', 'article_id']);
  579. },
  580. ]);
  581. $comments = $results->first()->comments;
  582. $expected = [
  583. [
  584. 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng'],
  585. ],
  586. [
  587. 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng'],
  588. ],
  589. [
  590. 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng'],
  591. ],
  592. [
  593. 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
  594. 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa'],
  595. ],
  596. ];
  597. $translations = $this->_extractTranslations($comments);
  598. $this->assertEquals($expected, $translations->toArray());
  599. }
  600. /**
  601. * Tests that it is possible to both override fields with a translation and
  602. * also find separately other translations
  603. *
  604. * @return void
  605. */
  606. public function testTranslationsHasManyWithOverride()
  607. {
  608. $table = $this->getTableLocator()->get('Articles');
  609. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  610. $table->hasMany('Comments');
  611. $comments = $table->hasMany('Comments')->getTarget();
  612. $comments->addBehavior('Translate', ['fields' => ['comment']]);
  613. $table->setLocale('cze');
  614. $comments->setLocale('eng');
  615. $results = $table->find('translations')->contain([
  616. 'Comments' => function ($q) {
  617. return $q->find('translations')->select(['id', 'comment', 'article_id']);
  618. },
  619. ]);
  620. $comments = $results->first()->comments;
  621. $expected = [
  622. 1 => 'Comment #1',
  623. 2 => 'Comment #2',
  624. 3 => 'Comment #3',
  625. 4 => 'Comment #4',
  626. ];
  627. $list = new Collection($comments);
  628. $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
  629. $expected = [
  630. [
  631. 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng'],
  632. ],
  633. [
  634. 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng'],
  635. ],
  636. [
  637. 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng'],
  638. ],
  639. [
  640. 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
  641. 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa'],
  642. ],
  643. ];
  644. $translations = $this->_extractTranslations($comments);
  645. $this->assertEquals($expected, $translations->toArray());
  646. $this->assertEquals('Titulek #1', $results->first()->title);
  647. $this->assertEquals('Obsah #1', $results->first()->body);
  648. }
  649. /**
  650. * Tests that it is possible to translate belongsTo associations
  651. *
  652. * @return void
  653. */
  654. public function testFindSingleLocaleBelongsto()
  655. {
  656. $table = $this->getTableLocator()->get('Articles');
  657. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  658. $authors = $table->belongsTo('Authors')->getTarget();
  659. $authors->addBehavior('Translate', ['fields' => ['name']]);
  660. $table->setLocale('eng');
  661. $authors->setLocale('eng');
  662. $results = $table->find()
  663. ->select(['title', 'body'])
  664. ->order(['title' => 'asc'])
  665. ->contain(['Authors' => function ($q) {
  666. return $q->select(['id', 'name']);
  667. }]);
  668. $expected = [
  669. [
  670. 'title' => 'Title #1',
  671. 'body' => 'Content #1',
  672. 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
  673. '_locale' => 'eng',
  674. ],
  675. [
  676. 'title' => 'Title #2',
  677. 'body' => 'Content #2',
  678. 'author' => ['id' => 3, 'name' => 'larry', '_locale' => 'eng'],
  679. '_locale' => 'eng',
  680. ],
  681. [
  682. 'title' => 'Title #3',
  683. 'body' => 'Content #3',
  684. 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
  685. '_locale' => 'eng',
  686. ],
  687. ];
  688. $results = array_map(function ($r) {
  689. return $r->toArray();
  690. }, $results->toArray());
  691. $this->assertEquals($expected, $results);
  692. }
  693. /**
  694. * Tests that it is possible to translate belongsTo associations using loadInto
  695. *
  696. * @return void
  697. */
  698. public function testFindSingleLocaleBelongstoLoadInto()
  699. {
  700. $table = $this->getTableLocator()->get('Articles');
  701. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  702. $authors = $table->belongsTo('Authors')->getTarget();
  703. $authors->addBehavior('Translate', ['fields' => ['name']]);
  704. $table->setLocale('eng');
  705. $authors->setLocale('eng');
  706. $entity = $table->get(1);
  707. $result = $table->loadInto($entity, ['Authors']);
  708. $this->assertSame($entity, $result);
  709. $this->assertNotEmpty($entity->author);
  710. $this->assertNotEmpty($entity->author->name);
  711. $expected = $table->get(1, ['contain' => ['Authors']]);
  712. $this->assertEquals($expected, $result);
  713. $this->assertNotEmpty($entity->author);
  714. $this->assertNotEmpty($entity->author->name);
  715. }
  716. /**
  717. * Tests that it is possible to translate belongsToMany associations
  718. *
  719. * @return void
  720. */
  721. public function testFindSingleLocaleBelongsToMany()
  722. {
  723. $table = $this->getTableLocator()->get('Articles');
  724. $specialTags = $this->getTableLocator()->get('SpecialTags');
  725. $specialTags->addBehavior('Translate', ['fields' => ['extra_info']]);
  726. $table->belongsToMany('Tags', [
  727. 'through' => $specialTags,
  728. ]);
  729. $specialTags->setLocale('eng');
  730. $result = $table->get(2, ['contain' => 'Tags']);
  731. $this->assertNotEmpty($result);
  732. $this->assertNotEmpty($result->tags);
  733. $this->assertEquals('Translated Info', $result->tags[0]->special_tags[0]->extra_info);
  734. }
  735. /**
  736. * Tests that updating an existing record translations work
  737. *
  738. * @return void
  739. */
  740. public function testUpdateSingleLocale()
  741. {
  742. $table = $this->getTableLocator()->get('Articles');
  743. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  744. $table->setLocale('eng');
  745. $article = $table->find()->first();
  746. $this->assertEquals(1, $article->get('id'));
  747. $article->set('title', 'New translated article');
  748. $table->save($article);
  749. $this->assertNull($article->get('_i18n'));
  750. $article = $table->find()->first();
  751. $this->assertEquals(1, $article->get('id'));
  752. $this->assertEquals('New translated article', $article->get('title'));
  753. $this->assertEquals('Content #1', $article->get('body'));
  754. $table->setLocale(false);
  755. $article = $table->find()->first();
  756. $this->assertEquals(1, $article->get('id'));
  757. $this->assertEquals('First Article', $article->get('title'));
  758. $table->setLocale('eng');
  759. $article->set('title', 'Wow, such translated article');
  760. $article->set('body', 'A translated body');
  761. $table->save($article);
  762. $this->assertNull($article->get('_i18n'));
  763. $article = $table->find()->first();
  764. $this->assertEquals(1, $article->get('id'));
  765. $this->assertEquals('Wow, such translated article', $article->get('title'));
  766. $this->assertEquals('A translated body', $article->get('body'));
  767. }
  768. /**
  769. * Tests adding new translation to a record
  770. *
  771. * @return void
  772. */
  773. public function testInsertNewTranslations()
  774. {
  775. $table = $this->getTableLocator()->get('Articles');
  776. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  777. $table->setLocale('fra');
  778. $article = $table->find()->first();
  779. $this->assertEquals(1, $article->get('id'));
  780. $article->set('title', 'Le titre');
  781. $table->save($article);
  782. $this->assertEquals('fra', $article->get('_locale'));
  783. $article = $table->find()->first();
  784. $this->assertEquals(1, $article->get('id'));
  785. $this->assertEquals('Le titre', $article->get('title'));
  786. $this->assertEquals('First Article Body', $article->get('body'));
  787. $article->set('title', 'Un autre titre');
  788. $article->set('body', 'Le contenu');
  789. $table->save($article);
  790. $this->assertNull($article->get('_i18n'));
  791. $article = $table->find()->first();
  792. $this->assertEquals('Un autre titre', $article->get('title'));
  793. $this->assertEquals('Le contenu', $article->get('body'));
  794. }
  795. /**
  796. * Tests adding new translation to a record
  797. *
  798. * @return void
  799. */
  800. public function testAllowEmptyFalse()
  801. {
  802. $table = $this->getTableLocator()->get('Articles');
  803. $table->addBehavior('Translate', ['fields' => ['title'], 'allowEmptyTranslations' => false]);
  804. $article = $table->find()->first();
  805. $this->assertEquals(1, $article->get('id'));
  806. $article = $table->patchEntity($article, [
  807. '_translations' => [
  808. 'fra' => [
  809. 'title' => '',
  810. ],
  811. ],
  812. ]);
  813. $table->save($article);
  814. // Remove the Behavior to unset the content != '' condition
  815. $table->removeBehavior('Translate');
  816. $noFra = $table->I18n->find()->where(['locale' => 'fra'])->first();
  817. $this->assertEmpty($noFra);
  818. }
  819. /**
  820. * Tests adding new translation to a record
  821. *
  822. * @return void
  823. */
  824. public function testMixedAllowEmptyFalse()
  825. {
  826. $table = $this->getTableLocator()->get('Articles');
  827. $table->addBehavior('Translate', ['fields' => ['title', 'body'], 'allowEmptyTranslations' => false]);
  828. $article = $table->find()->first();
  829. $this->assertEquals(1, $article->get('id'));
  830. $article = $table->patchEntity($article, [
  831. '_translations' => [
  832. 'fra' => [
  833. 'title' => '',
  834. 'body' => 'Bonjour',
  835. ],
  836. ],
  837. ]);
  838. $table->save($article);
  839. $fra = $table->I18n->find()
  840. ->where([
  841. 'locale' => 'fra',
  842. 'field' => 'body',
  843. ])
  844. ->first();
  845. $this->assertSame('Bonjour', $fra->content);
  846. // Remove the Behavior to unset the content != '' condition
  847. $table->removeBehavior('Translate');
  848. $noTitle = $table->I18n->find()
  849. ->where([
  850. 'locale' => 'fra',
  851. 'field' => 'title',
  852. ])
  853. ->first();
  854. $this->assertEmpty($noTitle);
  855. }
  856. /**
  857. * Tests adding new translation to a record
  858. *
  859. * @return void
  860. */
  861. public function testMultipleAllowEmptyFalse()
  862. {
  863. $table = $this->getTableLocator()->get('Articles');
  864. $table->addBehavior('Translate', ['fields' => ['title', 'body'], 'allowEmptyTranslations' => false]);
  865. $article = $table->find()->first();
  866. $this->assertEquals(1, $article->get('id'));
  867. $article = $table->patchEntity($article, [
  868. '_translations' => [
  869. 'fra' => [
  870. 'title' => '',
  871. 'body' => 'Bonjour',
  872. ],
  873. 'de' => [
  874. 'title' => 'Titel',
  875. 'body' => 'Hallo',
  876. ],
  877. ],
  878. ]);
  879. $table->save($article);
  880. $fra = $table->I18n->find()
  881. ->where([
  882. 'locale' => 'fra',
  883. 'field' => 'body',
  884. ])
  885. ->first();
  886. $this->assertSame('Bonjour', $fra->content);
  887. $deTitle = $table->I18n->find()
  888. ->where([
  889. 'locale' => 'de',
  890. 'field' => 'title',
  891. ])
  892. ->first();
  893. $this->assertSame('Titel', $deTitle->content);
  894. $deBody = $table->I18n->find()
  895. ->where([
  896. 'locale' => 'de',
  897. 'field' => 'body',
  898. ])
  899. ->first();
  900. $this->assertSame('Hallo', $deBody->content);
  901. // Remove the Behavior to unset the content != '' condition
  902. $table->removeBehavior('Translate');
  903. $noTitle = $table->I18n->find()
  904. ->where([
  905. 'locale' => 'fra',
  906. 'field' => 'title',
  907. ])
  908. ->first();
  909. $this->assertEmpty($noTitle);
  910. }
  911. /**
  912. * Tests that it is possible to use the _locale property to specify the language
  913. * to use for saving an entity
  914. *
  915. * @return void
  916. */
  917. public function testUpdateTranslationWithLocaleInEntity()
  918. {
  919. $table = $this->getTableLocator()->get('Articles');
  920. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  921. $article = $table->find()->first();
  922. $this->assertEquals(1, $article->get('id'));
  923. $article->set('_locale', 'fra');
  924. $article->set('title', 'Le titre');
  925. $table->save($article);
  926. $this->assertNull($article->get('_i18n'));
  927. $article = $table->find()->first();
  928. $this->assertEquals(1, $article->get('id'));
  929. $this->assertEquals('First Article', $article->get('title'));
  930. $this->assertEquals('First Article Body', $article->get('body'));
  931. $table->setLocale('fra');
  932. $article = $table->find()->first();
  933. $this->assertEquals(1, $article->get('id'));
  934. $this->assertEquals('Le titre', $article->get('title'));
  935. $this->assertEquals('First Article Body', $article->get('body'));
  936. }
  937. /**
  938. * Tests that translations are added to the whitelist of associations to be
  939. * saved
  940. *
  941. * @return void
  942. */
  943. public function testSaveTranslationWithAssociationWhitelist()
  944. {
  945. $table = $this->getTableLocator()->get('Articles');
  946. $table->hasMany('Comments');
  947. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  948. $table->setLocale('fra');
  949. $article = $table->find()->first();
  950. $this->assertEquals(1, $article->get('id'));
  951. $article->set('title', 'Le titre');
  952. $table->save($article, ['associated' => ['Comments']]);
  953. $this->assertNull($article->get('_i18n'));
  954. $article = $table->find()->first();
  955. $this->assertEquals('Le titre', $article->get('title'));
  956. }
  957. /**
  958. * Tests that after deleting a translated entity, all translations are also removed
  959. *
  960. * @return void
  961. */
  962. public function testDelete()
  963. {
  964. $table = $this->getTableLocator()->get('Articles');
  965. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  966. $article = $table->find()->first();
  967. $this->assertTrue($table->delete($article));
  968. $translations = $this->getTableLocator()->get('I18n')->find()
  969. ->where(['model' => 'Articles', 'foreign_key' => $article->id])
  970. ->count();
  971. $this->assertEquals(0, $translations);
  972. }
  973. /**
  974. * Tests saving multiple translations at once when the translations already
  975. * exist in the database
  976. *
  977. * @return void
  978. */
  979. public function testSaveMultipleTranslations()
  980. {
  981. $table = $this->getTableLocator()->get('Articles');
  982. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  983. $article = $results = $table->find('translations')->first();
  984. $translations = $article->get('_translations');
  985. $translations['deu']->set('title', 'Another title');
  986. $translations['eng']->set('body', 'Another body');
  987. $article->set('_translations', $translations);
  988. $table->save($article);
  989. $this->assertNull($article->get('_i18n'));
  990. $article = $results = $table->find('translations')->first();
  991. $translations = $article->get('_translations');
  992. $this->assertEquals('Another title', $translations['deu']->get('title'));
  993. $this->assertEquals('Another body', $translations['eng']->get('body'));
  994. }
  995. /**
  996. * Tests saving multiple existing translations and adding new ones
  997. *
  998. * @return void
  999. */
  1000. public function testSaveMultipleNewTranslations()
  1001. {
  1002. $table = $this->getTableLocator()->get('Articles');
  1003. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1004. $article = $results = $table->find('translations')->first();
  1005. $translations = $article->get('_translations');
  1006. $translations['deu']->set('title', 'Another title');
  1007. $translations['eng']->set('body', 'Another body');
  1008. $translations['spa'] = new Entity(['title' => 'Titulo']);
  1009. $translations['fre'] = new Entity(['title' => 'Titre']);
  1010. $article->set('_translations', $translations);
  1011. $table->save($article);
  1012. $this->assertNull($article->get('_i18n'));
  1013. $article = $results = $table->find('translations')->first();
  1014. $translations = $article->get('_translations');
  1015. $this->assertEquals('Another title', $translations['deu']->get('title'));
  1016. $this->assertEquals('Another body', $translations['eng']->get('body'));
  1017. $this->assertEquals('Titulo', $translations['spa']->get('title'));
  1018. $this->assertEquals('Titre', $translations['fre']->get('title'));
  1019. }
  1020. /**
  1021. * Tests that iterating a resultset twice when using the translations finder
  1022. * will not cause any errors nor information loss
  1023. *
  1024. * @return void
  1025. */
  1026. public function testUseCountInFindTranslations()
  1027. {
  1028. $table = $this->getTableLocator()->get('Articles');
  1029. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1030. $articles = $results = $table->find('translations');
  1031. $all = $articles->all();
  1032. $this->assertCount(3, $all);
  1033. $article = $all->first();
  1034. $this->assertNotEmpty($article->get('_translations'));
  1035. }
  1036. /**
  1037. * Tests that multiple translations saved when having a default locale
  1038. * are correctly saved
  1039. *
  1040. * @return void
  1041. */
  1042. public function testSavingWithNonDefaultLocale()
  1043. {
  1044. $table = $this->getTableLocator()->get('Articles');
  1045. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1046. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1047. I18n::setLocale('fra');
  1048. $translations = [
  1049. 'fra' => ['title' => 'Un article'],
  1050. 'spa' => ['title' => 'Un artículo'],
  1051. ];
  1052. $article = $table->get(1);
  1053. foreach ($translations as $lang => $data) {
  1054. $article->translation($lang)->set($data, ['guard' => false]);
  1055. }
  1056. $table->save($article);
  1057. $article = $table->find('translations')->where(['Articles.id' => 1])->first();
  1058. $this->assertEquals('Un article', $article->translation('fra')->title);
  1059. $this->assertEquals('Un artículo', $article->translation('spa')->title);
  1060. }
  1061. /**
  1062. * Tests that translation queries are added to union queries as well.
  1063. *
  1064. * @return void
  1065. */
  1066. public function testTranslationWithUnionQuery()
  1067. {
  1068. $table = $this->getTableLocator()->get('Comments');
  1069. $table->addBehavior('Translate', ['fields' => ['comment']]);
  1070. $table->setLocale('spa');
  1071. $query = $table->find()->where(['Comments.id' => 6]);
  1072. $query2 = $table->find()->where(['Comments.id' => 5]);
  1073. $query->union($query2);
  1074. $results = $query->sortBy('id', SORT_ASC)->toList();
  1075. $this->assertCount(2, $results);
  1076. $this->assertEquals('First Comment for Second Article', $results[0]->comment);
  1077. $this->assertEquals('Second Comment for Second Article', $results[1]->comment);
  1078. }
  1079. /**
  1080. * Tests the use of `referenceName` config option.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testAutoReferenceName()
  1085. {
  1086. $table = $this->getTableLocator()->get('Articles');
  1087. $table->hasMany('OtherComments', ['className' => 'Comments']);
  1088. $table->OtherComments->addBehavior(
  1089. 'Translate',
  1090. ['fields' => ['comment']]
  1091. );
  1092. $items = $table->OtherComments->associations();
  1093. $association = $items->getByProperty('comment_translation');
  1094. $this->assertNotEmpty($association, 'Translation association not found');
  1095. $found = false;
  1096. foreach ($association->getConditions() as $key => $value) {
  1097. if (strpos($key, 'comment_translation.model') !== false) {
  1098. $found = true;
  1099. $this->assertEquals('Comments', $value);
  1100. break;
  1101. }
  1102. }
  1103. $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
  1104. }
  1105. /**
  1106. * Tests the use of unconventional `referenceName` config option.
  1107. *
  1108. * @return void
  1109. */
  1110. public function testChangingReferenceName()
  1111. {
  1112. $table = $this->getTableLocator()->get('Articles');
  1113. $table->setAlias('FavoritePost');
  1114. $table->addBehavior(
  1115. 'Translate',
  1116. ['fields' => ['body'], 'referenceName' => 'Posts']
  1117. );
  1118. $items = $table->associations();
  1119. $association = $items->getByProperty('body_translation');
  1120. $this->assertNotEmpty($association, 'Translation association not found');
  1121. $found = false;
  1122. foreach ($association->getConditions() as $key => $value) {
  1123. if (strpos($key, 'body_translation.model') !== false) {
  1124. $found = true;
  1125. $this->assertEquals('Posts', $value);
  1126. break;
  1127. }
  1128. }
  1129. $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
  1130. }
  1131. /**
  1132. * Tests that onlyTranslated will remove records from the result set
  1133. * if they are not fully translated
  1134. *
  1135. * @return void
  1136. */
  1137. public function testFilterUntranslated()
  1138. {
  1139. $table = $this->getTableLocator()->get('Articles');
  1140. $table->addBehavior('Translate', [
  1141. 'fields' => ['title', 'body'],
  1142. 'onlyTranslated' => true,
  1143. ]);
  1144. $table->setLocale('eng');
  1145. $results = $table->find()->where(['Articles.id' => 1])->all();
  1146. $this->assertCount(1, $results);
  1147. $table->setLocale('fr');
  1148. $results = $table->find()->where(['Articles.id' => 1])->all();
  1149. $this->assertCount(0, $results);
  1150. }
  1151. /**
  1152. * Tests that records not translated in the current locale will not be
  1153. * present in the results for the translations finder, and also proves
  1154. * that this can be overridden.
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFilterUntranslatedWithFinder()
  1159. {
  1160. $table = $this->getTableLocator()->get('Comments');
  1161. $table->addBehavior('Translate', [
  1162. 'fields' => ['comment'],
  1163. 'onlyTranslated' => true,
  1164. ]);
  1165. $table->setLocale('eng');
  1166. $results = $table->find('translations')->all();
  1167. $this->assertCount(4, $results);
  1168. $table->setLocale('spa');
  1169. $results = $table->find('translations')->all();
  1170. $this->assertCount(1, $results);
  1171. $table->setLocale('spa');
  1172. $results = $table->find('translations', ['filterByCurrentLocale' => false])->all();
  1173. $this->assertCount(6, $results);
  1174. $table->setLocale('spa');
  1175. $results = $table->find('translations')->all();
  1176. $this->assertCount(1, $results);
  1177. }
  1178. /**
  1179. * Tests that allowEmptyTranslations takes effect
  1180. *
  1181. * @return void
  1182. */
  1183. public function testEmptyTranslations()
  1184. {
  1185. $table = $this->getTableLocator()->get('Articles');
  1186. $table->addBehavior('Translate', [
  1187. 'fields' => ['title', 'body', 'description'],
  1188. 'allowEmptyTranslations' => false,
  1189. ]);
  1190. $table->setLocale('spa');
  1191. $result = $table->find()->first();
  1192. $this->assertNull($result->description);
  1193. }
  1194. /**
  1195. * Test save with clean translate fields
  1196. *
  1197. * @return void
  1198. */
  1199. public function testSaveWithCleanFields()
  1200. {
  1201. $table = $this->getTableLocator()->get('Articles');
  1202. $table->addBehavior('Translate', ['fields' => ['title']]);
  1203. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1204. I18n::setLocale('fra');
  1205. $article = $table->get(1);
  1206. $article->set('body', 'New Body');
  1207. $table->save($article);
  1208. $result = $table->get(1);
  1209. $this->assertEquals('New Body', $result->body);
  1210. $this->assertSame($article->title, $result->title);
  1211. }
  1212. /**
  1213. * Test save new entity with _translations field
  1214. *
  1215. * @return void
  1216. */
  1217. public function testSaveNewRecordWithTranslatesField()
  1218. {
  1219. $table = $this->getTableLocator()->get('Articles');
  1220. $table->addBehavior('Translate', [
  1221. 'fields' => ['title'],
  1222. 'validator' => (new \Cake\Validation\Validator())->add('title', 'notBlank', ['rule' => 'notBlank']),
  1223. ]);
  1224. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1225. $data = [
  1226. 'author_id' => 1,
  1227. 'published' => 'N',
  1228. '_translations' => [
  1229. 'en' => [
  1230. 'title' => 'Title EN',
  1231. 'body' => 'Body EN',
  1232. ],
  1233. 'es' => [
  1234. 'title' => 'Title ES',
  1235. ],
  1236. ],
  1237. ];
  1238. $article = $table->patchEntity($table->newEntity(), $data);
  1239. $result = $table->save($article);
  1240. $this->assertNotFalse($result);
  1241. $expected = [
  1242. [
  1243. 'en' => [
  1244. 'title' => 'Title EN',
  1245. 'locale' => 'en',
  1246. ],
  1247. 'es' => [
  1248. 'title' => 'Title ES',
  1249. 'locale' => 'es',
  1250. ],
  1251. ],
  1252. ];
  1253. $result = $table->find('translations')->where(['id' => $result->id]);
  1254. $this->assertEquals($expected, $this->_extractTranslations($result)->toArray());
  1255. }
  1256. /**
  1257. * Tests adding new translation to a record where the only field is the translated one and it's not the default locale
  1258. *
  1259. * @return void
  1260. */
  1261. public function testSaveNewRecordWithOnlyTranslationsNotDefaultLocale()
  1262. {
  1263. $table = $this->getTableLocator()->get('Groups');
  1264. $table->addBehavior('Translate', [
  1265. 'fields' => ['title'],
  1266. 'validator' => (new \Cake\Validation\Validator())->add('title', 'notBlank', ['rule' => 'notBlank']),
  1267. ]);
  1268. $data = [
  1269. '_translations' => [
  1270. 'es' => [
  1271. 'title' => 'Title ES',
  1272. ],
  1273. ],
  1274. ];
  1275. $group = $table->newEntity($data);
  1276. $result = $table->save($group);
  1277. $this->assertNotFalse($result, 'Record should save.');
  1278. $expected = [
  1279. [
  1280. 'es' => [
  1281. 'title' => 'Title ES',
  1282. 'locale' => 'es',
  1283. ],
  1284. ],
  1285. ];
  1286. $result = $table->find('translations')->where(['id' => $result->id]);
  1287. $this->assertEquals($expected, $this->_extractTranslations($result)->toArray());
  1288. }
  1289. /**
  1290. * Test that existing records can be updated when only translations
  1291. * are modified/dirty.
  1292. *
  1293. * @return void
  1294. */
  1295. public function testSaveExistingRecordOnlyTranslations()
  1296. {
  1297. $table = $this->getTableLocator()->get('Articles');
  1298. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1299. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1300. $data = [
  1301. '_translations' => [
  1302. 'es' => [
  1303. 'title' => 'Spanish Translation',
  1304. ],
  1305. ],
  1306. ];
  1307. $article = $table->find()->first();
  1308. $article = $table->patchEntity($article, $data);
  1309. $this->assertNotFalse($table->save($article));
  1310. $results = $this->_extractTranslations(
  1311. $table->find('translations')->where(['id' => 1])
  1312. )->first();
  1313. $this->assertArrayHasKey('es', $results, 'New translation added');
  1314. $this->assertArrayHasKey('eng', $results, 'Old translations present');
  1315. $this->assertEquals('Spanish Translation', $results['es']['title']);
  1316. }
  1317. /**
  1318. * Test update entity with _translations field.
  1319. *
  1320. * @return void
  1321. */
  1322. public function testSaveExistingRecordWithTranslatesField()
  1323. {
  1324. $table = $this->getTableLocator()->get('Articles');
  1325. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1326. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1327. $data = [
  1328. 'author_id' => 1,
  1329. 'published' => 'Y',
  1330. '_translations' => [
  1331. 'eng' => [
  1332. 'title' => 'First Article1',
  1333. 'body' => 'First Article content has been updated',
  1334. ],
  1335. 'spa' => [
  1336. 'title' => 'Mi nuevo titulo',
  1337. 'body' => 'Contenido Actualizado',
  1338. ],
  1339. ],
  1340. ];
  1341. $article = $table->find()->first();
  1342. $article = $table->patchEntity($article, $data);
  1343. $this->assertNotFalse($table->save($article));
  1344. $results = $this->_extractTranslations(
  1345. $table->find('translations')->where(['id' => 1])
  1346. )->first();
  1347. $this->assertEquals('Mi nuevo titulo', $results['spa']['title']);
  1348. $this->assertEquals('Contenido Actualizado', $results['spa']['body']);
  1349. $this->assertEquals('First Article1', $results['eng']['title']);
  1350. $this->assertEquals('Description #1', $results['eng']['description']);
  1351. }
  1352. /**
  1353. * Tests that default locale saves ok.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testSaveDefaultLocale()
  1358. {
  1359. $table = $this->getTableLocator()->get('Articles');
  1360. $table->hasMany('Comments');
  1361. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1362. $article = $table->get(1);
  1363. $data = [
  1364. 'title' => 'New title',
  1365. 'body' => 'New body',
  1366. ];
  1367. $article = $table->patchEntity($article, $data);
  1368. $table->save($article);
  1369. $this->assertNull($article->get('_i18n'));
  1370. $article = $table->get(1);
  1371. $this->assertEquals('New title', $article->get('title'));
  1372. $this->assertEquals('New body', $article->get('body'));
  1373. }
  1374. /**
  1375. * Tests that translations are added to the whitelist of associations to be
  1376. * saved
  1377. *
  1378. * @return void
  1379. */
  1380. public function testSaveTranslationDefaultLocale()
  1381. {
  1382. $table = $this->getTableLocator()->get('Articles');
  1383. $table->hasMany('Comments');
  1384. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1385. $article = $table->get(1);
  1386. $data = [
  1387. 'title' => 'New title',
  1388. 'body' => 'New body',
  1389. '_translations' => [
  1390. 'es' => [
  1391. 'title' => 'ES title',
  1392. 'body' => 'ES body',
  1393. ],
  1394. ],
  1395. ];
  1396. $article = $table->patchEntity($article, $data);
  1397. $table->save($article);
  1398. $this->assertNull($article->get('_i18n'));
  1399. $article = $table->find('translations')->where(['id' => 1])->first();
  1400. $this->assertEquals('New title', $article->get('title'));
  1401. $this->assertEquals('New body', $article->get('body'));
  1402. $this->assertEquals('ES title', $article->_translations['es']->title);
  1403. $this->assertEquals('ES body', $article->_translations['es']->body);
  1404. }
  1405. /**
  1406. * Test that no properties are enabled when the translations
  1407. * option is off.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testBuildMarshalMapTranslationsOff()
  1412. {
  1413. $table = $this->getTableLocator()->get('Articles');
  1414. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1415. $marshaller = $table->marshaller();
  1416. $translate = $table->behaviors()->get('Translate');
  1417. $result = $translate->buildMarshalMap($marshaller, [], ['translations' => false]);
  1418. $this->assertSame([], $result);
  1419. }
  1420. /**
  1421. * Test building a marshal map with translations on.
  1422. *
  1423. * @return void
  1424. */
  1425. public function testBuildMarshalMapTranslationsOn()
  1426. {
  1427. $table = $this->getTableLocator()->get('Articles');
  1428. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1429. $marshaller = $table->marshaller();
  1430. $translate = $table->behaviors()->get('Translate');
  1431. $result = $translate->buildMarshalMap($marshaller, [], ['translations' => true]);
  1432. $this->assertArrayHasKey('_translations', $result);
  1433. $this->assertInstanceOf('Closure', $result['_translations']);
  1434. $result = $translate->buildMarshalMap($marshaller, [], []);
  1435. $this->assertArrayHasKey('_translations', $result);
  1436. $this->assertInstanceOf('Closure', $result['_translations']);
  1437. }
  1438. /**
  1439. * Test marshalling non-array data
  1440. *
  1441. * @return void
  1442. */
  1443. public function testBuildMarshalMapNonArrayData()
  1444. {
  1445. $table = $this->getTableLocator()->get('Articles');
  1446. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1447. $translate = $table->behaviors()->get('Translate');
  1448. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1449. $entity = $table->newEntity();
  1450. $result = $map['_translations']('garbage', $entity);
  1451. $this->assertNull($result, 'Non-array should not error out.');
  1452. $this->assertEmpty($entity->getErrors());
  1453. $this->assertEmpty($entity->get('_translations'));
  1454. }
  1455. /**
  1456. * Test buildMarshalMap() builds new entities.
  1457. *
  1458. * @return void
  1459. */
  1460. public function testBuildMarshalMapBuildEntities()
  1461. {
  1462. $table = $this->getTableLocator()->get('Articles');
  1463. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1464. $translate = $table->behaviors()->get('Translate');
  1465. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1466. $entity = $table->newEntity();
  1467. $data = [
  1468. 'en' => [
  1469. 'title' => 'English Title',
  1470. 'body' => 'English Content',
  1471. ],
  1472. 'es' => [
  1473. 'title' => 'Titulo Español',
  1474. 'body' => 'Contenido Español',
  1475. ],
  1476. ];
  1477. $result = $map['_translations']($data, $entity);
  1478. $this->assertEmpty($entity->getErrors(), 'No validation errors.');
  1479. $this->assertCount(2, $result);
  1480. $this->assertArrayHasKey('en', $result);
  1481. $this->assertArrayHasKey('es', $result);
  1482. $this->assertEquals('English Title', $result['en']->title);
  1483. $this->assertEquals('Titulo Español', $result['es']->title);
  1484. }
  1485. /**
  1486. * Test that validation errors are added to the original entity.
  1487. *
  1488. * @return void
  1489. */
  1490. public function testBuildMarshalMapBuildEntitiesValidationErrors()
  1491. {
  1492. $table = $this->getTableLocator()->get('Articles');
  1493. $table->addBehavior('Translate', [
  1494. 'fields' => ['title', 'body'],
  1495. 'validator' => 'custom',
  1496. ]);
  1497. $validator = (new Validator())->add('title', 'notBlank', ['rule' => 'notBlank']);
  1498. $table->setValidator('custom', $validator);
  1499. $translate = $table->behaviors()->get('Translate');
  1500. $entity = $table->newEntity();
  1501. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1502. $data = [
  1503. 'en' => [
  1504. 'title' => 'English Title',
  1505. 'body' => 'English Content',
  1506. ],
  1507. 'es' => [
  1508. 'title' => '',
  1509. 'body' => 'Contenido Español',
  1510. ],
  1511. ];
  1512. $result = $map['_translations']($data, $entity);
  1513. $this->assertNotEmpty($entity->getErrors(), 'Needs validation errors.');
  1514. $expected = [
  1515. 'title' => [
  1516. '_empty' => 'This field cannot be left empty',
  1517. ],
  1518. ];
  1519. $this->assertEquals($expected, $entity->getError('es'));
  1520. $this->assertEquals('English Title', $result['en']->title);
  1521. $this->assertEquals('', $result['es']->title);
  1522. }
  1523. /**
  1524. * Test that marshalling updates existing translation entities.
  1525. *
  1526. * @return void
  1527. */
  1528. public function testBuildMarshalMapUpdateExistingEntities()
  1529. {
  1530. $table = $this->getTableLocator()->get('Articles');
  1531. $table->addBehavior('Translate', [
  1532. 'fields' => ['title', 'body'],
  1533. ]);
  1534. $translate = $table->behaviors()->get('Translate');
  1535. $entity = $table->newEntity();
  1536. $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1537. $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1538. $entity->set('_translations', [
  1539. 'es' => $es,
  1540. 'en' => $en,
  1541. ]);
  1542. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1543. $data = [
  1544. 'en' => [
  1545. 'title' => 'English Title',
  1546. ],
  1547. 'es' => [
  1548. 'title' => 'Spanish Title',
  1549. ],
  1550. ];
  1551. $result = $map['_translations']($data, $entity);
  1552. $this->assertEmpty($entity->getErrors(), 'No validation errors.');
  1553. $this->assertSame($en, $result['en']);
  1554. $this->assertSame($es, $result['es']);
  1555. $this->assertSame($en, $entity->get('_translations')['en']);
  1556. $this->assertSame($es, $entity->get('_translations')['es']);
  1557. $this->assertEquals('English Title', $result['en']->title);
  1558. $this->assertEquals('Spanish Title', $result['es']->title);
  1559. $this->assertEquals('Old body', $result['en']->body);
  1560. $this->assertEquals('Old body', $result['es']->body);
  1561. }
  1562. /**
  1563. * Test that updating translation records works with validations.
  1564. *
  1565. * @return void
  1566. */
  1567. public function testBuildMarshalMapUpdateEntitiesValidationErrors()
  1568. {
  1569. $table = $this->getTableLocator()->get('Articles');
  1570. $table->addBehavior('Translate', [
  1571. 'fields' => ['title', 'body'],
  1572. 'validator' => 'custom',
  1573. ]);
  1574. $validator = (new Validator())->add('title', 'notBlank', ['rule' => 'notBlank']);
  1575. $table->setValidator('custom', $validator);
  1576. $translate = $table->behaviors()->get('Translate');
  1577. $entity = $table->newEntity();
  1578. $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1579. $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1580. $entity->set('_translations', [
  1581. 'es' => $es,
  1582. 'en' => $en,
  1583. ]);
  1584. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1585. $data = [
  1586. 'en' => [
  1587. 'title' => 'English Title',
  1588. 'body' => 'English Content',
  1589. ],
  1590. 'es' => [
  1591. 'title' => '',
  1592. 'body' => 'Contenido Español',
  1593. ],
  1594. ];
  1595. $result = $map['_translations']($data, $entity);
  1596. $this->assertNotEmpty($entity->getErrors(), 'Needs validation errors.');
  1597. $expected = [
  1598. 'title' => [
  1599. '_empty' => 'This field cannot be left empty',
  1600. ],
  1601. ];
  1602. $this->assertEquals($expected, $entity->getError('es'));
  1603. }
  1604. /**
  1605. * Test that the behavior uses associations' locator.
  1606. *
  1607. * @return void
  1608. */
  1609. public function testDefaultTableLocator()
  1610. {
  1611. $locator = new TableLocator();
  1612. $table = $locator->get('Articles');
  1613. $table->addBehavior('Translate', [
  1614. 'fields' => ['title', 'body'],
  1615. 'validator' => 'custom',
  1616. ]);
  1617. $behaviorLocator = $table->behaviors()->get('Translate')->getTableLocator();
  1618. $this->assertSame($locator, $behaviorLocator);
  1619. $this->assertSame($table->associations()->getTableLocator(), $behaviorLocator);
  1620. $this->assertNotSame($this->getTableLocator(), $behaviorLocator);
  1621. }
  1622. /**
  1623. * Test that the behavior uses a custom locator.
  1624. *
  1625. * @return void
  1626. */
  1627. public function testCustomTableLocator()
  1628. {
  1629. $locator = new TableLocator();
  1630. $table = $this->getTableLocator()->get('Articles');
  1631. $table->addBehavior('Translate', [
  1632. 'fields' => ['title', 'body'],
  1633. 'validator' => 'custom',
  1634. 'tableLocator' => $locator,
  1635. ]);
  1636. $behaviorLocator = $table->behaviors()->get('Translate')->getTableLocator();
  1637. $this->assertSame($locator, $behaviorLocator);
  1638. $this->assertNotSame($table->associations()->getTableLocator(), $behaviorLocator);
  1639. $this->assertNotSame($this->getTableLocator(), $behaviorLocator);
  1640. }
  1641. /**
  1642. * Tests that using deep matching doesn't cause an association property to be created.
  1643. *
  1644. * @return void
  1645. */
  1646. public function testDeepMatchingDoesNotCreateAssociationProperty()
  1647. {
  1648. $table = $this->getTableLocator()->get('Articles');
  1649. $table->hasMany('Comments');
  1650. $table->Comments->belongsTo('Authors')->setForeignKey('user_id');
  1651. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  1652. $table->Comments->setLocale('abc');
  1653. $table->Comments->Authors->addBehavior('Translate', ['fields' => ['name']]);
  1654. $table->Comments->Authors->setLocale('xyz');
  1655. $this->assertNotEquals($table->Comments->getLocale(), I18n::getLocale());
  1656. $this->assertNotEquals($table->Comments->Authors->getLocale(), I18n::getLocale());
  1657. $result = $table
  1658. ->find()
  1659. ->contain('Comments')
  1660. ->matching('Comments.Authors')
  1661. ->first();
  1662. $this->assertArrayNotHasKey('author', $result->comments);
  1663. }
  1664. /**
  1665. * Tests that the _locale property is set on the entity in the _matchingData property.
  1666. *
  1667. * @return void
  1668. */
  1669. public function testLocalePropertyIsSetInMatchingData()
  1670. {
  1671. $table = $this->getTableLocator()->get('Articles');
  1672. $table->hasMany('Comments');
  1673. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  1674. $table->Comments->setLocale('abc');
  1675. $this->assertNotEquals($table->Comments->getLocale(), I18n::getLocale());
  1676. $result = $table
  1677. ->find()
  1678. ->contain('Comments')
  1679. ->matching('Comments')
  1680. ->first();
  1681. $this->assertArrayNotHasKey('_locale', $result->comments);
  1682. $this->assertEquals('abc', $result->_matchingData['Comments']->_locale);
  1683. }
  1684. /**
  1685. * Tests that the _locale property is set on the entity in the _matchingData property
  1686. * when using deep matching.
  1687. *
  1688. * @return void
  1689. */
  1690. public function testLocalePropertyIsSetInMatchingDataWhenUsingDeepMatching()
  1691. {
  1692. $table = $this->getTableLocator()->get('Articles');
  1693. $table->hasMany('Comments');
  1694. $table->Comments->belongsTo('Authors')->setForeignKey('user_id');
  1695. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  1696. $table->Comments->setLocale('abc');
  1697. $table->Comments->Authors->addBehavior('Translate', ['fields' => ['name']]);
  1698. $table->Comments->Authors->setLocale('xyz');
  1699. $this->assertNotEquals($table->Comments->getLocale(), I18n::getLocale());
  1700. $this->assertNotEquals($table->Comments->Authors->getLocale(), I18n::getLocale());
  1701. $result = $table
  1702. ->find()
  1703. ->contain('Comments.Authors')
  1704. ->matching('Comments.Authors')
  1705. ->first();
  1706. $this->assertArrayNotHasKey('_locale', $result->comments);
  1707. $this->assertEquals('abc', $result->_matchingData['Comments']->_locale);
  1708. $this->assertEquals('xyz', $result->_matchingData['Authors']->_locale);
  1709. }
  1710. /**
  1711. * Tests that the _locale property is set on the entity in the _matchingData property
  1712. * when using contained matching.
  1713. *
  1714. * @return void
  1715. */
  1716. public function testLocalePropertyIsSetInMatchingDataWhenUsingContainedMatching()
  1717. {
  1718. $table = $this->getTableLocator()->get('Authors');
  1719. $table->hasMany('Articles');
  1720. $table->Articles->belongsToMany('Tags');
  1721. $table->Articles->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1722. $table->Articles->setLocale('abc');
  1723. $table->Articles->Tags->addBehavior('Translate', ['fields' => ['name']]);
  1724. $table->Articles->Tags->setLocale('xyz');
  1725. $this->assertNotEquals($table->Articles->getLocale(), I18n::getLocale());
  1726. $this->assertNotEquals($table->Articles->Tags->getLocale(), I18n::getLocale());
  1727. $result = $table
  1728. ->find()
  1729. ->contain([
  1730. 'Articles' => function ($query) {
  1731. return $query->matching('Tags');
  1732. },
  1733. 'Articles.Tags',
  1734. ])
  1735. ->first();
  1736. $this->assertArrayNotHasKey('_locale', $result->articles);
  1737. $this->assertArrayNotHasKey('_locale', $result->articles[0]->tags);
  1738. $this->assertEquals('abc', $result->articles[0]->_locale);
  1739. $this->assertEquals('xyz', $result->articles[0]->_matchingData['Tags']->_locale);
  1740. }
  1741. }