|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Translation\Model\Js; |
| 9 | + |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | +use Magento\Framework\View\Asset\PreProcessor\Chain; |
| 12 | +use Magento\Framework\View\Asset\LocalInterface; |
| 13 | +use Magento\Framework\View\Asset\File\FallbackContext; |
| 14 | +use Magento\Framework\View\FileSystem; |
| 15 | +use Magento\TestFramework\Helper\CacheCleaner; |
| 16 | +use Magento\Framework\Translate; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class for testing translation. |
| 20 | + */ |
| 21 | +class PreProcessorTest extends \PHPUnit\Framework\TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var PreProcessor |
| 25 | + */ |
| 26 | + private $model; |
| 27 | + |
| 28 | + /** |
| 29 | + * Set up. |
| 30 | + */ |
| 31 | + protected function setUp() |
| 32 | + { |
| 33 | + $viewFileSystem = $this->createPartialMock(FileSystem::class, ['getLocaleFileName']); |
| 34 | + $viewFileSystem->expects($this->any())->method('getLocaleFileName') |
| 35 | + ->willReturn(dirname(__DIR__) . '/_files/Magento/Store/i18n/en_AU.csv'); |
| 36 | + |
| 37 | + $objectManager = Bootstrap::getObjectManager(); |
| 38 | + $objectManager->addSharedInstance($viewFileSystem, FileSystem::class); |
| 39 | + $translator = $objectManager->create(Translate::class); |
| 40 | + $objectManager->addSharedInstance($translator, Translate::class); |
| 41 | + |
| 42 | + $config = $this->createPartialMock(Config::class, ['isEmbeddedStrategy', 'getPatterns']); |
| 43 | + $config->expects($this->atLeastOnce())->method('isEmbeddedStrategy')->willReturn(true); |
| 44 | + $config->expects($this->atLeastOnce())->method('getPatterns')->willReturn( |
| 45 | + [ |
| 46 | + "~(?:\\$|jQuery)\\.mage\\.__\\((?s)[^'\\\")]*?(['\\\"])(?P<translate>.+?)(?<!\\\\)\\1(?s).*?\\)~", |
| 47 | + "~\\\$t\\((?s)[^'\\\")]*?([\\\"'])(?P<translate>.+?)\\1(?s).*?\\)~" |
| 48 | + ] |
| 49 | + ); |
| 50 | + $this->model = $objectManager->create( |
| 51 | + PreProcessor::class, |
| 52 | + [ |
| 53 | + 'config' => $config |
| 54 | + ] |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test for backend translation strategy. |
| 60 | + * |
| 61 | + * @param string $content |
| 62 | + * @param string $translation |
| 63 | + * @return void |
| 64 | + * @dataProvider contentForTranslateDataProvider |
| 65 | + */ |
| 66 | + public function testProcess(string $content, string $translation) |
| 67 | + { |
| 68 | + CacheCleaner::cleanAll(); |
| 69 | + $locale = $this->getMockBuilder( |
| 70 | + LocalInterface::class |
| 71 | + )->getMockForAbstractClass(); |
| 72 | + $context = $this->createPartialMock( |
| 73 | + FallbackContext::class, |
| 74 | + ['getAreaCode', 'getLocale'] |
| 75 | + ); |
| 76 | + |
| 77 | + $context->expects($this->atLeastOnce())->method('getAreaCode')->willReturn('base'); |
| 78 | + $context->expects($this->atLeastOnce())->method('getLocale')->willReturn('en_AU'); |
| 79 | + $locale->expects($this->atLeastOnce())->method('getContext')->willReturn($context); |
| 80 | + |
| 81 | + $chain = Bootstrap::getObjectManager()->create( |
| 82 | + Chain::class, |
| 83 | + ['asset' => $locale, 'origContent' => '', 'origContentType' => '', 'origAssetPath' => ''] |
| 84 | + ); |
| 85 | + $chain->setContent($content); |
| 86 | + $this->model->process($chain); |
| 87 | + $this->assertEquals($translation, $chain->getContent()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Data provider for translation. |
| 92 | + * |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + public function contentForTranslateDataProvider() |
| 96 | + { |
| 97 | + return [ |
| 98 | + [ |
| 99 | + 'setTranslateProp = function (el, original) { |
| 100 | + var location = $(el).prop(\'tagName\').toLowerCase(), |
| 101 | + translated = $.mage.__(original), |
| 102 | + translationData = { |
| 103 | + shown: translated, |
| 104 | + translated: translated, |
| 105 | + original: original |
| 106 | + }, |
| 107 | + translateAttr = composeTranslateAttr(translationData, location); |
| 108 | +
|
| 109 | + $(el).attr(\'data-translate\', translateAttr); |
| 110 | +
|
| 111 | + setText(el, translationData.shown); |
| 112 | + },', |
| 113 | + 'setTranslateProp = function (el, original) { |
| 114 | + var location = $(el).prop(\'tagName\').toLowerCase(), |
| 115 | + translated = $.mage.__(original), |
| 116 | + translationData = { |
| 117 | + shown: translated, |
| 118 | + translated: translated, |
| 119 | + original: original |
| 120 | + }, |
| 121 | + translateAttr = composeTranslateAttr(translationData, location); |
| 122 | +
|
| 123 | + $(el).attr(\'data-translate\', translateAttr); |
| 124 | +
|
| 125 | + setText(el, translationData.shown); |
| 126 | + },' |
| 127 | + ], |
| 128 | + [ |
| 129 | + <<<EOT |
| 130 | + title: $.mage.__( |
| 131 | + 'Original value for Magento_Store module' |
| 132 | + ) |
| 133 | +EOT |
| 134 | + , |
| 135 | + <<<EOT |
| 136 | + title: 'Translated value for Magento_Store module in en_AU' |
| 137 | +EOT |
| 138 | + ], |
| 139 | + [ |
| 140 | + <<<EOT |
| 141 | + title: \$t( |
| 142 | + 'Original value for Magento_Store module' |
| 143 | + ) |
| 144 | +EOT |
| 145 | + , |
| 146 | + <<<EOT |
| 147 | + title: 'Translated value for Magento_Store module in en_AU' |
| 148 | +EOT |
| 149 | + ], |
| 150 | + ]; |
| 151 | + } |
| 152 | +} |
0 commit comments