PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/propelorm/Propel
PHP | 410 lines | 357 code | 37 blank | 16 comment | 1 complexity | a9303c4efb0ae4123637a7d2a73b17fd 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. <table name="i18n_behavior_test_local_column">
  71. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
  72. <column name="foo" type="INTEGER" />
  73. <column name="bar" type="VARCHAR" size="100" />
  74. <behavior name="i18n">
  75. <parameter name="i18n_columns" value="bar" />
  76. <parameter name="locale_column" value="my_lang" />
  77. </behavior>
  78. </table>
  79. </database>
  80. EOF;
  81. PropelQuickBuilder::buildSchema($schema);
  82. }
  83. }
  84. public function testPostDeleteEmulatesOnDeleteCascade()
  85. {
  86. I18nBehaviorTest1Query::create()->deleteAll();
  87. I18nBehaviorTest1I18nQuery::create()->deleteAll();
  88. $o = new I18nBehaviorTest1();
  89. $o->setFoo(123);
  90. $o->setLocale('en_US');
  91. $o->setBar('hello');
  92. $o->setLocale('fr_FR');
  93. $o->setBar('bonjour');
  94. $o->save();
  95. $this->assertEquals(2, I18nBehaviorTest1I18nQuery::create()->count());
  96. $o->clearI18nBehaviorTest1I18ns();
  97. $o->delete();
  98. $this->assertEquals(0, I18nBehaviorTest1I18nQuery::create()->count());
  99. }
  100. public function testGetTranslationReturnsTranslationObject()
  101. {
  102. $o = new I18nBehaviorTest1();
  103. $translation = $o->getTranslation();
  104. $this->assertTrue($translation instanceof I18nBehaviorTest1I18n);
  105. }
  106. public function testGetTranslationOnNewObjectReturnsNewTranslation()
  107. {
  108. $o = new I18nBehaviorTest1();
  109. $translation = $o->getTranslation();
  110. $this->assertTrue($translation->isNew());
  111. }
  112. public function testGetTranslationOnPersistedObjectReturnsNewTranslation()
  113. {
  114. $o = new I18nBehaviorTest1();
  115. $o->save();
  116. $translation = $o->getTranslation();
  117. $this->assertTrue($translation->isNew());
  118. }
  119. public function testGetTranslationOnPersistedObjectWithTranslationReturnsExistingTranslation()
  120. {
  121. $o = new I18nBehaviorTest1();
  122. $translation = new I18nBehaviorTest1I18n();
  123. $o->addI18nBehaviorTest1I18n($translation);
  124. $o->save();
  125. $translation = $o->getTranslation();
  126. $this->assertFalse($translation->isNew());
  127. }
  128. public function testGetTranslationAcceptsALocaleParameter()
  129. {
  130. $o = new I18nBehaviorTest1();
  131. $translation1 = new I18nBehaviorTest1I18n();
  132. $translation1->setLocale('en_US');
  133. $o->addI18nBehaviorTest1I18n($translation1);
  134. $translation2 = new I18nBehaviorTest1I18n();
  135. $translation2->setLocale('fr_FR');
  136. $o->addI18nBehaviorTest1I18n($translation2);
  137. $o->save();
  138. $this->assertEquals($translation1, $o->getTranslation('en_US'));
  139. $this->assertEquals($translation2, $o->getTranslation('fr_FR'));
  140. }
  141. public function testGetTranslationSetsTheLocaleOnTheTranslation()
  142. {
  143. $o = new I18nBehaviorTest1();
  144. $o->save();
  145. $translation = $o->getTranslation();
  146. $this->assertEquals('en_US', $translation->getLocale());
  147. $o = new I18nBehaviorTest2();
  148. $o->save();
  149. $translation = $o->getTranslation();
  150. $this->assertEquals('fr_FR', $translation->getLocale());
  151. }
  152. public function testGetTranslationUsesInternalCollectionIfAvailable()
  153. {
  154. $o = new I18nBehaviorTest1();
  155. $translation1 = new I18nBehaviorTest1I18n();
  156. $translation1->setLocale('en_US');
  157. $o->addI18nBehaviorTest1I18n($translation1);
  158. $translation2 = new I18nBehaviorTest1I18n();
  159. $translation2->setLocale('fr_FR');
  160. $o->addI18nBehaviorTest1I18n($translation2);
  161. $translation = $o->getTranslation('en_US');
  162. $this->assertEquals($translation1, $translation);
  163. }
  164. public function testRemoveTranslation()
  165. {
  166. $o = new I18nBehaviorTest1();
  167. $translation1 = new I18nBehaviorTest1I18n();
  168. $translation1->setLocale('en_US');
  169. $o->addI18nBehaviorTest1I18n($translation1);
  170. $translation2 = new I18nBehaviorTest1I18n();
  171. $translation2->setLocale('fr_FR');
  172. $translation2->setBar('bonjour');
  173. $o->addI18nBehaviorTest1I18n($translation2);
  174. $o->save();
  175. $this->assertEquals(2, $o->countI18nBehaviorTest1I18ns());
  176. $o->removeTranslation('fr_FR');
  177. $this->assertEquals(1, $o->countI18nBehaviorTest1I18ns());
  178. $translation = $o->getTranslation('fr_FR');
  179. $this->assertNotEquals($translation->getBar(), $translation2->getBar());
  180. }
  181. public function testLocaleSetterAndGetterExist()
  182. {
  183. $this->assertTrue(method_exists('I18nBehaviorTest1', 'setLocale'));
  184. $this->assertTrue(method_exists('I18nBehaviorTest1', 'getLocale'));
  185. }
  186. public function testGetLocaleReturnsDefaultLocale()
  187. {
  188. $o = new I18nBehaviorTest1();
  189. $this->assertEquals('en_US', $o->getLocale());
  190. $o = new I18nBehaviorTest2();
  191. $this->assertEquals('fr_FR', $o->getLocale());
  192. }
  193. public function testSetLocale()
  194. {
  195. $o = new I18nBehaviorTest1();
  196. $o->setLocale('fr_FR');
  197. $this->assertEquals('fr_FR', $o->getLocale());
  198. }
  199. public function testSetLocaleUsesDefaultLocale()
  200. {
  201. $o = new I18nBehaviorTest1();
  202. $o->setLocale('fr_FR');
  203. $o->setLocale();
  204. $this->assertEquals('en_US', $o->getLocale());
  205. }
  206. public function testLocaleSetterAndGetterAliasesExist()
  207. {
  208. $this->assertTrue(method_exists('I18nBehaviorTest2', 'setCulture'));
  209. $this->assertTrue(method_exists('I18nBehaviorTest2', 'getCulture'));
  210. }
  211. public function testGetLocaleAliasReturnsDefaultLocale()
  212. {
  213. $o = new I18nBehaviorTest2();
  214. $this->assertEquals('fr_FR', $o->getCulture());
  215. }
  216. public function testSetLocaleAlias()
  217. {
  218. $o = new I18nBehaviorTest2();
  219. $o->setCulture('en_US');
  220. $this->assertEquals('en_US', $o->getCulture());
  221. }
  222. public function testGetCurrentTranslationUsesDefaultLocale()
  223. {
  224. $o = new I18nBehaviorTest1();
  225. $t = $o->getCurrentTranslation();
  226. $this->assertEquals('en_US', $t->getLocale());
  227. $o = new I18nBehaviorTest2();
  228. $t = $o->getCurrentTranslation();
  229. $this->assertEquals('fr_FR', $t->getLocale());
  230. }
  231. public function testGetCurrentTranslationUsesCurrentLocale()
  232. {
  233. $o = new I18nBehaviorTest1();
  234. $o->setLocale('fr_FR');
  235. $this->assertEquals('fr_FR', $o->getCurrentTranslation()->getLocale());
  236. $o->setLocale('pt_PT');
  237. $this->assertEquals('pt_PT', $o->getCurrentTranslation()->getLocale());
  238. }
  239. public function testI18nColumnGetterUsesCurrentTranslation()
  240. {
  241. $o = new I18nBehaviorTest1();
  242. $t1 = $o->getCurrentTranslation();
  243. $t1->setBar('hello');
  244. $o->setLocale('fr_FR');
  245. $t2 = $o->getCurrentTranslation();
  246. $t2->setBar('bonjour');
  247. //$o->save();
  248. $o->setLocale('en_US');
  249. $this->assertEquals('hello', $o->getBar());
  250. $o->setLocale('fr_FR');
  251. $this->assertEquals('bonjour', $o->getBar());
  252. }
  253. public function testI18nColumnSetterUsesCurrentTranslation()
  254. {
  255. $o = new I18nBehaviorTest1();
  256. $o->setBar('hello');
  257. $o->setLocale('fr_FR');
  258. $o->setBar('bonjour');
  259. $o->setLocale('en_US');
  260. $this->assertEquals('hello', $o->getBar());
  261. $o->setLocale('fr_FR');
  262. $this->assertEquals('bonjour', $o->getBar());
  263. }
  264. public function testTranslationsArePersisted()
  265. {
  266. $o = new I18nBehaviorTest1();
  267. $o->save();
  268. $count = I18nBehaviorTest1I18nQuery::create()
  269. ->filterById($o->getId())
  270. ->count();
  271. $this->assertEquals(0, $count);
  272. $o->setBar('hello');
  273. $o->setLocale('fr_FR');
  274. $o->setBar('bonjour');
  275. $o->save();
  276. $count = I18nBehaviorTest1I18nQuery::create()
  277. ->filterById($o->getId())
  278. ->count();
  279. $this->assertEquals(2, $count);
  280. }
  281. public function testClearRemovesExistingTranslations()
  282. {
  283. $o = new I18nBehaviorTest1();
  284. $translation1 = new I18nBehaviorTest1I18n();
  285. $translation1->setBar('baz');
  286. $translation1->setLocale('fr_FR');
  287. $o->addI18nBehaviorTest1I18n($translation1);
  288. $o->clear();
  289. $this->assertEquals('en_US', $o->getLocale());
  290. $t1 = $o->getTranslation('fr_FR');
  291. $this->assertEquals('', $t1->getBar());
  292. }
  293. public function testI18nWithRelations()
  294. {
  295. MovieQuery::create()->deleteAll();
  296. $count = MovieQuery::create()->count();
  297. $this->assertEquals(0, $count, 'No movie before the test');
  298. ToyQuery::create()->deleteAll();
  299. $count = ToyQuery::create()->count();
  300. $this->assertEquals(0, $count, 'No toy before the test');
  301. MovieI18nQuery::create()->deleteAll();
  302. $count = MovieI18nQuery::create()->count();
  303. $this->assertEquals(0, $count, 'No i18n movies before the test');
  304. $m = new Movie();
  305. $m->setLocale('en');
  306. $m->setTitle('V For Vendetta');
  307. $m->setLocale('fr');
  308. $m->setTitle('V Pour Vendetta');
  309. $m->setLocale('en');
  310. $this->assertEquals('V For Vendetta', $m->getTitle());
  311. $m->setLocale('fr');
  312. $this->assertEquals('V Pour Vendetta', $m->getTitle());
  313. $t = new Toy();
  314. $t->setMovie($m);
  315. $t->save();
  316. $count = MovieQuery::create()->count();
  317. $this->assertEquals(1, $count, '1 movie');
  318. $count = ToyQuery::create()->count();
  319. $this->assertEquals(1, $count, '1 toy');
  320. $count = MovieI18nQuery::create()->count();
  321. $this->assertEquals(2, $count, '2 i18n movies');
  322. $count = ToyI18nQuery::create()->count();
  323. $this->assertEquals(0, $count, '0 i18n toys');
  324. }
  325. public function testI18nWithRelations2()
  326. {
  327. MovieQuery::create()->deleteAll();
  328. $count = MovieQuery::create()->count();
  329. $this->assertEquals(0, $count, 'No movie before the test');
  330. ToyQuery::create()->deleteAll();
  331. $count = ToyQuery::create()->count();
  332. $this->assertEquals(0, $count, 'No toy before the test');
  333. ToyI18nQuery::create()->deleteAll();
  334. $count = ToyI18nQuery::create()->count();
  335. $this->assertEquals(0, $count, 'No i18n toys before the test');
  336. MovieI18nQuery::create()->deleteAll();
  337. $count = MovieI18nQuery::create()->count();
  338. $this->assertEquals(0, $count, 'No i18n movies before the test');
  339. $t = new Toy();
  340. $t->setLocale('en');
  341. $t->setName('My Name');
  342. $t->setLocale('fr');
  343. $t->setName('Mon Nom');
  344. $t->setLocale('en');
  345. $this->assertEquals('My Name', $t->getName());
  346. $t->setLocale('fr');
  347. $this->assertEquals('Mon Nom', $t->getName());
  348. $m = new Movie();
  349. $m->addToy($t);
  350. $m->save();
  351. $count = MovieQuery::create()->count();
  352. $this->assertEquals(1, $count, '1 movie');
  353. $count = ToyQuery::create()->count();
  354. $this->assertEquals(1, $count, '1 toy');
  355. $count = ToyI18nQuery::create()->count();
  356. $this->assertEquals(2, $count, '2 i18n toys');
  357. $count = MovieI18nQuery::create()->count();
  358. $this->assertEquals(0, $count, '0 i18n movies');
  359. }
  360. public function testUseLocalColumnParameter()
  361. {
  362. $o = new I18nBehaviorTestLocalColumn();
  363. $translation1 = new I18nBehaviorTestLocalColumnI18n();
  364. $translation1->setMyLang('en_US');
  365. $o->addI18nBehaviorTestLocalColumnI18n($translation1);
  366. $translation2 = new I18nBehaviorTestLocalColumnI18n();
  367. $translation2->setMyLang('fr_FR');
  368. $o->addI18nBehaviorTestLocalColumnI18n($translation2);
  369. $o->save();
  370. $this->assertEquals($translation1, $o->getTranslation('en_US'));
  371. $this->assertEquals($translation2, $o->getTranslation('fr_FR'));
  372. }
  373. }