FileSystemTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test for view filesystem model
  8. */
  9. namespace Magento\Framework\View\Test\Unit;
  10. class FileSystemTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\View\FileSystem|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $_model;
  16. /**
  17. * @var \Magento\Framework\View\Design\FileResolution\Fallback\File|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $_fileResolution;
  20. /**
  21. * @var \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $_templateFileResolution;
  24. /**
  25. * @var \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $_localeFileResolution;
  28. /**
  29. * @var \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $_staticFileResolution;
  32. /**
  33. * @var \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile
  34. * |\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $_emailTemplateFileResolution;
  37. /**
  38. * @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $_assetRepo;
  41. protected function setUp()
  42. {
  43. $this->_fileResolution = $this->createMock(\Magento\Framework\View\Design\FileResolution\Fallback\File::class);
  44. $this->_templateFileResolution = $this->createMock(
  45. \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile::class
  46. );
  47. $this->_localeFileResolution = $this->createMock(
  48. \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile::class
  49. );
  50. $this->_staticFileResolution = $this->createMock(
  51. \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile::class
  52. );
  53. $this->_emailTemplateFileResolution = $this->createMock(
  54. \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile::class
  55. );
  56. $this->_assetRepo = $this->createPartialMock(
  57. \Magento\Framework\View\Asset\Repository::class,
  58. ['extractScope', 'updateDesignParams', 'createAsset']
  59. );
  60. $this->_model = new \Magento\Framework\View\FileSystem(
  61. $this->_fileResolution,
  62. $this->_templateFileResolution,
  63. $this->_localeFileResolution,
  64. $this->_staticFileResolution,
  65. $this->_emailTemplateFileResolution,
  66. $this->_assetRepo
  67. );
  68. }
  69. public function testGetFilename()
  70. {
  71. $params = [
  72. 'area' => 'some_area',
  73. 'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
  74. 'module' => 'Some_Module', //It should be set in \Magento\Framework\View\Asset\Repository::extractScope
  75. // but PHPUnit has troubles with passing arguments by reference
  76. ];
  77. $file = 'Some_Module::some_file.ext';
  78. $expected = 'path/to/some_file.ext';
  79. $this->_fileResolution->expects($this->once())
  80. ->method('getFile')
  81. ->with($params['area'], $params['themeModel'], 'some_file.ext', 'Some_Module')
  82. ->will($this->returnValue($expected));
  83. $this->_assetRepo->expects($this->any())
  84. ->method('extractScope')
  85. ->with($file, $params)
  86. ->will($this->returnValue('some_file.ext'));
  87. $actual = $this->_model->getFilename($file, $params);
  88. $this->assertEquals($expected, $actual);
  89. }
  90. public function testGetTemplateFileName()
  91. {
  92. $params = [
  93. 'area' => 'some_area',
  94. 'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
  95. 'module' => 'Some_Module', //It should be set in \Magento\Framework\View\Asset\Repository::extractScope
  96. // but PHPUnit has troubles with passing arguments by reference
  97. ];
  98. $file = 'Some_Module::some_file.ext';
  99. $expected = 'path/to/some_file.ext';
  100. $this->_templateFileResolution->expects($this->once())
  101. ->method('getFile')
  102. ->with($params['area'], $params['themeModel'], 'some_file.ext', 'Some_Module')
  103. ->will($this->returnValue($expected));
  104. $this->_assetRepo->expects($this->any())
  105. ->method('extractScope')
  106. ->with($file, $params)
  107. ->will($this->returnValue('some_file.ext'));
  108. $actual = $this->_model->getTemplateFileName($file, $params);
  109. $this->assertEquals($expected, $actual);
  110. }
  111. public function testGetLocaleFileName()
  112. {
  113. $params = [
  114. 'area' => 'some_area',
  115. 'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
  116. 'locale' => 'some_locale',
  117. ];
  118. $file = 'some_file.ext';
  119. $expected = 'path/to/some_file.ext';
  120. $this->_localeFileResolution->expects($this->once())
  121. ->method('getFile')
  122. ->with($params['area'], $params['themeModel'], $params['locale'], 'some_file.ext')
  123. ->will($this->returnValue($expected));
  124. $actual = $this->_model->getLocaleFileName($file, $params);
  125. $this->assertEquals($expected, $actual);
  126. }
  127. public function testGetViewFile()
  128. {
  129. $params = [
  130. 'area' => 'some_area',
  131. 'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
  132. 'locale' => 'some_locale',
  133. 'module' => 'Some_Module',
  134. ];
  135. $file = 'Some_Module::some_file.ext';
  136. $expected = 'path/to/some_file.ext';
  137. $this->_staticFileResolution->expects($this->once())
  138. ->method('getFile')
  139. ->with($params['area'], $params['themeModel'], $params['locale'], 'some_file.ext', 'Some_Module')
  140. ->will($this->returnValue($expected));
  141. $actual = $this->_model->getStaticFileName($file, $params);
  142. $this->assertEquals($expected, $actual);
  143. }
  144. /**
  145. * @param string $path
  146. * @param string $expectedResult
  147. * @dataProvider normalizePathDataProvider
  148. */
  149. public function testNormalizePath($path, $expectedResult)
  150. {
  151. $result = $this->_model->normalizePath($path);
  152. $this->assertEquals($expectedResult, $result);
  153. }
  154. /**
  155. * @return array
  156. */
  157. public function normalizePathDataProvider()
  158. {
  159. return [
  160. 'standard path' => ['/dir/somedir/somefile.ext', '/dir/somedir/somefile.ext'],
  161. 'one dot path' => ['/dir/somedir/./somefile.ext', '/dir/somedir/somefile.ext'],
  162. 'two dots path' => ['/dir/somedir/../somefile.ext', '/dir/somefile.ext'],
  163. 'two times two dots path' => ['/dir/../somedir/../somefile.ext', '/somefile.ext']
  164. ];
  165. }
  166. /**
  167. * @param string $relatedPath
  168. * @param string $path
  169. * @param string $expectedResult
  170. * @dataProvider offsetPathDataProvider
  171. */
  172. public function testOffsetPath($relatedPath, $path, $expectedResult)
  173. {
  174. $result = $this->_model->offsetPath($relatedPath, $path);
  175. $this->assertEquals($expectedResult, $result);
  176. }
  177. /**
  178. * @return array
  179. */
  180. public function offsetPathDataProvider()
  181. {
  182. return [
  183. 'local path' => [
  184. '/some/directory/two/another/file.ext',
  185. '/some/directory/one/file.ext',
  186. '../two/another',
  187. ],
  188. 'local path reverted' => [
  189. '/some/directory/one/file.ext',
  190. '/some/directory/two/another/file.ext',
  191. '../../one',
  192. ],
  193. 'url' => [
  194. 'http://example.com/images/logo.gif',
  195. 'http://example.com/themes/demo/css/styles.css',
  196. '../../../images',
  197. ],
  198. 'same path' => [
  199. '/some/directory/file.ext',
  200. '/some/directory/file1.ext',
  201. '.',
  202. ],
  203. 'non-normalized' => [
  204. '/some/directory/../one/file.ext',
  205. '/some/directory/./two/another/file.ext',
  206. '../../../one',
  207. ],
  208. ];
  209. }
  210. public function testGetEmailTemplateFile()
  211. {
  212. $locale = \Magento\Setup\Module\I18n\Locale::DEFAULT_SYSTEM_LOCALE;
  213. $params = [
  214. 'area' => 'some_area',
  215. 'themeModel' => $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class),
  216. 'module' => 'Some_Module',
  217. 'locale' => $locale
  218. ];
  219. $file = 'Some_Module::some_file.ext';
  220. $expected = 'path/to/some_file.ext';
  221. $this->_emailTemplateFileResolution->expects($this->once())
  222. ->method('getFile')
  223. ->with($params['area'], $params['themeModel'], $locale, $file, 'Some_Module')
  224. ->will($this->returnValue($expected));
  225. $actual = $this->_model->getEmailTemplateFileName($file, $params, 'Some_Module');
  226. $this->assertEquals($expected, $actual);
  227. }
  228. }