PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/model/behaviors/translate.test.php

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