Modifies Assets class to use manifest file
Adds unit tests
This commit is contained in:
@@ -66,7 +66,8 @@ class Renderer {
|
||||
function setupGlobalVariables() {
|
||||
$this->renderer->addExtension(new Twig\Assets(array(
|
||||
'version' => Env::$version,
|
||||
'assets_url' => Env::$assets_url
|
||||
'assets_url' => Env::$assets_url,
|
||||
'assets_path' => Env::$assets_path
|
||||
)));
|
||||
}
|
||||
|
||||
|
@@ -3,11 +3,7 @@ namespace MailPoet\Twig;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Assets
|
||||
extends \Twig_Extension
|
||||
implements \Twig_Extension_GlobalsInterface
|
||||
{
|
||||
|
||||
class Assets extends \Twig_Extension implements \Twig_Extension_GlobalsInterface {
|
||||
private $_globals;
|
||||
|
||||
public function __construct($globals) {
|
||||
@@ -47,12 +43,10 @@ class Assets
|
||||
$output = array();
|
||||
|
||||
foreach($stylesheets as $stylesheet) {
|
||||
$url = $this->appendVersionToUrl(
|
||||
$this->_globals['assets_url'] . '/css/' . $stylesheet
|
||||
);
|
||||
$output[] = sprintf(
|
||||
'<link rel="stylesheet" type="text/css" href="%s">',
|
||||
$url
|
||||
'<link rel="stylesheet" type="text/css" href="%s/css/%s" />',
|
||||
$this->_globals['assets_url'],
|
||||
$this->getAssetFilename('css', $stylesheet)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -64,12 +58,10 @@ class Assets
|
||||
$output = array();
|
||||
|
||||
foreach($scripts as $script) {
|
||||
$url = $this->appendVersionToUrl(
|
||||
$this->_globals['assets_url'] . '/js/' . $script
|
||||
);
|
||||
$output[] = sprintf(
|
||||
'<script type="text/javascript" src="%s"></script>',
|
||||
$url
|
||||
'<script type="text/javascript" src="%s/js/%s"></script>',
|
||||
$this->_globals['assets_url'],
|
||||
$this->getAssetFilename('js', $script)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -85,4 +77,16 @@ class Assets
|
||||
public function appendVersionToUrl($url) {
|
||||
return add_query_arg('mailpoet_version', $this->_globals['version'], $url);
|
||||
}
|
||||
|
||||
public function getAssetFileName($asset_type, $asset) {
|
||||
$manifest = sprintf(
|
||||
'%s/%s/manifest.json',
|
||||
$this->_globals['assets_path'],
|
||||
$asset_type,
|
||||
$asset
|
||||
);
|
||||
if(!is_file($manifest)) return $asset;
|
||||
$manifest = json_decode(file_get_contents($manifest), true);
|
||||
return (!empty($manifest[$asset])) ? $manifest[$asset] : $asset;
|
||||
}
|
||||
}
|
@@ -1,54 +1,120 @@
|
||||
<?php
|
||||
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\Twig\Assets;
|
||||
|
||||
class AssetsTest extends MailPoetTest {
|
||||
function _before() {
|
||||
function __construct() {
|
||||
$this->assets_url = 'https://www.testing.com/wp-content/plugins/mailpoet/assets';
|
||||
$this->assets_path = Env::$temp_path;
|
||||
$this->assets_path_js = $this->assets_path . '/js';
|
||||
$this->assets_path_css = $this->assets_path . '/css';
|
||||
$this->assets_manifest_js = $this->assets_path_js . '/manifest.json';
|
||||
$this->assets_manifest_css = $this->assets_path_css . '/manifest.json';
|
||||
$this->version = '1.2.3';
|
||||
$this->assetsExtension = new Assets(array(
|
||||
'assets_url' => $this->assets_url,
|
||||
'version' => $this->version
|
||||
));
|
||||
$this->assets_extension = new Assets(
|
||||
array(
|
||||
'assets_url' => $this->assets_url,
|
||||
'assets_path' => $this->assets_path,
|
||||
'version' => $this->version
|
||||
)
|
||||
);
|
||||
$this->_after(); // delete folders if any were present before the test suite was initialized
|
||||
}
|
||||
|
||||
function testItGeneratesJavascriptTags() {
|
||||
expect($this->assetsExtension->generateJavascript('script1.js', 'script2.js'))->equals(
|
||||
'<script type="text/javascript" src="' . $this->assets_url . '/js/script1.js?mailpoet_version=' . $this->version . '"></script>'
|
||||
function _before() {
|
||||
$this->createDir($this->assets_path_js);
|
||||
$this->createDir($this->assets_path_css);
|
||||
}
|
||||
|
||||
function testItGeneratesJavascriptTagsForAssetsUsinManifestFile() {
|
||||
$manifest = file_put_contents(
|
||||
$this->assets_manifest_js,
|
||||
json_encode(
|
||||
array(
|
||||
'script1.js' => 'script1.hash.js',
|
||||
'script2.js' => 'script2.hash.js'
|
||||
)
|
||||
)
|
||||
);
|
||||
expect($this->assets_extension->generateJavascript('script1.js', 'script2.js'))->equals(
|
||||
'<script type="text/javascript" src="' . $this->assets_url . '/js/script1.hash.js"></script>'
|
||||
. "\n"
|
||||
. '<script type="text/javascript" src="' . $this->assets_url . '/js/script2.js?mailpoet_version=' . $this->version . '"></script>'
|
||||
. '<script type="text/javascript" src="' . $this->assets_url . '/js/script2.hash.js"></script>'
|
||||
);
|
||||
}
|
||||
|
||||
function testItGeneratesStylesheetTags() {
|
||||
expect($this->assetsExtension->generateStylesheet('style1.css', 'style2.css'))->equals(
|
||||
'<link rel="stylesheet" type="text/css" href="' . $this->assets_url . '/css/style1.css?mailpoet_version=' . $this->version . '">'
|
||||
function testItGeneratesJavascriptTagsForAssetsWhenManifestFileDoesNotExist() {
|
||||
expect($this->assets_extension->generateJavascript('script1.js', 'script2.js'))->equals(
|
||||
'<script type="text/javascript" src="' . $this->assets_url . '/js/script1.js"></script>'
|
||||
. "\n"
|
||||
. '<link rel="stylesheet" type="text/css" href="' . $this->assets_url . '/css/style2.css?mailpoet_version=' . $this->version . '">'
|
||||
. '<script type="text/javascript" src="' . $this->assets_url . '/js/script2.js"></script>'
|
||||
);
|
||||
}
|
||||
|
||||
function testItGeneratesStylesheetTagsForAssetsUsingManifestFile() {
|
||||
$manifest = file_put_contents(
|
||||
$this->assets_manifest_css,
|
||||
json_encode(
|
||||
array(
|
||||
'style1.css' => 'style1.hash.css',
|
||||
'style2.css' => 'style2.hash.css'
|
||||
)
|
||||
)
|
||||
);
|
||||
expect($this->assets_extension->generateStylesheet('style1.css', 'style2.css'))->equals(
|
||||
'<link rel="stylesheet" type="text/css" href="' . $this->assets_url . '/css/style1.hash.css" />'
|
||||
. "\n"
|
||||
. '<link rel="stylesheet" type="text/css" href="' . $this->assets_url . '/css/style2.hash.css" />'
|
||||
);
|
||||
}
|
||||
|
||||
function testItGeneratesStylesheetTagsWhenManifestFileDoesNotExist() {
|
||||
expect($this->assets_extension->generateStylesheet('style1.css', 'style2.css'))->equals(
|
||||
'<link rel="stylesheet" type="text/css" href="' . $this->assets_url . '/css/style1.css" />'
|
||||
. "\n"
|
||||
. '<link rel="stylesheet" type="text/css" href="' . $this->assets_url . '/css/style2.css" />'
|
||||
);
|
||||
}
|
||||
|
||||
function testItGeneratesImageUrls() {
|
||||
expect($this->assetsExtension->generateImageUrl('image1.png'))->equals(
|
||||
expect($this->assets_extension->generateImageUrl('image1.png'))->equals(
|
||||
$this->assets_url . '/img/image1.png?mailpoet_version=' . $this->version
|
||||
);
|
||||
}
|
||||
|
||||
function testItAppendsVersionToUrl() {
|
||||
$without_file = 'http://url.com/';
|
||||
expect($this->assetsExtension->appendVersionToUrl($without_file))->equals(
|
||||
expect($this->assets_extension->appendVersionToUrl($without_file))->equals(
|
||||
$without_file . '?mailpoet_version=' . $this->version
|
||||
);
|
||||
$with_file = 'http://url.com/file.php';
|
||||
expect($this->assetsExtension->appendVersionToUrl($with_file))->equals(
|
||||
expect($this->assets_extension->appendVersionToUrl($with_file))->equals(
|
||||
$with_file . '?mailpoet_version=' . $this->version
|
||||
);
|
||||
$with_folder = 'http://url.com/folder/file.php';
|
||||
expect($this->assetsExtension->appendVersionToUrl($with_folder))->equals(
|
||||
expect($this->assets_extension->appendVersionToUrl($with_folder))->equals(
|
||||
$with_folder . '?mailpoet_version=' . $this->version
|
||||
);
|
||||
$with_query_string = 'http://url.com/folder/file.php?name=value';
|
||||
expect($this->assetsExtension->appendVersionToUrl($with_query_string))->equals(
|
||||
expect($this->assets_extension->appendVersionToUrl($with_query_string))->equals(
|
||||
$with_query_string . '&mailpoet_version=' . $this->version
|
||||
);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
$this->removeDir($this->assets_path_js);
|
||||
$this->removeDir($this->assets_path_css);
|
||||
}
|
||||
|
||||
private function createDir($dir) {
|
||||
if(is_dir($dir)) return;
|
||||
mkdir($dir);
|
||||
}
|
||||
|
||||
private function removeDir($dir) {
|
||||
if(!is_dir($dir)) return;
|
||||
array_map('unlink', glob($dir . '/*.*'));
|
||||
rmdir($dir);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user