TranslateBehaviorTest.php 29 KB

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