TranslatableTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. $this->populate();
  47. }
  48. public function testFixtureGeneratedTranslations()
  49. {
  50. $repo = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  51. $this->assertTrue($repo instanceof Entity\Repository\TranslationRepository);
  52. $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
  53. $this->assertTrue($article instanceof Translatable);
  54. $translations = $repo->findTranslations($article);
  55. $this->assertEquals(count($translations), 1);
  56. $this->assertArrayHasKey('en_us', $translations);
  57. $this->assertArrayHasKey('content', $translations['en_us']);
  58. $this->assertEquals('content in en', $translations['en_us']['content']);
  59. $this->assertArrayHasKey('title', $translations['en_us']);
  60. $this->assertEquals('title in en', $translations['en_us']['title']);
  61. $comments = $article->getComments();
  62. $this->assertEquals(count($comments), 2);
  63. foreach ($comments as $num => $comment) {
  64. $translations = $repo->findTranslations($comment);
  65. $this->assertEquals(count($translations), 1);
  66. $this->assertArrayHasKey('en_us', $translations);
  67. $number = $num + 1;
  68. $this->assertArrayHasKey('subject', $translations['en_us']);
  69. $expected = "subject{$number} in en";
  70. $this->assertEquals($expected, $translations['en_us']['subject']);
  71. $this->assertArrayHasKey('message', $translations['en_us']);
  72. $expected = "message{$number} in en";
  73. $this->assertEquals($expected, $translations['en_us']['message']);
  74. }
  75. // test default locale
  76. $this->translatableListener->setDefaultLocale('en_us');
  77. $article = $this->em->find(
  78. self::TEST_ENTITY_CLASS_ARTICLE,
  79. $this->articleId
  80. );
  81. $article->setTranslatableLocale('de_de');
  82. $article->setContent('content in de');
  83. $article->setTitle('title in de');
  84. $this->em->persist($article);
  85. $this->em->flush();
  86. $this->em->clear();
  87. $qb = $this->em->createQueryBuilder();
  88. $qb->select('art')
  89. ->from(get_class($article), 'art')
  90. ->where('art.id = :id');
  91. $q = $qb->getQuery();
  92. $result = $q->execute(
  93. array('id' => $article->getId()),
  94. \Doctrine\ORM\Query::HYDRATE_ARRAY
  95. );
  96. $this->assertEquals(1, count($result));
  97. $this->assertEquals($result[0]['title'], 'title in en');
  98. $this->assertEquals($result[0]['content'], 'content in en');
  99. $repo = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  100. $translations = $repo->findTranslations($article);
  101. $this->assertEquals(count($translations), 2);
  102. $this->assertArrayHasKey('de_de', $translations);
  103. $this->assertArrayHasKey('content', $translations['de_de']);
  104. $this->assertEquals('content in de', $translations['de_de']['content']);
  105. $this->assertArrayHasKey('title', $translations['de_de']);
  106. $this->assertEquals('title in de', $translations['de_de']['title']);
  107. $this->translatableListener->setDefaultLocale('');
  108. // test second translations
  109. $article = $this->em->find(
  110. self::TEST_ENTITY_CLASS_ARTICLE,
  111. $this->articleId
  112. );
  113. $this->translatableListener->setDefaultLocale('en_us');
  114. $article->setTranslatableLocale('de_de');
  115. $article->setContent('content in de');
  116. $article->setTitle('title in de');
  117. $comments = $article->getComments();
  118. foreach ($comments as $comment) {
  119. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  120. $comment->setTranslatableLocale('de_de');
  121. $comment->setSubject("subject{$number} in de");
  122. $comment->setMessage("message{$number} in de");
  123. $this->em->persist($comment);
  124. }
  125. $this->em->persist($article);
  126. $this->em->flush();
  127. $this->em->clear();
  128. $repo = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
  129. $translations = $repo->findTranslations($article);
  130. $this->assertEquals(count($translations), 2);
  131. $this->assertArrayHasKey('de_de', $translations);
  132. $this->assertArrayHasKey('content', $translations['de_de']);
  133. $this->assertEquals('content in de', $translations['de_de']['content']);
  134. $this->assertArrayHasKey('title', $translations['de_de']);
  135. $this->assertEquals('title in de', $translations['de_de']['title']);
  136. $comments = $article->getComments();
  137. $this->assertEquals(count($comments), 2);
  138. foreach ($comments as $comment) {
  139. $translations = $repo->findTranslations($comment);
  140. $this->assertEquals(count($translations), 2);
  141. $this->assertArrayHasKey('de_de', $translations);
  142. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  143. $this->assertArrayHasKey('subject', $translations['de_de']);
  144. $expected = "subject{$number} in de";
  145. $this->assertEquals($expected, $translations['de_de']['subject']);
  146. $this->assertArrayHasKey('message', $translations['de_de']);
  147. $expected = "message{$number} in de";
  148. $this->assertEquals($expected, $translations['de_de']['message']);
  149. }
  150. $this->translatableListener->setTranslatableLocale('en_us');
  151. $article = $this->em->find(
  152. self::TEST_ENTITY_CLASS_ARTICLE,
  153. $this->articleId
  154. );
  155. $this->assertEquals($article->getTitle(), 'title in en');
  156. $this->assertEquals($article->getContent(), 'content in en');
  157. $comments = $article->getComments();
  158. foreach ($comments as $comment) {
  159. $number = preg_replace("@[^\d]+@", '', $comment->getSubject());
  160. $this->assertEquals($comment->getSubject(), "subject{$number} in en");
  161. $this->assertEquals($comment->getMessage(), "message{$number} in en");
  162. }
  163. // test deletion
  164. $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
  165. $this->em->remove($article);
  166. $this->em->flush();
  167. $this->em->clear();
  168. $translations = $repo->findTranslationsByObjectId($this->articleId);
  169. $this->assertEquals(0, count($translations));
  170. }
  171. /**
  172. * Translation fallback, related to issue #9 on github
  173. */
  174. public function testTranslationFallback()
  175. {
  176. $this->translatableListener->setTranslationFallback(false);
  177. $this->translatableListener->setTranslatableLocale('ru_RU');
  178. $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
  179. $this->assertFalse((bool)$article->getTitle());
  180. $this->assertFalse((bool)$article->getContent());
  181. foreach ($article->getComments() as $comment) {
  182. $this->assertFalse((bool)$comment->getSubject());
  183. $this->assertFalse((bool)$comment->getMessage());
  184. }
  185. $this->em->clear();
  186. $this->translatableListener->setTranslationFallback(true);
  187. $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
  188. $this->assertEquals($article->getTitle(), 'title in en');
  189. $this->assertEquals($article->getContent(), 'content in en');
  190. }
  191. private function populate()
  192. {
  193. $article = new Article();
  194. $article->setTitle('title in en');
  195. $article->setContent('content in en');
  196. $comment1 = new Comment();
  197. $comment1->setSubject('subject1 in en');
  198. $comment1->setMessage('message1 in en');
  199. $comment2 = new Comment();
  200. $comment2->setSubject('subject2 in en');
  201. $comment2->setMessage('message2 in en');
  202. $article->addComment($comment1);
  203. $article->addComment($comment2);
  204. $this->em->persist($article);
  205. $this->em->persist($comment1);
  206. $this->em->persist($comment2);
  207. $this->em->flush();
  208. $this->articleId = $article->getId();
  209. $this->em->clear();
  210. }
  211. }