TranslatableTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\Util\Debug,
  4. Translatable\Fixture\Article,
  5. Translatable\Fixture\Comment;
  6. /**
  7. * These are tests for translatable behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Translatable
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TranslatableTest extends \PHPUnit_Framework_TestCase
  15. {
  16. const TEST_ENTITY_CLASS_ARTICLE = 'Translatable\Fixture\Article';
  17. const TEST_ENTITY_CLASS_COMMENT = 'Translatable\Fixture\Comment';
  18. private $articleId;
  19. private $translatableListener;
  20. private $em;
  21. public function setUp()
  22. {
  23. $config = new \Doctrine\ORM\Configuration();
  24. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  26. $config->setProxyDir(__DIR__ . '/Proxy');
  27. $config->setProxyNamespace('Gedmo\Translatable\Proxies');
  28. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  29. $conn = array(
  30. 'driver' => 'pdo_sqlite',
  31. 'memory' => true,
  32. );
  33. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  34. $evm = new \Doctrine\Common\EventManager();
  35. $this->translatableListener = new TranslationListener();
  36. $this->translatableListener->setTranslatableLocale('en_us');
  37. $evm->addEventSubscriber($this->translatableListener);
  38. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  39. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  40. $schemaTool->dropSchema(array());
  41. $schemaTool->createSchema(array(
  42. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS_ARTICLE),
  43. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS_COMMENT),
  44. $this->em->getClassMetadata('Gedmo\Translatable\Entity\Translation'),
  45. ));
  46. }
  47. public function testFixtureGeneratedTranslations()
  48. {
  49. $article = new Article();
  50. $article->setTitle('title in en');
  51. $article->setContent('content in en');
  52. $comment1 = new Comment();
  53. $comment1->setSubject('subject1 in en');
  54. $comment1->setMessage('message1 in en');
  55. $comment2 = new Comment();
  56. $comment2->setSubject('subject2 in en');
  57. $comment2->setMessage('message2 in en');
  58. $article->addComment($comment1);
  59. $article->addComment($comment2);
  60. $this->em->persist($article);
  61. $this->em->persist($comment1);
  62. $this->em->persist($comment2);
  63. $this->em->flush();
  64. $this->articleId = $article->getId();
  65. $this->em->clear();
  66. $repo = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  67. $this->assertTrue($repo instanceof Repository\TranslationRepository);
  68. $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
  69. $this->assertTrue($article instanceof Translatable);
  70. $translations = $repo->findTranslations($article);
  71. $this->assertEquals(count($translations), 1);
  72. $this->assertArrayHasKey('en_us', $translations);
  73. $this->assertArrayHasKey('content', $translations['en_us']);
  74. $this->assertEquals('content in en', $translations['en_us']['content']);
  75. $this->assertArrayHasKey('title', $translations['en_us']);
  76. $this->assertEquals('title in en', $translations['en_us']['title']);
  77. $comments = $article->getComments();
  78. $this->assertEquals(count($comments), 2);
  79. foreach ($comments as $num => $comment) {
  80. $translations = $repo->findTranslations($comment);
  81. $this->assertEquals(count($translations), 1);
  82. $this->assertArrayHasKey('en_us', $translations);
  83. $number = $num + 1;
  84. $this->assertArrayHasKey('subject', $translations['en_us']);
  85. $expected = "subject{$number} in en";
  86. $this->assertEquals($expected, $translations['en_us']['subject']);
  87. $this->assertArrayHasKey('message', $translations['en_us']);
  88. $expected = "message{$number} in en";
  89. $this->assertEquals($expected, $translations['en_us']['message']);
  90. }
  91. // test default locale
  92. $this->translatableListener->setDefaultLocale('en_us');
  93. $article = $this->em->find(
  94. self::TEST_ENTITY_CLASS_ARTICLE,
  95. $this->articleId
  96. );
  97. $article->setTranslatableLocale('de_de');
  98. $article->setContent('content in de');
  99. $article->setTitle('title in de');
  100. $this->em->persist($article);
  101. $this->em->flush();
  102. $this->em->clear();
  103. $qb = $this->em->createQueryBuilder();
  104. $qb->select('art')
  105. ->from(get_class($article), 'art')
  106. ->where('art.id = :id');
  107. $q = $qb->getQuery();
  108. $result = $q->execute(
  109. array('id' => $article->getId()),
  110. \Doctrine\ORM\Query::HYDRATE_ARRAY
  111. );
  112. $this->assertEquals(1, count($result));
  113. $this->assertEquals($result[0]['title'], 'title in en');
  114. $this->assertEquals($result[0]['content'], 'content in en');
  115. $repo = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  116. $translations = $repo->findTranslations($article);
  117. $this->assertEquals(count($translations), 2);
  118. $this->assertArrayHasKey('de_de', $translations);
  119. $this->assertArrayHasKey('content', $translations['de_de']);
  120. $this->assertEquals('content in de', $translations['de_de']['content']);
  121. $this->assertArrayHasKey('title', $translations['de_de']);
  122. $this->assertEquals('title in de', $translations['de_de']['title']);
  123. $this->translatableListener->setDefaultLocale('');
  124. // test second translations
  125. $article = $this->em->find(
  126. self::TEST_ENTITY_CLASS_ARTICLE,
  127. $this->articleId
  128. );
  129. $this->translatableListener->setDefaultLocale('en_us');
  130. $article->setTranslatableLocale('de_de');
  131. $article->setContent('content in de');
  132. $article->setTitle('title in de');
  133. $comments = $article->getComments();
  134. foreach ($comments as $comment) {
  135. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  136. $comment->setTranslatableLocale('de_de');
  137. $comment->setSubject("subject{$number} in de");
  138. $comment->setMessage("message{$number} in de");
  139. $this->em->persist($comment);
  140. }
  141. $this->em->persist($article);
  142. $this->em->flush();
  143. $this->em->clear();
  144. $repo = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  145. $translations = $repo->findTranslations($article);
  146. $this->assertEquals(count($translations), 2);
  147. $this->assertArrayHasKey('de_de', $translations);
  148. $this->assertArrayHasKey('content', $translations['de_de']);
  149. $this->assertEquals('content in de', $translations['de_de']['content']);
  150. $this->assertArrayHasKey('title', $translations['de_de']);
  151. $this->assertEquals('title in de', $translations['de_de']['title']);
  152. $comments = $article->getComments();
  153. $this->assertEquals(count($comments), 2);
  154. foreach ($comments as $comment) {
  155. $translations = $repo->findTranslations($comment);
  156. $this->assertEquals(count($translations), 2);
  157. $this->assertArrayHasKey('de_de', $translations);
  158. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  159. $this->assertArrayHasKey('subject', $translations['de_de']);
  160. $expected = "subject{$number} in de";
  161. $this->assertEquals($expected, $translations['de_de']['subject']);
  162. $this->assertArrayHasKey('message', $translations['de_de']);
  163. $expected = "message{$number} in de";
  164. $this->assertEquals($expected, $translations['de_de']['message']);
  165. }
  166. $this->translatableListener->setTranslatableLocale('en_us');
  167. $article = $this->em->find(
  168. self::TEST_ENTITY_CLASS_ARTICLE,
  169. $this->articleId
  170. );
  171. $this->assertEquals($article->getTitle(), 'title in en');
  172. $this->assertEquals($article->getContent(), 'content in en');
  173. $comments = $article->getComments();
  174. foreach ($comments as $comment) {
  175. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  176. $this->assertEquals($comment->getSubject(), "subject{$number} in en");
  177. $this->assertEquals($comment->getMessage(), "message{$number} in en");
  178. }
  179. // test deletion
  180. $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
  181. $this->em->remove($article);
  182. $this->em->flush();
  183. $this->em->clear();
  184. $translations = $repo->findTranslationsByEntityId($this->articleId);
  185. $this->assertEquals(count($translations), 0);
  186. }
  187. }