Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileSystemTest.php
Go to the documentation of this file.
1 <?php
11 
12 class FileSystemTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $_model;
18 
22  protected $_fileResolution;
23 
28 
33 
38 
44 
48  protected $_assetRepo;
49 
50  protected function setUp()
51  {
52  $this->_fileResolution = $this->createMock(\Magento\Framework\View\Design\FileResolution\Fallback\File::class);
53  $this->_templateFileResolution = $this->createMock(
54  \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile::class
55  );
56  $this->_localeFileResolution = $this->createMock(
57  \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile::class
58  );
59  $this->_staticFileResolution = $this->createMock(
60  \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile::class
61  );
62  $this->_emailTemplateFileResolution = $this->createMock(
63  \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile::class
64  );
65  $this->_assetRepo = $this->createPartialMock(
66  \Magento\Framework\View\Asset\Repository::class,
67  ['extractScope', 'updateDesignParams', 'createAsset']
68  );
69 
70  $this->_model = new \Magento\Framework\View\FileSystem(
71  $this->_fileResolution,
72  $this->_templateFileResolution,
73  $this->_localeFileResolution,
74  $this->_staticFileResolution,
75  $this->_emailTemplateFileResolution,
76  $this->_assetRepo
77  );
78  }
79 
80  public function testGetFilename()
81  {
82  $params = [
83  'area' => 'some_area',
84  'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
85  'module' => 'Some_Module', //It should be set in \Magento\Framework\View\Asset\Repository::extractScope
86  // but PHPUnit has troubles with passing arguments by reference
87  ];
88  $file = 'Some_Module::some_file.ext';
89  $expected = 'path/to/some_file.ext';
90 
91  $this->_fileResolution->expects($this->once())
92  ->method('getFile')
93  ->with($params['area'], $params['themeModel'], 'some_file.ext', 'Some_Module')
94  ->will($this->returnValue($expected));
95 
96  $this->_assetRepo->expects($this->any())
97  ->method('extractScope')
98  ->with($file, $params)
99  ->will($this->returnValue('some_file.ext'));
100 
101  $actual = $this->_model->getFilename($file, $params);
102  $this->assertEquals($expected, $actual);
103  }
104 
105  public function testGetTemplateFileName()
106  {
107  $params = [
108  'area' => 'some_area',
109  'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
110  'module' => 'Some_Module', //It should be set in \Magento\Framework\View\Asset\Repository::extractScope
111  // but PHPUnit has troubles with passing arguments by reference
112  ];
113  $file = 'Some_Module::some_file.ext';
114  $expected = 'path/to/some_file.ext';
115 
116  $this->_templateFileResolution->expects($this->once())
117  ->method('getFile')
118  ->with($params['area'], $params['themeModel'], 'some_file.ext', 'Some_Module')
119  ->will($this->returnValue($expected));
120 
121  $this->_assetRepo->expects($this->any())
122  ->method('extractScope')
123  ->with($file, $params)
124  ->will($this->returnValue('some_file.ext'));
125 
126  $actual = $this->_model->getTemplateFileName($file, $params);
127  $this->assertEquals($expected, $actual);
128  }
129 
130  public function testGetLocaleFileName()
131  {
132  $params = [
133  'area' => 'some_area',
134  'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
135  'locale' => 'some_locale',
136  ];
137  $file = 'some_file.ext';
138  $expected = 'path/to/some_file.ext';
139 
140  $this->_localeFileResolution->expects($this->once())
141  ->method('getFile')
142  ->with($params['area'], $params['themeModel'], $params['locale'], 'some_file.ext')
143  ->will($this->returnValue($expected));
144 
145  $actual = $this->_model->getLocaleFileName($file, $params);
146  $this->assertEquals($expected, $actual);
147  }
148 
149  public function testGetViewFile()
150  {
151  $params = [
152  'area' => 'some_area',
153  'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
154  'locale' => 'some_locale',
155  'module' => 'Some_Module',
156  ];
157  $file = 'Some_Module::some_file.ext';
158  $expected = 'path/to/some_file.ext';
159 
160  $this->_staticFileResolution->expects($this->once())
161  ->method('getFile')
162  ->with($params['area'], $params['themeModel'], $params['locale'], 'some_file.ext', 'Some_Module')
163  ->will($this->returnValue($expected));
164 
165  $actual = $this->_model->getStaticFileName($file, $params);
166  $this->assertEquals($expected, $actual);
167  }
168 
174  public function testNormalizePath($path, $expectedResult)
175  {
176  $result = $this->_model->normalizePath($path);
177  $this->assertEquals($expectedResult, $result);
178  }
179 
183  public function normalizePathDataProvider()
184  {
185  return [
186  'standard path' => ['/dir/somedir/somefile.ext', '/dir/somedir/somefile.ext'],
187  'one dot path' => ['/dir/somedir/./somefile.ext', '/dir/somedir/somefile.ext'],
188  'two dots path' => ['/dir/somedir/../somefile.ext', '/dir/somefile.ext'],
189  'two times two dots path' => ['/dir/../somedir/../somefile.ext', '/somefile.ext']
190  ];
191  }
192 
199  public function testOffsetPath($relatedPath, $path, $expectedResult)
200  {
201  $result = $this->_model->offsetPath($relatedPath, $path);
202  $this->assertEquals($expectedResult, $result);
203  }
204 
208  public function offsetPathDataProvider()
209  {
210  return [
211  'local path' => [
212  '/some/directory/two/another/file.ext',
213  '/some/directory/one/file.ext',
214  '../two/another',
215  ],
216  'local path reverted' => [
217  '/some/directory/one/file.ext',
218  '/some/directory/two/another/file.ext',
219  '../../one',
220  ],
221  'url' => [
222  'http://example.com/images/logo.gif',
223  'http://example.com/themes/demo/css/styles.css',
224  '../../../images',
225  ],
226  'same path' => [
227  '/some/directory/file.ext',
228  '/some/directory/file1.ext',
229  '.',
230  ],
231  'non-normalized' => [
232  '/some/directory/../one/file.ext',
233  '/some/directory/./two/another/file.ext',
234  '../../../one',
235  ],
236  ];
237  }
238 
239  public function testGetEmailTemplateFile()
240  {
241  $locale = \Magento\Setup\Module\I18n\Locale::DEFAULT_SYSTEM_LOCALE;
242  $params = [
243  'area' => 'some_area',
244  'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
245  'module' => 'Some_Module',
246  'locale' => $locale
247  ];
248  $file = 'Some_Module::some_file.ext';
249  $expected = 'path/to/some_file.ext';
250 
251  $this->_emailTemplateFileResolution->expects($this->once())
252  ->method('getFile')
253  ->with($params['area'], $params['themeModel'], $locale, $file, 'Some_Module')
254  ->will($this->returnValue($expected));
255 
256  $actual = $this->_model->getEmailTemplateFileName($file, $params, 'Some_Module');
257  $this->assertEquals($expected, $actual);
258  }
259 }
testOffsetPath($relatedPath, $path, $expectedResult)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18