diff --git a/app/code/Magento/Backend/etc/config.xml b/app/code/Magento/Backend/etc/config.xml index 459dd377e36ba..760bba1fc12f4 100644 --- a/app/code/Magento/Backend/etc/config.xml +++ b/app/code/Magento/Backend/etc/config.xml @@ -11,6 +11,9 @@ + + 1 + diff --git a/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php b/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php index 30f398b4f6000..187c043945d05 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php @@ -5,7 +5,8 @@ */ namespace Magento\Framework\App\Test\Unit\View\Deployment; -use \Magento\Framework\App\View\Deployment\Version; +use Magento\Framework\App\View\Deployment\Version; +use Magento\Framework\Exception\FileSystemException; /** * Class VersionTest @@ -18,29 +19,39 @@ class VersionTest extends \PHPUnit_Framework_TestCase private $object; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject */ - private $appState; + private $appStateMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\View\Deployment\Version\StorageInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $versionStorage; + private $versionStorageMock; + + /** + * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $loggerMock; protected function setUp() { - $this->appState = $this->getMock(\Magento\Framework\App\State::class, [], [], '', false); - $this->versionStorage = $this->getMock(\Magento\Framework\App\View\Deployment\Version\StorageInterface::class); - $this->object = new Version($this->appState, $this->versionStorage); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->appStateMock = $this->getMock(\Magento\Framework\App\State::class, [], [], '', false); + $this->versionStorageMock = $this->getMock( + \Magento\Framework\App\View\Deployment\Version\StorageInterface::class + ); + $this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); + $this->object = new Version($this->appStateMock, $this->versionStorageMock); + $objectManager->setBackwardCompatibleProperty($this->object, 'logger', $this->loggerMock); } public function testGetValueDeveloperMode() { - $this->appState + $this->appStateMock ->expects($this->once()) ->method('getMode') ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER)); - $this->versionStorage->expects($this->never())->method($this->anything()); + $this->versionStorageMock->expects($this->never())->method($this->anything()); $this->assertInternalType('integer', $this->object->getValue()); $this->object->getValue(); // Ensure computation occurs only once and result is cached in memory } @@ -51,12 +62,12 @@ public function testGetValueDeveloperMode() */ public function testGetValueFromStorage($appMode) { - $this->appState + $this->appStateMock ->expects($this->once()) ->method('getMode') ->will($this->returnValue($appMode)); - $this->versionStorage->expects($this->once())->method('load')->will($this->returnValue('123')); - $this->versionStorage->expects($this->never())->method('save'); + $this->versionStorageMock->expects($this->once())->method('load')->will($this->returnValue('123')); + $this->versionStorageMock->expects($this->never())->method('save'); $this->assertEquals('123', $this->object->getValue()); $this->object->getValue(); // Ensure caching in memory } @@ -70,20 +81,106 @@ public function getValueFromStorageDataProvider() ]; } - public function testGetValueDefaultModeSaving() - { + /** + * $param bool $isUnexpectedValueExceptionThrown + * $param bool $isFileSystemExceptionThrown + * @dataProvider getValueDefaultModeDataProvider + */ + public function testGetValueDefaultMode( + $isUnexpectedValueExceptionThrown, + $isFileSystemExceptionThrown = null + ) { $versionType = 'integer'; - $this->appState + $this->appStateMock ->expects($this->once()) ->method('getMode') - ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEFAULT)); - $storageException = new \UnexpectedValueException('Does not exist in the storage'); - $this->versionStorage - ->expects($this->once()) - ->method('load') - ->will($this->throwException($storageException)); - $this->versionStorage->expects($this->once())->method('save')->with($this->isType($versionType)); + ->willReturn(\Magento\Framework\App\State::MODE_DEFAULT); + if ($isUnexpectedValueExceptionThrown) { + $storageException = new \UnexpectedValueException('Does not exist in the storage'); + $this->versionStorageMock + ->expects($this->once()) + ->method('load') + ->will($this->throwException($storageException)); + $this->versionStorageMock->expects($this->once()) + ->method('save') + ->with($this->isType($versionType)); + if ($isFileSystemExceptionThrown) { + $fileSystemException = new FileSystemException( + new \Magento\Framework\Phrase('Can not load static content version') + ); + $this->versionStorageMock + ->expects($this->once()) + ->method('save') + ->will($this->throwException($fileSystemException)); + $this->loggerMock->expects($this->once()) + ->method('critical') + ->with('Can not save static content version.'); + } else { + $this->loggerMock->expects($this->never()) + ->method('critical'); + } + } else { + $this->versionStorageMock + ->expects($this->once()) + ->method('load') + ->willReturn(1475779229); + $this->loggerMock->expects($this->never()) + ->method('critical'); + } $this->assertInternalType($versionType, $this->object->getValue()); - $this->object->getValue(); // Ensure caching in memory + $this->object->getValue(); + } + + /** + * @return array + */ + public function getValueDefaultModeDataProvider() + { + return [ + [false], + [true, false], + [true, true] + ]; + } + + /** + * @param bool $isUnexpectedValueExceptionThrown + * @dataProvider getValueProductionModeDataProvider + */ + public function testGetValueProductionMode( + $isUnexpectedValueExceptionThrown + ) { + $this->appStateMock + ->expects($this->once()) + ->method('getMode') + ->willReturn(\Magento\Framework\App\State::MODE_PRODUCTION); + if ($isUnexpectedValueExceptionThrown) { + $storageException = new \UnexpectedValueException('Does not exist in the storage'); + $this->versionStorageMock + ->expects($this->once()) + ->method('load') + ->will($this->throwException($storageException)); + $this->loggerMock->expects($this->once()) + ->method('critical') + ->with('Can not load static content version.'); + } else { + $this->versionStorageMock + ->expects($this->once()) + ->method('load') + ->willReturn(1475779229); + } + $this->assertInternalType('integer', $this->object->getValue()); + $this->object->getValue(); + } + + /** + * @return array + */ + public function getValueProductionModeDataProvider() + { + return [ + [false], + [true], + ]; } } diff --git a/lib/internal/Magento/Framework/App/View/Deployment/Version.php b/lib/internal/Magento/Framework/App/View/Deployment/Version.php index 8e927e47921a0..7f6dc7fa9285c 100644 --- a/lib/internal/Magento/Framework/App/View/Deployment/Version.php +++ b/lib/internal/Magento/Framework/App/View/Deployment/Version.php @@ -6,6 +6,9 @@ namespace Magento\Framework\App\View\Deployment; +use Psr\Log\LoggerInterface; +use Magento\Framework\Exception\FileSystemException; + /** * Deployment version of static files */ @@ -26,6 +29,11 @@ class Version */ private $cachedValue; + /** + * @var LoggerInterface + */ + private $logger; + /** * @param \Magento\Framework\App\State $appState * @param Version\StorageInterface $versionStorage @@ -59,23 +67,49 @@ public function getValue() */ protected function readValue($appMode) { - switch ($appMode) { - case \Magento\Framework\App\State::MODE_DEFAULT: - try { - $result = $this->versionStorage->load(); - } catch (\UnexpectedValueException $e) { - $result = (new \DateTime())->getTimestamp(); - $this->versionStorage->save($result); + if ($appMode == \Magento\Framework\App\State::MODE_DEVELOPER) { + $result = $this->generateVersion(); + } else { + try { + $result = $this->versionStorage->load(); + } catch (\UnexpectedValueException $e) { + $result = $this->generateVersion(); + if ($appMode == \Magento\Framework\App\State::MODE_DEFAULT) { + try { + $this->versionStorage->save($result); + } catch (FileSystemException $e) { + $this->getLogger()->critical('Can not save static content version.'); + } + } else { + $this->getLogger()->critical('Can not load static content version.'); } - break; + } + } + return $result; + } - case \Magento\Framework\App\State::MODE_DEVELOPER: - $result = (new \DateTime())->getTimestamp(); - break; + /** + * Generate version of static content + * + * @return int + */ + private function generateVersion() + { + return time(); + } - default: - $result = $this->versionStorage->load(); + /** + * Get logger + * + * @return LoggerInterface + * @deprecated + */ + private function getLogger() + { + if ($this->logger == null) { + $this->logger = \Magento\Framework\App\ObjectManager::getInstance() + ->get(LoggerInterface::class); } - return $result; + return $this->logger; } }