Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* @file
* Contains \Drupal\system\Tests\Asset\CssOptimizerUnitTest.
*/
namespace {
/**
* CssOptimizer uses file_create_url(), which *is* available when using the
* Simpletest test runner, but not when using the PHPUnit test runner; hence
* this hack.
*/
if (!function_exists('file_create_url')) {
/**
* Temporary mock for file_create_url(), until that is moved into
* Component/Utility.
*/
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;
/**
* Tests the CssOptimizer class.
*
* @group Asset
*/
class CssOptimizerUnitTest extends UnitTestCase {
/**
* A CSS asset optimizer.
*
* @var \Drupal\Core\Asset\CssOptimizer object.
*/
protected $optimizer;
/**
* A valid file CSS asset group.
*
* @var array
*/
protected $file_css_group;
/**
* A valid inline CSS asset group.
*
* @var array
*/
protected $inline_css_group;
public static function getInfo() {
return array(
'name' => 'CSS asset optimizer functionality',
'description' => 'Tests the CSS asset optimizer.',
'group' => 'Asset handling',
);
}
function setUp() {
parent::setUp();
$this->optimizer = new CssOptimizer();
}
/**
* Provides data for the CSS asset optimizing test.
*/
function testOptimizeProvider() {
$path = dirname(__FILE__) . '/css_test_files/';
return array(
// File. Tests:
// - Stripped comments and white-space.
// - Retain white-space in selectors. (http://drupal.org/node/472820)
// - Retain pseudo-selectors. (http://drupal.org/node/460448)
0 => array(
array(
'group' => -100,
'every_page' => TRUE,
'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'),
),
// File. Tests:
// - Proper URLs in imported files. (http://drupal.org/node/265719)
// - A background image with relative paths, which must be rewritten.
// - The rewritten background image path must also be passed through
// file_create_url(). (https://drupal.org/node/1961340)
1 => array(
array(
'group' => -100,
'every_page' => TRUE,
'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')),
),
// File. Tests:
// - Retain comment hacks.
2 => array(
array(
'group' => -100,
'every_page' => TRUE,
'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'),
),
// Inline. Preprocessing enabled.
3 => array(
array(
'group' => 0,
'every_page' => FALSE,
'type' => 'inline',
'weight' => 0.012,
'media' => 'all',
'preprocess' => TRUE,
'data' => '.girlfriend { display: none; }',
'browsers' => array('IE' => TRUE, '!IE' => TRUE),
),
".girlfriend{display:none;}\n",
),
// Inline. Preprocessing disabled.
4 => array(
array(
'group' => 0,
'every_page' => FALSE,
'type' => 'inline',
'weight' => 0.013,
'media' => 'all',
'preprocess' => FALSE,
'data' => '#home body { position: fixed; }',
'browsers' => array('IE' => TRUE, '!IE' => TRUE),
),
'#home body { position: fixed; }',
)
);
}
/**
* Tests optimizing a CSS asset group containing 'type' => 'file'.
*
* @dataProvider testOptimizeProvider
*/
function testOptimize($css_asset, $expected) {
$this->assertEquals($expected, $this->optimizer->optimize($css_asset), 'Group of file CSS assets optimized correctly.');
}
/**
* Tests a file CSS asset with preprocessing disabled.
*/
function testTypeFilePreprocessingDisabled() {
$this->setExpectedException('Exception', 'Only file CSS assets with preprocessing enabled can be optimized.');
$css_asset = array(
'group' => -100,
'every_page' => TRUE,
'type' => 'file',
'weight' => 0.012,
'media' => 'all',
// Preprocessing disabled.
'preprocess' => FALSE,
'data' => 'tests/Drupal/Tests/Core/Asset/foo.css',
'browsers' => array('IE' => TRUE, '!IE' => TRUE),
'basename' => 'foo.css',
);
$this->optimizer->optimize($css_asset);
}
/**
* Tests a CSS asset with 'type' => 'external'.
*/
function testTypeExternal() {
$this->setExpectedException('Exception', 'Only file or inline CSS assets can be optimized.');
$css_asset = array(
'group' => -100,
'every_page' => TRUE,
// Type external.
'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);
}
}
}