Skip to content

Commit

Permalink
MAGETWO-71201: Improved calculating version hash for the js-translati…
Browse files Browse the repository at this point in the history
…on.json file. #10378
  • Loading branch information
ishakhsuvarov authored Aug 17, 2017
2 parents 45e8b94 + 8cf3eec commit a6ff645
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 4 deletions.
10 changes: 10 additions & 0 deletions app/code/Magento/Translation/Block/Js.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ public function getTranslationFilePath()
{
return $this->fileManager->getTranslationFilePath();
}

/**
* Gets current version of the translation file.
*
* @return string
*/
public function getTranslationFileVersion()
{
return $this->fileManager->getTranslationFileVersion();
}
}
32 changes: 29 additions & 3 deletions app/code/Magento/Translation/Model/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Translation\Model;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Translation\Model\Inline\File as TranslationFile;

/**
* A service for handling Translation config files
Expand All @@ -32,19 +34,27 @@ class FileManager
*/
private $driverFile;

/**
* @var TranslationFile
*/
private $translationFile;

/**
* @param \Magento\Framework\View\Asset\Repository $assetRepo
* @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
* @param \Magento\Framework\Filesystem\Driver\File $driverFile,
* @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList
* @param \Magento\Framework\Filesystem\Driver\File $driverFile
* @param TranslationFile $translationFile
*/
public function __construct(
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Framework\App\Filesystem\DirectoryList $directoryList,
\Magento\Framework\Filesystem\Driver\File $driverFile
\Magento\Framework\Filesystem\Driver\File $driverFile,
\Magento\Translation\Model\Inline\File $translationFile = null
) {
$this->assetRepo = $assetRepo;
$this->directoryList = $directoryList;
$this->driverFile = $driverFile;
$this->translationFile = $translationFile ?: ObjectManager::getInstance()->get(TranslationFile::class);
}

/**
Expand Down Expand Up @@ -110,4 +120,20 @@ public function updateTranslationFileContent($content)
}
$this->driverFile->filePutContents($this->getTranslationFileFullPath(), $content);
}

/**
* Calculate translation file version hash.
*
* @return string
*/
public function getTranslationFileVersion()
{
$translationFile = $this->getTranslationFileFullPath();
if (!$this->driverFile->isExists($translationFile)) {
$this->updateTranslationFileContent($this->translationFile->getTranslationFileContent());
}

$translationFileHash = sha1_file($translationFile);
return sha1($translationFileHash . $this->getTranslationFilePath());
}
}
60 changes: 60 additions & 0 deletions app/code/Magento/Translation/Model/Inline/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Translation\Model\Inline;

use Magento\Framework\Translate\ResourceInterface;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Prepares content of inline translations file.
*/
class File
{
/**
* @var ResourceInterface
*/
private $translateResource;

/**
* @var ResolverInterface
*/
private $localeResolver;

/**
* @var Json
*/
private $jsonSerializer;

/**
* Initialize dependencies
*
* @param ResourceInterface $translateResource
* @param ResolverInterface $localeResolver
* @param Json $jsonSerializer
*/
public function __construct(
ResourceInterface $translateResource,
ResolverInterface $localeResolver,
Json $jsonSerializer
) {
$this->translateResource = $translateResource;
$this->localeResolver = $localeResolver;
$this->jsonSerializer = $jsonSerializer;
}

/**
* Generate translation file content for the current locale.
*
* @return string
*/
public function getTranslationFileContent()
{
$translations = $this->translateResource->getTranslationArray(null, $this->localeResolver->getLocale());
$translations = $this->jsonSerializer->serialize($translations);
return $translations;
}
}
10 changes: 10 additions & 0 deletions app/code/Magento/Translation/Test/Unit/Block/JsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@ public function testGetTranslationFilePath()
->willReturn('frontend/Magento/luma/en_EN');
$this->assertEquals('frontend/Magento/luma/en_EN', $this->model->getTranslationFilePath());
}

public function testGetTranslationFileVersion()
{
$version = sha1('translationFile');

$this->fileManagerMock->expects($this->once())
->method('getTranslationFileVersion')
->willReturn($version);
$this->assertEquals($version, $this->model->getTranslationFileVersion());
}
}
60 changes: 60 additions & 0 deletions app/code/Magento/Translation/Test/Unit/Model/Inline/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Translation\Test\Unit\Model\Inline;

class FileTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Translation\Model\Inline\File
*/
private $model;

/**
* @var \Magento\Framework\Translate\ResourceInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $translateResourceMock;

/**
* @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $localeResolverMock;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $jsonSerializer;

protected function setUp()
{
$this->translateResourceMock = $this->getMockBuilder(\Magento\Framework\Translate\ResourceInterface::class)
->disableOriginalConstructor()
->getMock();
$this->localeResolverMock = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
->disableOriginalConstructor()
->getMock();
$this->jsonSerializer = new \Magento\Framework\Serialize\Serializer\Json();

$this->model = new \Magento\Translation\Model\Inline\File(
$this->translateResourceMock,
$this->localeResolverMock,
$this->jsonSerializer
);
}

public function testGetTranslationFileContent()
{
$translations = ['string' => 'translatedString'];

$this->localeResolverMock->expects($this->atLeastOnce())->method('getLocale')->willReturn('en_US');
$this->translateResourceMock->expects($this->atLeastOnce())->method('getTranslationArray')
->willReturn($translations);

$this->assertEquals(
$this->jsonSerializer->serialize($translations),
$this->model->getTranslationFileContent()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$.initNamespaceStorage('mage-translation-file-version');
versionObj = $.localStorage.get('mage-translation-file-version');

<?php $version = sha1($block->getTranslationFileTimestamp() . $block->getTranslationFilePath()); ?>
<?php $version = $block->getTranslationFileVersion(); ?>

if (versionObj.version !== '<?= /* @noEscape */ $version ?>') {
dependencies.push(
Expand Down

0 comments on commit a6ff645

Please sign in to comment.