PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/testsuite/generator/behavior/i18n/I18nBehaviorObjectBuilderModifierTest.php

https://github.com/1989gaurav/Propel
PHP | 387 lines | 335 code | 36 blank | 16 comment | 1 complexity | b92226fc323fc26d035f615303960228 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: VersionableBehaviorTest.php 1460 2010-01-17 22:36:48Z francois $
  4. * This file is part of the Propel package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. require_once dirname(__FILE__) . '/../../../../../generator/lib/util/PropelQuickBuilder.php';
  11. require_once dirname(__FILE__) . '/../../../../../generator/lib/behavior/i18n/I18nBehavior.php';
  12. require_once dirname(__FILE__) . '/../../../../../runtime/lib/Propel.php';
  13. /**
  14. * Tests for I18nBehavior class object modifier
  15. *
  16. * @author François Zaninotto
  17. * @version $Revision$
  18. * @package generator.behavior.i18n
  19. */
  20. class I18nBehaviorObjectBuilderModifierTest extends PHPUnit_Framework_TestCase
  21. {
  22. public function setUp()
  23. {
  24. if (!class_exists('I18nBehaviorTest1')) {
  25. $schema = <<<EOF
  26. <database name="i18n_behavior_test_1">
  27. <table name="i18n_behavior_test_1">
  28. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
  29. <column name="foo" type="INTEGER" />
  30. <column name="bar" type="VARCHAR" size="100" />
  31. <behavior name="i18n">
  32. <parameter name="i18n_columns" value="bar" />
  33. </behavior>
  34. </table>
  35. <table name="i18n_behavior_test_2">
  36. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
  37. <column name="foo" type="INTEGER" />
  38. <column name="bar1" type="VARCHAR" size="100" />
  39. <column name="bar2" type="LONGVARCHAR" lazyLoad="true" />
  40. <column name="bar3" type="TIMESTAMP" />
  41. <column name="bar4" type="LONGVARCHAR" description="This is the Bar4 column" />
  42. <behavior name="i18n">
  43. <parameter name="i18n_columns" value="bar1,bar2,bar3,bar4" />
  44. <parameter name="default_locale" value="fr_FR" />
  45. <parameter name="locale_alias" value="culture" />
  46. </behavior>
  47. </table>
  48. <table name="movie">
  49. <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true" />
  50. <column name="director" type="varchar" size="255" />
  51. <column name="title" type="varchar" primaryString="true" />
  52. <behavior name="i18n">
  53. <parameter name="i18n_columns" value="title" />
  54. <parameter name="locale_alias" value="culture" />
  55. </behavior>
  56. </table>
  57. <table name="toy">
  58. <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true" />
  59. <column name="ref" type="varchar" size="255" />
  60. <column name="name" type="varchar" size="255" />
  61. <behavior name="i18n">
  62. <parameter name="i18n_columns" value="name" />
  63. <parameter name="locale_alias" value="culture" />
  64. </behavior>
  65. <column name="movie_id" type="integer" />
  66. <foreign-key foreignTable="movie">
  67. <reference local="movie_id" foreign="id" />
  68. </foreign-key>
  69. </table>
  70. </database>
  71. EOF;
  72. PropelQuickBuilder::buildSchema($schema);
  73. }
  74. }
  75. public function testPostDeleteEmulatesOnDeleteCascade()
  76. {
  77. I18nBehaviorTest1Query::create()->deleteAll();
  78. I18nBehaviorTest1I18nQuery::create()->deleteAll();
  79. $o = new I18nBehaviorTest1();
  80. $o->setFoo(123);
  81. $o->setLocale('en_EN');
  82. $o->setBar('hello');
  83. $o->setLocale('fr_FR');
  84. $o->setBar('bonjour');
  85. $o->save();
  86. $this->assertEquals(2, I18nBehaviorTest1I18nQuery::create()->count());
  87. $o->clearI18nBehaviorTest1I18ns();
  88. $o->delete();
  89. $this->assertEquals(0, I18nBehaviorTest1I18nQuery::create()->count());
  90. }
  91. public function testGetTranslationReturnsTranslationObject()
  92. {
  93. $o = new I18nBehaviorTest1();
  94. $translation = $o->getTranslation();
  95. $this->assertTrue($translation instanceof I18nBehaviorTest1I18n);
  96. }
  97. public function testGetTranslationOnNewObjectReturnsNewTranslation()
  98. {
  99. $o = new I18nBehaviorTest1();
  100. $translation = $o->getTranslation();
  101. $this->assertTrue($translation->isNew());
  102. }
  103. public function testGetTranslationOnPersistedObjectReturnsNewTranslation()
  104. {
  105. $o = new I18nBehaviorTest1();
  106. $o->save();
  107. $translation = $o->getTranslation();
  108. $this->assertTrue($translation->isNew());
  109. }
  110. public function testGetTranslationOnPersistedObjectWithTranslationReturnsExistingTranslation()
  111. {
  112. $o = new I18nBehaviorTest1();
  113. $translation = new I18nBehaviorTest1I18n();
  114. $o->addI18nBehaviorTest1I18n($translation);
  115. $o->save();
  116. $translation = $o->getTranslation();
  117. $this->assertFalse($translation->isNew());
  118. }
  119. public function testGetTranslationAcceptsALocaleParameter()
  120. {
  121. $o = new I18nBehaviorTest1();
  122. $translation1 = new I18nBehaviorTest1I18n();
  123. $translation1->setLocale('en_EN');
  124. $o->addI18nBehaviorTest1I18n($translation1);
  125. $translation2 = new I18nBehaviorTest1I18n();
  126. $translation2->setLocale('fr_FR');
  127. $o->addI18nBehaviorTest1I18n($translation2);
  128. $o->save();
  129. $this->assertEquals($translation1, $o->getTranslation('en_EN'));
  130. $this->assertEquals($translation2, $o->getTranslation('fr_FR'));
  131. }
  132. public function testGetTranslationSetsTheLocaleOnTheTranslation()
  133. {
  134. $o = new I18nBehaviorTest1();
  135. $o->save();
  136. $translation = $o->getTranslation();
  137. $this->assertEquals('en_EN', $translation->getLocale());
  138. $o = new I18nBehaviorTest2();
  139. $o->save();
  140. $translation = $o->getTranslation();
  141. $this->assertEquals('fr_FR', $translation->getLocale());
  142. }
  143. public function testGetTranslationUsesInternalCollectionIfAvailable()
  144. {
  145. $o = new I18nBehaviorTest1();
  146. $translation1 = new I18nBehaviorTest1I18n();
  147. $translation1->setLocale('en_EN');
  148. $o->addI18nBehaviorTest1I18n($translation1);
  149. $translation2 = new I18nBehaviorTest1I18n();
  150. $translation2->setLocale('fr_FR');
  151. $o->addI18nBehaviorTest1I18n($translation2);
  152. $translation = $o->getTranslation('en_EN');
  153. $this->assertEquals($translation1, $translation);
  154. }
  155. public function testRemoveTranslation()
  156. {
  157. $o = new I18nBehaviorTest1();
  158. $translation1 = new I18nBehaviorTest1I18n();
  159. $translation1->setLocale('en_EN');
  160. $o->addI18nBehaviorTest1I18n($translation1);
  161. $translation2 = new I18nBehaviorTest1I18n();
  162. $translation2->setLocale('fr_FR');
  163. $translation2->setBar('bonjour');
  164. $o->addI18nBehaviorTest1I18n($translation2);
  165. $o->save();
  166. $this->assertEquals(2, $o->countI18nBehaviorTest1I18ns());
  167. $o->removeTranslation('fr_FR');
  168. $this->assertEquals(1, $o->countI18nBehaviorTest1I18ns());
  169. $translation = $o->getTranslation('fr_FR');
  170. $this->assertNotEquals($translation->getBar(), $translation2->getBar());
  171. }
  172. public function testLocaleSetterAndGetterExist()
  173. {
  174. $this->assertTrue(method_exists('I18nBehaviorTest1', 'setLocale'));
  175. $this->assertTrue(method_exists('I18nBehaviorTest1', 'getLocale'));
  176. }
  177. public function testGetLocaleReturnsDefaultLocale()
  178. {
  179. $o = new I18nBehaviorTest1();
  180. $this->assertEquals('en_EN', $o->getLocale());
  181. $o = new I18nBehaviorTest2();
  182. $this->assertEquals('fr_FR', $o->getLocale());
  183. }
  184. public function testSetLocale()
  185. {
  186. $o = new I18nBehaviorTest1();
  187. $o->setLocale('fr_FR');
  188. $this->assertEquals('fr_FR', $o->getLocale());
  189. }
  190. public function testSetLocaleUsesDefaultLocale()
  191. {
  192. $o = new I18nBehaviorTest1();
  193. $o->setLocale('fr_FR');
  194. $o->setLocale();
  195. $this->assertEquals('en_EN', $o->getLocale());
  196. }
  197. public function testLocaleSetterAndGetterAliasesExist()
  198. {
  199. $this->assertTrue(method_exists('I18nBehaviorTest2', 'setCulture'));
  200. $this->assertTrue(method_exists('I18nBehaviorTest2', 'getCulture'));
  201. }
  202. public function testGetLocaleAliasReturnsDefaultLocale()
  203. {
  204. $o = new I18nBehaviorTest2();
  205. $this->assertEquals('fr_FR', $o->getCulture());
  206. }
  207. public function testSetLocaleAlias()
  208. {
  209. $o = new I18nBehaviorTest2();
  210. $o->setCulture('en_EN');
  211. $this->assertEquals('en_EN', $o->getCulture());
  212. }
  213. public function testGetCurrentTranslationUsesDefaultLocale()
  214. {
  215. $o = new I18nBehaviorTest1();
  216. $t = $o->getCurrentTranslation();
  217. $this->assertEquals('en_EN', $t->getLocale());
  218. $o = new I18nBehaviorTest2();
  219. $t = $o->getCurrentTranslation();
  220. $this->assertEquals('fr_FR', $t->getLocale());
  221. }
  222. public function testGetCurrentTranslationUsesCurrentLocale()
  223. {
  224. $o = new I18nBehaviorTest1();
  225. $o->setLocale('fr_FR');
  226. $this->assertEquals('fr_FR', $o->getCurrentTranslation()->getLocale());
  227. $o->setLocale('pt_PT');
  228. $this->assertEquals('pt_PT', $o->getCurrentTranslation()->getLocale());
  229. }
  230. public function testI18nColumnGetterUsesCurrentTranslation()
  231. {
  232. $o = new I18nBehaviorTest1();
  233. $t1 = $o->getCurrentTranslation();
  234. $t1->setBar('hello');
  235. $o->setLocale('fr_FR');
  236. $t2 = $o->getCurrentTranslation();
  237. $t2->setBar('bonjour');
  238. //$o->save();
  239. $o->setLocale('en_EN');
  240. $this->assertEquals('hello', $o->getBar());
  241. $o->setLocale('fr_FR');
  242. $this->assertEquals('bonjour', $o->getBar());
  243. }
  244. public function testI18nColumnSetterUsesCurrentTranslation()
  245. {
  246. $o = new I18nBehaviorTest1();
  247. $o->setBar('hello');
  248. $o->setLocale('fr_FR');
  249. $o->setBar('bonjour');
  250. $o->setLocale('en_EN');
  251. $this->assertEquals('hello', $o->getBar());
  252. $o->setLocale('fr_FR');
  253. $this->assertEquals('bonjour', $o->getBar());
  254. }
  255. public function testTranslationsArePersisted()
  256. {
  257. $o = new I18nBehaviorTest1();
  258. $o->save();
  259. $count = I18nBehaviorTest1I18nQuery::create()
  260. ->filterById($o->getId())
  261. ->count();
  262. $this->assertEquals(0, $count);
  263. $o->setBar('hello');
  264. $o->setLocale('fr_FR');
  265. $o->setBar('bonjour');
  266. $o->save();
  267. $count = I18nBehaviorTest1I18nQuery::create()
  268. ->filterById($o->getId())
  269. ->count();
  270. $this->assertEquals(2, $count);
  271. }
  272. public function testClearRemovesExistingTranlsations()
  273. {
  274. $o = new I18nBehaviorTest1();
  275. $translation1 = new I18nBehaviorTest1I18n();
  276. $translation1->setBar('baz');
  277. $translation1->setLocale('fr_FR');
  278. $o->addI18nBehaviorTest1I18n($translation1);
  279. $o->clear();
  280. $this->assertEquals('en_EN', $o->getLocale());
  281. $t1 = $o->getTranslation('fr_FR');
  282. $this->assertEquals('', $t1->getBar());
  283. }
  284. public function testI18nWithRelations()
  285. {
  286. MovieQuery::create()->deleteAll();
  287. $count = MovieQuery::create()->count();
  288. $this->assertEquals(0, $count, 'No movie before the test');
  289. ToyQuery::create()->deleteAll();
  290. $count = ToyQuery::create()->count();
  291. $this->assertEquals(0, $count, 'No toy before the test');
  292. MovieI18nQuery::create()->deleteAll();
  293. $count = MovieI18nQuery::create()->count();
  294. $this->assertEquals(0, $count, 'No i18n movies before the test');
  295. $m = new Movie();
  296. $m->setLocale('en');
  297. $m->setTitle('V For Vendetta');
  298. $m->setLocale('fr');
  299. $m->setTitle('V Pour Vendetta');
  300. $m->setLocale('en');
  301. $this->assertEquals('V For Vendetta', $m->getTitle());
  302. $m->setLocale('fr');
  303. $this->assertEquals('V Pour Vendetta', $m->getTitle());
  304. $t = new Toy();
  305. $t->setMovie($m);
  306. $t->save();
  307. $count = MovieQuery::create()->count();
  308. $this->assertEquals(1, $count, '1 movie');
  309. $count = ToyQuery::create()->count();
  310. $this->assertEquals(1, $count, '1 toy');
  311. $count = MovieI18nQuery::create()->count();
  312. $this->assertEquals(2, $count, '2 i18n movies');
  313. $count = ToyI18nQuery::create()->count();
  314. $this->assertEquals(0, $count, '0 i18n toys');
  315. }
  316. public function testI18nWithRelations2()
  317. {
  318. MovieQuery::create()->deleteAll();
  319. $count = MovieQuery::create()->count();
  320. $this->assertEquals(0, $count, 'No movie before the test');
  321. ToyQuery::create()->deleteAll();
  322. $count = ToyQuery::create()->count();
  323. $this->assertEquals(0, $count, 'No toy before the test');
  324. ToyI18nQuery::create()->deleteAll();
  325. $count = ToyI18nQuery::create()->count();
  326. $this->assertEquals(0, $count, 'No i18n toys before the test');
  327. MovieI18nQuery::create()->deleteAll();
  328. $count = MovieI18nQuery::create()->count();
  329. $this->assertEquals(0, $count, 'No i18n movies before the test');
  330. $t = new Toy();
  331. $t->setLocale('en');
  332. $t->setName('My Name');
  333. $t->setLocale('fr');
  334. $t->setName('Mon Nom');
  335. $t->setLocale('en');
  336. $this->assertEquals('My Name', $t->getName());
  337. $t->setLocale('fr');
  338. $this->assertEquals('Mon Nom', $t->getName());
  339. $m = new Movie();
  340. $m->addToy($t);
  341. $m->save();
  342. $count = MovieQuery::create()->count();
  343. $this->assertEquals(1, $count, '1 movie');
  344. $count = ToyQuery::create()->count();
  345. $this->assertEquals(1, $count, '1 toy');
  346. $count = ToyI18nQuery::create()->count();
  347. $this->assertEquals(2, $count, '2 i18n toys');
  348. $count = MovieI18nQuery::create()->count();
  349. $this->assertEquals(0, $count, '0 i18n movies');
  350. }
  351. }