diff --git a/dev/tests/integration/testsuite/Magento/Framework/App/View/Deployment/VersionTest.php b/dev/tests/integration/testsuite/Magento/Framework/App/View/Deployment/VersionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5a313c318f9e9126ac84848a3b7ddf62e71d6585 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Framework/App/View/Deployment/VersionTest.php @@ -0,0 +1,105 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\App\View\Deployment; + +use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\App\State; +use Magento\Framework\App\View\Deployment\Version\Storage\File; +use Magento\Framework\Filesystem\Directory\WriteInterface; + +class VersionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var File + */ + private $fileStorage; + + /** + * @var WriteInterface + */ + private $directoryWrite; + + /** + * @var string + */ + private $fileName = 'deployed_version.txt'; + + public function setUp() + { + $this->fileStorage = ObjectManager::getInstance()->create( + File::class, + [ + 'directoryCode' => DirectoryList::STATIC_VIEW, + 'fileName' => $this->fileName + ] + ); + /** @var \Magento\TestFramework\App\Filesystem $filesystem */ + $filesystem = ObjectManager::getInstance()->get(\Magento\TestFramework\App\Filesystem::class); + $this->directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW); + $this->removeDeployVersionFile(); + } + + /** + * @param string $mode + * @return Version + */ + public function getVersionModel($mode) + { + $appState = ObjectManager::getInstance()->create( + State::class, + [ + 'mode' => $mode + ] + ); + return ObjectManager::getInstance()->create( + Version::class, + [ + 'appState' => $appState + ] + ); + } + + protected function tearDown() + { + $this->removeDeployVersionFile(); + } + + private function removeDeployVersionFile() + { + if ($this->directoryWrite->isExist($this->fileName)) { + $this->directoryWrite->delete($this->fileName); + } + } + + /** + * @expectedException \UnexpectedValueException + */ + public function testGetValueInProductionModeWithoutVersion() + { + $this->assertFalse($this->directoryWrite->isExist($this->fileName)); + $this->getVersionModel(State::MODE_PRODUCTION)->getValue(); + } + + public function testGetValueInDeveloperMode() + { + $this->assertFalse($this->directoryWrite->isExist($this->fileName)); + $this->getVersionModel(State::MODE_DEVELOPER)->getValue(); + $this->assertTrue($this->directoryWrite->isExist($this->fileName)); + } + + /** + * Assert that version is not regenerated on each request in developer mode + */ + public function testGetValue() + { + $this->assertFalse($this->directoryWrite->isExist($this->fileName)); + $versionModel = $this->getVersionModel(State::MODE_DEVELOPER); + $version = $versionModel->getValue(); + $this->assertTrue($this->directoryWrite->isExist($this->fileName)); + $this->assertEquals($version, $versionModel->getValue()); + } +} diff --git a/dev/tools/grunt/configs/clean.js b/dev/tools/grunt/configs/clean.js index 53bcd8a1d830ebda056e59c6ba592106abcdde72..e720b6c40c27e45e8ca27579727ee6dec67bd400 100644 --- a/dev/tools/grunt/configs/clean.js +++ b/dev/tools/grunt/configs/clean.js @@ -21,7 +21,8 @@ _.each(themes, function(theme, name) { "<%= path.tmp %>/cache/**/*", "<%= combo.autopath(\""+name+"\", path.pub ) %>**/*", "<%= combo.autopath(\""+name+"\", path.tmpLess) %>**/*", - "<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*" + "<%= combo.autopath(\""+name+"\", path.tmpSource) %>**/*", + "<%= path.deployedVersion %>" ] } ] @@ -56,7 +57,8 @@ var cleanOptions = { "dot": true, "src": [ "<%= path.pub %>frontend/**/*", - "<%= path.pub %>adminhtml/**/*" + "<%= path.pub %>adminhtml/**/*", + "<%= path.deployedVersion %>" ] } ] @@ -73,7 +75,8 @@ var cleanOptions = { "<%= path.pub %>frontend/**/*.less", "<%= path.pub %>frontend/**/*.css", "<%= path.pub %>adminhtml/**/*.less", - "<%= path.pub %>adminhtml/**/*.css" + "<%= path.pub %>adminhtml/**/*.css", + "<%= path.deployedVersion %>" ] } ] @@ -102,7 +105,8 @@ var cleanOptions = { "src": [ "<%= path.pub %>**/*.js", "<%= path.pub %>**/*.html", - "<%= path.pub %>_requirejs/**/*" + "<%= path.pub %>_requirejs/**/*", + "<%= path.deployedVersion %>" ] } ] diff --git a/dev/tools/grunt/configs/path.js b/dev/tools/grunt/configs/path.js index 03621998c14a602c46eec390263aa8450dfc2657..e6a9cf71e81354b8e6b95e6d1623bbf799d831cf 100644 --- a/dev/tools/grunt/configs/path.js +++ b/dev/tools/grunt/configs/path.js @@ -13,6 +13,7 @@ module.exports = { tmpLess: 'var/view_preprocessed/less/', tmpSource: 'var/view_preprocessed/source/', tmp: 'var', + deployedVersion: 'pub/static/deployed_version.txt', css: { setup: 'setup/pub/styles', updater: '../magento2-updater/pub/css' diff --git a/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/Version/Storage/FileTest.php b/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/Version/Storage/FileTest.php index 3981511ad47018c8847c8c9cddf889122e059d58..e7206bf5566075f4df04a977185b5f343ed42b8f 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/Version/Storage/FileTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/Version/Storage/FileTest.php @@ -34,48 +34,15 @@ class FileTest extends \PHPUnit_Framework_TestCase public function testLoad() { - $this->directory - ->expects($this->once()) - ->method('readFile') + $this->directory->expects($this->once()) + ->method('isReadable') ->with('fixture_file.txt') - ->will($this->returnValue('123')); - $this->assertEquals('123', $this->object->load()); - } - - /** - * @expectedException \Exception - * @expectedExceptionMessage Exception to be propagated - */ - public function testLoadExceptionPropagation() - { - $this->directory - ->expects($this->once()) + ->willReturn(true); + $this->directory->expects($this->once()) ->method('readFile') ->with('fixture_file.txt') - ->will($this->throwException(new \Exception('Exception to be propagated'))); - $this->object->load(); - } - - /** - * @expectedException \UnexpectedValueException - * @expectedExceptionMessage Unable to retrieve deployment version of static files from the file system - */ - public function testLoadExceptionWrapping() - { - $filesystemException = new \Magento\Framework\Exception\FileSystemException( - new \Magento\Framework\Phrase('File does not exist') - ); - $this->directory - ->expects($this->once()) - ->method('readFile') - ->with('fixture_file.txt') - ->will($this->throwException($filesystemException)); - try { - $this->object->load(); - } catch (\Exception $e) { - $this->assertSame($filesystemException, $e->getPrevious(), 'Wrapping of original exception is expected'); - throw $e; - } + ->willReturn('123'); + $this->assertEquals('123', $this->object->load()); } public function testSave() 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 187c043945d059992d6dc59a8713fc413b1a9752..8d804102f7a56038fded4118692931030593caa7 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 @@ -6,7 +6,6 @@ namespace Magento\Framework\App\Test\Unit\View\Deployment; use Magento\Framework\App\View\Deployment\Version; -use Magento\Framework\Exception\FileSystemException; /** * Class VersionTest @@ -45,17 +44,6 @@ class VersionTest extends \PHPUnit_Framework_TestCase $objectManager->setBackwardCompatibleProperty($this->object, 'logger', $this->loggerMock); } - public function testGetValueDeveloperMode() - { - $this->appStateMock - ->expects($this->once()) - ->method('getMode') - ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER)); - $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 - } - /** * @param string $appMode * @dataProvider getValueFromStorageDataProvider @@ -81,106 +69,46 @@ class VersionTest extends \PHPUnit_Framework_TestCase ]; } - /** - * $param bool $isUnexpectedValueExceptionThrown - * $param bool $isFileSystemExceptionThrown - * @dataProvider getValueDefaultModeDataProvider - */ - public function testGetValueDefaultMode( - $isUnexpectedValueExceptionThrown, - $isFileSystemExceptionThrown = null - ) { - $versionType = 'integer'; - $this->appStateMock - ->expects($this->once()) - ->method('getMode') - ->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()); + public function testGetValueInNonProductionMode() + { + $version = 123123123123; + $this->versionStorageMock->expects($this->once()) + ->method('load') + ->willReturn($version); + + $this->assertEquals($version, $this->object->getValue()); $this->object->getValue(); } /** - * @return array + * @expectedException \UnexpectedValueException */ - public function getValueDefaultModeDataProvider() + public function testGetValueWithProductionModeAndException() { - return [ - [false], - [true, false], - [true, true] - ]; - } - - /** - * @param bool $isUnexpectedValueExceptionThrown - * @dataProvider getValueProductionModeDataProvider - */ - public function testGetValueProductionMode( - $isUnexpectedValueExceptionThrown - ) { - $this->appStateMock - ->expects($this->once()) + $this->versionStorageMock->expects($this->once()) + ->method('load') + ->willReturn(false); + $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->loggerMock->expects($this->once()) + ->method('critical') + ->with('Can not load static content version.'); + $this->object->getValue(); } - /** - * @return array - */ - public function getValueProductionModeDataProvider() + public function testGetValueWithProductionMode() { - return [ - [false], - [true], - ]; + $this->versionStorageMock->expects($this->once()) + ->method('load') + ->willReturn(false); + $this->appStateMock->expects($this->once()) + ->method('getMode') + ->willReturn(\Magento\Framework\App\State::MODE_DEFAULT); + $this->versionStorageMock->expects($this->once()) + ->method('save'); + + $this->assertNotNull($this->object->getValue()); } } diff --git a/lib/internal/Magento/Framework/App/View/Deployment/Version.php b/lib/internal/Magento/Framework/App/View/Deployment/Version.php index 7f6dc7fa9285ccc3987d84091bc9edb76d09f794..73d4a0c926cea88c5b2cfbd6db34888f3ef651fb 100644 --- a/lib/internal/Magento/Framework/App/View/Deployment/Version.php +++ b/lib/internal/Magento/Framework/App/View/Deployment/Version.php @@ -7,7 +7,6 @@ namespace Magento\Framework\App\View\Deployment; use Psr\Log\LoggerInterface; -use Magento\Framework\Exception\FileSystemException; /** * Deployment version of static files @@ -67,23 +66,16 @@ class Version */ protected function readValue($appMode) { - 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.'); - } + $result = $this->versionStorage->load(); + if (!$result) { + if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) { + $this->getLogger()->critical('Can not load static content version.'); + throw new \UnexpectedValueException( + "Unable to retrieve deployment version of static files from the file system." + ); } + $result = $this->generateVersion(); + $this->versionStorage->save($result); } return $result; } diff --git a/lib/internal/Magento/Framework/App/View/Deployment/Version/Storage/File.php b/lib/internal/Magento/Framework/App/View/Deployment/Version/Storage/File.php index 4f8813df774d7edc9a7490e31f447e6827dd2e38..0967cb634cbd706689bc0d54aa2c9a1b5b7a8b5e 100644 --- a/lib/internal/Magento/Framework/App/View/Deployment/Version/Storage/File.php +++ b/lib/internal/Magento/Framework/App/View/Deployment/Version/Storage/File.php @@ -40,15 +40,10 @@ class File implements \Magento\Framework\App\View\Deployment\Version\StorageInte */ public function load() { - try { + if ($this->directory->isReadable($this->fileName)) { return $this->directory->readFile($this->fileName); - } catch (\Magento\Framework\Exception\FileSystemException $e) { - throw new \UnexpectedValueException( - 'Unable to retrieve deployment version of static files from the file system.', - 0, - $e - ); } + return false; } /**