View source
<?php
namespace {
if (!function_exists('file_create_url')) {
function file_create_url($uri) {
return 'file_create_url:' . $uri;
}
}
if (!function_exists('file_uri_scheme')) {
function file_uri_scheme($uri) {
return FALSE;
}
}
}
namespace Drupal\Tests\Core\Asset {
use Drupal\Core\Asset\CssOptimizer;
use Drupal\Tests\UnitTestCase;
class CssOptimizerUnitTest extends UnitTestCase {
protected $optimizer;
protected function setUp() {
parent::setUp();
$this->optimizer = new CssOptimizer();
}
function providerTestOptimize() {
$path = dirname(__FILE__) . '/css_test_files/';
return array(
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.012,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_without_import.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'css_input_without_import.css',
),
file_get_contents($path . 'css_input_without_import.css.optimized.css'),
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_import.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'css_input_with_import.css',
),
str_replace('url(images/icon.png)', 'url(' . file_create_url($path . 'images/icon.png') . ')', file_get_contents($path . 'css_input_with_import.css.optimized.css')),
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'comment_hacks.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'comment_hacks.css',
),
file_get_contents($path . 'comment_hacks.css.optimized.css'),
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_subfolder/css_input_with_import.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'css_input_with_import.css',
),
str_replace('url(../images/icon.png)', 'url(' . file_create_url($path . 'images/icon.png') . ')', file_get_contents($path . 'css_subfolder/css_input_with_import.css.optimized.css')),
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'charset_sameline.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'charset_sameline.css',
),
file_get_contents($path . 'charset.css.optimized.css'),
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'charset_newline.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'charset_newline.css',
),
file_get_contents($path . 'charset.css.optimized.css'),
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_bom.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'css_input_with_bom.css',
),
'.byte-order-mark-test{content:"☃";}' . "\n",
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_charset.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'css_input_with_charset.css',
),
'.charset-test{content:"€";}' . "\n",
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_bom_and_charset.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'css_input_with_bom_and_charset.css',
),
'.byte-order-mark-charset-test{content:"☃";}' . "\n",
),
array(
array(
'group' => -100,
'type' => 'file',
'weight' => 0.013,
'media' => 'all',
'preprocess' => TRUE,
'data' => $path . 'css_input_with_utf16_bom.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'css_input_with_utf16_bom.css',
),
'.utf16-byte-order-mark-test{content:"☃";}' . "\n",
),
);
}
function testOptimize($css_asset, $expected) {
$this
->assertEquals($expected, $this->optimizer
->optimize($css_asset), 'Group of file CSS assets optimized correctly.');
}
function testTypeFilePreprocessingDisabled() {
$this
->setExpectedException('Exception', 'Only file CSS assets with preprocessing enabled can be optimized.');
$css_asset = array(
'group' => -100,
'type' => 'file',
'weight' => 0.012,
'media' => 'all',
'preprocess' => FALSE,
'data' => 'tests/Drupal/Tests/Core/Asset/foo.css',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
'basename' => 'foo.css',
);
$this->optimizer
->optimize($css_asset);
}
function testTypeExternal() {
$this
->setExpectedException('Exception', 'Only file or inline CSS assets can be optimized.');
$css_asset = array(
'group' => -100,
'type' => 'external',
'weight' => 0.012,
'media' => 'all',
'preprocess' => TRUE,
'data' => 'http://example.com/foo.js',
'browsers' => array(
'IE' => TRUE,
'!IE' => TRUE,
),
);
$this->optimizer
->optimize($css_asset);
}
}
}