| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\ORM\Behavior;
- use Cake\Collection\Collection;
- use Cake\I18n\I18n;
- use Cake\ORM\Behavior\Translate\TranslateTrait;
- use Cake\ORM\Entity;
- use Cake\ORM\TableRegistry;
- use Cake\TestSuite\TestCase;
- use Cake\Validation\Validator;
- /**
- * Stub entity class
- */
- class Article extends Entity
- {
- use TranslateTrait;
- }
- /**
- * Translate behavior test case
- */
- class TranslateBehaviorTest extends TestCase
- {
- /**
- * fixtures
- *
- * @var array
- */
- public $fixtures = [
- 'core.articles',
- 'core.authors',
- 'core.groups',
- 'core.special_tags',
- 'core.tags',
- 'core.comments',
- 'core.translates'
- ];
- public function tearDown()
- {
- parent::tearDown();
- I18n::setLocale(I18n::getDefaultLocale());
- TableRegistry::clear();
- }
- /**
- * Returns an array with all the translations found for a set of records
- *
- * @param array|\Traversable $data
- * @return Collection
- */
- protected function _extractTranslations($data)
- {
- return (new Collection($data))->map(function ($row) {
- $translations = $row->get('_translations');
- if (!$translations) {
- return [];
- }
- return array_map(function ($t) {
- return $t->toArray();
- }, $translations);
- });
- }
- /**
- * Tests that custom translation tables are respected
- *
- * @return void
- */
- public function testCustomTranslationTable()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'translationTable' => '\TestApp\Model\Table\I18nTable',
- 'fields' => ['title', 'body']
- ]);
- $items = $table->associations();
- $i18n = $items->getByProperty('_i18n');
- $this->assertEquals('\TestApp\Model\Table\I18nTable', $i18n->name());
- $this->assertInstanceOf('TestApp\Model\Table\I18nTable', $i18n->target());
- $this->assertEquals('test_custom_i18n_datasource', $i18n->target()->connection()->configName());
- $this->assertEquals('custom_i18n_table', $i18n->target()->table());
- }
- /**
- * Tests that the strategy can be changed for i18n
- *
- * @return void
- */
- public function testStrategy()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'strategy' => 'select',
- 'fields' => ['title', 'body']
- ]);
- $items = $table->associations();
- $i18n = $items->getByProperty('_i18n');
- $this->assertEquals('select', $i18n->strategy());
- }
- /**
- * Tests that fields from a translated model are overridden
- *
- * @return void
- */
- public function testFindSingleLocale()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('eng');
- $results = $table->find()->combine('title', 'body', 'id')->toArray();
- $expected = [
- 1 => ['Title #1' => 'Content #1'],
- 2 => ['Title #2' => 'Content #2'],
- 3 => ['Title #3' => 'Content #3'],
- ];
- $this->assertSame($expected, $results);
- }
- /**
- * Test that iterating in a formatResults() does not drop data.
- *
- * @return void
- */
- public function testFindTranslationsFormatResultsIteration()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('eng');
- $results = $table->find('translations')
- ->limit(1)
- ->formatResults(function ($results) {
- foreach ($results as $res) {
- $res->first = 'val';
- }
- foreach ($results as $res) {
- $res->second = 'loop';
- }
- return $results;
- })
- ->toArray();
- $this->assertCount(1, $results);
- $this->assertSame('Title #1', $results[0]->title);
- $this->assertSame('val', $results[0]->first);
- $this->assertSame('loop', $results[0]->second);
- $this->assertNotEmpty($results[0]->_translations);
- }
- /**
- * Tests that fields from a translated model use the I18n class locale
- * and that it propogates to associated models
- *
- * @return void
- */
- public function testFindSingleLocaleAssociatedEnv()
- {
- I18n::setLocale('eng');
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->hasMany('Comments');
- $table->Comments->addBehavior('Translate', ['fields' => ['comment']]);
- $results = $table->find()
- ->select(['id', 'title', 'body'])
- ->contain(['Comments' => ['fields' => ['article_id', 'comment']]])
- ->hydrate(false)
- ->toArray();
- $expected = [
- [
- 'id' => 1,
- 'title' => 'Title #1',
- 'body' => 'Content #1',
- 'comments' => [
- ['article_id' => 1, 'comment' => 'Comment #1', '_locale' => 'eng'],
- ['article_id' => 1, 'comment' => 'Comment #2', '_locale' => 'eng'],
- ['article_id' => 1, 'comment' => 'Comment #3', '_locale' => 'eng'],
- ['article_id' => 1, 'comment' => 'Comment #4', '_locale' => 'eng']
- ],
- '_locale' => 'eng'
- ],
- [
- 'id' => 2,
- 'title' => 'Title #2',
- 'body' => 'Content #2',
- 'comments' => [
- ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'eng'],
- ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'eng']
- ],
- '_locale' => 'eng'
- ],
- [
- 'id' => 3,
- 'title' => 'Title #3',
- 'body' => 'Content #3',
- 'comments' => [],
- '_locale' => 'eng'
- ]
- ];
- $this->assertSame($expected, $results);
- I18n::setLocale('spa');
- $results = $table->find()
- ->select(['id', 'title', 'body'])
- ->contain([
- 'Comments' => [
- 'fields' => ['article_id', 'comment'],
- 'sort' => ['Comments.id' => 'ASC']
- ]
- ])
- ->hydrate(false)
- ->toArray();
- $expected = [
- [
- 'id' => 1,
- 'title' => 'First Article',
- 'body' => 'Contenido #1',
- 'comments' => [
- ['article_id' => 1, 'comment' => 'First Comment for First Article', '_locale' => 'spa'],
- ['article_id' => 1, 'comment' => 'Second Comment for First Article', '_locale' => 'spa'],
- ['article_id' => 1, 'comment' => 'Third Comment for First Article', '_locale' => 'spa'],
- ['article_id' => 1, 'comment' => 'Comentario #4', '_locale' => 'spa']
- ],
- '_locale' => 'spa'
- ],
- [
- 'id' => 2,
- 'title' => 'Second Article',
- 'body' => 'Second Article Body',
- 'comments' => [
- ['article_id' => 2, 'comment' => 'First Comment for Second Article', '_locale' => 'spa'],
- ['article_id' => 2, 'comment' => 'Second Comment for Second Article', '_locale' => 'spa']
- ],
- '_locale' => 'spa'
- ],
- [
- 'id' => 3,
- 'title' => 'Third Article',
- 'body' => 'Third Article Body',
- 'comments' => [],
- '_locale' => 'spa'
- ]
- ];
- $this->assertSame($expected, $results);
- }
- /**
- * Tests that fields from a translated model are not overridden if translation
- * is null
- *
- * @return void
- */
- public function testFindSingleLocaleWithNullTranslation()
- {
- $table = TableRegistry::get('Comments');
- $table->addBehavior('Translate', ['fields' => ['comment']]);
- $table->locale('spa');
- $results = $table->find()
- ->where(['Comments.id' => 6])
- ->combine('id', 'comment')->toArray();
- $expected = [6 => 'Second Comment for Second Article'];
- $this->assertSame($expected, $results);
- }
- /**
- * Tests that overriding fields with the translate behavior works when
- * using conditions and that all other columns are preserved
- *
- * @return void
- */
- public function testFindSingleLocaleWithConditions()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('eng');
- $results = $table->find()
- ->where(['Articles.id' => 2])
- ->all();
- $this->assertCount(1, $results);
- $row = $results->first();
- $expected = [
- 'id' => 2,
- 'title' => 'Title #2',
- 'body' => 'Content #2',
- 'author_id' => 3,
- 'published' => 'Y',
- '_locale' => 'eng'
- ];
- $this->assertEquals($expected, $row->toArray());
- }
- /**
- * Tests translationField method for translated fields.
- *
- * @return void
- */
- public function testTranslationFieldForTranslatedFields()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'fields' => ['title', 'body'],
- 'defaultLocale' => 'en_US'
- ]);
- $expectedSameLocale = 'Articles.title';
- $expectedOtherLocale = 'Articles_title_translation.content';
- $field = $table->translationField('title');
- $this->assertSame($expectedSameLocale, $field);
- I18n::setLocale('es_ES');
- $field = $table->translationField('title');
- $this->assertSame($expectedOtherLocale, $field);
- I18n::setLocale('en');
- $field = $table->translationField('title');
- $this->assertSame($expectedOtherLocale, $field);
- $table->removeBehavior('Translate');
- $table->addBehavior('Translate', [
- 'fields' => ['title', 'body'],
- 'defaultLocale' => 'de_DE'
- ]);
- I18n::setLocale('de_DE');
- $field = $table->translationField('title');
- $this->assertSame($expectedSameLocale, $field);
- I18n::setLocale('en_US');
- $field = $table->translationField('title');
- $this->assertSame($expectedOtherLocale, $field);
- $table->locale('de_DE');
- $field = $table->translationField('title');
- $this->assertSame($expectedSameLocale, $field);
- $table->locale('es');
- $field = $table->translationField('title');
- $this->assertSame($expectedOtherLocale, $field);
- }
- /**
- * Tests translationField method for other fields.
- *
- * @return void
- */
- public function testTranslationFieldForOtherFields()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $expected = 'Articles.foo';
- $field = $table->translationField('foo');
- $this->assertSame($expected, $field);
- }
- /**
- * Tests that translating fields work when other formatters are used
- *
- * @return void
- */
- public function testFindList()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('eng');
- $results = $table->find('list')->toArray();
- $expected = [1 => 'Title #1', 2 => 'Title #2', 3 => 'Title #3'];
- $this->assertSame($expected, $results);
- }
- /**
- * Tests that the query count return the correct results
- *
- * @return void
- */
- public function testFindCount()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('eng');
- $this->assertEquals(3, $table->find()->count());
- }
- /**
- * Tests that it is possible to get all translated fields at once
- *
- * @return void
- */
- public function testFindTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $results = $table->find('translations');
- $expected = [
- [
- 'eng' => ['title' => 'Title #1', 'body' => 'Content #1', 'description' => 'Description #1', 'locale' => 'eng'],
- 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze'],
- 'spa' => ['body' => 'Contenido #1', 'locale' => 'spa', 'description' => '']
- ],
- [
- 'eng' => ['title' => 'Title #2', 'body' => 'Content #2', 'locale' => 'eng'],
- 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
- ],
- [
- 'eng' => ['title' => 'Title #3', 'body' => 'Content #3', 'locale' => 'eng'],
- 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
- ]
- ];
- $translations = $this->_extractTranslations($results);
- $this->assertEquals($expected, $translations->toArray());
- $expected = [
- 1 => ['First Article' => 'First Article Body'],
- 2 => ['Second Article' => 'Second Article Body'],
- 3 => ['Third Article' => 'Third Article Body']
- ];
- $grouped = $results->combine('title', 'body', 'id');
- $this->assertEquals($expected, $grouped->toArray());
- }
- /**
- * Tests that it is possible to request just a few translations
- *
- * @return void
- */
- public function testFindFilteredTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
- $expected = [
- [
- 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
- ],
- [
- 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
- ],
- [
- 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
- ]
- ];
- $translations = $this->_extractTranslations($results);
- $this->assertEquals($expected, $translations->toArray());
- $expected = [
- 1 => ['First Article' => 'First Article Body'],
- 2 => ['Second Article' => 'Second Article Body'],
- 3 => ['Third Article' => 'Third Article Body']
- ];
- $grouped = $results->combine('title', 'body', 'id');
- $this->assertEquals($expected, $grouped->toArray());
- }
- /**
- * Tests that it is possible to combine find('list') and find('translations')
- *
- * @return void
- */
- public function testFindTranslationsList()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $results = $table
- ->find('list', [
- 'keyField' => 'title',
- 'valueField' => '_translations.deu.title',
- 'groupField' => 'id'
- ])
- ->find('translations', ['locales' => ['deu']]);
- $expected = [
- 1 => ['First Article' => 'Titel #1'],
- 2 => ['Second Article' => 'Titel #2'],
- 3 => ['Third Article' => 'Titel #3']
- ];
- $this->assertEquals($expected, $results->toArray());
- }
- /**
- * Tests that you can both override fields and find all translations
- *
- * @return void
- */
- public function testFindTranslationsWithFieldOverriding()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('cze');
- $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
- $expected = [
- [
- 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
- ],
- [
- 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
- ],
- [
- 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
- 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
- ]
- ];
- $translations = $this->_extractTranslations($results);
- $this->assertEquals($expected, $translations->toArray());
- $expected = [
- 1 => ['Titulek #1' => 'Obsah #1'],
- 2 => ['Titulek #2' => 'Obsah #2'],
- 3 => ['Titulek #3' => 'Obsah #3']
- ];
- $grouped = $results->combine('title', 'body', 'id');
- $this->assertEquals($expected, $grouped->toArray());
- }
- /**
- * Tests that fields can be overridden in a hasMany association
- *
- * @return void
- */
- public function testFindSingleLocaleHasMany()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->hasMany('Comments');
- $comments = $table->hasMany('Comments')->target();
- $comments->addBehavior('Translate', ['fields' => ['comment']]);
- $table->locale('eng');
- $comments->locale('eng');
- $results = $table->find()->contain(['Comments' => function ($q) {
- return $q->select(['id', 'comment', 'article_id']);
- }]);
- $list = new Collection($results->first()->comments);
- $expected = [
- 1 => 'Comment #1',
- 2 => 'Comment #2',
- 3 => 'Comment #3',
- 4 => 'Comment #4'
- ];
- $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
- }
- /**
- * Test that it is possible to bring translations from hasMany relations
- *
- * @return void
- */
- public function testTranslationsHasMany()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->hasMany('Comments');
- $comments = $table->hasMany('Comments')->target();
- $comments->addBehavior('Translate', ['fields' => ['comment']]);
- $results = $table->find('translations')->contain([
- 'Comments' => function ($q) {
- return $q->find('translations')->select(['id', 'comment', 'article_id']);
- }
- ]);
- $comments = $results->first()->comments;
- $expected = [
- [
- 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng']
- ],
- [
- 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng']
- ],
- [
- 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng']
- ],
- [
- 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
- 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']
- ]
- ];
- $translations = $this->_extractTranslations($comments);
- $this->assertEquals($expected, $translations->toArray());
- }
- /**
- * Tests that it is possible to both override fields with a translation and
- * also find separately other translations
- *
- * @return void
- */
- public function testTranslationsHasManyWithOverride()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->hasMany('Comments');
- $comments = $table->hasMany('Comments')->target();
- $comments->addBehavior('Translate', ['fields' => ['comment']]);
- $table->locale('cze');
- $comments->locale('eng');
- $results = $table->find('translations')->contain([
- 'Comments' => function ($q) {
- return $q->find('translations')->select(['id', 'comment', 'article_id']);
- }
- ]);
- $comments = $results->first()->comments;
- $expected = [
- 1 => 'Comment #1',
- 2 => 'Comment #2',
- 3 => 'Comment #3',
- 4 => 'Comment #4'
- ];
- $list = new Collection($comments);
- $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
- $expected = [
- [
- 'eng' => ['comment' => 'Comment #1', 'locale' => 'eng']
- ],
- [
- 'eng' => ['comment' => 'Comment #2', 'locale' => 'eng']
- ],
- [
- 'eng' => ['comment' => 'Comment #3', 'locale' => 'eng']
- ],
- [
- 'eng' => ['comment' => 'Comment #4', 'locale' => 'eng'],
- 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']
- ]
- ];
- $translations = $this->_extractTranslations($comments);
- $this->assertEquals($expected, $translations->toArray());
- $this->assertEquals('Titulek #1', $results->first()->title);
- $this->assertEquals('Obsah #1', $results->first()->body);
- }
- /**
- * Tests that it is possible to translate belongsTo associations
- *
- * @return void
- */
- public function testFindSingleLocaleBelongsto()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $authors = $table->belongsTo('Authors')->target();
- $authors->addBehavior('Translate', ['fields' => ['name']]);
- $table->locale('eng');
- $authors->locale('eng');
- $results = $table->find()
- ->select(['title', 'body'])
- ->order(['title' => 'asc'])
- ->contain(['Authors' => function ($q) {
- return $q->select(['id', 'name']);
- }]);
- $expected = [
- [
- 'title' => 'Title #1',
- 'body' => 'Content #1',
- 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
- '_locale' => 'eng'
- ],
- [
- 'title' => 'Title #2',
- 'body' => 'Content #2',
- 'author' => ['id' => 3, 'name' => 'larry', '_locale' => 'eng'],
- '_locale' => 'eng'
- ],
- [
- 'title' => 'Title #3',
- 'body' => 'Content #3',
- 'author' => ['id' => 1, 'name' => 'May-rianoh', '_locale' => 'eng'],
- '_locale' => 'eng'
- ]
- ];
- $results = array_map(function ($r) {
- return $r->toArray();
- }, $results->toArray());
- $this->assertEquals($expected, $results);
- }
- /**
- * Tests that it is possible to translate belongsToMany associations
- *
- * @return void
- */
- public function testFindSingleLocaleBelongsToMany()
- {
- $table = TableRegistry::get('Articles');
- $specialTags = TableRegistry::get('SpecialTags');
- $specialTags->addBehavior('Translate', ['fields' => ['extra_info']]);
- $table->belongsToMany('Tags', [
- 'through' => $specialTags
- ]);
- $specialTags->locale('eng');
- $result = $table->get(2, ['contain' => 'Tags']);
- $this->assertNotEmpty($result);
- $this->assertNotEmpty($result->tags);
- $this->assertEquals('Translated Info', $result->tags[0]->special_tags[0]->extra_info);
- }
- /**
- * Tests that updating an existing record translations work
- *
- * @return void
- */
- public function testUpdateSingleLocale()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('eng');
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $article->set('title', 'New translated article');
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $this->assertEquals('New translated article', $article->get('title'));
- $this->assertEquals('Content #1', $article->get('body'));
- $table->locale(false);
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $this->assertEquals('First Article', $article->get('title'));
- $table->locale('eng');
- $article->set('title', 'Wow, such translated article');
- $article->set('body', 'A translated body');
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $this->assertEquals('Wow, such translated article', $article->get('title'));
- $this->assertEquals('A translated body', $article->get('body'));
- }
- /**
- * Tests adding new translation to a record
- *
- * @return void
- */
- public function testInsertNewTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('fra');
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $article->set('title', 'Le titre');
- $table->save($article);
- $this->assertEquals('fra', $article->get('_locale'));
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $this->assertEquals('Le titre', $article->get('title'));
- $this->assertEquals('First Article Body', $article->get('body'));
- $article->set('title', 'Un autre titre');
- $article->set('body', 'Le contenu');
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $table->find()->first();
- $this->assertEquals('Un autre titre', $article->get('title'));
- $this->assertEquals('Le contenu', $article->get('body'));
- }
- /**
- * Tests adding new translation to a record
- *
- * @return void
- */
- public function testAllowEmptyFalse()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title'], 'allowEmptyTranslations' => false]);
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $article = $table->patchEntity($article, [
- '_translations' => [
- 'fra' => [
- 'title' => ''
- ]
- ]
- ]);
- $table->save($article);
- // Remove the Behavior to unset the content != '' condition
- $table->removeBehavior('Translate');
- $noFra = $table->I18n->find()->where(['locale' => 'fra'])->first();
- $this->assertEmpty($noFra);
- }
- /**
- * Tests adding new translation to a record
- *
- * @return void
- */
- public function testMixedAllowEmptyFalse()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body'], 'allowEmptyTranslations' => false]);
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $article = $table->patchEntity($article, [
- '_translations' => [
- 'fra' => [
- 'title' => '',
- 'body' => 'Bonjour'
- ]
- ]
- ]);
- $table->save($article);
- $fra = $table->I18n->find()
- ->where([
- 'locale' => 'fra',
- 'field' => 'body'
- ])
- ->first();
- $this->assertSame('Bonjour', $fra->content);
- // Remove the Behavior to unset the content != '' condition
- $table->removeBehavior('Translate');
- $noTitle = $table->I18n->find()
- ->where([
- 'locale' => 'fra',
- 'field' => 'title'
- ])
- ->first();
- $this->assertEmpty($noTitle);
- }
- /**
- * Tests adding new translation to a record
- *
- * @return void
- */
- public function testMultipleAllowEmptyFalse()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body'], 'allowEmptyTranslations' => false]);
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $article = $table->patchEntity($article, [
- '_translations' => [
- 'fra' => [
- 'title' => '',
- 'body' => 'Bonjour'
- ],
- 'de' => [
- 'title' => 'Titel',
- 'body' => 'Hallo'
- ]
- ]
- ]);
- $table->save($article);
- $fra = $table->I18n->find()
- ->where([
- 'locale' => 'fra',
- 'field' => 'body'
- ])
- ->first();
- $this->assertSame('Bonjour', $fra->content);
- $deTitle = $table->I18n->find()
- ->where([
- 'locale' => 'de',
- 'field' => 'title'
- ])
- ->first();
- $this->assertSame('Titel', $deTitle->content);
- $deBody = $table->I18n->find()
- ->where([
- 'locale' => 'de',
- 'field' => 'body'
- ])
- ->first();
- $this->assertSame('Hallo', $deBody->content);
- // Remove the Behavior to unset the content != '' condition
- $table->removeBehavior('Translate');
- $noTitle = $table->I18n->find()
- ->where([
- 'locale' => 'fra',
- 'field' => 'title'
- ])
- ->first();
- $this->assertEmpty($noTitle);
- }
- /**
- * Tests that it is possible to use the _locale property to specify the language
- * to use for saving an entity
- *
- * @return void
- */
- public function testUpdateTranslationWithLocaleInEntity()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $article->set('_locale', 'fra');
- $article->set('title', 'Le titre');
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $this->assertEquals('First Article', $article->get('title'));
- $this->assertEquals('First Article Body', $article->get('body'));
- $table->locale('fra');
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $this->assertEquals('Le titre', $article->get('title'));
- $this->assertEquals('First Article Body', $article->get('body'));
- }
- /**
- * Tests that translations are added to the whitelist of associations to be
- * saved
- *
- * @return void
- */
- public function testSaveTranslationWithAssociationWhitelist()
- {
- $table = TableRegistry::get('Articles');
- $table->hasMany('Comments');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->locale('fra');
- $article = $table->find()->first();
- $this->assertEquals(1, $article->get('id'));
- $article->set('title', 'Le titre');
- $table->save($article, ['associated' => ['Comments']]);
- $this->assertNull($article->get('_i18n'));
- $article = $table->find()->first();
- $this->assertEquals('Le titre', $article->get('title'));
- }
- /**
- * Tests that after deleting a translated entity, all translations are also removed
- *
- * @return void
- */
- public function testDelete()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $article = $table->find()->first();
- $this->assertTrue($table->delete($article));
- $translations = TableRegistry::get('I18n')->find()
- ->where(['model' => 'Articles', 'foreign_key' => $article->id])
- ->count();
- $this->assertEquals(0, $translations);
- }
- /**
- * Tests saving multiple translations at once when the translations already
- * exist in the database
- *
- * @return void
- */
- public function testSaveMultipleTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $article = $results = $table->find('translations')->first();
- $translations = $article->get('_translations');
- $translations['deu']->set('title', 'Another title');
- $translations['eng']->set('body', 'Another body');
- $article->set('_translations', $translations);
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $results = $table->find('translations')->first();
- $translations = $article->get('_translations');
- $this->assertEquals('Another title', $translations['deu']->get('title'));
- $this->assertEquals('Another body', $translations['eng']->get('body'));
- }
- /**
- * Tests saving multiple existing translations and adding new ones
- *
- * @return void
- */
- public function testSaveMultipleNewTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $article = $results = $table->find('translations')->first();
- $translations = $article->get('_translations');
- $translations['deu']->set('title', 'Another title');
- $translations['eng']->set('body', 'Another body');
- $translations['spa'] = new Entity(['title' => 'Titulo']);
- $translations['fre'] = new Entity(['title' => 'Titre']);
- $article->set('_translations', $translations);
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $results = $table->find('translations')->first();
- $translations = $article->get('_translations');
- $this->assertEquals('Another title', $translations['deu']->get('title'));
- $this->assertEquals('Another body', $translations['eng']->get('body'));
- $this->assertEquals('Titulo', $translations['spa']->get('title'));
- $this->assertEquals('Titre', $translations['fre']->get('title'));
- }
- /**
- * Tests that iterating a resultset twice when using the translations finder
- * will not cause any errors nor information loss
- *
- * @return void
- */
- public function testUseCountInFindTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $articles = $results = $table->find('translations');
- $all = $articles->all();
- $this->assertCount(3, $all);
- $article = $all->first();
- $this->assertNotEmpty($article->get('_translations'));
- }
- /**
- * Tests that multiple translations saved when having a default locale
- * are correctly saved
- *
- * @return void
- */
- public function testSavingWithNonDefaultLocale()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->entityClass(__NAMESPACE__ . '\Article');
- I18n::setLocale('fra');
- $translations = [
- 'fra' => ['title' => 'Un article'],
- 'spa' => ['title' => 'Un artículo']
- ];
- $article = $table->get(1);
- foreach ($translations as $lang => $data) {
- $article->translation($lang)->set($data, ['guard' => false]);
- }
- $table->save($article);
- $article = $table->find('translations')->where(['Articles.id' => 1])->first();
- $this->assertEquals('Un article', $article->translation('fra')->title);
- $this->assertEquals('Un artículo', $article->translation('spa')->title);
- }
- /**
- * Tests that translation queries are added to union queries as well.
- *
- * @return void
- */
- public function testTranslationWithUnionQuery()
- {
- $table = TableRegistry::get('Comments');
- $table->addBehavior('Translate', ['fields' => ['comment']]);
- $table->locale('spa');
- $query = $table->find()->where(['Comments.id' => 6]);
- $query2 = $table->find()->where(['Comments.id' => 5]);
- $query->union($query2);
- $results = $query->sortBy('id', SORT_ASC)->toList();
- $this->assertCount(2, $results);
- $this->assertEquals('First Comment for Second Article', $results[0]->comment);
- $this->assertEquals('Second Comment for Second Article', $results[1]->comment);
- }
- /**
- * Tests the use of `referenceName` config option.
- *
- * @return void
- */
- public function testAutoReferenceName()
- {
- $table = TableRegistry::get('Articles');
- $table->hasMany('OtherComments', ['className' => 'Comments']);
- $table->OtherComments->addBehavior(
- 'Translate',
- ['fields' => ['comment']]
- );
- $items = $table->OtherComments->associations();
- $association = $items->getByProperty('comment_translation');
- $this->assertNotEmpty($association, 'Translation association not found');
- $found = false;
- foreach ($association->conditions() as $key => $value) {
- if (strpos($key, 'comment_translation.model') !== false) {
- $found = true;
- $this->assertEquals('Comments', $value);
- break;
- }
- }
- $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
- }
- /**
- * Tests the use of unconventional `referenceName` config option.
- *
- * @return void
- */
- public function testChangingReferenceName()
- {
- $table = TableRegistry::get('Articles');
- $table->alias('FavoritePost');
- $table->addBehavior(
- 'Translate',
- ['fields' => ['body'], 'referenceName' => 'Posts']
- );
- $items = $table->associations();
- $association = $items->getByProperty('body_translation');
- $this->assertNotEmpty($association, 'Translation association not found');
- $found = false;
- foreach ($association->conditions() as $key => $value) {
- if (strpos($key, 'body_translation.model') !== false) {
- $found = true;
- $this->assertEquals('Posts', $value);
- break;
- }
- }
- $this->assertTrue($found, '`referenceName` field condition on a Translation association was not found');
- }
- /**
- * Tests that onlyTranslated will remove records from the result set
- * if they are not fully translated
- *
- * @return void
- */
- public function testFilterUntranslated()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'fields' => ['title', 'body'],
- 'onlyTranslated' => true
- ]);
- $table->locale('eng');
- $results = $table->find()->where(['Articles.id' => 1])->all();
- $this->assertCount(1, $results);
- $table->locale('fr');
- $results = $table->find()->where(['Articles.id' => 1])->all();
- $this->assertCount(0, $results);
- }
- /**
- * Tests that records not translated in the current locale will not be
- * present in the results for the translations finder, and also proves
- * that this can be overridden.
- *
- * @return void
- */
- public function testFilterUntranslatedWithFinder()
- {
- $table = TableRegistry::get('Comments');
- $table->addBehavior('Translate', [
- 'fields' => ['comment'],
- 'onlyTranslated' => true
- ]);
- $table->locale('eng');
- $results = $table->find('translations')->all();
- $this->assertCount(4, $results);
- $table->locale('spa');
- $results = $table->find('translations')->all();
- $this->assertCount(1, $results);
- $table->locale('spa');
- $results = $table->find('translations', ['filterByCurrentLocale' => false])->all();
- $this->assertCount(6, $results);
- $table->locale('spa');
- $results = $table->find('translations')->all();
- $this->assertCount(1, $results);
- }
- /**
- * Tests that allowEmptyTranslations takes effect
- *
- * @return void
- */
- public function testEmptyTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'fields' => ['title', 'body', 'description'],
- 'allowEmptyTranslations' => false,
- ]);
- $table->locale('spa');
- $result = $table->find()->first();
- $this->assertNull($result->description);
- }
- /**
- * Test save with clean translate fields
- *
- * @return void
- */
- public function testSaveWithCleanFields()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title']]);
- $table->entityClass(__NAMESPACE__ . '\Article');
- I18n::setLocale('fra');
- $article = $table->get(1);
- $article->set('body', 'New Body');
- $table->save($article);
- $result = $table->get(1);
- $this->assertEquals('New Body', $result->body);
- $this->assertSame($article->title, $result->title);
- }
- /**
- * Test save new entity with _translations field
- *
- * @return void
- */
- public function testSaveNewRecordWithTranslatesField()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'fields' => ['title'],
- 'validator' => (new \Cake\Validation\Validator)->add('title', 'notBlank', ['rule' => 'notBlank'])
- ]);
- $table->entityClass(__NAMESPACE__ . '\Article');
- $data = [
- 'author_id' => 1,
- 'published' => 'N',
- '_translations' => [
- 'en' => [
- 'title' => 'Title EN',
- 'body' => 'Body EN'
- ],
- 'es' => [
- 'title' => 'Title ES'
- ]
- ]
- ];
- $article = $table->patchEntity($table->newEntity(), $data);
- $result = $table->save($article);
- $this->assertNotFalse($result);
- $expected = [
- [
- 'en' => [
- 'title' => 'Title EN',
- 'locale' => 'en'
- ],
- 'es' => [
- 'title' => 'Title ES',
- 'locale' => 'es'
- ]
- ]
- ];
- $result = $table->find('translations')->where(['id' => $result->id]);
- $this->assertEquals($expected, $this->_extractTranslations($result)->toArray());
- }
- /**
- * Tests adding new translation to a record where the only field is the translated one and it's not the default locale
- *
- * @return void
- */
- public function testSaveNewRecordWithOnlyTranslationsNotDefaultLocale()
- {
- $table = TableRegistry::get('Groups');
- $table->addBehavior('Translate', [
- 'fields' => ['title'],
- 'validator' => (new \Cake\Validation\Validator)->add('title', 'notBlank', ['rule' => 'notBlank'])
- ]);
- $data = [
- '_translations' => [
- 'es' => [
- 'title' => 'Title ES'
- ]
- ]
- ];
- $group = $table->newEntity($data);
- $result = $table->save($group);
- $this->assertNotFalse($result, 'Record should save.');
- $expected = [
- [
- 'es' => [
- 'title' => 'Title ES',
- 'locale' => 'es'
- ]
- ]
- ];
- $result = $table->find('translations')->where(['id' => $result->id]);
- $this->assertEquals($expected, $this->_extractTranslations($result)->toArray());
- }
- /**
- * Test that existing records can be updated when only translations
- * are modified/dirty.
- *
- * @return void
- */
- public function testSaveExistingRecordOnlyTranslations()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->entityClass(__NAMESPACE__ . '\Article');
- $data = [
- '_translations' => [
- 'es' => [
- 'title' => 'Spanish Translation',
- ],
- ]
- ];
- $article = $table->find()->first();
- $article = $table->patchEntity($article, $data);
- $this->assertNotFalse($table->save($article));
- $results = $this->_extractTranslations(
- $table->find('translations')->where(['id' => 1])
- )->first();
- $this->assertArrayHasKey('es', $results, 'New translation added');
- $this->assertArrayHasKey('eng', $results, 'Old translations present');
- $this->assertEquals('Spanish Translation', $results['es']['title']);
- }
- /**
- * Test update entity with _translations field.
- *
- * @return void
- */
- public function testSaveExistingRecordWithTranslatesField()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $table->entityClass(__NAMESPACE__ . '\Article');
- $data = [
- 'author_id' => 1,
- 'published' => 'Y',
- '_translations' => [
- 'eng' => [
- 'title' => 'First Article1',
- 'body' => 'First Article content has been updated'
- ],
- 'spa' => [
- 'title' => 'Mi nuevo titulo',
- 'body' => 'Contenido Actualizado'
- ]
- ]
- ];
- $article = $table->find()->first();
- $article = $table->patchEntity($article, $data);
- $this->assertNotFalse($table->save($article));
- $results = $this->_extractTranslations(
- $table->find('translations')->where(['id' => 1])
- )->first();
- $this->assertEquals('Mi nuevo titulo', $results['spa']['title']);
- $this->assertEquals('Contenido Actualizado', $results['spa']['body']);
- $this->assertEquals('First Article1', $results['eng']['title']);
- $this->assertEquals('Description #1', $results['eng']['description']);
- }
- /**
- * Tests that default locale saves ok.
- *
- * @return void
- */
- public function testSaveDefaultLocale()
- {
- $table = TableRegistry::get('Articles');
- $table->hasMany('Comments');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $article = $table->get(1);
- $data = [
- 'title' => 'New title',
- 'body' => 'New body',
- ];
- $article = $table->patchEntity($article, $data);
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $table->get(1);
- $this->assertEquals('New title', $article->get('title'));
- $this->assertEquals('New body', $article->get('body'));
- }
- /**
- * Tests that translations are added to the whitelist of associations to be
- * saved
- *
- * @return void
- */
- public function testSaveTranslationDefaultLocale()
- {
- $table = TableRegistry::get('Articles');
- $table->hasMany('Comments');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $article = $table->get(1);
- $data = [
- 'title' => 'New title',
- 'body' => 'New body',
- '_translations' => [
- 'es' => [
- 'title' => 'ES title',
- 'body' => 'ES body'
- ]
- ]
- ];
- $article = $table->patchEntity($article, $data);
- $table->save($article);
- $this->assertNull($article->get('_i18n'));
- $article = $table->find('translations')->where(['id' => 1])->first();
- $this->assertEquals('New title', $article->get('title'));
- $this->assertEquals('New body', $article->get('body'));
- $this->assertEquals('ES title', $article->_translations['es']->title);
- $this->assertEquals('ES body', $article->_translations['es']->body);
- }
- /**
- * Test that no properties are enabled when the translations
- * option is off.
- *
- * @return void
- */
- public function testBuildMarshalMapTranslationsOff()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $marshaller = $table->marshaller();
- $translate = $table->behaviors()->get('Translate');
- $result = $translate->buildMarshalMap($marshaller, [], ['translations' => false]);
- $this->assertSame([], $result);
- }
- /**
- * Test building a marshal map with translations on.
- *
- * @return void
- */
- public function testBuildMarshalMapTranslationsOn()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $marshaller = $table->marshaller();
- $translate = $table->behaviors()->get('Translate');
- $result = $translate->buildMarshalMap($marshaller, [], ['translations' => true]);
- $this->assertArrayHasKey('_translations', $result);
- $this->assertInstanceOf('Closure', $result['_translations']);
- $result = $translate->buildMarshalMap($marshaller, [], []);
- $this->assertArrayHasKey('_translations', $result);
- $this->assertInstanceOf('Closure', $result['_translations']);
- }
- /**
- * Test marshalling non-array data
- *
- * @return void
- */
- public function testBuildMarshalMapNonArrayData()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $translate = $table->behaviors()->get('Translate');
- $map = $translate->buildMarshalMap($table->marshaller(), [], []);
- $entity = $table->newEntity();
- $result = $map['_translations']('garbage', $entity);
- $this->assertNull($result, 'Non-array should not error out.');
- $this->assertEmpty($entity->errors());
- $this->assertEmpty($entity->get('_translations'));
- }
- /**
- * Test buildMarshalMap() builds new entities.
- *
- * @return void
- */
- public function testBuildMarshalMapBuildEntities()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
- $translate = $table->behaviors()->get('Translate');
- $map = $translate->buildMarshalMap($table->marshaller(), [], []);
- $entity = $table->newEntity();
- $data = [
- 'en' => [
- 'title' => 'English Title',
- 'body' => 'English Content'
- ],
- 'es' => [
- 'title' => 'Titulo Español',
- 'body' => 'Contenido Español'
- ]
- ];
- $result = $map['_translations']($data, $entity);
- $this->assertEmpty($entity->errors(), 'No validation errors.');
- $this->assertCount(2, $result);
- $this->assertArrayHasKey('en', $result);
- $this->assertArrayHasKey('es', $result);
- $this->assertEquals('English Title', $result['en']->title);
- $this->assertEquals('Titulo Español', $result['es']->title);
- }
- /**
- * Test that validation errors are added to the original entity.
- *
- * @return void
- */
- public function testBuildMarshalMapBuildEntitiesValidationErrors()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'fields' => ['title', 'body'],
- 'validator' => 'custom'
- ]);
- $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
- $table->setValidator('custom', $validator);
- $translate = $table->behaviors()->get('Translate');
- $entity = $table->newEntity();
- $map = $translate->buildMarshalMap($table->marshaller(), [], []);
- $data = [
- 'en' => [
- 'title' => 'English Title',
- 'body' => 'English Content'
- ],
- 'es' => [
- 'title' => '',
- 'body' => 'Contenido Español'
- ]
- ];
- $result = $map['_translations']($data, $entity);
- $this->assertNotEmpty($entity->errors(), 'Needs validation errors.');
- $expected = [
- 'title' => [
- '_empty' => 'This field cannot be left empty'
- ]
- ];
- $this->assertEquals($expected, $entity->errors('es'));
- $this->assertEquals('English Title', $result['en']->title);
- $this->assertEquals('', $result['es']->title);
- }
- /**
- * Test that marshalling updates existing translation entities.
- *
- * @return void
- */
- public function testBuildMarshalMapUpdateExistingEntities()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'fields' => ['title', 'body'],
- ]);
- $translate = $table->behaviors()->get('Translate');
- $entity = $table->newEntity();
- $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
- $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
- $entity->set('_translations', [
- 'es' => $es,
- 'en' => $en,
- ]);
- $map = $translate->buildMarshalMap($table->marshaller(), [], []);
- $data = [
- 'en' => [
- 'title' => 'English Title',
- ],
- 'es' => [
- 'title' => 'Spanish Title',
- ]
- ];
- $result = $map['_translations']($data, $entity);
- $this->assertEmpty($entity->errors(), 'No validation errors.');
- $this->assertSame($en, $result['en']);
- $this->assertSame($es, $result['es']);
- $this->assertSame($en, $entity->get('_translations')['en']);
- $this->assertSame($es, $entity->get('_translations')['es']);
- $this->assertEquals('English Title', $result['en']->title);
- $this->assertEquals('Spanish Title', $result['es']->title);
- $this->assertEquals('Old body', $result['en']->body);
- $this->assertEquals('Old body', $result['es']->body);
- }
- /**
- * Test that updating translation records works with validations.
- *
- * @return void
- */
- public function testBuildMarshalMapUpdateEntitiesValidationErrors()
- {
- $table = TableRegistry::get('Articles');
- $table->addBehavior('Translate', [
- 'fields' => ['title', 'body'],
- 'validator' => 'custom'
- ]);
- $validator = (new Validator)->add('title', 'notBlank', ['rule' => 'notBlank']);
- $table->setValidator('custom', $validator);
- $translate = $table->behaviors()->get('Translate');
- $entity = $table->newEntity();
- $es = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
- $en = $table->newEntity(['title' => 'Old title', 'body' => 'Old body']);
- $entity->set('_translations', [
- 'es' => $es,
- 'en' => $en,
- ]);
- $map = $translate->buildMarshalMap($table->marshaller(), [], []);
- $data = [
- 'en' => [
- 'title' => 'English Title',
- 'body' => 'English Content'
- ],
- 'es' => [
- 'title' => '',
- 'body' => 'Contenido Español'
- ]
- ];
- $result = $map['_translations']($data, $entity);
- $this->assertNotEmpty($entity->errors(), 'Needs validation errors.');
- $expected = [
- 'title' => [
- '_empty' => 'This field cannot be left empty'
- ]
- ];
- $this->assertEquals($expected, $entity->errors('es'));
- }
- }
|