TranslatableDocumentCollectionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Translatable\Fixture\Document\SimpleArticle as Article;
  6. /**
  7. * These are tests for translatable behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class TranslatableDocumentCollectionTest extends BaseTestCaseMongoODM
  14. {
  15. const ARTICLE = 'Translatable\\Fixture\\Document\\SimpleArticle';
  16. const TRANSLATION = 'Gedmo\\Translatable\\Document\\Translation';
  17. private $translatableListener;
  18. private $id;
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $evm = new EventManager;
  23. $this->translatableListener = new TranslatableListener();
  24. $this->translatableListener->setDefaultLocale('en_us');
  25. $this->translatableListener->setTranslatableLocale('en_us');
  26. $evm->addEventSubscriber($this->translatableListener);
  27. $this->getMockDocumentManager($evm);
  28. $this->populate();
  29. }
  30. /**
  31. * @test
  32. */
  33. function shouldPersistMultipleTranslations()
  34. {
  35. $repo = $this->dm->getRepository(self::TRANSLATION);
  36. $sport = $this->dm->getRepository(self::ARTICLE)->find($this->id);
  37. $translations = $repo->findTranslations($sport);
  38. $this->assertArrayHasKey('de_de', $translations);
  39. $this->assertArrayHasKey('title', $translations['de_de']);
  40. $this->assertArrayHasKey('content', $translations['de_de']);
  41. $this->assertEquals('sport de', $translations['de_de']['title']);
  42. $this->assertEquals('content de', $translations['de_de']['content']);
  43. $this->assertArrayHasKey('ru_ru', $translations);
  44. $this->assertArrayHasKey('title', $translations['ru_ru']);
  45. $this->assertArrayHasKey('content', $translations['ru_ru']);
  46. $this->assertEquals('sport ru', $translations['ru_ru']['title']);
  47. $this->assertEquals('content ru', $translations['ru_ru']['content']);
  48. }
  49. /**
  50. * @test
  51. */
  52. function shouldUpdateTranslation()
  53. {
  54. $repo = $this->dm->getRepository(self::TRANSLATION);
  55. $sport = $this->dm->getRepository(self::ARTICLE)->find($this->id);
  56. $repo
  57. ->translate($sport, 'title', 'ru_ru', 'sport ru change')
  58. ->translate($sport, 'content', 'ru_ru', 'content ru change')
  59. ;
  60. $this->dm->flush();
  61. $translations = $repo->findTranslations($sport);
  62. $this->assertCount(2, $translations);
  63. $this->assertArrayHasKey('ru_ru', $translations);
  64. $this->assertArrayHasKey('title', $translations['ru_ru']);
  65. $this->assertArrayHasKey('content', $translations['ru_ru']);
  66. $this->assertEquals('sport ru change', $translations['ru_ru']['title']);
  67. $this->assertEquals('content ru change', $translations['ru_ru']['content']);
  68. }
  69. /**
  70. * @test
  71. */
  72. function shouldUpdateMultipleTranslations()
  73. {
  74. $repo = $this->dm->getRepository(self::TRANSLATION);
  75. $sport = $this->dm->getRepository(self::ARTICLE)->find($this->id);
  76. $sport->setTitle('Changed');
  77. $repo
  78. ->translate($sport, 'title', 'lt_lt', 'sport lt')
  79. ->translate($sport, 'content', 'lt_lt', 'content lt')
  80. ->translate($sport, 'title', 'ru_ru', 'sport ru change')
  81. ->translate($sport, 'content', 'ru_ru', 'content ru change')
  82. ->translate($sport, 'title', 'en_us', 'sport en update')
  83. ->translate($sport, 'content', 'en_us', 'content en update')
  84. ;
  85. $this->dm->flush();
  86. $this->assertEquals('sport en update', $sport->getTitle());
  87. $this->assertEquals('content en update', $sport->getContent());
  88. $translations = $repo->findTranslations($sport);
  89. $this->assertArrayHasKey('de_de', $translations);
  90. $this->assertArrayHasKey('title', $translations['de_de']);
  91. $this->assertArrayHasKey('content', $translations['de_de']);
  92. $this->assertEquals('sport de', $translations['de_de']['title']);
  93. $this->assertEquals('content de', $translations['de_de']['content']);
  94. $this->assertArrayHasKey('ru_ru', $translations);
  95. $this->assertArrayHasKey('title', $translations['ru_ru']);
  96. $this->assertArrayHasKey('content', $translations['ru_ru']);
  97. $this->assertEquals('sport ru change', $translations['ru_ru']['title']);
  98. $this->assertEquals('content ru change', $translations['ru_ru']['content']);
  99. $this->assertArrayHasKey('lt_lt', $translations);
  100. $this->assertArrayHasKey('title', $translations['lt_lt']);
  101. $this->assertArrayHasKey('content', $translations['lt_lt']);
  102. $this->assertEquals('sport lt', $translations['lt_lt']['title']);
  103. $this->assertEquals('content lt', $translations['lt_lt']['content']);
  104. }
  105. private function populate()
  106. {
  107. $repo = $this->dm->getRepository(self::TRANSLATION);
  108. $sport = new Article;
  109. $sport->setTitle('Sport');
  110. $sport->setContent('about sport');
  111. $repo
  112. ->translate($sport, 'title', 'de_de', 'sport de')
  113. ->translate($sport, 'content', 'de_de', 'content de')
  114. ->translate($sport, 'title', 'ru_ru', 'sport ru')
  115. ->translate($sport, 'content', 'ru_ru', 'content ru')
  116. ;
  117. $this->dm->persist($sport);
  118. $this->dm->flush();
  119. $this->id = $sport->getId();
  120. }
  121. }