PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Propel/Tests/Generator/Behavior/I18n/I18nBehaviorObjectBuilderModifierTest.php

http://github.com/propelorm/Propel2
PHP | 499 lines | 371 code | 39 blank | 89 comment | 1 complexity | 192bef41c617f59929135d24d156be3b MD5 | raw file
  1. <?php
  2. /**
  3. * MIT License. This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. */
  7. namespace Propel\Tests\Generator\Behavior\I18n;
  8. use I18nBehaviorTest1;
  9. use I18nBehaviorTest1I18n;
  10. use I18nBehaviorTest1I18nQuery;
  11. use I18nBehaviorTest1Query;
  12. use I18nBehaviorTest2;
  13. use I18nBehaviorTestLocalColumn;
  14. use I18nBehaviorTestLocalColumnI18n;
  15. use Movie;
  16. use MovieI18nQuery;
  17. use MovieQuery;
  18. use Propel\Generator\Util\QuickBuilder;
  19. use Propel\Tests\TestCase;
  20. use Toy;
  21. use ToyI18nQuery;
  22. use ToyQuery;
  23. /**
  24. * Tests for I18nBehavior class object modifier
  25. *
  26. * @author François Zaninotto
  27. */
  28. class I18nBehaviorObjectBuilderModifierTest extends TestCase
  29. {
  30. /**
  31. * @return void
  32. */
  33. public function setUp(): void
  34. {
  35. if (!class_exists('\I18nBehaviorTest1')) {
  36. $schema = <<<EOF
  37. <database name="i18n_behavior_test_1">
  38. <table name="i18n_behavior_test_1">
  39. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/>
  40. <column name="foo" type="INTEGER"/>
  41. <column name="bar" type="VARCHAR" size="100"/>
  42. <behavior name="i18n">
  43. <parameter name="i18n_columns" value="bar"/>
  44. </behavior>
  45. </table>
  46. <table name="i18n_behavior_test_2">
  47. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/>
  48. <column name="foo" type="INTEGER"/>
  49. <column name="bar1" type="VARCHAR" size="100"/>
  50. <column name="bar2" type="LONGVARCHAR" lazyLoad="true"/>
  51. <column name="bar3" type="TIMESTAMP"/>
  52. <column name="bar4" type="LONGVARCHAR" description="This is the Bar4 column"/>
  53. <behavior name="i18n">
  54. <parameter name="i18n_columns" value="bar1,bar2,bar3,bar4"/>
  55. <parameter name="default_locale" value="fr_FR"/>
  56. <parameter name="locale_alias" value="culture"/>
  57. </behavior>
  58. </table>
  59. <table name="movie">
  60. <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true"/>
  61. <column name="director" type="varchar" size="255"/>
  62. <column name="title" type="varchar" primaryString="true"/>
  63. <behavior name="i18n">
  64. <parameter name="i18n_columns" value="title"/>
  65. <parameter name="locale_alias" value="culture"/>
  66. </behavior>
  67. </table>
  68. <table name="toy">
  69. <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true"/>
  70. <column name="ref" type="varchar" size="255"/>
  71. <column name="name" type="varchar" size="255"/>
  72. <behavior name="i18n">
  73. <parameter name="i18n_columns" value="name"/>
  74. <parameter name="locale_alias" value="culture"/>
  75. </behavior>
  76. <column name="movie_id" type="integer"/>
  77. <foreign-key foreignTable="movie" onDelete="cascade">
  78. <reference local="movie_id" foreign="id"/>
  79. </foreign-key>
  80. </table>
  81. <table name="i18n_behavior_test_local_column">
  82. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/>
  83. <column name="foo" type="INTEGER"/>
  84. <column name="bar" type="VARCHAR" size="100"/>
  85. <behavior name="i18n">
  86. <parameter name="i18n_columns" value="bar"/>
  87. <parameter name="locale_column" value="my_lang"/>
  88. </behavior>
  89. </table>
  90. </database>
  91. EOF;
  92. $this->con = QuickBuilder::buildSchema($schema);
  93. }
  94. }
  95. /**
  96. * @return void
  97. */
  98. public function testPostDeleteEmulatesOnDeleteCascade()
  99. {
  100. I18nBehaviorTest1Query::create()->deleteAll();
  101. I18nBehaviorTest1I18nQuery::create()->deleteAll();
  102. $o = new I18nBehaviorTest1();
  103. $o->setFoo(123);
  104. $o->setLocale('en_US');
  105. $o->setBar('hello');
  106. $o->setLocale('fr_FR');
  107. $o->setBar('bonjour');
  108. $o->save();
  109. $this->assertEquals(2, I18nBehaviorTest1I18nQuery::create()->count());
  110. $o->clearI18nBehaviorTest1I18ns();
  111. $o->delete();
  112. $this->assertEquals(0, I18nBehaviorTest1I18nQuery::create()->count());
  113. }
  114. /**
  115. * @return void
  116. */
  117. public function testGetTranslationReturnsTranslationObject()
  118. {
  119. $o = new I18nBehaviorTest1();
  120. $translation = $o->getTranslation();
  121. $this->assertTrue($translation instanceof I18nBehaviorTest1I18n);
  122. }
  123. /**
  124. * @return void
  125. */
  126. public function testGetTranslationOnNewObjectReturnsNewTranslation()
  127. {
  128. $o = new I18nBehaviorTest1();
  129. $translation = $o->getTranslation();
  130. $this->assertTrue($translation->isNew());
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function testGetTranslationOnPersistedObjectReturnsNewTranslation()
  136. {
  137. $o = new I18nBehaviorTest1();
  138. $o->save();
  139. $translation = $o->getTranslation();
  140. $this->assertTrue($translation->isNew());
  141. }
  142. /**
  143. * @return void
  144. */
  145. public function testGetTranslationOnPersistedObjectWithTranslationReturnsExistingTranslation()
  146. {
  147. $o = new I18nBehaviorTest1();
  148. $translation = new I18nBehaviorTest1I18n();
  149. $o->addI18nBehaviorTest1I18n($translation);
  150. $o->save();
  151. $translation = $o->getTranslation();
  152. $this->assertFalse($translation->isNew());
  153. }
  154. /**
  155. * @return void
  156. */
  157. public function testGetTranslationAcceptsALocaleParameter()
  158. {
  159. $o = new I18nBehaviorTest1();
  160. $translation1 = new I18nBehaviorTest1I18n();
  161. $translation1->setLocale('en_US');
  162. $o->addI18nBehaviorTest1I18n($translation1);
  163. $translation2 = new I18nBehaviorTest1I18n();
  164. $translation2->setLocale('fr_FR');
  165. $o->addI18nBehaviorTest1I18n($translation2);
  166. $o->save();
  167. $this->assertEquals($translation1, $o->getTranslation('en_US'));
  168. $this->assertEquals($translation2, $o->getTranslation('fr_FR'));
  169. }
  170. /**
  171. * @return void
  172. */
  173. public function testGetTranslationSetsTheLocaleOnTheTranslation()
  174. {
  175. $o = new I18nBehaviorTest1();
  176. $o->save();
  177. $translation = $o->getTranslation();
  178. $this->assertEquals('en_US', $translation->getLocale());
  179. $o = new I18nBehaviorTest2();
  180. $o->save();
  181. $translation = $o->getTranslation();
  182. $this->assertEquals('fr_FR', $translation->getLocale());
  183. }
  184. /**
  185. * @return void
  186. */
  187. public function testGetTranslationUsesInternalCollectionIfAvailable()
  188. {
  189. $o = new I18nBehaviorTest1();
  190. $translation1 = new I18nBehaviorTest1I18n();
  191. $translation1->setLocale('en_US');
  192. $o->addI18nBehaviorTest1I18n($translation1);
  193. $translation2 = new I18nBehaviorTest1I18n();
  194. $translation2->setLocale('fr_FR');
  195. $o->addI18nBehaviorTest1I18n($translation2);
  196. $translation = $o->getTranslation('en_US');
  197. $this->assertEquals($translation1, $translation);
  198. }
  199. /**
  200. * @return void
  201. */
  202. public function testRemoveTranslation()
  203. {
  204. $o = new I18nBehaviorTest1();
  205. $translation1 = new I18nBehaviorTest1I18n();
  206. $translation1->setLocale('en_US');
  207. $o->addI18nBehaviorTest1I18n($translation1);
  208. $translation2 = new I18nBehaviorTest1I18n();
  209. $translation2->setLocale('fr_FR');
  210. $translation2->setBar('bonjour');
  211. $o->addI18nBehaviorTest1I18n($translation2);
  212. $o->save();
  213. $this->assertEquals(2, $o->countI18nBehaviorTest1I18ns());
  214. $o->removeTranslation('fr_FR');
  215. $this->assertEquals(1, $o->countI18nBehaviorTest1I18ns());
  216. $translation = $o->getTranslation('fr_FR');
  217. $this->assertNotEquals($translation->getBar(), $translation2->getBar());
  218. }
  219. /**
  220. * @return void
  221. */
  222. public function testLocaleSetterAndGetterExist()
  223. {
  224. $this->assertTrue(method_exists('\I18nBehaviorTest1', 'setLocale'));
  225. $this->assertTrue(method_exists('\I18nBehaviorTest1', 'getLocale'));
  226. }
  227. /**
  228. * @return void
  229. */
  230. public function testGetLocaleReturnsDefaultLocale()
  231. {
  232. $o = new I18nBehaviorTest1();
  233. $this->assertEquals('en_US', $o->getLocale());
  234. $o = new I18nBehaviorTest2();
  235. $this->assertEquals('fr_FR', $o->getLocale());
  236. }
  237. /**
  238. * @return void
  239. */
  240. public function testSetLocale()
  241. {
  242. $o = new I18nBehaviorTest1();
  243. $o->setLocale('fr_FR');
  244. $this->assertEquals('fr_FR', $o->getLocale());
  245. }
  246. /**
  247. * @return void
  248. */
  249. public function testSetLocaleUsesDefaultLocale()
  250. {
  251. $o = new I18nBehaviorTest1();
  252. $o->setLocale('fr_FR');
  253. $o->setLocale();
  254. $this->assertEquals('en_US', $o->getLocale());
  255. }
  256. /**
  257. * @return void
  258. */
  259. public function testLocaleSetterAndGetterAliasesExist()
  260. {
  261. $this->assertTrue(method_exists('\I18nBehaviorTest2', 'setCulture'));
  262. $this->assertTrue(method_exists('\I18nBehaviorTest2', 'getCulture'));
  263. }
  264. /**
  265. * @return void
  266. */
  267. public function testGetLocaleAliasReturnsDefaultLocale()
  268. {
  269. $o = new I18nBehaviorTest2();
  270. $this->assertEquals('fr_FR', $o->getCulture());
  271. }
  272. /**
  273. * @return void
  274. */
  275. public function testSetLocaleAlias()
  276. {
  277. $o = new I18nBehaviorTest2();
  278. $o->setCulture('en_US');
  279. $this->assertEquals('en_US', $o->getCulture());
  280. }
  281. /**
  282. * @return void
  283. */
  284. public function testGetCurrentTranslationUsesDefaultLocale()
  285. {
  286. $o = new I18nBehaviorTest1();
  287. $t = $o->getCurrentTranslation();
  288. $this->assertEquals('en_US', $t->getLocale());
  289. $o = new I18nBehaviorTest2();
  290. $t = $o->getCurrentTranslation();
  291. $this->assertEquals('fr_FR', $t->getLocale());
  292. }
  293. /**
  294. * @return void
  295. */
  296. public function testGetCurrentTranslationUsesCurrentLocale()
  297. {
  298. $o = new I18nBehaviorTest1();
  299. $o->setLocale('fr_FR');
  300. $this->assertEquals('fr_FR', $o->getCurrentTranslation()->getLocale());
  301. $o->setLocale('pt_PT');
  302. $this->assertEquals('pt_PT', $o->getCurrentTranslation()->getLocale());
  303. }
  304. /**
  305. * @return void
  306. */
  307. public function testI18nColumnGetterUsesCurrentTranslation()
  308. {
  309. $o = new I18nBehaviorTest1();
  310. $t1 = $o->getCurrentTranslation();
  311. $t1->setBar('hello');
  312. $o->setLocale('fr_FR');
  313. $t2 = $o->getCurrentTranslation();
  314. $t2->setBar('bonjour');
  315. //$o->save();
  316. $o->setLocale('en_US');
  317. $this->assertEquals('hello', $o->getBar());
  318. $o->setLocale('fr_FR');
  319. $this->assertEquals('bonjour', $o->getBar());
  320. }
  321. /**
  322. * @return void
  323. */
  324. public function testI18nColumnSetterUsesCurrentTranslation()
  325. {
  326. $o = new I18nBehaviorTest1();
  327. $o->setBar('hello');
  328. $o->setLocale('fr_FR');
  329. $o->setBar('bonjour');
  330. $o->setLocale('en_US');
  331. $this->assertEquals('hello', $o->getBar());
  332. $o->setLocale('fr_FR');
  333. $this->assertEquals('bonjour', $o->getBar());
  334. }
  335. /**
  336. * @return void
  337. */
  338. public function testTranslationsArePersisted()
  339. {
  340. $o = new I18nBehaviorTest1();
  341. $o->save();
  342. $count = I18nBehaviorTest1I18nQuery::create()
  343. ->filterById($o->getId())
  344. ->count();
  345. $this->assertEquals(0, $count);
  346. $o->setBar('hello');
  347. $o->setLocale('fr_FR');
  348. $o->setBar('bonjour');
  349. $o->save();
  350. $count = I18nBehaviorTest1I18nQuery::create()
  351. ->filterById($o->getId())
  352. ->count();
  353. $this->assertEquals(2, $count);
  354. }
  355. /**
  356. * @return void
  357. */
  358. public function testClearRemovesExistingTranslations()
  359. {
  360. $o = new I18nBehaviorTest1();
  361. $translation1 = new I18nBehaviorTest1I18n();
  362. $translation1->setBar('baz');
  363. $translation1->setLocale('fr_FR');
  364. $o->addI18nBehaviorTest1I18n($translation1);
  365. $o->clear();
  366. $this->assertEquals('en_US', $o->getLocale());
  367. $t1 = $o->getTranslation('fr_FR');
  368. $this->assertEquals('', $t1->getBar());
  369. }
  370. /**
  371. * @return void
  372. */
  373. public function testI18nWithRelations()
  374. {
  375. MovieQuery::create()->deleteAll();
  376. $count = MovieQuery::create()->count();
  377. $this->assertEquals(0, $count, 'No movie before the test');
  378. ToyQuery::create()->deleteAll();
  379. $count = ToyQuery::create()->count();
  380. $this->assertEquals(0, $count, 'No toy before the test');
  381. MovieI18nQuery::create()->deleteAll();
  382. $count = MovieI18nQuery::create()->count();
  383. $this->assertEquals(0, $count, 'No i18n movies before the test');
  384. $m = new Movie();
  385. $m->setLocale('en');
  386. $m->setTitle('V For Vendetta');
  387. $m->setLocale('fr');
  388. $m->setTitle('V Pour Vendetta');
  389. $m->setLocale('en');
  390. $this->assertEquals('V For Vendetta', $m->getTitle());
  391. $m->setLocale('fr');
  392. $this->assertEquals('V Pour Vendetta', $m->getTitle());
  393. $t = new Toy();
  394. $t->setMovie($m);
  395. $t->save();
  396. $count = MovieQuery::create()->count();
  397. $this->assertEquals(1, $count, '1 movie');
  398. $count = ToyQuery::create()->count();
  399. $this->assertEquals(1, $count, '1 toy');
  400. $count = MovieI18nQuery::create()->count();
  401. $this->assertEquals(2, $count, '2 i18n movies');
  402. $count = ToyI18nQuery::create()->count();
  403. $this->assertEquals(0, $count, '0 i18n toys');
  404. }
  405. /**
  406. * @return void
  407. */
  408. public function testI18nWithRelations2()
  409. {
  410. MovieI18nQuery::create()->deleteAll();
  411. MovieQuery::create()->deleteAll();
  412. $count = MovieQuery::create()->count();
  413. $this->assertEquals(0, $count, 'No movie before the test');
  414. ToyQuery::create()->deleteAll();
  415. $count = ToyQuery::create()->count();
  416. $this->assertEquals(0, $count, 'No toy before the test');
  417. ToyI18nQuery::create()->deleteAll();
  418. $count = ToyI18nQuery::create()->count();
  419. $this->assertEquals(0, $count, 'No i18n toys before the test');
  420. MovieI18nQuery::create()->deleteAll();
  421. $count = MovieI18nQuery::create()->count();
  422. $this->assertEquals(0, $count, 'No i18n movies before the test');
  423. $t = new Toy();
  424. $t->setLocale('en');
  425. $t->setName('My Name');
  426. $t->setLocale('fr');
  427. $t->setName('Mon Nom');
  428. $t->setLocale('en');
  429. $this->assertEquals('My Name', $t->getName());
  430. $t->setLocale('fr');
  431. $this->assertEquals('Mon Nom', $t->getName());
  432. $m = new Movie();
  433. $m->addToy($t);
  434. $m->save();
  435. $count = MovieQuery::create()->count();
  436. $this->assertEquals(1, $count, '1 movie');
  437. $count = ToyQuery::create()->count();
  438. $this->assertEquals(1, $count, '1 toy');
  439. $count = ToyI18nQuery::create()->count();
  440. $this->assertEquals(2, $count, '2 i18n toys');
  441. $count = MovieI18nQuery::create()->count();
  442. $this->assertEquals(0, $count, '0 i18n movies');
  443. }
  444. /**
  445. * @return void
  446. */
  447. public function testUseLocalColumnParameter()
  448. {
  449. $o = new I18nBehaviorTestLocalColumn();
  450. $translation1 = new I18nBehaviorTestLocalColumnI18n();
  451. $translation1->setMyLang('en_US');
  452. $o->addI18nBehaviorTestLocalColumnI18n($translation1);
  453. $translation2 = new I18nBehaviorTestLocalColumnI18n();
  454. $translation2->setMyLang('fr_FR');
  455. $o->addI18nBehaviorTestLocalColumnI18n($translation2);
  456. $o->save();
  457. $this->assertEquals($translation1, $o->getTranslation('en_US'));
  458. $this->assertEquals($translation2, $o->getTranslation('fr_FR'));
  459. }
  460. }