PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 883 lines | 640 code | 93 blank | 150 comment | 2 complexity | e73f6975c9013b377baefe2652e39d7b MD5 | raw file
  1. <?php
  2. /**
  3. * TranslateBehaviorTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model.Behavior
  16. * @since CakePHP(tm) v 1.2.0.5669
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  20. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  21. }
  22. App::uses('Model', 'Model');
  23. App::uses('AppModel', 'Model');
  24. require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
  25. /**
  26. * TranslateBehaviorTest class
  27. *
  28. * @package Cake.Test.Case.Model.Behavior
  29. */
  30. class TranslateBehaviorTest extends CakeTestCase {
  31. /**
  32. * autoFixtures property
  33. *
  34. * @var bool false
  35. */
  36. public $autoFixtures = false;
  37. /**
  38. * fixtures property
  39. *
  40. * @var array
  41. */
  42. public $fixtures = array(
  43. 'core.translated_item', 'core.translate', 'core.translate_table',
  44. 'core.translated_article', 'core.translate_article', 'core.user', 'core.comment', 'core.tag', 'core.articles_tag',
  45. 'core.translate_with_prefix'
  46. );
  47. /**
  48. * tearDown method
  49. *
  50. * @return void
  51. */
  52. public function tearDown() {
  53. ClassRegistry::flush();
  54. }
  55. /**
  56. * testTranslateModel method
  57. *
  58. * @return void
  59. */
  60. public function testTranslateModel() {
  61. $this->loadFixtures('TranslateTable', 'Tag', 'TranslatedItem', 'Translate', 'User', 'TranslatedArticle', 'TranslateArticle');
  62. $TestModel = new Tag();
  63. $TestModel->translateTable = 'another_i18n';
  64. $TestModel->Behaviors->attach('Translate', array('title'));
  65. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  66. $this->assertEquals($translateModel->name, 'I18nModel');
  67. $this->assertEquals($translateModel->useTable, 'another_i18n');
  68. $TestModel = new User();
  69. $TestModel->Behaviors->attach('Translate', array('title'));
  70. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  71. $this->assertEquals($translateModel->name, 'I18nModel');
  72. $this->assertEquals($translateModel->useTable, 'i18n');
  73. $TestModel = new TranslatedArticle();
  74. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  75. $this->assertEquals($translateModel->name, 'TranslateArticleModel');
  76. $this->assertEquals($translateModel->useTable, 'article_i18n');
  77. $TestModel = new TranslatedItem();
  78. $translateModel = $TestModel->Behaviors->Translate->translateModel($TestModel);
  79. $this->assertEquals($translateModel->name, 'TranslateTestModel');
  80. $this->assertEquals($translateModel->useTable, 'i18n');
  81. }
  82. /**
  83. * testLocaleFalsePlain method
  84. *
  85. * @return void
  86. */
  87. public function testLocaleFalsePlain() {
  88. $this->loadFixtures('Translate', 'TranslatedItem', 'User');
  89. $TestModel = new TranslatedItem();
  90. $TestModel->locale = false;
  91. $result = $TestModel->read(null, 1);
  92. $expected = array('TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'));
  93. $this->assertEquals($expected, $result);
  94. $result = $TestModel->find('all', array('fields' => array('slug')));
  95. $expected = array(
  96. array('TranslatedItem' => array('slug' => 'first_translated')),
  97. array('TranslatedItem' => array('slug' => 'second_translated')),
  98. array('TranslatedItem' => array('slug' => 'third_translated'))
  99. );
  100. $this->assertEquals($expected, $result);
  101. }
  102. /**
  103. * testLocaleFalseAssociations method
  104. *
  105. * @return void
  106. */
  107. public function testLocaleFalseAssociations() {
  108. $this->loadFixtures('Translate', 'TranslatedItem');
  109. $TestModel = new TranslatedItem();
  110. $TestModel->locale = false;
  111. $TestModel->unbindTranslation();
  112. $translations = array('title' => 'Title', 'content' => 'Content');
  113. $TestModel->bindTranslation($translations, false);
  114. $result = $TestModel->read(null, 1);
  115. $expected = array(
  116. 'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
  117. 'Title' => array(
  118. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
  119. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
  120. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
  121. ),
  122. 'Content' => array(
  123. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
  124. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
  125. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
  126. )
  127. );
  128. $this->assertEquals($expected, $result);
  129. $TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
  130. $TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
  131. $result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug')));
  132. $expected = array(
  133. array(
  134. 'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated'),
  135. 'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
  136. 'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
  137. ),
  138. array(
  139. 'TranslatedItem' => array('id' => 2, 'slug' => 'second_translated'),
  140. 'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
  141. 'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
  142. ),
  143. array(
  144. 'TranslatedItem' => array('id' => 3, 'slug' => 'third_translated'),
  145. 'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
  146. 'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
  147. )
  148. );
  149. $this->assertEquals($expected, $result);
  150. }
  151. /**
  152. * testLocaleSingle method
  153. *
  154. * @return void
  155. */
  156. public function testLocaleSingle() {
  157. $this->loadFixtures('Translate', 'TranslatedItem');
  158. $TestModel = new TranslatedItem();
  159. $TestModel->locale = 'eng';
  160. $result = $TestModel->read(null, 1);
  161. $expected = array(
  162. 'TranslatedItem' => array(
  163. 'id' => 1,
  164. 'slug' => 'first_translated',
  165. 'locale' => 'eng',
  166. 'title' => 'Title #1',
  167. 'content' => 'Content #1'
  168. )
  169. );
  170. $this->assertEquals($expected, $result);
  171. $result = $TestModel->find('all');
  172. $expected = array(
  173. array(
  174. 'TranslatedItem' => array(
  175. 'id' => 1,
  176. 'slug' => 'first_translated',
  177. 'locale' => 'eng',
  178. 'title' => 'Title #1',
  179. 'content' => 'Content #1'
  180. )
  181. ),
  182. array(
  183. 'TranslatedItem' => array(
  184. 'id' => 2,
  185. 'slug' => 'second_translated',
  186. 'locale' => 'eng',
  187. 'title' => 'Title #2',
  188. 'content' => 'Content #2'
  189. )
  190. ),
  191. array(
  192. 'TranslatedItem' => array(
  193. 'id' => 3,
  194. 'slug' => 'third_translated',
  195. 'locale' => 'eng',
  196. 'title' => 'Title #3',
  197. 'content' => 'Content #3'
  198. )
  199. )
  200. );
  201. $this->assertEquals($expected, $result);
  202. }
  203. /**
  204. * testLocaleSingleWithConditions method
  205. *
  206. * @return void
  207. */
  208. public function testLocaleSingleWithConditions() {
  209. $this->loadFixtures('Translate', 'TranslatedItem');
  210. $TestModel = new TranslatedItem();
  211. $TestModel->locale = 'eng';
  212. $result = $TestModel->find('all', array('conditions' => array('slug' => 'first_translated')));
  213. $expected = array(
  214. array(
  215. 'TranslatedItem' => array(
  216. 'id' => 1,
  217. 'slug' => 'first_translated',
  218. 'locale' => 'eng',
  219. 'title' => 'Title #1',
  220. 'content' => 'Content #1'
  221. )
  222. )
  223. );
  224. $this->assertEquals($expected, $result);
  225. $result = $TestModel->find('all', array('conditions' => "TranslatedItem.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. )
  235. )
  236. );
  237. $this->assertEquals($expected, $result);
  238. }
  239. /**
  240. * testLocaleSingleAssociations method
  241. *
  242. * @return void
  243. */
  244. public function testLocaleSingleAssociations() {
  245. $this->loadFixtures('Translate', 'TranslatedItem');
  246. $TestModel = new TranslatedItem();
  247. $TestModel->locale = 'eng';
  248. $TestModel->unbindTranslation();
  249. $translations = array('title' => 'Title', 'content' => 'Content');
  250. $TestModel->bindTranslation($translations, false);
  251. $result = $TestModel->read(null, 1);
  252. $expected = array(
  253. 'TranslatedItem' => array(
  254. 'id' => 1,
  255. 'slug' => 'first_translated',
  256. 'locale' => 'eng',
  257. 'title' => 'Title #1',
  258. 'content' => 'Content #1'
  259. ),
  260. 'Title' => array(
  261. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Title #1'),
  262. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
  263. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1')
  264. ),
  265. 'Content' => array(
  266. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
  267. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
  268. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Obsah #1')
  269. )
  270. );
  271. $this->assertEquals($expected, $result);
  272. $TestModel->hasMany['Title']['fields'] = $TestModel->hasMany['Content']['fields'] = array('content');
  273. $TestModel->hasMany['Title']['conditions']['locale'] = $TestModel->hasMany['Content']['conditions']['locale'] = 'eng';
  274. $result = $TestModel->find('all', array('fields' => array('TranslatedItem.title')));
  275. $expected = array(
  276. array(
  277. 'TranslatedItem' => array('id' => 1, 'locale' => 'eng', 'title' => 'Title #1', 'slug' => 'first_translated'),
  278. 'Title' => array(array('foreign_key' => 1, 'content' => 'Title #1')),
  279. 'Content' => array(array('foreign_key' => 1, 'content' => 'Content #1'))
  280. ),
  281. array(
  282. 'TranslatedItem' => array('id' => 2, 'locale' => 'eng', 'title' => 'Title #2', 'slug' => 'second_translated'),
  283. 'Title' => array(array('foreign_key' => 2, 'content' => 'Title #2')),
  284. 'Content' => array(array('foreign_key' => 2, 'content' => 'Content #2'))
  285. ),
  286. array(
  287. 'TranslatedItem' => array('id' => 3, 'locale' => 'eng', 'title' => 'Title #3','slug' => 'third_translated'),
  288. 'Title' => array(array('foreign_key' => 3, 'content' => 'Title #3')),
  289. 'Content' => array(array('foreign_key' => 3, 'content' => 'Content #3'))
  290. )
  291. );
  292. $this->assertEquals($expected, $result);
  293. }
  294. /**
  295. * testLocaleMultiple method
  296. *
  297. * @return void
  298. */
  299. public function testLocaleMultiple() {
  300. $this->loadFixtures('Translate', 'TranslatedItem');
  301. $TestModel = new TranslatedItem();
  302. $TestModel->locale = array('deu', 'eng', 'cze');
  303. $result = $TestModel->read(null, 1);
  304. $expected = array(
  305. 'TranslatedItem' => array(
  306. 'id' => 1,
  307. 'slug' => 'first_translated',
  308. 'locale' => 'deu',
  309. 'title' => 'Titel #1',
  310. 'content' => 'Inhalt #1'
  311. )
  312. );
  313. $this->assertEquals($expected, $result);
  314. $result = $TestModel->find('all', array('fields' => array('slug', 'title', 'content')));
  315. $expected = array(
  316. array(
  317. 'TranslatedItem' => array(
  318. 'slug' => 'first_translated',
  319. 'locale' => 'deu',
  320. 'content' => 'Inhalt #1',
  321. 'title' => 'Titel #1'
  322. )
  323. ),
  324. array(
  325. 'TranslatedItem' => array(
  326. 'slug' => 'second_translated',
  327. 'locale' => 'deu',
  328. 'title' => 'Titel #2',
  329. 'content' => 'Inhalt #2'
  330. )
  331. ),
  332. array(
  333. 'TranslatedItem' => array(
  334. 'slug' => 'third_translated',
  335. 'locale' => 'deu',
  336. 'title' => 'Titel #3',
  337. 'content' => 'Inhalt #3'
  338. )
  339. )
  340. );
  341. $this->assertEquals($expected, $result);
  342. }
  343. /**
  344. * testMissingTranslation method
  345. *
  346. * @return void
  347. */
  348. public function testMissingTranslation() {
  349. $this->loadFixtures('Translate', 'TranslatedItem');
  350. $TestModel = new TranslatedItem();
  351. $TestModel->locale = 'rus';
  352. $result = $TestModel->read(null, 1);
  353. $this->assertFalse($result);
  354. $TestModel->locale = array('rus');
  355. $result = $TestModel->read(null, 1);
  356. $expected = array(
  357. 'TranslatedItem' => array(
  358. 'id' => 1,
  359. 'slug' => 'first_translated',
  360. 'locale' => 'rus',
  361. 'title' => '',
  362. 'content' => ''
  363. )
  364. );
  365. $this->assertEquals($expected, $result);
  366. }
  367. /**
  368. * testTranslatedFindList method
  369. *
  370. * @return void
  371. */
  372. public function testTranslatedFindList() {
  373. $this->loadFixtures('Translate', 'TranslatedItem');
  374. $TestModel = new TranslatedItem();
  375. $TestModel->locale = 'deu';
  376. $TestModel->displayField = 'title';
  377. $result = $TestModel->find('list', array('recursive' => 1));
  378. $expected = array(1 => 'Titel #1', 2 => 'Titel #2', 3 => 'Titel #3');
  379. $this->assertEquals($expected, $result);
  380. // SQL Server trigger an error and stops the page even if the debug = 0
  381. if ($this->db instanceof Sqlserver) {
  382. $debug = Configure::read('debug');
  383. Configure::write('debug', 0);
  384. $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => false));
  385. $this->assertEquals($result, array());
  386. $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'after'));
  387. $this->assertEquals($result, array());
  388. Configure::write('debug', $debug);
  389. }
  390. $result = $TestModel->find('list', array('recursive' => 1, 'callbacks' => 'before'));
  391. $expected = array(1 => null, 2 => null, 3 => null);
  392. $this->assertEquals($expected, $result);
  393. }
  394. /**
  395. * testReadSelectedFields method
  396. *
  397. * @return void
  398. */
  399. public function testReadSelectedFields() {
  400. $this->loadFixtures('Translate', 'TranslatedItem');
  401. $TestModel = new TranslatedItem();
  402. $TestModel->locale = 'eng';
  403. $result = $TestModel->find('all', array('fields' => array('slug', 'TranslatedItem.content')));
  404. $expected = array(
  405. array('TranslatedItem' => array('slug' => 'first_translated', 'locale' => 'eng', 'content' => 'Content #1')),
  406. array('TranslatedItem' => array('slug' => 'second_translated', 'locale' => 'eng', 'content' => 'Content #2')),
  407. array('TranslatedItem' => array('slug' => 'third_translated', 'locale' => 'eng', 'content' => 'Content #3'))
  408. );
  409. $this->assertEquals($expected, $result);
  410. $result = $TestModel->find('all', array('fields' => array('TranslatedItem.slug', 'content')));
  411. $this->assertEquals($expected, $result);
  412. $TestModel->locale = array('eng', 'deu', 'cze');
  413. $delete = array(array('locale' => 'deu'), array('field' => 'content', 'locale' => 'eng'));
  414. $I18nModel = ClassRegistry::getObject('TranslateTestModel');
  415. $I18nModel->deleteAll(array('or' => $delete));
  416. $result = $TestModel->find('all', array('fields' => array('title', 'content')));
  417. $expected = array(
  418. array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #1', 'content' => 'Obsah #1')),
  419. array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #2', 'content' => 'Obsah #2')),
  420. array('TranslatedItem' => array('locale' => 'eng', 'title' => 'Title #3', 'content' => 'Obsah #3'))
  421. );
  422. $this->assertEquals($expected, $result);
  423. }
  424. /**
  425. * testSaveCreate method
  426. *
  427. * @return void
  428. */
  429. public function testSaveCreate() {
  430. $this->loadFixtures('Translate', 'TranslatedItem');
  431. $TestModel = new TranslatedItem();
  432. $TestModel->locale = 'spa';
  433. $data = array('slug' => 'fourth_translated', 'title' => 'Leyenda #4', 'content' => 'Contenido #4');
  434. $TestModel->create($data);
  435. $TestModel->save();
  436. $result = $TestModel->read();
  437. $expected = array('TranslatedItem' => array_merge($data, array('id' => $TestModel->id, 'locale' => 'spa')));
  438. $this->assertEquals($expected, $result);
  439. }
  440. /**
  441. * testSaveUpdate method
  442. *
  443. * @return void
  444. */
  445. public function testSaveUpdate() {
  446. $this->loadFixtures('Translate', 'TranslatedItem');
  447. $TestModel = new TranslatedItem();
  448. $TestModel->locale = 'spa';
  449. $oldData = array('slug' => 'fourth_translated', 'title' => 'Leyenda #4');
  450. $TestModel->create($oldData);
  451. $TestModel->save();
  452. $id = $TestModel->id;
  453. $newData = array('id' => $id, 'content' => 'Contenido #4');
  454. $TestModel->create($newData);
  455. $TestModel->save();
  456. $result = $TestModel->read(null, $id);
  457. $expected = array('TranslatedItem' => array_merge($oldData, $newData, array('locale' => 'spa')));
  458. $this->assertEquals($expected, $result);
  459. }
  460. /**
  461. * testMultipleCreate method
  462. *
  463. * @return void
  464. */
  465. public function testMultipleCreate() {
  466. $this->loadFixtures('Translate', 'TranslatedItem');
  467. $TestModel = new TranslatedItem();
  468. $TestModel->locale = 'deu';
  469. $data = array(
  470. 'slug' => 'new_translated',
  471. 'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
  472. 'content' => array('eng' => 'New content', 'spa' => 'Nuevo contenido')
  473. );
  474. $TestModel->create($data);
  475. $TestModel->save();
  476. $TestModel->unbindTranslation();
  477. $translations = array('title' => 'Title', 'content' => 'Content');
  478. $TestModel->bindTranslation($translations, false);
  479. $TestModel->locale = array('eng', 'spa');
  480. $result = $TestModel->read();
  481. $expected = array(
  482. 'TranslatedItem' => array('id' => 4, 'slug' => 'new_translated', 'locale' => 'eng', 'title' => 'New title', 'content' => 'New content'),
  483. 'Title' => array(
  484. array('id' => 21, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'New title'),
  485. array('id' => 22, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'title', 'content' => 'Nuevo leyenda')
  486. ),
  487. 'Content' => array(
  488. array('id' => 19, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'New content'),
  489. array('id' => 20, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 4, 'field' => 'content', 'content' => 'Nuevo contenido')
  490. )
  491. );
  492. $this->assertEquals($expected, $result);
  493. }
  494. /**
  495. * testMultipleUpdate method
  496. *
  497. * @return void
  498. */
  499. public function testMultipleUpdate() {
  500. $this->loadFixtures('Translate', 'TranslatedItem');
  501. $TestModel = new TranslatedItem();
  502. $TestModel->locale = 'eng';
  503. $TestModel->validate['title'] = 'notEmpty';
  504. $data = array('TranslatedItem' => array(
  505. 'id' => 1,
  506. 'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
  507. 'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
  508. ));
  509. $TestModel->create();
  510. $TestModel->save($data);
  511. $TestModel->unbindTranslation();
  512. $translations = array('title' => 'Title', 'content' => 'Content');
  513. $TestModel->bindTranslation($translations, false);
  514. $result = $TestModel->read(null, 1);
  515. $expected = array(
  516. 'TranslatedItem' => array('id' => '1', 'slug' => 'first_translated', 'locale' => 'eng', 'title' => 'New Title #1', 'content' => 'New Content #1'),
  517. 'Title' => array(
  518. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'New Title #1'),
  519. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Neue Titel #1'),
  520. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Novy Titulek #1')
  521. ),
  522. 'Content' => array(
  523. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'New Content #1'),
  524. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Neue Inhalt #1'),
  525. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Novy Obsah #1')
  526. )
  527. );
  528. $this->assertEquals($expected, $result);
  529. $TestModel->unbindTranslation($translations);
  530. $TestModel->bindTranslation(array('title', 'content'), false);
  531. }
  532. /**
  533. * testMixedCreateUpdateWithArrayLocale method
  534. *
  535. * @return void
  536. */
  537. public function testMixedCreateUpdateWithArrayLocale() {
  538. $this->loadFixtures('Translate', 'TranslatedItem');
  539. $TestModel = new TranslatedItem();
  540. $TestModel->locale = array('cze', 'deu');
  541. $data = array('TranslatedItem' => array(
  542. 'id' => 1,
  543. 'title' => array('eng' => 'Updated Title #1', 'spa' => 'Nuevo leyenda #1'),
  544. 'content' => 'Upraveny obsah #1'
  545. ));
  546. $TestModel->create();
  547. $TestModel->save($data);
  548. $TestModel->unbindTranslation();
  549. $translations = array('title' => 'Title', 'content' => 'Content');
  550. $TestModel->bindTranslation($translations, false);
  551. $result = $TestModel->read(null, 1);
  552. $result['Title'] = Set::sort($result['Title'], '{n}.id', 'asc');
  553. $result['Content'] = Set::sort($result['Content'], '{n}.id', 'asc');
  554. $expected = array(
  555. 'TranslatedItem' => array('id' => 1, 'slug' => 'first_translated', 'locale' => 'cze', 'title' => 'Titulek #1', 'content' => 'Upraveny obsah #1'),
  556. 'Title' => array(
  557. array('id' => 1, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Updated Title #1'),
  558. array('id' => 3, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titel #1'),
  559. array('id' => 5, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Titulek #1'),
  560. array('id' => 19, 'locale' => 'spa', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'title', 'content' => 'Nuevo leyenda #1')
  561. ),
  562. 'Content' => array(
  563. array('id' => 2, 'locale' => 'eng', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Content #1'),
  564. array('id' => 4, 'locale' => 'deu', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Inhalt #1'),
  565. array('id' => 6, 'locale' => 'cze', 'model' => 'TranslatedItem', 'foreign_key' => 1, 'field' => 'content', 'content' => 'Upraveny obsah #1')
  566. )
  567. );
  568. $this->assertEquals($expected, $result);
  569. }
  570. /**
  571. * testValidation method
  572. *
  573. * @return void
  574. */
  575. public function testValidation() {
  576. $this->loadFixtures('Translate', 'TranslatedItem');
  577. $TestModel = new TranslatedItem();
  578. $TestModel->locale = 'eng';
  579. $TestModel->validate['title'] = '/Only this title/';
  580. $data = array('TranslatedItem' => array(
  581. 'id' => 1,
  582. 'title' => array('eng' => 'New Title #1', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
  583. 'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
  584. ));
  585. $TestModel->create();
  586. $this->assertFalse($TestModel->save($data));
  587. $this->assertEquals($TestModel->validationErrors['title'], array('This field cannot be left blank'));
  588. $TestModel->locale = 'eng';
  589. $TestModel->validate['title'] = '/Only this title/';
  590. $data = array('TranslatedItem' => array(
  591. 'id' => 1,
  592. 'title' => array('eng' => 'Only this title', 'deu' => 'Neue Titel #1', 'cze' => 'Novy Titulek #1'),
  593. 'content' => array('eng' => 'New Content #1', 'deu' => 'Neue Inhalt #1', 'cze' => 'Novy Obsah #1')
  594. ));
  595. $TestModel->create();
  596. $result = $TestModel->save($data);
  597. $this->assertFalse(empty($result));
  598. }
  599. /**
  600. * testAttachDetach method
  601. *
  602. * @return void
  603. */
  604. public function testAttachDetach() {
  605. $this->loadFixtures('Translate', 'TranslatedItem');
  606. $TestModel = new TranslatedItem();
  607. $TestModel->unbindTranslation();
  608. $translations = array('title' => 'Title', 'content' => 'Content');
  609. $TestModel->bindTranslation($translations, false);
  610. $result = array_keys($TestModel->hasMany);
  611. $expected = array('Title', 'Content');
  612. $this->assertEquals($expected, $result);
  613. $TestModel->Behaviors->detach('Translate');
  614. $result = array_keys($TestModel->hasMany);
  615. $expected = array();
  616. $this->assertEquals($expected, $result);
  617. $result = isset($TestModel->Behaviors->Translate);
  618. $this->assertFalse($result);
  619. $result = isset($Behavior->settings[$TestModel->alias]);
  620. $this->assertFalse($result);
  621. $result = isset($Behavior->runtime[$TestModel->alias]);
  622. $this->assertFalse($result);
  623. $TestModel->Behaviors->attach('Translate', array('title' => 'Title', 'content' => 'Content'));
  624. $result = array_keys($TestModel->hasMany);
  625. $expected = array('Title', 'Content');
  626. $this->assertEquals($expected, $result);
  627. $result = isset($TestModel->Behaviors->Translate);
  628. $this->assertTrue($result);
  629. $Behavior = $TestModel->Behaviors->Translate;
  630. $result = isset($Behavior->settings[$TestModel->alias]);
  631. $this->assertTrue($result);
  632. $result = isset($Behavior->runtime[$TestModel->alias]);
  633. $this->assertTrue($result);
  634. }
  635. /**
  636. * testAnotherTranslateTable method
  637. *
  638. * @return void
  639. */
  640. public function testAnotherTranslateTable() {
  641. $this->loadFixtures('Translate', 'TranslatedItem', 'TranslateTable');
  642. $TestModel = new TranslatedItemWithTable();
  643. $TestModel->locale = 'eng';
  644. $result = $TestModel->read(null, 1);
  645. $expected = array(
  646. 'TranslatedItemWithTable' => array(
  647. 'id' => 1,
  648. 'slug' => 'first_translated',
  649. 'locale' => 'eng',
  650. 'title' => 'Another Title #1',
  651. 'content' => 'Another Content #1'
  652. )
  653. );
  654. $this->assertEquals($expected, $result);
  655. }
  656. /**
  657. * testTranslateWithAssociations method
  658. *
  659. * @return void
  660. */
  661. public function testTranslateWithAssociations() {
  662. $this->loadFixtures('TranslateArticle', 'TranslatedArticle', 'User', 'Comment', 'ArticlesTag', 'Tag');
  663. $TestModel = new TranslatedArticle();
  664. $TestModel->locale = 'eng';
  665. $recursive = $TestModel->recursive;
  666. $result = $TestModel->read(null, 1);
  667. $expected = array(
  668. 'TranslatedArticle' => array(
  669. 'id' => 1,
  670. 'user_id' => 1,
  671. 'published' => 'Y',
  672. 'created' => '2007-03-18 10:39:23',
  673. 'updated' => '2007-03-18 10:41:31',
  674. 'locale' => 'eng',
  675. 'title' => 'Title (eng) #1',
  676. 'body' => 'Body (eng) #1'
  677. ),
  678. 'User' => array(
  679. 'id' => 1,
  680. 'user' => 'mariano',
  681. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  682. 'created' => '2007-03-17 01:16:23',
  683. 'updated' => '2007-03-17 01:18:31'
  684. )
  685. );
  686. $this->assertEquals($expected, $result);
  687. $result = $TestModel->find('all', array('recursive' => -1));
  688. $expected = array(
  689. array(
  690. 'TranslatedArticle' => array(
  691. 'id' => 1,
  692. 'user_id' => 1,
  693. 'published' => 'Y',
  694. 'created' => '2007-03-18 10:39:23',
  695. 'updated' => '2007-03-18 10:41:31',
  696. 'locale' => 'eng',
  697. 'title' => 'Title (eng) #1',
  698. 'body' => 'Body (eng) #1'
  699. )
  700. ),
  701. array(
  702. 'TranslatedArticle' => array(
  703. 'id' => 2,
  704. 'user_id' => 3,
  705. 'published' => 'Y',
  706. 'created' => '2007-03-18 10:41:23',
  707. 'updated' => '2007-03-18 10:43:31',
  708. 'locale' => 'eng',
  709. 'title' => 'Title (eng) #2',
  710. 'body' => 'Body (eng) #2'
  711. )
  712. ),
  713. array(
  714. 'TranslatedArticle' => array(
  715. 'id' => 3,
  716. 'user_id' => 1,
  717. 'published' => 'Y',
  718. 'created' => '2007-03-18 10:43:23',
  719. 'updated' => '2007-03-18 10:45:31',
  720. 'locale' => 'eng',
  721. 'title' => 'Title (eng) #3',
  722. 'body' => 'Body (eng) #3'
  723. )
  724. )
  725. );
  726. $this->assertEquals($expected, $result);
  727. $this->assertEquals($TestModel->recursive, $recursive);
  728. $TestModel->recursive = -1;
  729. $result = $TestModel->read(null, 1);
  730. $expected = array(
  731. 'TranslatedArticle' => array(
  732. 'id' => 1,
  733. 'user_id' => 1,
  734. 'published' => 'Y',
  735. 'created' => '2007-03-18 10:39:23',
  736. 'updated' => '2007-03-18 10:41:31',
  737. 'locale' => 'eng',
  738. 'title' => 'Title (eng) #1',
  739. 'body' => 'Body (eng) #1'
  740. )
  741. );
  742. $this->assertEquals($expected, $result);
  743. }
  744. /**
  745. * testTranslateTableWithPrefix method
  746. * Tests that is possible to have a translation model with a custom tablePrefix
  747. *
  748. * @return void
  749. */
  750. public function testTranslateTableWithPrefix() {
  751. $this->loadFixtures('TranslateWithPrefix', 'TranslatedItem');
  752. $TestModel = new TranslatedItem2;
  753. $TestModel->locale = 'eng';
  754. $result = $TestModel->read(null, 1);
  755. $expected = array('TranslatedItem' => array(
  756. 'id' => 1,
  757. 'slug' => 'first_translated',
  758. 'locale' => 'eng',
  759. 'content' => 'Content #1',
  760. 'title' => 'Title #1'
  761. ));
  762. $this->assertEquals($expected, $result);
  763. }
  764. /**
  765. * Test infinite loops not occuring with unbindTranslation()
  766. *
  767. * @return void
  768. */
  769. public function testUnbindTranslationInfinteLoop() {
  770. $this->loadFixtures('Translate', 'TranslatedItem');
  771. $TestModel = new TranslatedItem();
  772. $TestModel->Behaviors->detach('Translate');
  773. $TestModel->actsAs = array();
  774. $TestModel->Behaviors->attach('Translate');
  775. $TestModel->bindTranslation(array('title', 'content'), true);
  776. $result = $TestModel->unbindTranslation();
  777. $this->assertFalse($result);
  778. }
  779. /**
  780. * Test that an exception is raised when you try to over-write the name attribute.
  781. *
  782. * @expectedException CakeException
  783. * @return void
  784. */
  785. public function testExceptionOnNameTranslation() {
  786. $this->loadFixtures('Translate', 'TranslatedItem');
  787. $TestModel = new TranslatedItem();
  788. $TestModel->bindTranslation(array('name' => 'name'));
  789. }
  790. }