View source
<?php
namespace Drupal\Tests\advagg_js_minify\Kernel\Asset;
use Drupal\KernelTests\KernelTestBase;
class JsMinifierUnitTest extends KernelTestBase {
public static $modules = [
'advagg_js_minify',
];
protected $optimizer;
protected function setUp() {
parent::setUp();
$this
->installConfig('advagg_js_minify');
$this->optimizer = \Drupal::service('advagg.js_minifier');
}
public function testNoMinifier() {
$file = dirname(__FILE__) . '/js_test_files/drupal.js';
$contents = file_get_contents($file);
$this
->assertSame($contents, $this->optimizer
->optimize($contents, [], []));
}
public function providerTestClean() {
$dir = dirname(__FILE__) . '/js_test_files/';
return [
0 => [
file_get_contents($dir . 'source_mapping_url.min.js'),
file_get_contents($dir . 'source_mapping_url.min.js.optimized.js'),
],
1 => [
file_get_contents($dir . 'source_mapping_url_old.min.js'),
file_get_contents($dir . 'source_mapping_url_old.min.js.optimized.js'),
],
2 => [
file_get_contents($dir . 'source_url.min.js'),
file_get_contents($dir . 'source_url.min.js.optimized.js'),
],
3 => [
file_get_contents($dir . 'source_url_old.min.js'),
file_get_contents($dir . 'source_url_old.min.js.optimized.js'),
],
];
}
public function testClean($original, $expected) {
$this
->assertEquals($expected, $this->optimizer
->clean($original, []));
}
public function providerTestMinification() {
$path = dirname(__FILE__) . '/js_test_files/';
return [
[
[
'data' => $path . 'drupal.js',
],
file_get_contents($path . 'drupal.js'),
],
[
[
'data' => $path . 'ajax.js',
],
file_get_contents($path . 'ajax.js'),
],
[
[
'data' => $path . 'ToolbarVisualView.js',
],
file_get_contents($path . 'ToolbarVisualView.js'),
],
];
}
public function testMinifyJsmin(array $js_asset, $contents) {
if (!function_exists('jsmin')) {
return;
}
$this
->config('advagg_js_minify.settings')
->set('minifier', 3)
->save();
$expected = file_get_contents($js_asset['data'] . '.jsmin.js');
$this
->assertEquals($expected, $this->optimizer
->optimize($contents, $js_asset, []));
}
public function testMinifyJsminPlus(array $js_asset, $contents) {
$this
->config('advagg_js_minify.settings')
->set('minifier', 1)
->save();
$expected = file_get_contents($js_asset['data'] . '.jsminplus.js');
$this
->assertEquals($expected, $this->optimizer
->optimize($contents, $js_asset, []));
}
public function testMinifyJsqueeze(array $js_asset, $contents) {
$this
->config('advagg_js_minify.settings')
->set('minifier', 5)
->save();
if (version_compare(phpversion(), '7', '<')) {
$js_asset['data'] .= '.php5';
}
$expected = file_get_contents($js_asset['data'] . '.jsqueeze.js');
$this
->assertEquals($expected, $this->optimizer
->optimize($contents, $js_asset, []));
}
public function testMinifyJshrink(array $js_asset, $contents) {
$this
->config('advagg_js_minify.settings')
->set('minifier', 4)
->save();
$expected = file_get_contents($js_asset['data'] . '.jshrink.js');
$this
->assertEquals($expected, $this->optimizer
->optimize($contents, $js_asset, []));
}
public function testMinifyJsqueezeImportantComments() {
$file = dirname(__FILE__) . '/js_test_files/jquery.once.js';
$asset = [
'data' => $file,
];
$original = file_get_contents($file);
$withComments = file_get_contents($file . '.with-important.js');
$withoutComments = file_get_contents($file . '.without-important.js');
$config = $this
->config('advagg_js_minify.settings');
$config
->set('minifier', 5)
->set('add_license', TRUE)
->save();
$this
->assertEquals($withComments, $this->optimizer
->optimize($original, $asset, []));
$config
->set('add_license', FALSE)
->save();
$this
->assertEquals($withoutComments, $this->optimizer
->optimize($original, $asset, []));
}
}