TranslateBehaviorTest.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since CakePHP(tm) v 1.2.0.5669
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Model\Behavior;
  16. use Cake\Model\Model;
  17. use Cake\TestSuite\TestCase;
  18. require_once dirname(__DIR__) . DS . 'models.php';
  19. /**
  20. * TranslateBehaviorTest class
  21. *
  22. */
  23. class TranslateBehaviorTest extends TestCase {
  24. /**
  25. * autoFixtures property
  26. *
  27. * @var boolean
  28. */
  29. public $autoFixtures = false;
  30. /**
  31. * fixtures property
  32. *
  33. * @var array
  34. */
  35. public $fixtures = array(
  36. 'core.translated_item', 'core.translate', 'core.translate_table',
  37. 'core.translated_article', 'core.translate_article', 'core.user', 'core.comment', 'core.tag', 'core.articles_tag',
  38. 'core.translate_with_prefix'
  39. );
  40. public function setUp() {
  41. parent::setUp();
  42. $this->markTestIncomplete('Not runnable until Models are fixed.');
  43. }
  44. /**
  45. * Test that count queries with conditions get the correct joins
  46. *
  47. * @return void
  48. */
  49. public function testCountWithConditions() {
  50. $this->loadFixtures('Translate', 'TranslatedItem');
  51. $Model = new TranslatedItem();
  52. $Model->locale = 'eng';
  53. $result = $Model->find('count', array(
  54. 'conditions' => array(
  55. 'I18n__content.locale' => 'eng'
  56. )
  57. ));
  58. $this->assertEquals(3, $result);
  59. }
  60. /**
  61. * testTranslateModel method
  62. *
  63. * @return void
  64. */
  65. public function testTranslateModel() {
  66. $this->loadFixtures('TranslateTable', 'Tag', 'TranslatedItem', 'Translate', 'User', 'TranslatedArticle', 'TranslateArticle');
  67. $TestModel = new Tag();
  68. $TestModel->translateTable = 'another_i18n';
  69. $TestModel->Behaviors->load('Translate', array('title'));
  70. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  71. $this->assertEquals('I18nModel', $translateModel->name);
  72. $this->assertEquals('another_i18n', $translateModel->useTable);
  73. $TestModel = new User();
  74. $TestModel->Behaviors->load('Translate', array('title'));
  75. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  76. $this->assertEquals('I18nModel', $translateModel->name);
  77. $this->assertEquals('i18n', $translateModel->useTable);
  78. $TestModel = new TranslatedArticle();
  79. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  80. $this->assertEquals('TranslateArticleModel', $translateModel->name);
  81. $this->assertEquals('article_i18n', $translateModel->useTable);
  82. $TestModel = new TranslatedItem();
  83. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  84. $this->assertEquals('TranslateTestModel', $translateModel->name);
  85. $this->assertEquals('i18n', $translateModel->useTable);
  86. }
  87. /**
  88. * testLocaleFalsePlain method
  89. *
  90. * @return void
  91. */
  92. public function testLocaleFalsePlain() {
  93. $this->loadFixtures('Translate', 'TranslatedItem', 'User');
  94. $TestModel = new TranslatedItem();
  95. $TestModel->locale = false;
  96. $result = $TestModel->read(null, 1);
  97. $expected = array('TranslatedItem' => array(
  98. 'id' => 1,
  99. 'slug' => 'first_translated',
  100. 'translated_article_id' => 1,
  101. ));
  102. $this->assertEquals($expected, $result);
  103. $result = $TestModel->find('all', array('fields' => array('slug')));
  104. $expected = array(
  105. array('TranslatedItem' => array('slug' => 'first_translated')),
  106. array('TranslatedItem' => array('slug' => 'second_translated')),
  107. array('TranslatedItem' => array('slug' => 'third_translated'))
  108. );
  109. $this->assertEquals($expected, $result);
  110. }
  111. /**
  112. * testLocaleFalseAssociations method
  113. *
  114. * @return void
  115. */
  116. public function testLocaleFalseAssociations() {
  117. $this->loadFixtures('Translate', 'TranslatedItem');
  118. $TestModel = new TranslatedItem();
  119. $TestModel->locale = false;
  120. $TestModel->unbindTranslation();
  121. $translations = array('title' => 'Title', 'content' => 'Content');
  122. $TestModel->bindTranslation($translations, false);
  123. $result = $TestModel->read(null, 1);
  124. $expected = array(
  125. 'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated', 'translated_article_id' => 1),
  126. 'Title' => array(
  127. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
  128. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
  129. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
  130. ),
  131. 'Content' => array(
  132. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
  133. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
  134. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
  135. )
  136. );
  137. $this->assertEquals($expected, $result);
  138. $TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
  139. $TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
  140. $result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug')));
  141. $expected = array(
  142. array(
  143. 'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
  144. 'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
  145. 'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
  146. ),
  147. array(
  148. 'TranslatedItem' => array('id' => 2, 'slug' => 'second_translated'),
  149. 'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
  150. 'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
  151. ),
  152. array(
  153. 'TranslatedItem' => array('id' => 3, 'slug' => 'third_translated'),
  154. 'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
  155. 'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
  156. )
  157. );
  158. $this->assertEquals($expected, $result);
  159. }
  160. /**
  161. * testLocaleSingle method
  162. *
  163. * @return void
  164. */
  165. public function testLocaleSingle() {
  166. $this->loadFixtures('Translate', 'TranslatedItem');
  167. $TestModel = new TranslatedItem();
  168. $TestModel->locale = 'eng';
  169. $result = $TestModel->read(null, 1);
  170. $expected = array(
  171. 'TranslatedItem' => array(
  172. 'id' => 1,
  173. 'slug' => 'first_translated',
  174. 'locale' => 'eng',
  175. 'title' => 'Title #1',
  176. 'content' => 'Content #1',
  177. 'translated_article_id' => 1,
  178. )
  179. );
  180. $this->assertEquals($expected, $result);
  181. $result = $TestModel->find('all');
  182. $expected = array(
  183. array(
  184. 'TranslatedItem' => array(
  185. 'id' => 1,
  186. 'slug' => 'first_translated',
  187. 'locale' => 'eng',
  188. 'title' => 'Title #1',
  189. 'content' => 'Content #1',
  190. 'translated_article_id' => 1,
  191. )
  192. ),
  193. array(
  194. 'TranslatedItem' => array(
  195. 'id' => 2,
  196. 'slug' => 'second_translated',
  197. 'locale' => 'eng',
  198. 'title' => 'Title #2',
  199. 'content' => 'Content #2',
  200. 'translated_article_id' => 1,
  201. )
  202. ),
  203. array(
  204. 'TranslatedItem' => array(
  205. 'id' => 3,
  206. 'slug' => 'third_translated',
  207. 'locale' => 'eng',
  208. 'title' => 'Title #3',
  209. 'content' => 'Content #3',
  210. 'translated_article_id' => 1,
  211. )
  212. )
  213. );
  214. $this->assertEquals($expected, $result);
  215. }
  216. /**
  217. * testLocaleSingleWithConditions method
  218. *
  219. * @return void
  220. */
  221. public function testLocaleSingleWithConditions() {
  222. $this->loadFixtures('Translate', 'TranslatedItem');
  223. $TestModel = new TranslatedItem();
  224. $TestModel->locale = 'eng';
  225. $result = $TestModel->find('all', array('conditions' => array('slug' => 'first_translated')));
  226. $expected = array(
  227. array(
  228. 'TranslatedItem' => array(
  229. 'id' => 1,
  230. 'slug' => 'first_translated',
  231. 'locale' => 'eng',
  232. 'title' => 'Title #1',
  233. 'content' => 'Content #1',
  234. 'translated_article_id' => 1,
  235. )
  236. )
  237. );
  238. $this->assertEquals($expected, $result);
  239. $result = $TestModel->find('all', array('conditions' => "TranslatedItem.slug = 'first_translated'"));
  240. $expected = array(
  241. array(
  242. 'TranslatedItem' => array(
  243. 'id' => 1,
  244. 'slug' => 'first_translated',
  245. 'locale' => 'eng',
  246. 'title' => 'Title #1',
  247. 'content' => 'Content #1',
  248. 'translated_article_id' => 1,
  249. )
  250. )
  251. );
  252. $this->assertEquals($expected, $result);
  253. }
  254. /**
  255. * testLocaleSingleAssociations method
  256. *
  257. * @return void
  258. */
  259. public function testLocaleSingleAssociations() {
  260. $this->loadFixtures('Translate', 'TranslatedItem');
  261. $TestModel = new TranslatedItem();
  262. $TestModel->locale = 'eng';
  263. $TestModel->unbindTranslation();
  264. $translations = array('title' => 'Title', 'content' => 'Content');
  265. $TestModel->bindTranslation($translations, false);
  266. $result = $TestModel->read(null, 1);
  267. $expected = array(
  268. 'TranslatedItem' => array(
  269. 'id' => 1,
  270. 'slug' => 'first_translated',
  271. 'locale' => 'eng',
  272. 'title' => 'Title #1',
  273. 'content' => 'Content #1',
  274. 'translated_article_id' => 1,
  275. ),
  276. 'Title' => array(
  277. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
  278. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
  279. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
  280. ),
  281. 'Content' => array(
  282. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
  283. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
  284. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
  285. )
  286. );
  287. $this->assertEquals($expected, $result);
  288. $TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
  289. $TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
  290. $result = $TestModel->find('all', array('fields' => array('TranslatedItem.title')));
  291. $expected = array(
  292. array(
  293. 'TranslatedItem' => array(
  294. 'id' => 1,
  295. 'locale' => 'eng',
  296. 'title' => 'Title #1',
  297. 'slug' => 'first_translated',
  298. 'translated_article_id' => 1,
  299. ),
  300. 'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
  301. 'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
  302. ),
  303. array(
  304. 'TranslatedItem' => array(
  305. 'id' => 2,
  306. 'locale' => 'eng',
  307. 'title' => 'Title #2',
  308. 'slug' => 'second_translated',
  309. 'translated_article_id' => 1,
  310. ),
  311. 'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
  312. 'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
  313. ),
  314. array(
  315. 'TranslatedItem' => array(
  316. 'id' => 3,
  317. 'locale' => 'eng',
  318. 'title' => 'Title #3',
  319. 'slug' => 'third_translated',
  320. 'translated_article_id' => 1,
  321. ),
  322. 'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
  323. 'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
  324. )
  325. );
  326. $this->assertEquals($expected, $result);
  327. }
  328. /**
  329. * testLocaleMultiple method
  330. *
  331. * @return void
  332. */
  333. public function testLocaleMultiple() {
  334. $this->loadFixtures('Translate', 'TranslatedItem');
  335. $TestModel = new TranslatedItem();
  336. $TestModel->locale = array('deu', 'eng', 'cze');
  337. $result = $TestModel->read(null, 1);
  338. $expected = array(
  339. 'TranslatedItem' => array(
  340. 'id' => 1,
  341. 'slug' => 'first_translated',
  342. 'locale' => 'deu',
  343. 'title' => 'Titel #1',
  344. 'content' => 'Inhalt #1',
  345. 'translated_article_id' => 1,
  346. )
  347. );
  348. $this->assertEquals($expected, $result);
  349. $result = $TestModel->find('all', array('fields' => array('slug', 'title', 'content')));
  350. $expected = array(
  351. array(
  352. 'TranslatedItem' => array(
  353. 'slug' => 'first_translated',
  354. 'locale' => 'deu',
  355. 'content' => 'Inhalt #1',
  356. 'title' => 'Titel #1',
  357. )
  358. ),
  359. array(
  360. 'TranslatedItem' => array(
  361. 'slug' => 'second_translated',
  362. 'locale' => 'deu',
  363. 'title' => 'Titel #2',
  364. 'content' => 'Inhalt #2',
  365. )
  366. ),
  367. array(
  368. 'TranslatedItem' => array(
  369. 'slug' => 'third_translated',
  370. 'locale' => 'deu',
  371. 'title' => 'Titel #3',
  372. 'content' => 'Inhalt #3',
  373. )
  374. )
  375. );
  376. $this->assertEquals($expected, $result);
  377. $TestModel = new TranslatedItem();
  378. $TestModel->locale = array('pt-br');
  379. $result = $TestModel->find('all');
  380. $this->assertCount(3, $result, '3 records should have been found, no SQL error.');
  381. }
  382. /**
  383. * testMissingTranslation method
  384. *
  385. * @return void
  386. */
  387. public function testMissingTranslation() {
  388. $this->loadFixtures('Translate', 'TranslatedItem');
  389. $TestModel = new TranslatedItem();
  390. $TestModel->locale = 'rus';
  391. $result = $TestModel->read(null, 1);
  392. $this->assertSame(array(), $result);
  393. $TestModel->locale = array('rus');
  394. $result = $TestModel->read(null, 1);
  395. $expected = array(
  396. 'TranslatedItem' => array(
  397. 'id' => 1,
  398. 'slug' => 'first_translated',
  399. 'locale' => 'rus',
  400. 'title' => '',
  401. 'content' => '',
  402. 'translated_article_id' => 1,
  403. )
  404. );
  405. $this->assertEquals($expected, $result);
  406. }
  407. /**
  408. * testTranslatedFindList method
  409. *
  410. * @return void
  411. */
  412. public function testTranslatedFindList() {
  413. $this->loadFixtures('Translate', 'TranslatedItem');
  414. $TestModel = new TranslatedItem();
  415. $TestModel->locale = 'deu';
  416. $TestModel->displayField = 'title';
  417. $result = $TestModel->find('list', array('recursive' => 1));
  418. $expected = array(1 => 'Titel #1', 2 => 'Titel #2', 3 => 'Titel #3');
  419. $this->assertEquals($expected, $result);
  420. // SQL Server trigger an error and stops the page even if the debug = 0
  421. if ($this->db instanceof Sqlserver) {
  422. $debug = Configure::read('debug');
  423. Configure::write('debug', 0);
  424. $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => false));
  425. $this->assertSame(array(), $result);
  426. $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'after'));
  427. $this->assertSame(array(), $result);
  428. Configure::write('debug', $debug);
  429. }
  430. $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'before'));
  431. $expected = array(1 => null, 2 => null, 3 => null);
  432. $this->assertEquals($expected, $result);
  433. }
  434. /**
  435. * testReadSelectedFields method
  436. *
  437. * @return void
  438. */
  439. public function testReadSelectedFields() {
  440. $this->loadFixtures('Translate', 'TranslatedItem');
  441. $TestModel = new TranslatedItem();
  442. $TestModel->locale = 'eng';
  443. $result = $TestModel->find('all', array('fields' => array('slug', 'TranslatedItem.content')));
  444. $expected = array(
  445. array('TranslatedItem' => array('slug' => 'first_translated', 'locale' => 'eng', 'content' => 'Content #1')),
  446. array('TranslatedItem' => array('slug' => 'second_translated', 'locale' => 'eng', 'content' => 'Content #2')),
  447. array('TranslatedItem' => array('slug' => 'third_translated', 'locale' => 'eng', 'content' => 'Content #3'))
  448. );
  449. $this->assertEquals($expected, $result);
  450. $result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug', 'content')));
  451. $this->assertEquals($expected, $result);
  452. $TestModel->locale = array('eng', 'deu', 'cze');
  453. $delete = array(array('locale' => 'deu'), array('field' => 'content', 'locale' => 'eng'));
  454. $I18nModel = ClassRegistry::getObject('TranslateTestModel');
  455. $I18nModel->deleteAll(array('or' => $delete));
  456. $result = $TestModel->find('all', array('fields' => array('title', 'content')));
  457. $expected = array(
  458. array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #1', 'content' => 'Obsah #1')),
  459. array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #2', 'content' => 'Obsah #2')),
  460. array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #3', 'content' => 'Obsah #3'))
  461. );
  462. $this->assertEquals($expected, $result);
  463. }
  464. /**
  465. * testSaveCreate method
  466. *
  467. * @return void
  468. */
  469. public function testSaveCreate() {
  470. $this->loadFixtures('Translate', 'TranslatedItem');
  471. $TestModel = new TranslatedItem();
  472. $TestModel->locale = 'spa';
  473. $data = array(
  474. 'slug' => 'fourth_translated',
  475. 'title' => 'Leyenda #4',
  476. 'content' => 'Contenido #4',
  477. 'translated_article_id' => 1,
  478. );
  479. $TestModel->create($data);
  480. $TestModel->save();
  481. $result = $TestModel->read();
  482. $expected = array('TranslatedItem' => array_merge($data, array('id' => $TestModel->id, 'locale' => 'spa')));
  483. $this->assertEquals($expected, $result);
  484. }
  485. /**
  486. * test saving/deleting with an alias, uses the model name.
  487. *
  488. * @return void
  489. */
  490. public function testSaveDeleteIgnoreAlias() {
  491. $this->loadFixtures('Translate', 'TranslatedItem');
  492. $TestModel = new TranslatedItem(array('alias' => 'SomethingElse'));
  493. $TestModel->locale = 'spa';
  494. $data = array(
  495. 'slug' => 'fourth_translated',
  496. 'title' => 'Leyenda #4',
  497. 'content' => 'Contenido #4',
  498. 'translated_article_id' => 1,
  499. );
  500. $TestModel->create($data);
  501. $TestModel->save();
  502. $id = $TestModel->id;
  503. $result = $TestModel->read();
  504. $expected = array($TestModel->alias => array_merge($data, array('id' => $id, 'locale' => 'spa')));
  505. $this->assertEquals($expected, $result);
  506. $TestModel->delete($id);
  507. $result = $TestModel->translateModel()->find('count', array(
  508. 'conditions' => array('foreign_key' => $id)
  509. ));
  510. $this->assertEquals(0, $result);
  511. }
  512. /**
  513. * test save multiple locales method
  514. *
  515. * @return void
  516. */
  517. public function testSaveMultipleLocales() {
  518. $this->loadFixtures('Translate', 'TranslatedItem');
  519. $TestModel = new TranslatedItem();
  520. $data = array(
  521. 'slug' => 'fourth_translated',
  522. 'title' => array(
  523. 'eng' => 'Title #4',
  524. 'spa' => 'Leyenda #4',
  525. ),
  526. 'content' => array(
  527. 'eng' => 'Content #4',
  528. 'spa' => 'Contenido #4',
  529. ),
  530. 'translated_article_id' => 1,
  531. );
  532. $TestModel->create();
  533. $TestModel->save($data);
  534. $translations = array('title' => 'Title', 'content' => 'Content');
  535. $TestModel->bindTranslation($translations, false);
  536. $TestModel->locale = array('eng', 'spa');
  537. $result = $TestModel->read();
  538. $this->assertCount(2, $result['Title']);
  539. $this->assertEquals($result['Title'][0]['locale'], 'eng');
  540. $this->assertEquals($result['Title'][0]['content'], 'Title #4');
  541. $this->assertEquals($result['Title'][1]['locale'], 'spa');
  542. $this->assertEquals($result['Title'][1]['content'], 'Leyenda #4');
  543. $this->assertCount(2, $result['Content']);
  544. }
  545. /**
  546. * testSaveAssociatedCreate method
  547. *
  548. * @return void
  549. */
  550. public function testSaveAssociatedMultipleLocale() {
  551. $this->loadFixtures('Translate', 'TranslatedItem');
  552. $TestModel = new TranslatedItem();
  553. $data = array(
  554. 'slug' => 'fourth_translated',
  555. 'title' => array(
  556. 'eng' => 'Title #4',
  557. 'spa' => 'Leyenda #4',
  558. ),
  559. 'content' => array(
  560. 'eng' => 'Content #4',
  561. 'spa' => 'Contenido #4',
  562. ),
  563. 'translated_article_id' => 1,
  564. );
  565. $TestModel->create();
  566. $TestModel->saveAssociated($data);
  567. $translations = array('title' => 'Title', 'content' => 'Content');
  568. $TestModel->bindTranslation($translations, false);
  569. $TestModel->locale = array('eng', 'spa');
  570. $result = $TestModel->read();
  571. $this->assertCount(2, $result['Title']);
  572. $this->assertCount(2, $result['Content']);
  573. }
  574. /**
  575. * Test that saving only some of the translated fields allows the record to be found again.
  576. *
  577. * @return void
  578. */
  579. public function testSavePartialFields() {
  580. $this->loadFixtures('Translate', 'TranslatedItem');
  581. $TestModel = new TranslatedItem();
  582. $TestModel->locale = 'spa';
  583. $data = array(
  584. 'slug' => 'fourth_translated',
  585. 'title' => 'Leyenda #4',
  586. );
  587. $TestModel->create($data);
  588. $TestModel->save();
  589. $result = $TestModel->read();
  590. $expected = array(
  591. 'TranslatedItem' => array(
  592. 'id' => $TestModel->id,
  593. 'translated_article_id' => null,
  594. 'locale' => 'spa',
  595. 'content' => '',
  596. ) + $data
  597. );
  598. $this->assertEquals($expected, $result);
  599. }
  600. /**
  601. * Test that all fields are create with partial data + multiple locales.
  602. *
  603. * @return void
  604. */
  605. public function testSavePartialFieldMultipleLocales() {
  606. $this->loadFixtures('Translate', 'TranslatedItem');
  607. $TestModel = new TranslatedItem();
  608. $TestModel->locale = 'eng';
  609. $data = array(
  610. 'slug' => 'fifth_translated',
  611. 'title' => array('eng' => 'Title #5', 'spa' => 'Leyenda #5'),
  612. );
  613. $TestModel->create($data);
  614. $TestModel->save();
  615. $TestModel->unbindTranslation();
  616. $translations = array('title' => 'Title', 'content' => 'Content');
  617. $TestModel->bindTranslation($translations, false);
  618. $result = $TestModel->read(null, $TestModel->id);
  619. $expected = array(
  620. 'TranslatedItem' => array(
  621. 'id' => '4',
  622. 'translated_article_id' => null,
  623. 'slug' => 'fifth_translated',
  624. 'locale' => 'eng',
  625. 'title' => 'Title #5',
  626. 'content' => ''
  627. ),
  628. 'Title' => array(
  629. 0 => array(
  630. 'id' => '19',
  631. 'locale' => 'eng',
  632. 'model' => 'TranslatedItem',
  633. 'foreign_key' => '4',
  634. 'field' => 'title',
  635. 'content' => 'Title #5'
  636. ),
  637. 1 => array(
  638. 'id' => '20',
  639. 'locale' => 'spa',
  640. 'model' => 'TranslatedItem',
  641. 'foreign_key' => '4',
  642. 'field' => 'title',
  643. 'content' => 'Leyenda #5'
  644. )
  645. ),
  646. 'Content' => array(
  647. 0 => array(
  648. 'id' => '21',
  649. 'locale' => 'eng',
  650. 'model' => 'TranslatedItem',
  651. 'foreign_key' => '4',
  652. 'field' => 'content',
  653. 'content' => ''
  654. ),
  655. 1 => array(
  656. 'id' => '22',
  657. 'locale' => 'spa',
  658. 'model' => 'TranslatedItem',
  659. 'foreign_key' => '4',
  660. 'field' => 'content',
  661. 'content' => ''
  662. )
  663. )
  664. );
  665. $this->assertEquals($expected, $result);
  666. }
  667. /**
  668. * testSaveUpdate method
  669. *
  670. * @return void
  671. */
  672. public function testSaveUpdate() {
  673. $this->loadFixtures('Translate', 'TranslatedItem');
  674. $TestModel = new TranslatedItem();
  675. $TestModel->locale = 'spa';
  676. $oldData = array('slug' => 'fourth_translated', 'title' => 'Leyenda #4', 'translated_article_id' => 1);
  677. $TestModel->create($oldData);
  678. $TestModel->save();
  679. $id = $TestModel->id;
  680. $newData = array('id' => $id, 'content' => 'Contenido #4');
  681. $TestModel->create($newData);
  682. $TestModel->save();
  683. $result = $TestModel->read(null, $id);
  684. $expected = array('TranslatedItem' => array_merge($oldData, $newData, array('locale' => 'spa')));
  685. $this->assertEquals($expected, $result);
  686. }
  687. /**
  688. * testMultipleCreate method
  689. *
  690. * @return void
  691. */
  692. public function testMultipleCreate() {
  693. $this->loadFixtures('Translate', 'TranslatedItem');
  694. $TestModel = new TranslatedItem();
  695. $TestModel->locale = 'deu';
  696. $data = array(
  697. 'slug' => 'new_translated',
  698. 'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
  699. 'content' => array('eng' => 'New content', 'spa' => 'Nuevo contenido')
  700. );
  701. $TestModel->create($data);
  702. $TestModel->save();
  703. $TestModel->unbindTranslation();
  704. $translations = array('title' => 'Title', 'content' => 'Content');
  705. $TestModel->bindTranslation($translations, false);
  706. $TestModel->locale = array('eng', 'spa');
  707. $result = $TestModel->read();
  708. $expected = array(
  709. 'TranslatedItem' => array(
  710. 'id' => 4,
  711. 'slug' => 'new_translated',
  712. 'locale' => 'eng',
  713. 'title' => 'New title',
  714. 'content' => 'New content',
  715. 'translated_article_id' => null,
  716. ),
  717. 'Title' => array(
  718. array('id' => 21, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'New title'),
  719. array('id' => 22, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'Nuevo leyenda')
  720. ),
  721. 'Content' => array(
  722. array('id' => 19, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'New content'),
  723. array('id' => 20, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'Nuevo contenido')
  724. )
  725. );
  726. $this->assertEquals($expected, $result);
  727. }
  728. /**
  729. * testMultipleUpdate method
  730. *
  731. * @return void
  732. */
  733. public function testMultipleUpdate() {
  734. $this->loadFixtures('Translate', 'TranslatedItem');
  735. $TestModel = new TranslatedItem();
  736. $TestModel->locale = 'eng';
  737. $TestModel->validate['title'] = 'notEmpty';
  738. $data = array('TranslatedItem' => array(
  739. 'id' => 1,
  740. 'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
  741. 'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
  742. ));
  743. $TestModel->create();
  744. $TestModel->save($data);
  745. $TestModel->unbindTranslation();
  746. $translations = array('title' => 'Title', 'content' => 'Content');
  747. $TestModel->bindTranslation($translations, false);
  748. $result = $TestModel->read(null, 1);
  749. $expected = array(
  750. 'TranslatedItem' => array(
  751. 'id' => '1',
  752. 'slug' => 'first_translated',
  753. 'locale' => 'eng',
  754. 'title' => 'New Title #1',
  755. 'content' => 'New Content #1',
  756. 'translated_article_id' => 1,
  757. ),
  758. 'Title' => array(
  759. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'New Title #1'),
  760. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Neue Titel #1'),
  761. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Novy Titulek #1')
  762. ),
  763. 'Content' => array(
  764. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'New Content #1'),
  765. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Neue Inhalt #1'),
  766. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Novy Obsah #1')
  767. )
  768. );
  769. $this->assertEquals($expected, $result);
  770. $TestModel->unbindTranslation($translations);
  771. $TestModel->bindTranslation(array('title', 'content'), false);
  772. }
  773. /**
  774. * testMixedCreateUpdateWithArrayLocale method
  775. *
  776. * @return void
  777. */
  778. public function testMixedCreateUpdateWithArrayLocale() {
  779. $this->loadFixtures('Translate', 'TranslatedItem');
  780. $TestModel = new TranslatedItem();
  781. $TestModel->locale = array('cze', 'deu');
  782. $data = array('TranslatedItem' => array(
  783. 'id' => 1,
  784. 'title' => array('eng' => 'Updated Title #1', 'spa' => 'Nuevo leyenda #1'),
  785. 'content' => 'Upraveny obsah #1'
  786. ));
  787. $TestModel->create();
  788. $TestModel->save($data);
  789. $TestModel->unbindTranslation();
  790. $translations = array('title' => 'Title', 'content' => 'Content');
  791. $TestModel->bindTranslation($translations, false);
  792. $result = $TestModel->read(null, 1);
  793. $result['Title'] = Hash::sort($result['Title'], '{n}.id', 'asc');
  794. $result['Content'] = Hash::sort($result['Content'], '{n}.id', 'asc');
  795. $expected = array(
  796. 'TranslatedItem' => array(
  797. 'id' => 1,
  798. 'slug' => 'first_translated',
  799. 'locale' => 'cze',
  800. 'title' => 'Titulek #1',
  801. 'content' => 'Upraveny obsah #1',
  802. 'translated_article_id' => 1,
  803. ),
  804. 'Title' => array(
  805. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Updated Title #1'),
  806. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
  807. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'),
  808. array('id' => 19, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Nuevo leyenda #1')
  809. ),
  810. 'Content' => array(
  811. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
  812. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
  813. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Upraveny obsah #1')
  814. )
  815. );
  816. $this->assertEquals($expected, $result);
  817. }
  818. /**
  819. * Test that saveAll() works with hasMany associations that contain
  820. * translations.
  821. *
  822. * @return void
  823. */
  824. public function testSaveAllTranslatedAssociations() {
  825. $this->loadFixtures('Translate', 'TranslateArticle', 'TranslatedItem', 'TranslatedArticle', 'User');
  826. $Model = new TranslatedArticle();
  827. $Model->locale = 'eng';
  828. $data = array(
  829. 'TranslatedArticle' => array(
  830. 'id' => 4,
  831. 'user_id' => 1,
  832. 'published' => 'Y',
  833. 'title' => 'Title (eng) #1',
  834. 'body' => 'Body (eng) #1'
  835. ),
  836. 'TranslatedItem' => array(
  837. array(
  838. 'slug' => '',
  839. 'title' => 'Nuevo leyenda #1',
  840. 'content' => 'Upraveny obsah #1'
  841. ),
  842. array(
  843. 'slug' => '',
  844. 'title' => 'New Title #2',
  845. 'content' => 'New Content #2'
  846. ),
  847. )
  848. );
  849. $result = $Model->saveAll($data);
  850. $this->assertTrue($result);
  851. $result = $Model->TranslatedItem->find('all', array(
  852. 'conditions' => array('translated_article_id' => $Model->id)
  853. ));
  854. $this->assertCount(2, $result);
  855. $this->assertEquals($data['TranslatedItem'][0]['title'], $result[0]['TranslatedItem']['title']);
  856. $this->assertEquals($data['TranslatedItem'][1]['title'], $result[1]['TranslatedItem']['title']);
  857. }
  858. /**
  859. * testValidation method
  860. *
  861. * @return void
  862. */
  863. public function testValidation() {
  864. Configure::write('Config.language', 'eng');
  865. $this->loadFixtures('Translate', 'TranslatedItem');
  866. $TestModel = new TranslatedItem();
  867. $TestModel->locale = 'eng';
  868. $TestModel->validate['title'] = '/Only this title/';
  869. $data = array(
  870. 'TranslatedItem' => array(
  871. 'id' => 1,
  872. 'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
  873. 'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
  874. )
  875. );
  876. $TestModel->create();
  877. $this->assertFalse($TestModel->save($data));
  878. $this->assertEquals(array('The provided value is invalid'), $TestModel->validationErrors['title']);
  879. $TestModel->locale = 'eng';
  880. $TestModel->validate['title'] = '/Only this title/';
  881. $data = array('TranslatedItem' => array(
  882. 'id' => 1,
  883. 'title' => array('eng' => 'Only this title', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
  884. 'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
  885. ));
  886. $TestModel->create();
  887. $result = $TestModel->save($data);
  888. $this->assertFalse(empty($result));
  889. }
  890. /**
  891. * testAttachDetach method
  892. *
  893. * @return void
  894. */
  895. public function testAttachDetach() {
  896. $this->loadFixtures('Translate', 'TranslatedItem');
  897. $TestModel = new TranslatedItem();
  898. $TestModel->unbindTranslation();
  899. $translations = array('title' => 'Title', 'content' => 'Content');
  900. $TestModel->bindTranslation($translations, false);
  901. $result = array_keys($TestModel->hasMany);
  902. $expected = array('Title', 'Content');
  903. $this->assertEquals($expected, $result);
  904. $TestModel->Behaviors->unload('Translate');
  905. $result = array_keys($TestModel->hasMany);
  906. $expected = array();
  907. $this->assertEquals($expected, $result);
  908. $result = isset($TestModel->Behaviors->Translate);
  909. $this->assertFalse($result);
  910. $result = isset($Behavior->settings[$TestModel->alias]);
  911. $this->assertFalse($result);
  912. $result = isset($Behavior->runtime[$TestModel->alias]);
  913. $this->assertFalse($result);
  914. $TestModel->Behaviors->load('Translate', array('title' => 'Title', 'content' => 'Content'));
  915. $result = array_keys($TestModel->hasMany);
  916. $expected = array('Title', 'Content');
  917. $this->assertEquals($expected, $result);
  918. $result = isset($TestModel->Behaviors->Translate);
  919. $this->assertTrue($result);
  920. $Behavior = $TestModel->Behaviors->Translate;
  921. $result = isset($Behavior->settings[$TestModel->alias]);
  922. $this->assertTrue($result);
  923. $result = isset($Behavior->runtime[$TestModel->alias]);
  924. $this->assertTrue($result);
  925. }
  926. /**
  927. * testAnotherTranslateTable method
  928. *
  929. * @return void
  930. */
  931. public function testAnotherTranslateTable() {
  932. $this->loadFixtures('Translate', 'TranslatedItem', 'TranslateTable');
  933. $TestModel = new TranslatedItemWithTable();
  934. $TestModel->locale = 'eng';
  935. $result = $TestModel->read(null, 1);
  936. $expected = array(
  937. 'TranslatedItemWithTable' => array(
  938. 'id' => 1,
  939. 'slug' => 'first_translated',
  940. 'locale' => 'eng',
  941. 'title' => 'Another Title #1',
  942. 'content' => 'Another Content #1',
  943. 'translated_article_id' => 1,
  944. )
  945. );
  946. $this->assertEquals($expected, $result);
  947. }
  948. /**
  949. * testTranslateWithAssociations method
  950. *
  951. * @return void
  952. */
  953. public function testTranslateWithAssociations() {
  954. $this->loadFixtures('TranslateArticle', 'TranslatedArticle', 'TranslatedItem', 'User', 'Comment', 'ArticlesTag', 'Tag');
  955. $TestModel = new TranslatedArticle();
  956. $TestModel->locale = 'eng';
  957. $recursive = $TestModel->recursive;
  958. $result = $TestModel->read(null, 1);
  959. $expected = array(
  960. 'TranslatedArticle' => array(
  961. 'id' => 1,
  962. 'user_id' => 1,
  963. 'published' => 'Y',
  964. 'created' => '2007-03-18 10:39:23',
  965. 'updated' => '2007-03-18 10:41:31',
  966. 'locale' => 'eng',
  967. 'title' => 'Title (eng) #1',
  968. 'body' => 'Body (eng) #1'
  969. ),
  970. 'User' => array(
  971. 'id' => 1,
  972. 'user' => 'mariano',
  973. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  974. 'created' => '2007-03-17 01:16:23',
  975. 'updated' => '2007-03-17 01:18:31'
  976. ),
  977. 'TranslatedItem' => array(
  978. array(
  979. 'id' => 1,
  980. 'translated_article_id' => 1,
  981. 'slug' => 'first_translated'
  982. ),
  983. array(
  984. 'id' => 2,
  985. 'translated_article_id' => 1,
  986. 'slug' => 'second_translated'
  987. ),
  988. array(
  989. 'id' => 3,
  990. 'translated_article_id' => 1,
  991. 'slug' => 'third_translated'
  992. ),
  993. )
  994. );
  995. $this->assertEquals($expected, $result);
  996. $result = $TestModel->find('all', array('recursive' => -1));
  997. $expected = array(
  998. array(
  999. 'TranslatedArticle' => array(
  1000. 'id' => 1,
  1001. 'user_id' => 1,
  1002. 'published' => 'Y',
  1003. 'created' => '2007-03-18 10:39:23',
  1004. 'updated' => '2007-03-18 10:41:31',
  1005. 'locale' => 'eng',
  1006. 'title' => 'Title (eng) #1',
  1007. 'body' => 'Body (eng) #1'
  1008. )
  1009. ),
  1010. array(
  1011. 'TranslatedArticle' => array(
  1012. 'id' => 2,
  1013. 'user_id' => 3,
  1014. 'published' => 'Y',
  1015. 'created' => '2007-03-18 10:41:23',
  1016. 'updated' => '2007-03-18 10:43:31',
  1017. 'locale' => 'eng',
  1018. 'title' => 'Title (eng) #2',
  1019. 'body' => 'Body (eng) #2'
  1020. )
  1021. ),
  1022. array(
  1023. 'TranslatedArticle' => array(
  1024. 'id' => 3,
  1025. 'user_id' => 1,
  1026. 'published' => 'Y',
  1027. 'created' => '2007-03-18 10:43:23',
  1028. 'updated' => '2007-03-18 10:45:31',
  1029. 'locale' => 'eng',
  1030. 'title' => 'Title (eng) #3',
  1031. 'body' => 'Body (eng) #3'
  1032. )
  1033. )
  1034. );
  1035. $this->assertEquals($expected, $result);
  1036. $this->assertEquals($TestModel->recursive, $recursive);
  1037. $TestModel->recursive = -1;
  1038. $result = $TestModel->read(null, 1);
  1039. $expected = array(
  1040. 'TranslatedArticle' => array(
  1041. 'id' => 1,
  1042. 'user_id' => 1,
  1043. 'published' => 'Y',
  1044. 'created' => '2007-03-18 10:39:23',
  1045. 'updated' => '2007-03-18 10:41:31',
  1046. 'locale' => 'eng',
  1047. 'title' => 'Title (eng) #1',
  1048. 'body' => 'Body (eng) #1'
  1049. )
  1050. );
  1051. $this->assertEquals($expected, $result);
  1052. }
  1053. /**
  1054. * testTranslateTableWithPrefix method
  1055. * Tests that is possible to have a translation model with a custom tablePrefix
  1056. *
  1057. * @return void
  1058. */
  1059. public function testTranslateTableWithPrefix() {
  1060. $this->loadFixtures('TranslateWithPrefix', 'TranslatedItem');
  1061. $TestModel = new TranslatedItem2;
  1062. $TestModel->locale = 'eng';
  1063. $result = $TestModel->read(null, 1);
  1064. $expected = array('TranslatedItem' => array(
  1065. 'id' => 1,
  1066. 'slug' => 'first_translated',
  1067. 'locale' => 'eng',
  1068. 'content' => 'Content #1',
  1069. 'title' => 'Title #1',
  1070. 'translated_article_id' => 1,
  1071. ));
  1072. $this->assertEquals($expected, $result);
  1073. }
  1074. /**
  1075. * Test infinite loops not occurring with unbindTranslation()
  1076. *
  1077. * @return void
  1078. */
  1079. public function testUnbindTranslationInfinteLoop() {
  1080. $this->loadFixtures('Translate', 'TranslatedItem');
  1081. $TestModel = new TranslatedItem();
  1082. $TestModel->Behaviors->unload('Translate');
  1083. $TestModel->actsAs = array();
  1084. $TestModel->Behaviors->load('Translate');
  1085. $TestModel->bindTranslation(array('title', 'content'), true);
  1086. $result = $TestModel->unbindTranslation();
  1087. $this->assertFalse($result);
  1088. }
  1089. /**
  1090. * Test that an exception is raised when you try to over-write the name attribute.
  1091. *
  1092. * @expectedException CakeException
  1093. * @return void
  1094. */
  1095. public function testExceptionOnNameTranslation() {
  1096. $this->loadFixtures('Translate', 'TranslatedItem');
  1097. $TestModel = new TranslatedItem();
  1098. $TestModel->bindTranslation(array('name' => 'name'));
  1099. }
  1100. /**
  1101. * Test that translations can be bound and unbound dynamically.
  1102. *
  1103. * @return void
  1104. */
  1105. public function testUnbindTranslation() {
  1106. $this->loadFixtures('Translate', 'TranslatedItem');
  1107. $Model = new TranslatedItem();
  1108. $Model->unbindTranslation();
  1109. $Model->bindTranslation(array('body', 'slug'), false);
  1110. $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
  1111. $this->assertEquals(array('body', 'slug'), $result);
  1112. $Model->unbindTranslation(array('body'));
  1113. $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
  1114. $this->assertNotContains('body', $result);
  1115. $Model->unbindTranslation('slug');
  1116. $result = $Model->Behaviors->Translate->settings['TranslatedItem'];
  1117. $this->assertNotContains('slug', $result);
  1118. }
  1119. /**
  1120. * Test that additional records are not inserted for associated translations.
  1121. *
  1122. * @return void
  1123. */
  1124. public function testNoExtraRowsForAssociatedTranslations() {
  1125. $this->loadFixtures('Translate', 'TranslatedItem');
  1126. $TestModel = new TranslatedItem();
  1127. $TestModel->locale = 'spa';
  1128. $TestModel->unbindTranslation();
  1129. $TestModel->bindTranslation(array('name' => 'nameTranslate'));
  1130. $data = array(
  1131. 'TranslatedItem' => array(
  1132. 'slug' => 'spanish-name',
  1133. 'name' => 'Spanish name',
  1134. ),
  1135. );
  1136. $TestModel->create($data);
  1137. $TestModel->save();
  1138. $Translate = $TestModel->translateModel();
  1139. $results = $Translate->find('all', array(
  1140. 'conditions' => array(
  1141. 'locale' => $TestModel->locale,
  1142. 'foreign_key' => $TestModel->id
  1143. )
  1144. ));
  1145. $this->assertCount(1, $results, 'Only one field should be saved');
  1146. $this->assertEquals('name', $results[0]['TranslateTestModel']['field']);
  1147. }
  1148. }