Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FallbackTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Bootstrap as AppBootstrap;
12 
17 class FallbackTest extends \PHPUnit\Framework\TestCase
18 {
22  private $themeFactory;
23 
24  protected function setUp()
25  {
28  $registration = $objectManager->get(
29  \Magento\Theme\Model\Theme\Registration::class
30  );
31  $registration->register();
33  $this->themeFactory = $objectManager
34  ->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
35  }
36 
45  protected function reinitializeEnvironment()
46  {
47  Bootstrap::getInstance()->reinitialize([
48  AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS => [
50  'path' => __DIR__ . '/../../_files/fallback/lib/web',
51  ],
52  ],
53  ]);
54  }
55 
64  public function testGetTemplateFile($file, $themePath, $module, $expectedFilename)
65  {
66  $this->reinitializeEnvironment();
69  ->create(\Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile::class);
70  $themeModel = $this->themeFactory->create($themePath);
71 
72  $actualFilename = $model->getFile('frontend', $themeModel, $file, $module);
73  if ($expectedFilename) {
74  $this->assertInternalType('string', $actualFilename);
75  $this->assertStringMatchesFormat($expectedFilename, $actualFilename);
76  $this->assertFileExists($actualFilename);
77  } else {
78  $this->assertFalse($actualFilename);
79  }
80  }
81 
85  public function getTemplateFileDataProvider()
86  {
87  return [
88  'non-modular: no default inheritance' => [
89  'fixture_template.phtml', 'Vendor_ViewTest/standalone_theme', null,
90  null,
91  ],
92  'non-modular: inherit parent theme' => [
93  'fixture_template.phtml', 'Vendor_ViewTest/custom_theme', null,
94  '%s/frontend/Vendor/default/templates/fixture_template.phtml',
95  ],
96  'non-modular: inherit grandparent theme' => [
97  'fixture_template.phtml', 'Vendor_ViewTest/custom_theme2', null,
98  '%s/frontend/Vendor/default/templates/fixture_template.phtml',
99  ],
100  'modular: no default inheritance' => [
101  'fixture_template.phtml', 'Vendor_ViewTest/standalone_theme', 'ViewTest_Module',
102  null,
103  ],
104  'modular: no fallback to non-modular file' => [
105  'nonexistent_fixture_script.phtml', 'Vendor_ViewTest/default', 'ViewTest_Module',
106  null,
107  ],
108  'modular: inherit parent theme' => [
109  'fixture_template.phtml', 'Vendor_ViewTest/custom_theme', 'ViewTest_Module',
110  '%s/frontend/Vendor/default/ViewTest_Module/templates/fixture_template.phtml',
111  ],
112  'modular: inherit grandparent theme' => [
113  'fixture_template.phtml', 'Vendor_ViewTest/custom_theme2', 'ViewTest_Module',
114  '%s/frontend/Vendor/default/ViewTest_Module/templates/fixture_template.phtml',
115  ],
116  ];
117  }
118 
126  public function testGetI18nCsvFile($themePath, $locale, $expectedFilename)
127  {
128  $this->reinitializeEnvironment();
131  \Magento\Framework\View\Design\FileResolution\Fallback\File::class
132  );
133  $themeModel = $this->themeFactory->create($themePath);
134 
135  $actualFilename = $model->getFile('frontend', $themeModel, 'i18n/' . $locale . '.csv');
136 
137  if ($expectedFilename) {
138  $this->assertInternalType('string', $actualFilename);
139  $this->assertStringMatchesFormat($expectedFilename, $actualFilename);
140  $this->assertFileExists($actualFilename);
141  } else {
142  $this->assertFalse($actualFilename);
143  }
144  }
145 
146  public function getLocaleFileDataProvider()
147  {
148  return [
149  'no default inheritance' => [
150  'Vendor_ViewTest/standalone_theme', 'en_US',
151  null,
152  ],
153  'inherit parent theme' => [
154  'Vendor_ViewTest/custom_theme', 'en_US',
155  '%s/frontend/Vendor/custom_theme/i18n/en_US.csv',
156  ],
157  'inherit grandparent theme' => [
158  'Vendor_ViewTest/custom_theme2', 'en_US',
159  '%s/frontend/Vendor/custom_theme/i18n/en_US.csv',
160  ],
161  ];
162  }
163 
175  public function testGetViewFile($file, $themePath, $locale, $module, $expectedFilename)
176  {
177  $this->reinitializeEnvironment();
180  ->create(\Magento\Framework\View\Design\FileResolution\Fallback\StaticFile::class);
181  $themeModel = $this->themeFactory->create($themePath);
182 
183  $actualFilename = $model->getFile('frontend', $themeModel, $locale, $file, $module);
184  if ($expectedFilename) {
185  $this->assertInternalType('string', $actualFilename);
186  $this->assertStringMatchesFormat($expectedFilename, $actualFilename);
187  $this->assertFileExists($actualFilename);
188  } else {
189  $this->assertFalse($actualFilename);
190  }
191  }
192 
193  public function getViewFileDataProvider()
194  {
195  return [
196  'non-modular: no default inheritance' => [
197  'fixture_script.js', 'Vendor_ViewTest/standalone_theme', null, null,
198  null,
199  ],
200  'non-modular: inherit same package & parent theme' => [
201  'fixture_script.js', 'Vendor_ViewTest/custom_theme', null, null,
202  '%s/frontend/Vendor/default/web/fixture_script.js',
203  ],
204  'non-modular: inherit same package & grandparent theme' => [
205  'fixture_script.js', 'Vendor_ViewTest/custom_theme2', null, null,
206  '%s/frontend/Vendor/default/web/fixture_script.js',
207  ],
208  'non-modular: fallback to non-localized file' => [
209  'fixture_script.js', 'Vendor_ViewTest/default', 'en_US', null,
210  '%s/frontend/Vendor/default/web/fixture_script.js',
211  ],
212  'non-modular: localized file' => [
213  'fixture_script.js', 'Vendor_ViewTest/default', 'ru_RU', null,
214  '%s/frontend/Vendor/default/web/i18n/ru_RU/fixture_script.js',
215  ],
216  'non-modular: override js lib file' => [
217  'mage/script.js', 'Vendor_ViewTest/custom_theme', null, null,
218  '%s/frontend/Vendor/custom_theme/web/mage/script.js',
219  ],
220  'non-modular: inherit js lib file' => [
221  'mage/script.js', 'Vendor_ViewTest/default', null, null,
222  '%s/lib/web/mage/script.js',
223  ],
224  'modular: no default inheritance' => [
225  'fixture_script.js', 'Vendor_ViewTest/standalone_theme', null, 'ViewTest_Module',
226  null,
227  ],
228  'modular: no fallback to non-modular file' => [
229  'nonexistent_fixture_script.js', 'Vendor_ViewTest/default', null, 'ViewTest_Module',
230  null,
231  ],
232  'modular: no fallback to js lib file' => [
233  'mage/script.js', 'Vendor_ViewTest/default', null, 'ViewTest_Module',
234  null,
235  ],
236  'modular: no fallback to non-modular localized file' => [
237  'nonexistent_fixture_script.js', 'Vendor_ViewTest/default', 'ru_RU', 'ViewTest_Module',
238  null,
239  ],
240  'modular: inherit same package & parent theme' => [
241  'fixture_script.js', 'Vendor_ViewTest/custom_theme', null, 'ViewTest_Module',
242  '%s/frontend/Vendor/default/ViewTest_Module/web/fixture_script.js',
243  ],
244  'modular: inherit same package & grandparent theme' => [
245  'fixture_script.js', 'Vendor_ViewTest/custom_theme2', null, 'ViewTest_Module',
246  '%s/frontend/Vendor/default/ViewTest_Module/web/fixture_script.js',
247  ],
248  'modular: fallback to non-localized file' => [
249  'fixture_script.js', 'Vendor_ViewTest/default', 'en_US', 'ViewTest_Module',
250  '%s/frontend/Vendor/default/ViewTest_Module/web/fixture_script.js',
251  ],
252  'modular: localized file' => [
253  'fixture_script.js', 'Vendor_ViewTest/custom_theme2', 'ru_RU', 'ViewTest_Module',
254  '%s/frontend/Vendor/default/ViewTest_Module/web/i18n/ru_RU/fixture_script.js',
255  ],
256  ];
257  }
258 
272  public function testGetEmailTemplateFile($file, $themePath, $module, $expectedFilename)
273  {
275 
278  ->create(\Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile::class);
279 
280  $themeModel = $this->themeFactory->create($themePath);
281  $locale = \Magento\Setup\Module\I18n\Locale::DEFAULT_SYSTEM_LOCALE;
282 
283  $actualFilename = $model->getFile($area, $themeModel, $locale, $file, $module);
284  if ($expectedFilename) {
285  $this->assertInternalType('string', $actualFilename);
286  $this->assertStringMatchesFormat($expectedFilename, $actualFilename);
287  $this->assertFileExists($actualFilename);
288  } else {
289  $this->assertFalse($actualFilename);
290  }
291  }
292 
297  {
298  return [
299  'no fallback' => [
300  'account_new.html',
301  'Vendor_EmailTest/custom_theme',
302  'Magento_Customer',
303  '%s/frontend/Vendor/custom_theme/Magento_Customer/email/account_new.html',
304  ],
305  'inherit same package & parent theme' => [
306  'account_new_confirmation.html',
307  'Vendor_EmailTest/custom_theme',
308  'Magento_Customer',
309  '%s/frontend/Vendor/default/Magento_Customer/email/account_new_confirmation.html',
310  ],
311  'inherit parent package & grandparent theme' => [
312  'account_new_confirmed.html',
313  'Vendor_EmailTest/custom_theme',
314  'Magento_Customer',
315  '%s/frontend/Magento/default/Magento_Customer/email/account_new_confirmed.html',
316  ],
317  ];
318  }
319 }
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60