TranslateTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\TestFramework\Helper\CacheCleaner;
  9. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  10. /**
  11. * @magentoAppIsolation enabled
  12. * @magentoCache all disabled
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class TranslateTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Framework\Translate
  19. */
  20. private $translate;
  21. /**
  22. * @inheritdoc
  23. */
  24. protected function setUp()
  25. {
  26. /** @var \Magento\Framework\View\FileSystem|MockObject $viewFileSystem */
  27. $viewFileSystem = $this->createPartialMock(
  28. \Magento\Framework\View\FileSystem::class,
  29. ['getLocaleFileName']
  30. );
  31. $viewFileSystem->expects($this->any())
  32. ->method('getLocaleFileName')
  33. ->will(
  34. $this->returnValue(
  35. dirname(__DIR__) . '/Translation/Model/_files/Magento/design/Magento/theme/i18n/en_US.csv'
  36. )
  37. );
  38. /** @var \Magento\Framework\View\Design\ThemeInterface|MockObject $theme */
  39. $theme = $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class);
  40. $theme->expects($this->any())->method('getThemePath')->will($this->returnValue('Magento/luma'));
  41. /** @var \Magento\TestFramework\ObjectManager $objectManager */
  42. $objectManager = Bootstrap::getObjectManager();
  43. $objectManager->addSharedInstance($viewFileSystem, \Magento\Framework\View\FileSystem::class);
  44. /** @var $moduleReader \Magento\Framework\Module\Dir\Reader */
  45. $moduleReader = $objectManager->get(\Magento\Framework\Module\Dir\Reader::class);
  46. $moduleReader->setModuleDir(
  47. 'Magento_Store',
  48. 'i18n',
  49. dirname(__DIR__) . '/Translation/Model/_files/Magento/Store/i18n'
  50. );
  51. $moduleReader->setModuleDir(
  52. 'Magento_Catalog',
  53. 'i18n',
  54. dirname(__DIR__) . '/Translation/Model/_files/Magento/Catalog/i18n'
  55. );
  56. /** @var \Magento\Theme\Model\View\Design|MockObject $designModel */
  57. $designModel = $this->getMockBuilder(\Magento\Theme\Model\View\Design::class)
  58. ->setMethods(['getDesignTheme'])
  59. ->setConstructorArgs(
  60. [
  61. $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class),
  62. $objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class),
  63. $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class),
  64. $objectManager->get(\Magento\Theme\Model\ThemeFactory::class),
  65. $objectManager->get(\Magento\Framework\ObjectManagerInterface::class),
  66. $objectManager->get(\Magento\Framework\App\State::class),
  67. ['frontend' => 'Test/default']
  68. ]
  69. )
  70. ->getMock();
  71. $designModel->expects($this->any())->method('getDesignTheme')->willReturn($theme);
  72. $objectManager->addSharedInstance($designModel, \Magento\Theme\Model\View\Design\Proxy::class);
  73. $this->translate = $objectManager->create(\Magento\Framework\Translate::class);
  74. $objectManager->addSharedInstance($this->translate, \Magento\Framework\Translate::class);
  75. $objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Composite::class);
  76. $objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Translate::class);
  77. \Magento\Framework\Phrase::setRenderer(
  78. $objectManager->get(\Magento\Framework\Phrase\RendererInterface::class)
  79. );
  80. }
  81. public function testLoadData()
  82. {
  83. $data = $this->translate->loadData(null, true)->getData();
  84. CacheCleaner::cleanAll();
  85. $this->translate->loadData()->getData();
  86. $dataCached = $this->translate->loadData()->getData();
  87. $this->assertEquals($data, $dataCached);
  88. }
  89. /**
  90. * @magentoCache all disabled
  91. * @dataProvider translateDataProvider
  92. *
  93. * @param string $inputText
  94. * @param string $expectedTranslation
  95. * @return void
  96. * @throws Exception\LocalizedException
  97. */
  98. public function testTranslate($inputText, $expectedTranslation)
  99. {
  100. $this->translate->loadData(\Magento\Framework\App\Area::AREA_FRONTEND);
  101. $actualTranslation = new \Magento\Framework\Phrase($inputText);
  102. $this->assertEquals($expectedTranslation, $actualTranslation);
  103. }
  104. /**
  105. * @return array
  106. */
  107. public function translateDataProvider()
  108. {
  109. return [
  110. ['', ''],
  111. [
  112. 'Theme phrase will be translated',
  113. 'Theme phrase is translated',
  114. ],
  115. [
  116. 'Phrase in Magento_Store module that doesn\'t need translation',
  117. 'Phrase in Magento_Store module that doesn\'t need translation',
  118. ],
  119. [
  120. 'Phrase in Magento_Catalog module that doesn\'t need translation',
  121. 'Phrase in Magento_Catalog module that doesn\'t need translation',
  122. ],
  123. [
  124. 'Magento_Store module phrase will be overridden by theme translation',
  125. 'Magento_Store module phrase is overridden by theme translation',
  126. ],
  127. [
  128. 'Magento_Catalog module phrase will be overridden by theme translation',
  129. 'Magento_Catalog module phrase is overridden by theme translation',
  130. ],
  131. ];
  132. }
  133. }