TranslateBehaviorTest.php 61 KB

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