|
| 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\Framework\App\AreaList; |
| 11 | +use Magento\Framework\Phrase; |
| 12 | +use Magento\Framework\Phrase\RendererInterface; |
| 13 | +use Magento\Framework\Translate; |
| 14 | +use Magento\Framework\View\FileSystem; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\Helper\CacheCleaner; |
| 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 | + * @var RendererInterface |
| 30 | + */ |
| 31 | + private $origRenderer; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + protected function setUp() |
| 37 | + { |
| 38 | + $viewFileSystem = $this->createPartialMock(FileSystem::class, ['getLocaleFileName']); |
| 39 | + $viewFileSystem->expects($this->any()) |
| 40 | + ->method('getLocaleFileName') |
| 41 | + ->willReturn( |
| 42 | + // phpcs:ignore Magento2.Functions.DiscouragedFunction |
| 43 | + dirname(__DIR__) . '/_files/Magento/Store/i18n/en_AU.csv' |
| 44 | + ); |
| 45 | + |
| 46 | + $objectManager = Bootstrap::getObjectManager(); |
| 47 | + $objectManager->addSharedInstance($viewFileSystem, FileSystem::class); |
| 48 | + $translator = $objectManager->create(Translate::class); |
| 49 | + $objectManager->addSharedInstance($translator, Translate::class); |
| 50 | + $areaList = $this->getMockBuilder(AreaList::class) |
| 51 | + ->disableOriginalConstructor() |
| 52 | + ->getMock(); |
| 53 | + $this->origRenderer = Phrase::getRenderer(); |
| 54 | + Phrase::setRenderer( |
| 55 | + $objectManager->get(RendererInterface::class) |
| 56 | + ); |
| 57 | + |
| 58 | + $this->model = $objectManager->create( |
| 59 | + PreProcessor::class, |
| 60 | + [ |
| 61 | + 'translate' => $translator, |
| 62 | + 'areaList' => $areaList, |
| 63 | + ] |
| 64 | + ); |
| 65 | + |
| 66 | + $translator->setLocale('en_AU'); |
| 67 | + $translator->loadData(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritdoc |
| 72 | + */ |
| 73 | + protected function tearDown() |
| 74 | + { |
| 75 | + Phrase::setRenderer($this->origRenderer); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test for backend translation strategy. |
| 80 | + * |
| 81 | + * @param string $content |
| 82 | + * @param string $translation |
| 83 | + * @return void |
| 84 | + * @dataProvider contentForTranslateDataProvider |
| 85 | + */ |
| 86 | + public function testProcess(string $content, string $translation) |
| 87 | + { |
| 88 | + CacheCleaner::cleanAll(); |
| 89 | + $this->assertEquals($translation, $this->model->translate($content)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Data provider for translation. |
| 94 | + * |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public function contentForTranslateDataProvider(): array |
| 98 | + { |
| 99 | + return [ |
| 100 | + 'i18n_js_file_error' => [ |
| 101 | + 'setTranslateProp = function (el, original) { |
| 102 | + var location = $(el).prop(\'tagName\').toLowerCase(), |
| 103 | + translated = $.mage.__(original), |
| 104 | + translationData = { |
| 105 | + shown: translated, |
| 106 | + translated: translated, |
| 107 | + original: original |
| 108 | + }, |
| 109 | + translateAttr = composeTranslateAttr(translationData, location); |
| 110 | + $(el).attr(\'data-translate\', translateAttr); |
| 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 | + $(el).attr(\'data-translate\', translateAttr); |
| 123 | + setText(el, translationData.shown); |
| 124 | + },', |
| 125 | + ], |
| 126 | + 'checkTranslationWithWhiteSpaces' => [ |
| 127 | + <<<i18n |
| 128 | + title: $.mage.__( |
| 129 | + 'Original value for Magento_Store module' |
| 130 | + ), |
| 131 | + title: \$t( |
| 132 | + 'Original value for Magento_Store module' |
| 133 | + ); |
| 134 | + title: jQuery.mage.__( |
| 135 | + 'Original value for Magento_Store module' |
| 136 | + ); |
| 137 | +i18n |
| 138 | + , |
| 139 | + <<<i18n |
| 140 | + title: 'Translated value for Magento_Store module in en_AU', |
| 141 | + title: 'Translated value for Magento_Store module in en_AU'; |
| 142 | + title: 'Translated value for Magento_Store module in en_AU'; |
| 143 | +i18n |
| 144 | + , |
| 145 | + ], |
| 146 | + 'checkTranslationWithReplace' => [ |
| 147 | + <<<i18n |
| 148 | + $.mage.__('The maximum you may purchase is %1.').replace('%1', params.maxAllowed); |
| 149 | + \$t('The maximum you may purchase is %1.').replace('%1', params.maxAllowed); |
| 150 | +i18n |
| 151 | + , |
| 152 | + <<<i18n |
| 153 | + 'The maximum you may purchase is %1.'.replace('%1', params.maxAllowed); |
| 154 | + 'The maximum you may purchase is %1.'.replace('%1', params.maxAllowed); |
| 155 | +i18n |
| 156 | + , |
| 157 | + ], |
| 158 | + 'checkAvoidingMatchingWithJsInString' => [ |
| 159 | + <<<i18n |
| 160 | + \$t('Payment ' + this.getTitle() + ' can\'t be initialized') |
| 161 | + \$t( |
| 162 | + 'Set unique country-state combinations within the same fixed product tax. ' + |
| 163 | + 'Verify the combinations and try again.' |
| 164 | + ) |
| 165 | +i18n |
| 166 | + , |
| 167 | + <<<i18n |
| 168 | + \$t('Payment ' + this.getTitle() + ' can\'t be initialized') |
| 169 | + \$t( |
| 170 | + 'Set unique country-state combinations within the same fixed product tax. ' + |
| 171 | + 'Verify the combinations and try again.' |
| 172 | + ) |
| 173 | +i18n |
| 174 | + , |
| 175 | + ], |
| 176 | + 'checkAvoidMatchingPhtml' => [ |
| 177 | + <<<i18n |
| 178 | + globalMessageList.addErrorMessage({ |
| 179 | + message: \$t(<?= /* @noEscape */ json_encode(\$params['error_msg'])?>) |
| 180 | + }); |
| 181 | +i18n |
| 182 | + , |
| 183 | + <<<i18n |
| 184 | + globalMessageList.addErrorMessage({ |
| 185 | + message: \$t(<?= /* @noEscape */ json_encode(\$params['error_msg'])?>) |
| 186 | + }); |
| 187 | +i18n |
| 188 | + , |
| 189 | + ] |
| 190 | + ]; |
| 191 | + } |
| 192 | +} |
0 commit comments