Example #1
0
 /**
  * Clean compiled JS/CSS when updating configuration settings
  *
  * @return $this
  */
 public function afterSave()
 {
     if ($this->isValueChanged()) {
         $this->_mergeService->cleanMergedJsCss();
     }
     return parent::afterSave();
 }
Example #2
0
 /**
  * @param array $groupAssets
  * @param \Magento\Framework\View\Asset\PropertyGroup $group
  * @return array
  */
 protected function processMerge($groupAssets, $group)
 {
     if ($group->getProperty(GroupedCollection::PROPERTY_CAN_MERGE) && count($groupAssets) > 1) {
         $groupAssets = $this->assetMergeService->getMergedAssets($groupAssets, $group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE));
     }
     return $groupAssets;
 }
 /**
  * @param $groupOne
  * @param $groupTwo
  * @param $expectedResult
  * @dataProvider dataProviderRenderAssets
  */
 public function testRenderAssets($groupOne, $groupTwo, $expectedResult)
 {
     $assetUrl = 'url';
     $assetNoRoutUrl = 'no_route_url';
     $exception = new \Magento\Framework\Exception('my message');
     $assetMockOne = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface');
     $assetMockOne->expects($this->exactly(2))->method('getUrl')->willReturn($assetUrl);
     $groupAssetsOne = [$assetMockOne, $assetMockOne];
     $groupMockOne = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\PropertyGroup')->disableOriginalConstructor()->getMock();
     $groupMockOne->expects($this->once())->method('getAll')->willReturn($groupAssetsOne);
     $groupMockOne->expects($this->any())->method('getProperty')->willReturnMap([[GroupedCollection::PROPERTY_CAN_MERGE, true], [GroupedCollection::PROPERTY_CONTENT_TYPE, $groupOne['type']], ['attributes', $groupOne['attributes']], ['ie_condition', $groupOne['condition']]]);
     $assetMockTwo = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface');
     $assetMockTwo->expects($this->once())->method('getUrl')->willThrowException($exception);
     $groupAssetsTwo = [$assetMockTwo];
     $groupMockTwo = $this->getMockBuilder('Magento\\Framework\\View\\Asset\\PropertyGroup')->disableOriginalConstructor()->getMock();
     $groupMockTwo->expects($this->once())->method('getAll')->willReturn($groupAssetsTwo);
     $groupMockTwo->expects($this->any())->method('getProperty')->willReturnMap([[GroupedCollection::PROPERTY_CAN_MERGE, true], [GroupedCollection::PROPERTY_CONTENT_TYPE, $groupTwo['type']], ['attributes', $groupTwo['attributes']], ['ie_condition', $groupTwo['condition']]]);
     $this->pageConfigMock->expects($this->once())->method('getAssetCollection')->willReturn($this->assetsCollection);
     $this->assetsCollection->expects($this->once())->method('getGroups')->willReturn([$groupMockOne, $groupMockTwo]);
     $this->assetMinifyServiceMock->expects($this->exactly(2))->method('getAssets')->willReturnArgument(0);
     $this->assetMergeServiceMock->expects($this->exactly(1))->method('getMergedAssets')->willReturnArgument(0);
     $this->loggerMock->expects($this->once())->method('critical')->with($exception);
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('', ['_direct' => 'core/index/notFound'])->willReturn($assetNoRoutUrl);
     $this->assertEquals($expectedResult, $this->renderer->renderAssets($this->renderer->getAvailableResultGroups()));
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function cleanMergedJsCss()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'cleanMergedJsCss');
     if (!$pluginInfo) {
         return parent::cleanMergedJsCss();
     } else {
         return $this->___callPlugins('cleanMergedJsCss', func_get_args(), $pluginInfo);
     }
 }
Example #5
0
 /**
  * Render HTML for the added head items
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  *
  * @return string
  */
 public function getCssJsHtml()
 {
     foreach ($this->getLayout()->getChildBlocks($this->getNameInLayout()) as $block) {
         /** @var $block \Magento\Framework\View\Element\AbstractBlock */
         if ($block instanceof \Magento\Theme\Block\Html\Head\AssetBlockInterface) {
             /** @var \Magento\Framework\View\Asset\AssetInterface $asset */
             $asset = $block->getAsset();
             $this->_pageAssets->add($block->getNameInLayout(), $asset, (array) $block->getProperties());
         }
     }
     $result = '';
     /** @var $group \Magento\Framework\View\Asset\PropertyGroup */
     foreach ($this->_pageAssets->getGroups() as $group) {
         $contentType = $group->getProperty(\Magento\Framework\View\Asset\GroupedCollection::PROPERTY_CONTENT_TYPE);
         $canMerge = $group->getProperty(\Magento\Framework\View\Asset\GroupedCollection::PROPERTY_CAN_MERGE);
         $attributes = $group->getProperty('attributes');
         $ieCondition = $group->getProperty('ie_condition');
         $flagName = $group->getProperty('flag_name');
         if ($flagName && !$this->getData($flagName)) {
             continue;
         }
         $groupAssets = $group->getAll();
         $groupAssets = $this->_assetMinifyService->getAssets($groupAssets);
         if ($canMerge && count($groupAssets) > 1) {
             $groupAssets = $this->_assetMergeService->getMergedAssets($groupAssets, $contentType);
         }
         if (!empty($attributes)) {
             if (is_array($attributes)) {
                 $attributesString = '';
                 foreach ($attributes as $name => $value) {
                     $attributesString .= ' ' . $name . '="' . $this->escapeHtml($value) . '"';
                 }
                 $attributes = $attributesString;
             } else {
                 $attributes = ' ' . $attributes;
             }
         }
         if ($contentType == 'js') {
             $groupTemplate = '<script' . $attributes . ' type="text/javascript" src="%s"></script>' . "\n";
         } else {
             if ($contentType == 'css') {
                 $attributes = ' rel="stylesheet" type="text/css"' . ($attributes ?: ' media="all"');
             }
             $groupTemplate = '<link' . $attributes . ' href="%s" />' . "\n";
         }
         $groupHtml = $this->_renderHtml($groupTemplate, $groupAssets);
         if (!empty($ieCondition)) {
             $groupHtml = '<!--[if ' . $ieCondition . ']>' . "\n" . $groupHtml . '<![endif]-->' . "\n";
         }
         $result .= $groupHtml;
     }
     return $result;
 }
Example #6
0
 /**
  * Clean compiled JS/CSS when updating url configuration settings
  *
  * @return void
  */
 protected function _afterSave()
 {
     if ($this->isValueChanged()) {
         switch ($this->getPath()) {
             case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL:
             case \Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL:
             case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL:
             case \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL:
                 $this->_mergeService->cleanMergedJsCss();
                 break;
         }
     }
 }
Example #7
0
 /**
  * @param $contentType
  * @param $attributes
  * @param $ieCondition
  * @param $expectedResult
  * @dataProvider dataProviderRenderAsset
  */
 public function testRenderAsset($contentType, $attributes, $ieCondition, $expectedResult)
 {
     $assetUrl = 'url';
     $assetNoRoutUrl = 'no_route_url';
     $exception = new \Magento\Framework\Exception('my message');
     $assetMock1 = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface');
     $assetMock1->expects($this->once())->method('getUrl')->willReturn($assetUrl);
     $assetMock2 = $this->getMock('Magento\\Framework\\View\\Asset\\AssetInterface');
     $assetMock2->expects($this->once())->method('getUrl')->willThrowException($exception);
     $groupAssets = [$assetMock1, $assetMock2];
     $this->pageConfigMock->expects($this->once())->method('getAssetCollection')->willReturn($this->assetsCollection);
     $this->assetsCollection->expects($this->once())->method('getGroups')->willReturn([$this->propertyGroupMock]);
     $this->propertyGroupMock->expects($this->once())->method('getAll')->willReturn($groupAssets);
     $this->propertyGroupMock->expects($this->any())->method('getProperty')->willReturnMap([[GroupedCollection::PROPERTY_CAN_MERGE, true], [GroupedCollection::PROPERTY_CONTENT_TYPE, $contentType], ['attributes', $attributes], ['ie_condition', $ieCondition]]);
     $this->assetMinifyServiceMock->expects($this->once())->method('getAssets')->with($groupAssets)->willReturn($groupAssets);
     $this->assetMergeServiceMock->expects($this->once())->method('getMergedAssets')->with($groupAssets, $contentType)->willReturnArgument(0);
     $this->loggerMock->expects($this->once())->method('critical')->with($exception);
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('', ['_direct' => 'core/index/notFound'])->willReturn($assetNoRoutUrl);
     $this->assertEquals($expectedResult, $this->renderer->renderAssets());
 }
Example #8
0
 public function testCleanMergedJsCss()
 {
     $mergedDir = \Magento\Framework\View\Asset\Merged::getRelativeDir();
     $this->_directory->expects($this->once())->method('delete')->with($mergedDir);
     $this->_object->cleanMergedJsCss();
 }
Example #9
0
 /**
  * Clean compiled JS/CSS when updating configuration settings
  *
  * @return void
  */
 protected function _afterSave()
 {
     if ($this->isValueChanged()) {
         $this->_mergeService->cleanMergedJsCss();
     }
 }