TranslateBehaviorTest.php 68 KB

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