PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

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