TranslateBehaviorTest.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  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 parent entity isn't dirty when containing a translated association
  737. *
  738. * @return void
  739. */
  740. public function testGetAssociationNotDirtyBelongsTo()
  741. {
  742. $table = $this->getTableLocator()->get('Articles');
  743. $authors = $table->belongsTo('Authors')->getTarget();
  744. $authors->addBehavior('Translate', ['fields' => ['name']]);
  745. $authors->setLocale('eng');
  746. $entity = $table->get(1);
  747. $this->assertNotEmpty($entity);
  748. $entity = $table->loadInto($entity, ['Authors']);
  749. $this->assertFalse($entity->isDirty());
  750. $this->assertNotEmpty($entity->author);
  751. $this->assertFalse($entity->author->isDirty());
  752. $entity = $table->get(1, ['contain' => ['Authors']]);
  753. $this->assertNotEmpty($entity);
  754. $this->assertFalse($entity->isDirty());
  755. $this->assertNotEmpty($entity->author);
  756. $this->assertFalse($entity->author->isDirty());
  757. }
  758. /**
  759. * Tests that parent entity isn't dirty when containing a translated association
  760. *
  761. * @return void
  762. */
  763. public function testGetAssociationNotDirtyHasOne()
  764. {
  765. $table = $this->getTableLocator()->get('Authors');
  766. $table->hasOne('Articles');
  767. $table->Articles->addBehavior('Translate', ['fields' => ['title']]);
  768. $entity = $table->get(1);
  769. $this->assertNotEmpty($entity);
  770. $entity = $table->loadInto($entity, ['Articles']);
  771. $this->assertFalse($entity->isDirty());
  772. $this->assertNotEmpty($entity->article);
  773. $this->assertFalse($entity->article->isDirty());
  774. $entity = $table->get(1, ['contain' => 'Articles']);
  775. $this->assertNotEmpty($entity);
  776. $this->assertFalse($entity->isDirty());
  777. $this->assertNotEmpty($entity->article);
  778. $this->assertFalse($entity->article->isDirty());
  779. }
  780. /**
  781. * Tests that updating an existing record translations work
  782. *
  783. * @return void
  784. */
  785. public function testUpdateSingleLocale()
  786. {
  787. $table = $this->getTableLocator()->get('Articles');
  788. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  789. $table->setLocale('eng');
  790. $article = $table->find()->first();
  791. $this->assertEquals(1, $article->get('id'));
  792. $article->set('title', 'New translated article');
  793. $table->save($article);
  794. $this->assertNull($article->get('_i18n'));
  795. $article = $table->find()->first();
  796. $this->assertEquals(1, $article->get('id'));
  797. $this->assertEquals('New translated article', $article->get('title'));
  798. $this->assertEquals('Content #1', $article->get('body'));
  799. $table->setLocale(false);
  800. $article = $table->find()->first();
  801. $this->assertEquals(1, $article->get('id'));
  802. $this->assertEquals('First Article', $article->get('title'));
  803. $table->setLocale('eng');
  804. $article->set('title', 'Wow, such translated article');
  805. $article->set('body', 'A translated body');
  806. $table->save($article);
  807. $this->assertNull($article->get('_i18n'));
  808. $article = $table->find()->first();
  809. $this->assertEquals(1, $article->get('id'));
  810. $this->assertEquals('Wow, such translated article', $article->get('title'));
  811. $this->assertEquals('A translated body', $article->get('body'));
  812. }
  813. /**
  814. * Tests adding new translation to a record
  815. *
  816. * @return void
  817. */
  818. public function testInsertNewTranslations()
  819. {
  820. $table = $this->getTableLocator()->get('Articles');
  821. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  822. $table->setLocale('fra');
  823. $article = $table->find()->first();
  824. $this->assertEquals(1, $article->get('id'));
  825. $article->set('title', 'Le titre');
  826. $table->save($article);
  827. $this->assertEquals('fra', $article->get('_locale'));
  828. $article = $table->find()->first();
  829. $this->assertEquals(1, $article->get('id'));
  830. $this->assertEquals('Le titre', $article->get('title'));
  831. $this->assertEquals('First Article Body', $article->get('body'));
  832. $article->set('title', 'Un autre titre');
  833. $article->set('body', 'Le contenu');
  834. $table->save($article);
  835. $this->assertNull($article->get('_i18n'));
  836. $article = $table->find()->first();
  837. $this->assertEquals('Un autre titre', $article->get('title'));
  838. $this->assertEquals('Le contenu', $article->get('body'));
  839. }
  840. /**
  841. * Tests adding new translation to a record
  842. *
  843. * @return void
  844. */
  845. public function testAllowEmptyFalse()
  846. {
  847. $table = $this->getTableLocator()->get('Articles');
  848. $table->addBehavior('Translate', ['fields' => ['title'], 'allowEmptyTranslations' => false]);
  849. $article = $table->find()->first();
  850. $this->assertEquals(1, $article->get('id'));
  851. $article = $table->patchEntity($article, [
  852. '_translations' => [
  853. 'fra' => [
  854. 'title' => '',
  855. ],
  856. ],
  857. ]);
  858. $table->save($article);
  859. // Remove the Behavior to unset the content != '' condition
  860. $table->removeBehavior('Translate');
  861. $noFra = $table->I18n->find()->where(['locale' => 'fra'])->first();
  862. $this->assertEmpty($noFra);
  863. }
  864. /**
  865. * Tests adding new translation to a record
  866. *
  867. * @return void
  868. */
  869. public function testMixedAllowEmptyFalse()
  870. {
  871. $table = $this->getTableLocator()->get('Articles');
  872. $table->addBehavior('Translate', ['fields' => ['title', 'body'], 'allowEmptyTranslations' => false]);
  873. $article = $table->find()->first();
  874. $this->assertEquals(1, $article->get('id'));
  875. $article = $table->patchEntity($article, [
  876. '_translations' => [
  877. 'fra' => [
  878. 'title' => '',
  879. 'body' => 'Bonjour',
  880. ],
  881. ],
  882. ]);
  883. $table->save($article);
  884. $fra = $table->I18n->find()
  885. ->where([
  886. 'locale' => 'fra',
  887. 'field' => 'body',
  888. ])
  889. ->first();
  890. $this->assertSame('Bonjour', $fra->content);
  891. // Remove the Behavior to unset the content != '' condition
  892. $table->removeBehavior('Translate');
  893. $noTitle = $table->I18n->find()
  894. ->where([
  895. 'locale' => 'fra',
  896. 'field' => 'title',
  897. ])
  898. ->first();
  899. $this->assertEmpty($noTitle);
  900. }
  901. /**
  902. * Tests adding new translation to a record
  903. *
  904. * @return void
  905. */
  906. public function testMultipleAllowEmptyFalse()
  907. {
  908. $table = $this->getTableLocator()->get('Articles');
  909. $table->addBehavior('Translate', ['fields' => ['title', 'body'], 'allowEmptyTranslations' => false]);
  910. $article = $table->find()->first();
  911. $this->assertEquals(1, $article->get('id'));
  912. $article = $table->patchEntity($article, [
  913. '_translations' => [
  914. 'fra' => [
  915. 'title' => '',
  916. 'body' => 'Bonjour',
  917. ],
  918. 'de' => [
  919. 'title' => 'Titel',
  920. 'body' => 'Hallo',
  921. ],
  922. ],
  923. ]);
  924. $table->save($article);
  925. $fra = $table->I18n->find()
  926. ->where([
  927. 'locale' => 'fra',
  928. 'field' => 'body',
  929. ])
  930. ->first();
  931. $this->assertSame('Bonjour', $fra->content);
  932. $deTitle = $table->I18n->find()
  933. ->where([
  934. 'locale' => 'de',
  935. 'field' => 'title',
  936. ])
  937. ->first();
  938. $this->assertSame('Titel', $deTitle->content);
  939. $deBody = $table->I18n->find()
  940. ->where([
  941. 'locale' => 'de',
  942. 'field' => 'body',
  943. ])
  944. ->first();
  945. $this->assertSame('Hallo', $deBody->content);
  946. // Remove the Behavior to unset the content != '' condition
  947. $table->removeBehavior('Translate');
  948. $noTitle = $table->I18n->find()
  949. ->where([
  950. 'locale' => 'fra',
  951. 'field' => 'title',
  952. ])
  953. ->first();
  954. $this->assertEmpty($noTitle);
  955. }
  956. /**
  957. * Tests that it is possible to use the _locale property to specify the language
  958. * to use for saving an entity
  959. *
  960. * @return void
  961. */
  962. public function testUpdateTranslationWithLocaleInEntity()
  963. {
  964. $table = $this->getTableLocator()->get('Articles');
  965. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  966. $article = $table->find()->first();
  967. $this->assertEquals(1, $article->get('id'));
  968. $article->set('_locale', 'fra');
  969. $article->set('title', 'Le titre');
  970. $table->save($article);
  971. $this->assertNull($article->get('_i18n'));
  972. $article = $table->find()->first();
  973. $this->assertEquals(1, $article->get('id'));
  974. $this->assertEquals('First Article', $article->get('title'));
  975. $this->assertEquals('First Article Body', $article->get('body'));
  976. $table->setLocale('fra');
  977. $article = $table->find()->first();
  978. $this->assertEquals(1, $article->get('id'));
  979. $this->assertEquals('Le titre', $article->get('title'));
  980. $this->assertEquals('First Article Body', $article->get('body'));
  981. }
  982. /**
  983. * Tests that translations are added to the whitelist of associations to be
  984. * saved
  985. *
  986. * @return void
  987. */
  988. public function testSaveTranslationWithAssociationWhitelist()
  989. {
  990. $table = $this->getTableLocator()->get('Articles');
  991. $table->hasMany('Comments');
  992. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  993. $table->setLocale('fra');
  994. $article = $table->find()->first();
  995. $this->assertEquals(1, $article->get('id'));
  996. $article->set('title', 'Le titre');
  997. $table->save($article, ['associated' => ['Comments']]);
  998. $this->assertNull($article->get('_i18n'));
  999. $article = $table->find()->first();
  1000. $this->assertEquals('Le titre', $article->get('title'));
  1001. }
  1002. /**
  1003. * Tests that after deleting a translated entity, all translations are also removed
  1004. *
  1005. * @return void
  1006. */
  1007. public function testDelete()
  1008. {
  1009. $table = $this->getTableLocator()->get('Articles');
  1010. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1011. $article = $table->find()->first();
  1012. $this->assertTrue($table->delete($article));
  1013. $translations = $this->getTableLocator()->get('I18n')->find()
  1014. ->where(['model' => 'Articles', 'foreign_key' => $article->id])
  1015. ->count();
  1016. $this->assertEquals(0, $translations);
  1017. }
  1018. /**
  1019. * Tests saving multiple translations at once when the translations already
  1020. * exist in the database
  1021. *
  1022. * @return void
  1023. */
  1024. public function testSaveMultipleTranslations()
  1025. {
  1026. $table = $this->getTableLocator()->get('Articles');
  1027. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1028. $article = $results = $table->find('translations')->first();
  1029. $translations = $article->get('_translations');
  1030. $translations['deu']->set('title', 'Another title');
  1031. $translations['eng']->set('body', 'Another body');
  1032. $article->set('_translations', $translations);
  1033. $table->save($article);
  1034. $this->assertNull($article->get('_i18n'));
  1035. $article = $results = $table->find('translations')->first();
  1036. $translations = $article->get('_translations');
  1037. $this->assertEquals('Another title', $translations['deu']->get('title'));
  1038. $this->assertEquals('Another body', $translations['eng']->get('body'));
  1039. }
  1040. /**
  1041. * Tests saving multiple existing translations and adding new ones
  1042. *
  1043. * @return void
  1044. */
  1045. public function testSaveMultipleNewTranslations()
  1046. {
  1047. $table = $this->getTableLocator()->get('Articles');
  1048. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1049. $article = $results = $table->find('translations')->first();
  1050. $translations = $article->get('_translations');
  1051. $translations['deu']->set('title', 'Another title');
  1052. $translations['eng']->set('body', 'Another body');
  1053. $translations['spa'] = new Entity(['title' => 'Titulo']);
  1054. $translations['fre'] = new Entity(['title' => 'Titre']);
  1055. $article->set('_translations', $translations);
  1056. $table->save($article);
  1057. $this->assertNull($article->get('_i18n'));
  1058. $article = $results = $table->find('translations')->first();
  1059. $translations = $article->get('_translations');
  1060. $this->assertEquals('Another title', $translations['deu']->get('title'));
  1061. $this->assertEquals('Another body', $translations['eng']->get('body'));
  1062. $this->assertEquals('Titulo', $translations['spa']->get('title'));
  1063. $this->assertEquals('Titre', $translations['fre']->get('title'));
  1064. }
  1065. /**
  1066. * Tests that iterating a resultset twice when using the translations finder
  1067. * will not cause any errors nor information loss
  1068. *
  1069. * @return void
  1070. */
  1071. public function testUseCountInFindTranslations()
  1072. {
  1073. $table = $this->getTableLocator()->get('Articles');
  1074. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1075. $articles = $results = $table->find('translations');
  1076. $all = $articles->all();
  1077. $this->assertCount(3, $all);
  1078. $article = $all->first();
  1079. $this->assertNotEmpty($article->get('_translations'));
  1080. }
  1081. /**
  1082. * Tests that multiple translations saved when having a default locale
  1083. * are correctly saved
  1084. *
  1085. * @return void
  1086. */
  1087. public function testSavingWithNonDefaultLocale()
  1088. {
  1089. $table = $this->getTableLocator()->get('Articles');
  1090. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1091. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1092. I18n::setLocale('fra');
  1093. $translations = [
  1094. 'fra' => ['title' => 'Un article'],
  1095. 'spa' => ['title' => 'Un artículo'],
  1096. ];
  1097. $article = $table->get(1);
  1098. foreach ($translations as $lang => $data) {
  1099. $article->translation($lang)->set($data, ['guard' => false]);
  1100. }
  1101. $table->save($article);
  1102. $article = $table->find('translations')->where(['Articles.id' => 1])->first();
  1103. $this->assertEquals('Un article', $article->translation('fra')->title);
  1104. $this->assertEquals('Un artículo', $article->translation('spa')->title);
  1105. }
  1106. /**
  1107. * Tests that translation queries are added to union queries as well.
  1108. *
  1109. * @return void
  1110. */
  1111. public function testTranslationWithUnionQuery()
  1112. {
  1113. $table = $this->getTableLocator()->get('Comments');
  1114. $table->addBehavior('Translate', ['fields' => ['comment']]);
  1115. $table->setLocale('spa');
  1116. $query = $table->find()->where(['Comments.id' => 6]);
  1117. $query2 = $table->find()->where(['Comments.id' => 5]);
  1118. $query->union($query2);
  1119. $results = $query->sortBy('id', SORT_ASC)->toList();
  1120. $this->assertCount(2, $results);
  1121. $this->assertEquals('First Comment for Second Article', $results[0]->comment);
  1122. $this->assertEquals('Second Comment for Second Article', $results[1]->comment);
  1123. }
  1124. /**
  1125. * Tests the use of `referenceName` config option.
  1126. *
  1127. * @return void
  1128. */
  1129. public function testAutoReferenceName()
  1130. {
  1131. $table = $this->getTableLocator()->get('Articles');
  1132. $table->hasMany('OtherComments', ['className' => 'Comments']);
  1133. $table->OtherComments->addBehavior(
  1134. 'Translate',
  1135. ['fields' => ['comment']]
  1136. );
  1137. $items = $table->OtherComments->associations();
  1138. $association = $items->getByProperty('comment_translation');
  1139. $this->assertNotEmpty($association, 'Translation association not found');
  1140. $found = false;
  1141. foreach ($association->getConditions() as $key => $value) {
  1142. if (strpos($key, 'comment_translation.model') !== false) {
  1143. $found = true;
  1144. $this->assertEquals('Comments', $value);
  1145. break;
  1146. }
  1147. }
  1148. $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
  1149. }
  1150. /**
  1151. * Tests the use of unconventional `referenceName` config option.
  1152. *
  1153. * @return void
  1154. */
  1155. public function testChangingReferenceName()
  1156. {
  1157. $table = $this->getTableLocator()->get('Articles');
  1158. $table->setAlias('FavoritePost');
  1159. $table->addBehavior(
  1160. 'Translate',
  1161. ['fields' => ['body'], 'referenceName' => 'Posts']
  1162. );
  1163. $items = $table->associations();
  1164. $association = $items->getByProperty('body_translation');
  1165. $this->assertNotEmpty($association, 'Translation association not found');
  1166. $found = false;
  1167. foreach ($association->getConditions() as $key => $value) {
  1168. if (strpos($key, 'body_translation.model') !== false) {
  1169. $found = true;
  1170. $this->assertEquals('Posts', $value);
  1171. break;
  1172. }
  1173. }
  1174. $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
  1175. }
  1176. /**
  1177. * Tests that onlyTranslated will remove records from the result set
  1178. * if they are not fully translated
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFilterUntranslated()
  1183. {
  1184. $table = $this->getTableLocator()->get('Articles');
  1185. $table->addBehavior('Translate', [
  1186. 'fields' => ['title', 'body'],
  1187. 'onlyTranslated' => true,
  1188. ]);
  1189. $table->setLocale('eng');
  1190. $results = $table->find()->where(['Articles.id' => 1])->all();
  1191. $this->assertCount(1, $results);
  1192. $table->setLocale('fr');
  1193. $results = $table->find()->where(['Articles.id' => 1])->all();
  1194. $this->assertCount(0, $results);
  1195. }
  1196. /**
  1197. * Tests that records not translated in the current locale will not be
  1198. * present in the results for the translations finder, and also proves
  1199. * that this can be overridden.
  1200. *
  1201. * @return void
  1202. */
  1203. public function testFilterUntranslatedWithFinder()
  1204. {
  1205. $table = $this->getTableLocator()->get('Comments');
  1206. $table->addBehavior('Translate', [
  1207. 'fields' => ['comment'],
  1208. 'onlyTranslated' => true,
  1209. ]);
  1210. $table->setLocale('eng');
  1211. $results = $table->find('translations')->all();
  1212. $this->assertCount(4, $results);
  1213. $table->setLocale('spa');
  1214. $results = $table->find('translations')->all();
  1215. $this->assertCount(1, $results);
  1216. $table->setLocale('spa');
  1217. $results = $table->find('translations', ['filterByCurrentLocale' => false])->all();
  1218. $this->assertCount(6, $results);
  1219. $table->setLocale('spa');
  1220. $results = $table->find('translations')->all();
  1221. $this->assertCount(1, $results);
  1222. }
  1223. /**
  1224. * Tests that allowEmptyTranslations takes effect
  1225. *
  1226. * @return void
  1227. */
  1228. public function testEmptyTranslations()
  1229. {
  1230. $table = $this->getTableLocator()->get('Articles');
  1231. $table->addBehavior('Translate', [
  1232. 'fields' => ['title', 'body', 'description'],
  1233. 'allowEmptyTranslations' => false,
  1234. ]);
  1235. $table->setLocale('spa');
  1236. $result = $table->find()->first();
  1237. $this->assertNull($result->description);
  1238. }
  1239. /**
  1240. * Test save with clean translate fields
  1241. *
  1242. * @return void
  1243. */
  1244. public function testSaveWithCleanFields()
  1245. {
  1246. $table = $this->getTableLocator()->get('Articles');
  1247. $table->addBehavior('Translate', ['fields' => ['title']]);
  1248. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1249. I18n::setLocale('fra');
  1250. $article = $table->get(1);
  1251. $article->set('body', 'New Body');
  1252. $table->save($article);
  1253. $result = $table->get(1);
  1254. $this->assertEquals('New Body', $result->body);
  1255. $this->assertSame($article->title, $result->title);
  1256. }
  1257. /**
  1258. * Test save new entity with _translations field
  1259. *
  1260. * @return void
  1261. */
  1262. public function testSaveNewRecordWithTranslatesField()
  1263. {
  1264. $table = $this->getTableLocator()->get('Articles');
  1265. $table->addBehavior('Translate', [
  1266. 'fields' => ['title'],
  1267. 'validator' => (new \Cake\Validation\Validator())->add('title', 'notBlank', ['rule' => 'notBlank']),
  1268. ]);
  1269. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1270. $data = [
  1271. 'author_id' => 1,
  1272. 'published' => 'N',
  1273. '_translations' => [
  1274. 'en' => [
  1275. 'title' => 'Title EN',
  1276. 'body' => 'Body EN',
  1277. ],
  1278. 'es' => [
  1279. 'title' => 'Title ES',
  1280. ],
  1281. ],
  1282. ];
  1283. $article = $table->patchEntity($table->newEntity(), $data);
  1284. $result = $table->save($article);
  1285. $this->assertNotFalse($result);
  1286. $expected = [
  1287. [
  1288. 'en' => [
  1289. 'title' => 'Title EN',
  1290. 'locale' => 'en',
  1291. ],
  1292. 'es' => [
  1293. 'title' => 'Title ES',
  1294. 'locale' => 'es',
  1295. ],
  1296. ],
  1297. ];
  1298. $result = $table->find('translations')->where(['id' => $result->id]);
  1299. $this->assertEquals($expected, $this->_extractTranslations($result)->toArray());
  1300. }
  1301. /**
  1302. * Tests adding new translation to a record where the only field is the translated one and it's not the default locale
  1303. *
  1304. * @return void
  1305. */
  1306. public function testSaveNewRecordWithOnlyTranslationsNotDefaultLocale()
  1307. {
  1308. $table = $this->getTableLocator()->get('Groups');
  1309. $table->addBehavior('Translate', [
  1310. 'fields' => ['title'],
  1311. 'validator' => (new \Cake\Validation\Validator())->add('title', 'notBlank', ['rule' => 'notBlank']),
  1312. ]);
  1313. $data = [
  1314. '_translations' => [
  1315. 'es' => [
  1316. 'title' => 'Title ES',
  1317. ],
  1318. ],
  1319. ];
  1320. $group = $table->newEntity($data);
  1321. $result = $table->save($group);
  1322. $this->assertNotFalse($result, 'Record should save.');
  1323. $expected = [
  1324. [
  1325. 'es' => [
  1326. 'title' => 'Title ES',
  1327. 'locale' => 'es',
  1328. ],
  1329. ],
  1330. ];
  1331. $result = $table->find('translations')->where(['id' => $result->id]);
  1332. $this->assertEquals($expected, $this->_extractTranslations($result)->toArray());
  1333. }
  1334. /**
  1335. * Test that existing records can be updated when only translations
  1336. * are modified/dirty.
  1337. *
  1338. * @return void
  1339. */
  1340. public function testSaveExistingRecordOnlyTranslations()
  1341. {
  1342. $table = $this->getTableLocator()->get('Articles');
  1343. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1344. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1345. $data = [
  1346. '_translations' => [
  1347. 'es' => [
  1348. 'title' => 'Spanish Translation',
  1349. ],
  1350. ],
  1351. ];
  1352. $article = $table->find()->first();
  1353. $article = $table->patchEntity($article, $data);
  1354. $this->assertNotFalse($table->save($article));
  1355. $results = $this->_extractTranslations(
  1356. $table->find('translations')->where(['id' => 1])
  1357. )->first();
  1358. $this->assertArrayHasKey('es', $results, 'New translation added');
  1359. $this->assertArrayHasKey('eng', $results, 'Old translations present');
  1360. $this->assertEquals('Spanish Translation', $results['es']['title']);
  1361. }
  1362. /**
  1363. * Test update entity with _translations field.
  1364. *
  1365. * @return void
  1366. */
  1367. public function testSaveExistingRecordWithTranslatesField()
  1368. {
  1369. $table = $this->getTableLocator()->get('Articles');
  1370. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1371. $table->setEntityClass(__NAMESPACE__ . '\Article');
  1372. $data = [
  1373. 'author_id' => 1,
  1374. 'published' => 'Y',
  1375. '_translations' => [
  1376. 'eng' => [
  1377. 'title' => 'First Article1',
  1378. 'body' => 'First Article content has been updated',
  1379. ],
  1380. 'spa' => [
  1381. 'title' => 'Mi nuevo titulo',
  1382. 'body' => 'Contenido Actualizado',
  1383. ],
  1384. ],
  1385. ];
  1386. $article = $table->find()->first();
  1387. $article = $table->patchEntity($article, $data);
  1388. $this->assertNotFalse($table->save($article));
  1389. $results = $this->_extractTranslations(
  1390. $table->find('translations')->where(['id' => 1])
  1391. )->first();
  1392. $this->assertEquals('Mi nuevo titulo', $results['spa']['title']);
  1393. $this->assertEquals('Contenido Actualizado', $results['spa']['body']);
  1394. $this->assertEquals('First Article1', $results['eng']['title']);
  1395. $this->assertEquals('Description #1', $results['eng']['description']);
  1396. }
  1397. /**
  1398. * Tests that default locale saves ok.
  1399. *
  1400. * @return void
  1401. */
  1402. public function testSaveDefaultLocale()
  1403. {
  1404. $table = $this->getTableLocator()->get('Articles');
  1405. $table->hasMany('Comments');
  1406. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1407. $article = $table->get(1);
  1408. $data = [
  1409. 'title' => 'New title',
  1410. 'body' => 'New body',
  1411. ];
  1412. $article = $table->patchEntity($article, $data);
  1413. $table->save($article);
  1414. $this->assertNull($article->get('_i18n'));
  1415. $article = $table->get(1);
  1416. $this->assertEquals('New title', $article->get('title'));
  1417. $this->assertEquals('New body', $article->get('body'));
  1418. }
  1419. /**
  1420. * Tests that translations are added to the whitelist of associations to be
  1421. * saved
  1422. *
  1423. * @return void
  1424. */
  1425. public function testSaveTranslationDefaultLocale()
  1426. {
  1427. $table = $this->getTableLocator()->get('Articles');
  1428. $table->hasMany('Comments');
  1429. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1430. $article = $table->get(1);
  1431. $data = [
  1432. 'title' => 'New title',
  1433. 'body' => 'New body',
  1434. '_translations' => [
  1435. 'es' => [
  1436. 'title' => 'ES title',
  1437. 'body' => 'ES body',
  1438. ],
  1439. ],
  1440. ];
  1441. $article = $table->patchEntity($article, $data);
  1442. $table->save($article);
  1443. $this->assertNull($article->get('_i18n'));
  1444. $article = $table->find('translations')->where(['id' => 1])->first();
  1445. $this->assertEquals('New title', $article->get('title'));
  1446. $this->assertEquals('New body', $article->get('body'));
  1447. $this->assertEquals('ES title', $article->_translations['es']->title);
  1448. $this->assertEquals('ES body', $article->_translations['es']->body);
  1449. }
  1450. /**
  1451. * Test that no properties are enabled when the translations
  1452. * option is off.
  1453. *
  1454. * @return void
  1455. */
  1456. public function testBuildMarshalMapTranslationsOff()
  1457. {
  1458. $table = $this->getTableLocator()->get('Articles');
  1459. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1460. $marshaller = $table->marshaller();
  1461. $translate = $table->behaviors()->get('Translate');
  1462. $result = $translate->buildMarshalMap($marshaller, [], ['translations' => false]);
  1463. $this->assertSame([], $result);
  1464. }
  1465. /**
  1466. * Test building a marshal map with translations on.
  1467. *
  1468. * @return void
  1469. */
  1470. public function testBuildMarshalMapTranslationsOn()
  1471. {
  1472. $table = $this->getTableLocator()->get('Articles');
  1473. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1474. $marshaller = $table->marshaller();
  1475. $translate = $table->behaviors()->get('Translate');
  1476. $result = $translate->buildMarshalMap($marshaller, [], ['translations' => true]);
  1477. $this->assertArrayHasKey('_translations', $result);
  1478. $this->assertInstanceOf('Closure', $result['_translations']);
  1479. $result = $translate->buildMarshalMap($marshaller, [], []);
  1480. $this->assertArrayHasKey('_translations', $result);
  1481. $this->assertInstanceOf('Closure', $result['_translations']);
  1482. }
  1483. /**
  1484. * Test marshalling non-array data
  1485. *
  1486. * @return void
  1487. */
  1488. public function testBuildMarshalMapNonArrayData()
  1489. {
  1490. $table = $this->getTableLocator()->get('Articles');
  1491. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1492. $translate = $table->behaviors()->get('Translate');
  1493. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1494. $entity = $table->newEntity();
  1495. $result = $map['_translations']('garbage', $entity);
  1496. $this->assertNull($result, 'Non-array should not error out.');
  1497. $this->assertEmpty($entity->getErrors());
  1498. $this->assertEmpty($entity->get('_translations'));
  1499. }
  1500. /**
  1501. * Test buildMarshalMap() builds new entities.
  1502. *
  1503. * @return void
  1504. */
  1505. public function testBuildMarshalMapBuildEntities()
  1506. {
  1507. $table = $this->getTableLocator()->get('Articles');
  1508. $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1509. $translate = $table->behaviors()->get('Translate');
  1510. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1511. $entity = $table->newEntity();
  1512. $data = [
  1513. 'en' => [
  1514. 'title' => 'English Title',
  1515. 'body' => 'English Content',
  1516. ],
  1517. 'es' => [
  1518. 'title' => 'Titulo Español',
  1519. 'body' => 'Contenido Español',
  1520. ],
  1521. ];
  1522. $result = $map['_translations']($data, $entity);
  1523. $this->assertEmpty($entity->getErrors(), 'No validation errors.');
  1524. $this->assertCount(2, $result);
  1525. $this->assertArrayHasKey('en', $result);
  1526. $this->assertArrayHasKey('es', $result);
  1527. $this->assertEquals('English Title', $result['en']->title);
  1528. $this->assertEquals('Titulo Español', $result['es']->title);
  1529. }
  1530. /**
  1531. * Test that validation errors are added to the original entity.
  1532. *
  1533. * @return void
  1534. */
  1535. public function testBuildMarshalMapBuildEntitiesValidationErrors()
  1536. {
  1537. $table = $this->getTableLocator()->get('Articles');
  1538. $table->addBehavior('Translate', [
  1539. 'fields' => ['title', 'body'],
  1540. 'validator' => 'custom',
  1541. ]);
  1542. $validator = (new Validator())->add('title', 'notBlank', ['rule' => 'notBlank']);
  1543. $table->setValidator('custom', $validator);
  1544. $translate = $table->behaviors()->get('Translate');
  1545. $entity = $table->newEntity();
  1546. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1547. $data = [
  1548. 'en' => [
  1549. 'title' => 'English Title',
  1550. 'body' => 'English Content',
  1551. ],
  1552. 'es' => [
  1553. 'title' => '',
  1554. 'body' => 'Contenido Español',
  1555. ],
  1556. ];
  1557. $result = $map['_translations']($data, $entity);
  1558. $this->assertNotEmpty($entity->getErrors(), 'Needs validation errors.');
  1559. $expected = [
  1560. 'title' => [
  1561. '_empty' => 'This field cannot be left empty',
  1562. ],
  1563. ];
  1564. $this->assertEquals($expected, $entity->getError('es'));
  1565. $this->assertEquals('English Title', $result['en']->title);
  1566. $this->assertEquals('', $result['es']->title);
  1567. }
  1568. /**
  1569. * Test that marshalling updates existing translation entities.
  1570. *
  1571. * @return void
  1572. */
  1573. public function testBuildMarshalMapUpdateExistingEntities()
  1574. {
  1575. $table = $this->getTableLocator()->get('Articles');
  1576. $table->addBehavior('Translate', [
  1577. 'fields' => ['title', 'body'],
  1578. ]);
  1579. $translate = $table->behaviors()->get('Translate');
  1580. $entity = $table->newEntity();
  1581. $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1582. $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1583. $entity->set('_translations', [
  1584. 'es' => $es,
  1585. 'en' => $en,
  1586. ]);
  1587. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1588. $data = [
  1589. 'en' => [
  1590. 'title' => 'English Title',
  1591. ],
  1592. 'es' => [
  1593. 'title' => 'Spanish Title',
  1594. ],
  1595. ];
  1596. $result = $map['_translations']($data, $entity);
  1597. $this->assertEmpty($entity->getErrors(), 'No validation errors.');
  1598. $this->assertSame($en, $result['en']);
  1599. $this->assertSame($es, $result['es']);
  1600. $this->assertSame($en, $entity->get('_translations')['en']);
  1601. $this->assertSame($es, $entity->get('_translations')['es']);
  1602. $this->assertEquals('English Title', $result['en']->title);
  1603. $this->assertEquals('Spanish Title', $result['es']->title);
  1604. $this->assertEquals('Old body', $result['en']->body);
  1605. $this->assertEquals('Old body', $result['es']->body);
  1606. }
  1607. /**
  1608. * Test that updating translation records works with validations.
  1609. *
  1610. * @return void
  1611. */
  1612. public function testBuildMarshalMapUpdateEntitiesValidationErrors()
  1613. {
  1614. $table = $this->getTableLocator()->get('Articles');
  1615. $table->addBehavior('Translate', [
  1616. 'fields' => ['title', 'body'],
  1617. 'validator' => 'custom',
  1618. ]);
  1619. $validator = (new Validator())->add('title', 'notBlank', ['rule' => 'notBlank']);
  1620. $table->setValidator('custom', $validator);
  1621. $translate = $table->behaviors()->get('Translate');
  1622. $entity = $table->newEntity();
  1623. $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1624. $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
  1625. $entity->set('_translations', [
  1626. 'es' => $es,
  1627. 'en' => $en,
  1628. ]);
  1629. $map = $translate->buildMarshalMap($table->marshaller(), [], []);
  1630. $data = [
  1631. 'en' => [
  1632. 'title' => 'English Title',
  1633. 'body' => 'English Content',
  1634. ],
  1635. 'es' => [
  1636. 'title' => '',
  1637. 'body' => 'Contenido Español',
  1638. ],
  1639. ];
  1640. $result = $map['_translations']($data, $entity);
  1641. $this->assertNotEmpty($entity->getErrors(), 'Needs validation errors.');
  1642. $expected = [
  1643. 'title' => [
  1644. '_empty' => 'This field cannot be left empty',
  1645. ],
  1646. ];
  1647. $this->assertEquals($expected, $entity->getError('es'));
  1648. }
  1649. /**
  1650. * Test that the behavior uses associations' locator.
  1651. *
  1652. * @return void
  1653. */
  1654. public function testDefaultTableLocator()
  1655. {
  1656. $locator = new TableLocator();
  1657. $table = $locator->get('Articles');
  1658. $table->addBehavior('Translate', [
  1659. 'fields' => ['title', 'body'],
  1660. 'validator' => 'custom',
  1661. ]);
  1662. $behaviorLocator = $table->behaviors()->get('Translate')->getTableLocator();
  1663. $this->assertSame($locator, $behaviorLocator);
  1664. $this->assertSame($table->associations()->getTableLocator(), $behaviorLocator);
  1665. $this->assertNotSame($this->getTableLocator(), $behaviorLocator);
  1666. }
  1667. /**
  1668. * Test that the behavior uses a custom locator.
  1669. *
  1670. * @return void
  1671. */
  1672. public function testCustomTableLocator()
  1673. {
  1674. $locator = new TableLocator();
  1675. $table = $this->getTableLocator()->get('Articles');
  1676. $table->addBehavior('Translate', [
  1677. 'fields' => ['title', 'body'],
  1678. 'validator' => 'custom',
  1679. 'tableLocator' => $locator,
  1680. ]);
  1681. $behaviorLocator = $table->behaviors()->get('Translate')->getTableLocator();
  1682. $this->assertSame($locator, $behaviorLocator);
  1683. $this->assertNotSame($table->associations()->getTableLocator(), $behaviorLocator);
  1684. $this->assertNotSame($this->getTableLocator(), $behaviorLocator);
  1685. }
  1686. /**
  1687. * Tests that using deep matching doesn't cause an association property to be created.
  1688. *
  1689. * @return void
  1690. */
  1691. public function testDeepMatchingDoesNotCreateAssociationProperty()
  1692. {
  1693. $table = $this->getTableLocator()->get('Articles');
  1694. $table->hasMany('Comments');
  1695. $table->Comments->belongsTo('Authors')->setForeignKey('user_id');
  1696. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  1697. $table->Comments->setLocale('abc');
  1698. $table->Comments->Authors->addBehavior('Translate', ['fields' => ['name']]);
  1699. $table->Comments->Authors->setLocale('xyz');
  1700. $this->assertNotEquals($table->Comments->getLocale(), I18n::getLocale());
  1701. $this->assertNotEquals($table->Comments->Authors->getLocale(), I18n::getLocale());
  1702. $result = $table
  1703. ->find()
  1704. ->contain('Comments')
  1705. ->matching('Comments.Authors')
  1706. ->first();
  1707. $this->assertArrayNotHasKey('author', $result->comments);
  1708. }
  1709. /**
  1710. * Tests that the _locale property is set on the entity in the _matchingData property.
  1711. *
  1712. * @return void
  1713. */
  1714. public function testLocalePropertyIsSetInMatchingData()
  1715. {
  1716. $table = $this->getTableLocator()->get('Articles');
  1717. $table->hasMany('Comments');
  1718. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  1719. $table->Comments->setLocale('abc');
  1720. $this->assertNotEquals($table->Comments->getLocale(), I18n::getLocale());
  1721. $result = $table
  1722. ->find()
  1723. ->contain('Comments')
  1724. ->matching('Comments')
  1725. ->first();
  1726. $this->assertArrayNotHasKey('_locale', $result->comments);
  1727. $this->assertEquals('abc', $result->_matchingData['Comments']->_locale);
  1728. }
  1729. /**
  1730. * Tests that the _locale property is set on the entity in the _matchingData property
  1731. * when using deep matching.
  1732. *
  1733. * @return void
  1734. */
  1735. public function testLocalePropertyIsSetInMatchingDataWhenUsingDeepMatching()
  1736. {
  1737. $table = $this->getTableLocator()->get('Articles');
  1738. $table->hasMany('Comments');
  1739. $table->Comments->belongsTo('Authors')->setForeignKey('user_id');
  1740. $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
  1741. $table->Comments->setLocale('abc');
  1742. $table->Comments->Authors->addBehavior('Translate', ['fields' => ['name']]);
  1743. $table->Comments->Authors->setLocale('xyz');
  1744. $this->assertNotEquals($table->Comments->getLocale(), I18n::getLocale());
  1745. $this->assertNotEquals($table->Comments->Authors->getLocale(), I18n::getLocale());
  1746. $result = $table
  1747. ->find()
  1748. ->contain('Comments.Authors')
  1749. ->matching('Comments.Authors')
  1750. ->first();
  1751. $this->assertArrayNotHasKey('_locale', $result->comments);
  1752. $this->assertEquals('abc', $result->_matchingData['Comments']->_locale);
  1753. $this->assertEquals('xyz', $result->_matchingData['Authors']->_locale);
  1754. }
  1755. /**
  1756. * Tests that the _locale property is set on the entity in the _matchingData property
  1757. * when using contained matching.
  1758. *
  1759. * @return void
  1760. */
  1761. public function testLocalePropertyIsSetInMatchingDataWhenUsingContainedMatching()
  1762. {
  1763. $table = $this->getTableLocator()->get('Authors');
  1764. $table->hasMany('Articles');
  1765. $table->Articles->belongsToMany('Tags');
  1766. $table->Articles->addBehavior('Translate', ['fields' => ['title', 'body']]);
  1767. $table->Articles->setLocale('abc');
  1768. $table->Articles->Tags->addBehavior('Translate', ['fields' => ['name']]);
  1769. $table->Articles->Tags->setLocale('xyz');
  1770. $this->assertNotEquals($table->Articles->getLocale(), I18n::getLocale());
  1771. $this->assertNotEquals($table->Articles->Tags->getLocale(), I18n::getLocale());
  1772. $result = $table
  1773. ->find()
  1774. ->contain([
  1775. 'Articles' => function ($query) {
  1776. return $query->matching('Tags');
  1777. },
  1778. 'Articles.Tags',
  1779. ])
  1780. ->first();
  1781. $this->assertArrayNotHasKey('_locale', $result->articles);
  1782. $this->assertArrayNotHasKey('_locale', $result->articles[0]->tags);
  1783. $this->assertEquals('abc', $result->articles[0]->_locale);
  1784. $this->assertEquals('xyz', $result->articles[0]->_matchingData['Tags']->_locale);
  1785. }
  1786. }