MinificationTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit\Asset;
  7. use Magento\Framework\App\State;
  8. use Magento\Framework\View\Asset\Minification;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Unit test for Magento\Framework\View\Asset\Minification
  12. */
  13. class MinificationTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\View\Asset\Minification
  17. */
  18. protected $minification;
  19. /**
  20. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $scopeConfigMock;
  23. /**
  24. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $appStateMock;
  27. /**
  28. * {@inheritDoc}
  29. */
  30. protected function setUp()
  31. {
  32. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->appStateMock = $this->getMockBuilder(\Magento\Framework\App\State::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->minification = new Minification(
  39. $this->scopeConfigMock,
  40. $this->appStateMock
  41. );
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function testIsEnabled()
  47. {
  48. }
  49. /**
  50. * @param bool $configFlag
  51. * @param string $appMode
  52. * @param bool $result
  53. * @dataProvider isEnabledDataProvider
  54. * @return void
  55. */
  56. public function testIsAssetMinification($configFlag, $appMode, $result)
  57. {
  58. $contentType = 'content type';
  59. $this->scopeConfigMock
  60. ->expects($this->any())
  61. ->method('isSetFlag')
  62. ->with(
  63. sprintf(Minification::XML_PATH_MINIFICATION_ENABLED, $contentType),
  64. ScopeInterface::SCOPE_STORE
  65. )
  66. ->willReturn($configFlag);
  67. $this->appStateMock
  68. ->expects($this->any())
  69. ->method('getMode')
  70. ->willReturn($appMode);
  71. $this->assertEquals($result, $this->minification->isEnabled($contentType));
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function isEnabledDataProvider()
  77. {
  78. return [
  79. [false, State::MODE_DEFAULT, false],
  80. [false, State::MODE_PRODUCTION, false],
  81. [false, State::MODE_DEVELOPER, false],
  82. [true, State::MODE_DEFAULT, true],
  83. [true, State::MODE_PRODUCTION, true],
  84. [true, State::MODE_DEVELOPER, false]
  85. ];
  86. }
  87. /**
  88. * @param string $filename
  89. * @param bool $isEnabled
  90. * @param string $expected
  91. * @dataProvider addMinifiedSignDataProvider
  92. */
  93. public function testAddMinifiedSign($filename, $isEnabled, $expected)
  94. {
  95. $this->scopeConfigMock
  96. ->expects($this->any())
  97. ->method('isSetFlag')
  98. ->willReturn($isEnabled);
  99. $this->appStateMock
  100. ->expects($this->any())
  101. ->method('getMode')
  102. ->willReturn(State::MODE_DEFAULT);
  103. $this->assertEquals(
  104. $expected,
  105. $this->minification->addMinifiedSign($filename)
  106. );
  107. }
  108. /**
  109. * @return array
  110. */
  111. public function addMinifiedSignDataProvider()
  112. {
  113. return [
  114. ['test.css', true, 'test.min.css'],
  115. ['test.css', false, 'test.css'],
  116. ['test.min.css', true, 'test.min.css']
  117. ];
  118. }
  119. /**
  120. * @param string $filename
  121. * @param bool $isEnabled
  122. * @param string $expected
  123. * @dataProvider removeMinifiedSignDataProvider
  124. */
  125. public function testRemoveMinifiedSign($filename, $isEnabled, $expected)
  126. {
  127. $this->scopeConfigMock
  128. ->expects($this->any())
  129. ->method('isSetFlag')
  130. ->willReturn($isEnabled);
  131. $this->appStateMock
  132. ->expects($this->any())
  133. ->method('getMode')
  134. ->willReturn(State::MODE_DEFAULT);
  135. $this->assertEquals(
  136. $expected,
  137. $this->minification->removeMinifiedSign($filename)
  138. );
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function removeMinifiedSignDataProvider()
  144. {
  145. return [
  146. ['test.css', true, 'test.css'],
  147. ['test.min.css', true, 'test.css'],
  148. ['test.min.css', false, 'test.min.css']
  149. ];
  150. }
  151. /**
  152. * @param string $filename
  153. * @param bool $result
  154. * @return void
  155. * @dataProvider isMinifiedFilenameDataProvider
  156. */
  157. public function testIsMinifiedFilename($filename, $result)
  158. {
  159. $this->assertEquals(
  160. $result,
  161. $this->minification->isMinifiedFilename($filename)
  162. );
  163. }
  164. /**
  165. * @return array
  166. */
  167. public function isMinifiedFilenameDataProvider()
  168. {
  169. return [
  170. ['test.min.css', true],
  171. ['test.mincss', false],
  172. ['testmin.css', false],
  173. ['test.css', false],
  174. ['test.min', false]
  175. ];
  176. }
  177. /**
  178. * Test dev/js/minify_exclude system value as array
  179. *
  180. * @return void
  181. */
  182. public function testGetExcludes()
  183. {
  184. $this->scopeConfigMock
  185. ->expects($this->once())
  186. ->method('getValue')
  187. ->with('dev/js/minify_exclude')
  188. ->willReturn([
  189. 'tiny_mce' => '/tiny_mce/',
  190. 'some_other_unique_name' => '/tiny_mce2/'
  191. ]);
  192. $expected = ['/tiny_mce/', '/tiny_mce2/'];
  193. $this->assertEquals($expected, $this->minification->getExcludes('js'));
  194. /** check cache: */
  195. $this->assertEquals($expected, $this->minification->getExcludes('js'));
  196. }
  197. /**
  198. * Test dev/js/minify_exclude system value backward compatibility when value was a string
  199. *
  200. * @param string $value
  201. * @param array $expectedValue
  202. * @return void
  203. *
  204. * @dataProvider getExcludesTinyMceAsStringDataProvider
  205. */
  206. public function testGetExcludesTinyMceAsString(string $value, array $expectedValue)
  207. {
  208. $this->scopeConfigMock
  209. ->expects($this->once())
  210. ->method('getValue')
  211. ->with('dev/js/minify_exclude')
  212. ->willReturn($value);
  213. $this->assertEquals($expectedValue, $this->minification->getExcludes('js'));
  214. /** check cache: */
  215. $this->assertEquals($expectedValue, $this->minification->getExcludes('js'));
  216. }
  217. /**
  218. * @return array
  219. */
  220. public function getExcludesTinyMceAsStringDataProvider()
  221. {
  222. return [
  223. ["/tiny_mce/ \n /tiny_mce2/", ['/tiny_mce/', '/tiny_mce2/']],
  224. ['/tiny_mce/', ['/tiny_mce/']],
  225. [' /tiny_mce/', ['/tiny_mce/']],
  226. ];
  227. }
  228. }