1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Tests for Libraries API.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Tests basic Libraries API functions.
|
10 |
|
|
*/
|
11 |
|
|
class LibrariesUnitTestCase extends DrupalUnitTestCase {
|
12 |
|
|
public static function getInfo() {
|
13 |
|
|
return array(
|
14 |
|
|
'name' => 'Libraries API unit tests',
|
15 |
|
|
'description' => 'Tests basic functions provided by Libraries API.',
|
16 |
|
|
'group' => 'Libraries API',
|
17 |
|
|
);
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
function setUp() {
|
21 |
|
|
drupal_load('module', 'libraries');
|
22 |
|
|
parent::setUp();
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
/**
|
26 |
|
|
* Tests libraries_get_path().
|
27 |
|
|
*/
|
28 |
|
|
function testLibrariesGetPath() {
|
29 |
|
|
// Note that, even though libraries_get_path() doesn't find the 'example'
|
30 |
|
|
// library, we are able to make it 'installed' by specifying the 'library
|
31 |
|
|
// path' up-front. This is only used for testing purposed and is strongly
|
32 |
|
|
// discouraged as it defeats the purpose of Libraries API in the first
|
33 |
|
|
// place.
|
34 |
|
|
$this->assertEqual(libraries_get_path('example'), FALSE, 'libraries_get_path() returns FALSE for a missing library.');
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
/**
|
38 |
|
|
* Tests libraries_prepare_files().
|
39 |
|
|
*/
|
40 |
|
|
function testLibrariesPrepareFiles() {
|
41 |
|
|
$expected = array(
|
42 |
|
|
'files' => array(
|
43 |
|
|
'js' => array('example.js' => array()),
|
44 |
|
|
'css' => array('example.css' => array()),
|
45 |
|
|
'php' => array('example.php' => array()),
|
46 |
|
|
),
|
47 |
|
|
);
|
48 |
|
|
$library = array(
|
49 |
|
|
'files' => array(
|
50 |
|
|
'js' => array('example.js'),
|
51 |
|
|
'css' => array('example.css'),
|
52 |
|
|
'php' => array('example.php'),
|
53 |
|
|
),
|
54 |
|
|
);
|
55 |
|
|
libraries_prepare_files($library, NULL, NULL);
|
56 |
|
|
$this->assertEqual($expected, $library, 'libraries_prepare_files() works correctly.');
|
57 |
|
|
}
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* Tests basic detection and loading of libraries.
|
62 |
|
|
*/
|
63 |
|
|
class LibrariesTestCase extends DrupalWebTestCase {
|
64 |
|
|
protected $profile = 'testing';
|
65 |
|
|
|
66 |
|
|
public static function getInfo() {
|
67 |
|
|
return array(
|
68 |
|
|
'name' => 'Libraries detection and loading',
|
69 |
|
|
'description' => 'Tests detection and loading of libraries.',
|
70 |
|
|
'group' => 'Libraries API',
|
71 |
|
|
);
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
function setUp() {
|
75 |
3753f249
|
Assos Assos
|
parent::setUp('libraries', 'libraries_test_module');
|
76 |
|
|
theme_enable(array('libraries_test_theme'));
|
77 |
85ad3d82
|
Assos Assos
|
}
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* Tests libraries_detect_dependencies().
|
81 |
|
|
*/
|
82 |
|
|
function testLibrariesDetectDependencies() {
|
83 |
|
|
$library = array(
|
84 |
|
|
'name' => 'Example',
|
85 |
|
|
'dependencies' => array('example_missing'),
|
86 |
|
|
);
|
87 |
|
|
libraries_detect_dependencies($library);
|
88 |
|
|
$this->assertEqual($library['error'], 'missing dependency', 'libraries_detect_dependencies() detects missing dependency');
|
89 |
|
|
$error_message = t('The %dependency library, which the %library library depends on, is not installed.', array(
|
90 |
|
|
'%dependency' => 'Example missing',
|
91 |
|
|
'%library' => $library['name'],
|
92 |
|
|
));
|
93 |
|
|
$this->verbose("Expected:<br>$error_message");
|
94 |
|
|
$this->verbose('Actual:<br>' . $library['error message']);
|
95 |
|
|
$this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing dependency');
|
96 |
|
|
// Test versioned dependencies.
|
97 |
|
|
$version = '1.1';
|
98 |
|
|
$compatible = array(
|
99 |
|
|
'1.1',
|
100 |
|
|
'<=1.1',
|
101 |
|
|
'>=1.1',
|
102 |
|
|
'<1.2',
|
103 |
|
|
'<2.0',
|
104 |
|
|
'>1.0',
|
105 |
|
|
'>1.0-rc1',
|
106 |
|
|
'>1.0-beta2',
|
107 |
|
|
'>1.0-alpha3',
|
108 |
|
|
'>0.1',
|
109 |
|
|
'<1.2, >1.0',
|
110 |
|
|
'>0.1, <=1.1',
|
111 |
|
|
);
|
112 |
|
|
$incompatible = array(
|
113 |
|
|
'1.2',
|
114 |
|
|
'2.0',
|
115 |
|
|
'<1.1',
|
116 |
|
|
'>1.1',
|
117 |
|
|
'<=1.0',
|
118 |
|
|
'<=1.0-rc1',
|
119 |
|
|
'<=1.0-beta2',
|
120 |
|
|
'<=1.0-alpha3',
|
121 |
|
|
'>=1.2',
|
122 |
|
|
'<1.1, >0.9',
|
123 |
|
|
'>=0.1, <1.1',
|
124 |
|
|
);
|
125 |
|
|
$library = array(
|
126 |
|
|
'name' => 'Example',
|
127 |
|
|
);
|
128 |
|
|
foreach ($compatible as $version_string) {
|
129 |
|
|
$library['dependencies'][0] = "example_dependency ($version_string)";
|
130 |
|
|
// libraries_detect_dependencies() is a post-detect callback, so
|
131 |
|
|
// 'installed' is already set, when it is called. It sets the value to
|
132 |
|
|
// FALSE for missing or incompatible dependencies.
|
133 |
|
|
$library['installed'] = TRUE;
|
134 |
|
|
libraries_detect_dependencies($library);
|
135 |
3753f249
|
Assos Assos
|
$this->verbose('Library:<pre>' . var_export($library, TRUE) . '</pre>');
|
136 |
85ad3d82
|
Assos Assos
|
$this->assertTrue($library['installed'], "libraries_detect_dependencies() detects compatible version string: '$version_string' is compatible with '$version'");
|
137 |
|
|
}
|
138 |
|
|
foreach ($incompatible as $version_string) {
|
139 |
|
|
$library['dependencies'][0] = "example_dependency ($version_string)";
|
140 |
|
|
$library['installed'] = TRUE;
|
141 |
|
|
unset($library['error'], $library['error message']);
|
142 |
|
|
libraries_detect_dependencies($library);
|
143 |
3753f249
|
Assos Assos
|
$this->verbose('Library:<pre>' . var_export($library, TRUE) . '</pre>');
|
144 |
85ad3d82
|
Assos Assos
|
$this->assertEqual($library['error'], 'incompatible dependency', "libraries_detect_dependencies() detects incompatible version strings: '$version_string' is incompatible with '$version'");
|
145 |
|
|
}
|
146 |
|
|
// Instead of repeating this assertion for each version string, we just
|
147 |
|
|
// re-use the $library variable from the foreach loop.
|
148 |
|
|
$error_message = t('The version %dependency_version of the %dependency library is not compatible with the %library library.', array(
|
149 |
|
|
'%dependency_version' => $version,
|
150 |
|
|
'%dependency' => 'Example dependency',
|
151 |
|
|
'%library' => $library['name'],
|
152 |
|
|
));
|
153 |
|
|
$this->verbose("Expected:<br>$error_message");
|
154 |
|
|
$this->verbose('Actual:<br>' . $library['error message']);
|
155 |
|
|
$this->assertEqual($library['error message'], $error_message, 'Correct error message for an incompatible dependency');
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/**
|
159 |
|
|
* Tests libraries_scan_info_files().
|
160 |
|
|
*/
|
161 |
|
|
function testLibrariesScanInfoFiles() {
|
162 |
|
|
$expected = array('example_info_file' => (object) array(
|
163 |
3753f249
|
Assos Assos
|
'uri' => drupal_get_path('module', 'libraries') . '/tests/libraries/example_info_file.libraries.info',
|
164 |
85ad3d82
|
Assos Assos
|
'filename' => 'example_info_file.libraries.info',
|
165 |
|
|
'name' => 'example_info_file.libraries',
|
166 |
|
|
));
|
167 |
|
|
$this->assertEqual(libraries_scan_info_files(), $expected, 'libraries_scan_info_files() correctly finds the example info file.');
|
168 |
|
|
$this->verbose('<pre>' . var_export(libraries_scan_info_files(), TRUE) . '</pre>');
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
/**
|
172 |
|
|
* Tests libraries_info().
|
173 |
|
|
*/
|
174 |
|
|
function testLibrariesInfo() {
|
175 |
3753f249
|
Assos Assos
|
// Test that modules can provide and alter library information.
|
176 |
|
|
$info = libraries_info();
|
177 |
|
|
$this->assertTrue(isset($info['example_module']));
|
178 |
|
|
$this->verbose('Library:<pre>' . var_export($info['example_module'], TRUE) . '</pre>');
|
179 |
|
|
$this->assertEqual($info['example_module']['info type'], 'module');
|
180 |
|
|
$this->assertEqual($info['example_module']['module'], 'libraries_test_module');
|
181 |
|
|
$this->assertTrue($info['example_module']['module_altered']);
|
182 |
|
|
|
183 |
|
|
// Test that themes can provide and alter library information.
|
184 |
|
|
$this->assertTrue(isset($info['example_theme']));
|
185 |
|
|
$this->verbose('Library:<pre>' . var_export($info['example_theme'], TRUE) . '</pre>');
|
186 |
|
|
$this->assertEqual($info['example_theme']['info type'], 'theme');
|
187 |
|
|
$this->assertEqual($info['example_theme']['theme'], 'libraries_test_theme');
|
188 |
|
|
$this->assertTrue($info['example_theme']['theme_altered']);
|
189 |
|
|
|
190 |
85ad3d82
|
Assos Assos
|
// Test that library information is found correctly.
|
191 |
|
|
$expected = array(
|
192 |
|
|
'name' => 'Example files',
|
193 |
3753f249
|
Assos Assos
|
'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
|
194 |
85ad3d82
|
Assos Assos
|
'version' => '1',
|
195 |
|
|
'files' => array(
|
196 |
|
|
'js' => array('example_1.js' => array()),
|
197 |
|
|
'css' => array('example_1.css' => array()),
|
198 |
|
|
'php' => array('example_1.php' => array()),
|
199 |
|
|
),
|
200 |
3753f249
|
Assos Assos
|
'info type' => 'module',
|
201 |
|
|
'module' => 'libraries_test_module',
|
202 |
85ad3d82
|
Assos Assos
|
);
|
203 |
|
|
libraries_info_defaults($expected, 'example_files');
|
204 |
|
|
$library = libraries_info('example_files');
|
205 |
|
|
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
|
206 |
|
|
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
|
207 |
|
|
$this->assertEqual($library, $expected, 'Library information is correctly gathered.');
|
208 |
|
|
|
209 |
|
|
// Test a library specified with an .info file gets detected.
|
210 |
|
|
$expected = array(
|
211 |
|
|
'name' => 'Example info file',
|
212 |
3753f249
|
Assos Assos
|
'info type' => 'info file',
|
213 |
|
|
'info file' => drupal_get_path('module', 'libraries') . '/tests/libraries/example_info_file.libraries.info',
|
214 |
85ad3d82
|
Assos Assos
|
);
|
215 |
|
|
libraries_info_defaults($expected, 'example_info_file');
|
216 |
|
|
$library = libraries_info('example_info_file');
|
217 |
|
|
// If this module was downloaded from Drupal.org, the Drupal.org packaging
|
218 |
|
|
// system has corrupted the test info file.
|
219 |
|
|
// @see http://drupal.org/node/1606606
|
220 |
|
|
unset($library['core'], $library['datestamp'], $library['project'], $library['version']);
|
221 |
|
|
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
|
222 |
|
|
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
|
223 |
|
|
$this->assertEqual($library, $expected, 'Library specified with an .info file found');
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
/**
|
227 |
|
|
* Tests libraries_detect().
|
228 |
|
|
*/
|
229 |
|
|
function testLibrariesDetect() {
|
230 |
|
|
// Test missing library.
|
231 |
|
|
$library = libraries_detect('example_missing');
|
232 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
233 |
|
|
$this->assertEqual($library['error'], 'not found', 'Missing library not found.');
|
234 |
|
|
$error_message = t('The %library library could not be found.', array(
|
235 |
|
|
'%library' => $library['name'],
|
236 |
|
|
));
|
237 |
|
|
$this->assertEqual($library['error message'], $error_message, 'Correct error message for a missing library.');
|
238 |
|
|
|
239 |
|
|
// Test unknown library version.
|
240 |
|
|
$library = libraries_detect('example_undetected_version');
|
241 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
242 |
|
|
$this->assertEqual($library['error'], 'not detected', 'Undetected version detected as such.');
|
243 |
|
|
$error_message = t('The version of the %library library could not be detected.', array(
|
244 |
|
|
'%library' => $library['name'],
|
245 |
|
|
));
|
246 |
|
|
$this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an undetected version.');
|
247 |
|
|
|
248 |
|
|
// Test unsupported library version.
|
249 |
|
|
$library = libraries_detect('example_unsupported_version');
|
250 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
251 |
|
|
$this->assertEqual($library['error'], 'not supported', 'Unsupported version detected as such.');
|
252 |
|
|
$error_message = t('The installed version %version of the %library library is not supported.', array(
|
253 |
|
|
'%version' => $library['version'],
|
254 |
|
|
'%library' => $library['name'],
|
255 |
|
|
));
|
256 |
|
|
$this->assertEqual($library['error message'], $error_message, 'Correct error message for a library with an unsupported version.');
|
257 |
|
|
|
258 |
|
|
// Test supported library version.
|
259 |
|
|
$library = libraries_detect('example_supported_version');
|
260 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
261 |
|
|
$this->assertEqual($library['installed'], TRUE, 'Supported library version found.');
|
262 |
|
|
|
263 |
|
|
// Test libraries_get_version().
|
264 |
|
|
$library = libraries_detect('example_default_version_callback');
|
265 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
266 |
|
|
$this->assertEqual($library['version'], '1', 'Expected version returned by default version callback.');
|
267 |
|
|
|
268 |
|
|
// Test a multiple-parameter version callback.
|
269 |
|
|
$library = libraries_detect('example_multiple_parameter_version_callback');
|
270 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
271 |
|
|
$this->assertEqual($library['version'], '1', 'Expected version returned by multiple parameter version callback.');
|
272 |
|
|
|
273 |
|
|
// Test a top-level files property.
|
274 |
|
|
$library = libraries_detect('example_files');
|
275 |
|
|
$files = array(
|
276 |
|
|
'js' => array('example_1.js' => array()),
|
277 |
|
|
'css' => array('example_1.css' => array()),
|
278 |
|
|
'php' => array('example_1.php' => array()),
|
279 |
|
|
);
|
280 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
281 |
|
|
$this->assertEqual($library['files'], $files, 'Top-level files property works.');
|
282 |
|
|
|
283 |
|
|
// Test version-specific library files.
|
284 |
|
|
$library = libraries_detect('example_versions');
|
285 |
|
|
$files = array(
|
286 |
|
|
'js' => array('example_2.js' => array()),
|
287 |
|
|
'css' => array('example_2.css' => array()),
|
288 |
|
|
'php' => array('example_2.php' => array()),
|
289 |
|
|
);
|
290 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
291 |
|
|
$this->assertEqual($library['files'], $files, 'Version-specific library files found.');
|
292 |
|
|
|
293 |
|
|
// Test missing variant.
|
294 |
|
|
$library = libraries_detect('example_variant_missing');
|
295 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
296 |
|
|
$this->assertEqual($library['variants']['example_variant']['error'], 'not found', 'Missing variant not found');
|
297 |
|
|
$error_message = t('The %variant variant of the %library library could not be found.', array(
|
298 |
|
|
'%variant' => 'example_variant',
|
299 |
|
|
'%library' => 'Example variant missing',
|
300 |
|
|
));
|
301 |
|
|
$this->assertEqual($library['variants']['example_variant']['error message'], $error_message, 'Correct error message for a missing variant.');
|
302 |
|
|
|
303 |
|
|
// Test existing variant.
|
304 |
|
|
$library = libraries_detect('example_variant');
|
305 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
306 |
|
|
$this->assertEqual($library['variants']['example_variant']['installed'], TRUE, 'Existing variant found.');
|
307 |
|
|
}
|
308 |
|
|
|
309 |
|
|
/**
|
310 |
|
|
* Tests libraries_load().
|
311 |
|
|
*/
|
312 |
|
|
function testLibrariesLoad() {
|
313 |
|
|
// Test dependencies.
|
314 |
|
|
$library = libraries_load('example_dependency_missing');
|
315 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
316 |
|
|
$this->assertFalse($library['loaded'], 'Library with missing dependency cannot be loaded');
|
317 |
|
|
$library = libraries_load('example_dependency_incompatible');
|
318 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
319 |
|
|
$this->assertFalse($library['loaded'], 'Library with incompatible dependency cannot be loaded');
|
320 |
|
|
$library = libraries_load('example_dependency_compatible');
|
321 |
|
|
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
|
322 |
|
|
$this->assertEqual($library['loaded'], 1, 'Library with compatible dependency is loaded');
|
323 |
|
|
$loaded = &drupal_static('libraries_load');
|
324 |
|
|
$this->verbose('<pre>' . var_export($loaded, TRUE) . '</pre>');
|
325 |
|
|
$this->assertEqual($loaded['example_dependency']['loaded'], 1, 'Dependency library is also loaded');
|
326 |
3753f249
|
Assos Assos
|
|
327 |
|
|
// Test that PHP files that have a local $path variable do not break library
|
328 |
|
|
// loading.
|
329 |
|
|
// @see _libraries_require_once()
|
330 |
|
|
$library = libraries_load('example_path_variable_override');
|
331 |
|
|
$this->assertEqual($library['loaded'], 2, 'PHP files cannot break library loading.');
|
332 |
85ad3d82
|
Assos Assos
|
}
|
333 |
|
|
|
334 |
|
|
/**
|
335 |
|
|
* Tests the applying of callbacks.
|
336 |
|
|
*/
|
337 |
|
|
function testCallbacks() {
|
338 |
|
|
$expected = array(
|
339 |
|
|
'name' => 'Example callback',
|
340 |
3753f249
|
Assos Assos
|
'library path' => drupal_get_path('module', 'libraries') . '/tests/libraries/example',
|
341 |
85ad3d82
|
Assos Assos
|
'version' => '1',
|
342 |
|
|
'versions' => array(
|
343 |
|
|
'1' => array(
|
344 |
|
|
'variants' => array(
|
345 |
|
|
'example_variant' => array(
|
346 |
|
|
'info callback' => 'not applied',
|
347 |
|
|
'pre-detect callback' => 'not applied',
|
348 |
|
|
'post-detect callback' => 'not applied',
|
349 |
|
|
'pre-dependencies-load callback' => 'not applied',
|
350 |
|
|
'pre-load callback' => 'not applied',
|
351 |
|
|
'post-load callback' => 'not applied',
|
352 |
|
|
),
|
353 |
|
|
),
|
354 |
|
|
'info callback' => 'not applied',
|
355 |
|
|
'pre-detect callback' => 'not applied',
|
356 |
|
|
'post-detect callback' => 'not applied',
|
357 |
|
|
'pre-dependencies-load callback' => 'not applied',
|
358 |
|
|
'pre-load callback' => 'not applied',
|
359 |
|
|
'post-load callback' => 'not applied',
|
360 |
|
|
),
|
361 |
|
|
),
|
362 |
|
|
'variants' => array(
|
363 |
|
|
'example_variant' => array(
|
364 |
|
|
'info callback' => 'not applied',
|
365 |
|
|
'pre-detect callback' => 'not applied',
|
366 |
|
|
'post-detect callback' => 'not applied',
|
367 |
|
|
'pre-dependencies-load callback' => 'not applied',
|
368 |
|
|
'pre-load callback' => 'not applied',
|
369 |
|
|
'post-load callback' => 'not applied',
|
370 |
|
|
),
|
371 |
|
|
),
|
372 |
|
|
'callbacks' => array(
|
373 |
3753f249
|
Assos Assos
|
'info' => array('_libraries_test_module_info_callback'),
|
374 |
|
|
'pre-detect' => array('_libraries_test_module_pre_detect_callback'),
|
375 |
|
|
'post-detect' => array('_libraries_test_module_post_detect_callback'),
|
376 |
|
|
'pre-dependencies-load' => array('_libraries_test_module_pre_dependencies_load_callback'),
|
377 |
|
|
'pre-load' => array('_libraries_test_module_pre_load_callback'),
|
378 |
|
|
'post-load' => array('_libraries_test_module_post_load_callback'),
|
379 |
85ad3d82
|
Assos Assos
|
),
|
380 |
|
|
'info callback' => 'not applied',
|
381 |
|
|
'pre-detect callback' => 'not applied',
|
382 |
|
|
'post-detect callback' => 'not applied',
|
383 |
|
|
'pre-dependencies-load callback' => 'not applied',
|
384 |
|
|
'pre-load callback' => 'not applied',
|
385 |
|
|
'post-load callback' => 'not applied',
|
386 |
3753f249
|
Assos Assos
|
'info type' => 'module',
|
387 |
|
|
'module' => 'libraries_test_module',
|
388 |
85ad3d82
|
Assos Assos
|
);
|
389 |
|
|
libraries_info_defaults($expected, 'example_callback');
|
390 |
|
|
|
391 |
|
|
// Test a callback in the 'info' group.
|
392 |
|
|
$expected['info callback'] = 'applied (top-level)';
|
393 |
|
|
$expected['versions']['1']['info callback'] = 'applied (version 1)';
|
394 |
|
|
$expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
|
395 |
|
|
$expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)';
|
396 |
|
|
$library = libraries_info('example_callback');
|
397 |
|
|
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
|
398 |
|
|
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
|
399 |
|
|
$this->assertEqual($library, $expected, 'Prepare callback was applied correctly.');
|
400 |
|
|
|
401 |
|
|
// Test a callback in the 'pre-detect' and 'post-detect' phases.
|
402 |
|
|
// Successfully detected libraries should only contain version information
|
403 |
|
|
// for the detected version and thus, be marked as installed.
|
404 |
|
|
unset($expected['versions']);
|
405 |
|
|
$expected['installed'] = TRUE;
|
406 |
|
|
// Additionally, version-specific properties of the detected version are
|
407 |
|
|
// supposed to override the corresponding top-level properties.
|
408 |
|
|
$expected['info callback'] = 'applied (version 1)';
|
409 |
|
|
$expected['variants']['example_variant']['installed'] = TRUE;
|
410 |
|
|
$expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
|
411 |
|
|
// Version-overloading takes place after the 'pre-detect' callbacks have
|
412 |
|
|
// been applied.
|
413 |
|
|
$expected['pre-detect callback'] = 'applied (version 1)';
|
414 |
|
|
$expected['post-detect callback'] = 'applied (top-level)';
|
415 |
|
|
$expected['variants']['example_variant']['pre-detect callback'] = 'applied (version 1, variant example_variant)';
|
416 |
|
|
$expected['variants']['example_variant']['post-detect callback'] = 'applied (variant example_variant)';
|
417 |
|
|
$library = libraries_detect('example_callback');
|
418 |
|
|
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
|
419 |
|
|
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
|
420 |
|
|
$this->assertEqual($library, $expected, 'Detect callback was applied correctly.');
|
421 |
|
|
|
422 |
|
|
// Test a callback in the 'pre-dependencies-load', 'pre-load' and
|
423 |
|
|
// 'post-load' phases.
|
424 |
|
|
// Successfully loaded libraries should only contain information about the
|
425 |
|
|
// already loaded variant.
|
426 |
|
|
unset($expected['variants']);
|
427 |
|
|
$expected['loaded'] = 0;
|
428 |
|
|
$expected['pre-dependencies-load callback'] = 'applied (top-level)';
|
429 |
|
|
$expected['pre-load callback'] = 'applied (top-level)';
|
430 |
|
|
$expected['post-load callback'] = 'applied (top-level)';
|
431 |
|
|
$library = libraries_load('example_callback');
|
432 |
|
|
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
|
433 |
|
|
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
|
434 |
|
|
$this->assertEqual($library, $expected, 'Pre-load and post-load callbacks were applied correctly.');
|
435 |
|
|
// This is not recommended usually and is only used for testing purposes.
|
436 |
|
|
drupal_static_reset('libraries_load');
|
437 |
|
|
// Successfully loaded library variants are supposed to contain the specific
|
438 |
|
|
// variant information only.
|
439 |
|
|
$expected['info callback'] = 'applied (version 1, variant example_variant)';
|
440 |
|
|
$expected['pre-detect callback'] = 'applied (version 1, variant example_variant)';
|
441 |
|
|
$expected['post-detect callback'] = 'applied (variant example_variant)';
|
442 |
|
|
$library = libraries_load('example_callback', 'example_variant');
|
443 |
|
|
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
|
444 |
|
|
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
|
445 |
|
|
$this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.');
|
446 |
|
|
}
|
447 |
|
|
|
448 |
|
|
/**
|
449 |
|
|
* Tests that library files are properly added to the page output.
|
450 |
|
|
*
|
451 |
|
|
* We check for JavaScript and CSS files directly in the DOM and add a list of
|
452 |
|
|
* included PHP files manually to the page output.
|
453 |
|
|
*
|
454 |
3753f249
|
Assos Assos
|
* @see _libraries_test_module_load()
|
455 |
85ad3d82
|
Assos Assos
|
*/
|
456 |
|
|
function testLibrariesOutput() {
|
457 |
|
|
// Test loading of a simple library with a top-level files property.
|
458 |
3753f249
|
Assos Assos
|
$this->drupalGet('libraries-test-module/files');
|
459 |
85ad3d82
|
Assos Assos
|
$this->assertLibraryFiles('example_1', 'File loading');
|
460 |
|
|
|
461 |
|
|
// Test loading of integration files.
|
462 |
3753f249
|
Assos Assos
|
$this->drupalGet('libraries-test-module/module-integration-files');
|
463 |
|
|
$this->assertRaw('libraries_test_module.js', 'Integration file loading: libraries_test_module.js found');
|
464 |
|
|
$this->assertRaw('libraries_test_module.css', 'Integration file loading: libraries_test_module.css found');
|
465 |
|
|
$this->assertRaw('libraries_test_module.inc', 'Integration file loading: libraries_test_module.inc found');
|
466 |
|
|
$this->drupalGet('libraries-test-module/theme-integration-files');
|
467 |
|
|
$this->assertRaw('libraries_test_theme.js', 'Integration file loading: libraries_test_theme.js found');
|
468 |
|
|
$this->assertRaw('libraries_test_theme.css', 'Integration file loading: libraries_test_theme.css found');
|
469 |
|
|
$this->assertRaw('libraries_test_theme.inc', 'Integration file loading: libraries_test_theme.inc found');
|
470 |
|
|
|
471 |
|
|
// Test loading of post-load integration files.
|
472 |
|
|
$this->drupalGet('libraries-test-module/module-integration-files-post-load');
|
473 |
|
|
// If the files were not loaded correctly, a fatal error occurs.
|
474 |
|
|
$this->assertResponse(200, 'Post-load integration files are loaded correctly.');
|
475 |
85ad3d82
|
Assos Assos
|
|
476 |
|
|
// Test version overloading.
|
477 |
3753f249
|
Assos Assos
|
$this->drupalGet('libraries-test-module/versions');
|
478 |
85ad3d82
|
Assos Assos
|
$this->assertLibraryFiles('example_2', 'Version overloading');
|
479 |
|
|
|
480 |
|
|
// Test variant loading.
|
481 |
3753f249
|
Assos Assos
|
$this->drupalGet('libraries-test-module/variant');
|
482 |
85ad3d82
|
Assos Assos
|
$this->assertLibraryFiles('example_3', 'Variant loading');
|
483 |
|
|
|
484 |
|
|
// Test version overloading and variant loading.
|
485 |
3753f249
|
Assos Assos
|
$this->drupalGet('libraries-test-module/versions-and-variants');
|
486 |
85ad3d82
|
Assos Assos
|
$this->assertLibraryFiles('example_4', 'Concurrent version and variant overloading');
|
487 |
|
|
|
488 |
|
|
// Test caching.
|
489 |
3753f249
|
Assos Assos
|
variable_set('libraries_test_module_cache', TRUE);
|
490 |
85ad3d82
|
Assos Assos
|
cache_clear_all('example_callback', 'cache_libraries');
|
491 |
|
|
// When the library information is not cached, all callback groups should be
|
492 |
|
|
// invoked.
|
493 |
3753f249
|
Assos Assos
|
$this->drupalGet('libraries-test-module/cache');
|
494 |
85ad3d82
|
Assos Assos
|
$this->assertRaw('The <em>info</em> callback group was invoked.', 'Info callback invoked for uncached libraries.');
|
495 |
|
|
$this->assertRaw('The <em>pre-detect</em> callback group was invoked.', 'Pre-detect callback invoked for uncached libraries.');
|
496 |
|
|
$this->assertRaw('The <em>post-detect</em> callback group was invoked.', 'Post-detect callback invoked for uncached libraries.');
|
497 |
|
|
$this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for uncached libraries.');
|
498 |
|
|
$this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for uncached libraries.');
|
499 |
|
|
// When the library information is cached only the 'pre-load' and
|
500 |
|
|
// 'post-load' callback groups should be invoked.
|
501 |
3753f249
|
Assos Assos
|
$this->drupalGet('libraries-test-module/cache');
|
502 |
85ad3d82
|
Assos Assos
|
$this->assertNoRaw('The <em>info</em> callback group was not invoked.', 'Info callback not invoked for cached libraries.');
|
503 |
|
|
$this->assertNoRaw('The <em>pre-detect</em> callback group was not invoked.', 'Pre-detect callback not invoked for cached libraries.');
|
504 |
|
|
$this->assertNoRaw('The <em>post-detect</em> callback group was not invoked.', 'Post-detect callback not invoked for cached libraries.');
|
505 |
|
|
$this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for cached libraries.');
|
506 |
|
|
$this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for cached libraries.');
|
507 |
3753f249
|
Assos Assos
|
variable_set('libraries_test_module_cache', FALSE);
|
508 |
85ad3d82
|
Assos Assos
|
}
|
509 |
|
|
|
510 |
|
|
/**
|
511 |
|
|
* Helper function to assert that a library was correctly loaded.
|
512 |
|
|
*
|
513 |
|
|
* Asserts that all the correct files were loaded and all the incorrect ones
|
514 |
|
|
* were not.
|
515 |
|
|
*
|
516 |
|
|
* @param $name
|
517 |
|
|
* The name of the files that should be loaded. The current testing system
|
518 |
|
|
* knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name
|
519 |
|
|
* has an associated JavaScript, CSS and PHP file that will be asserted. All
|
520 |
|
|
* other files will be asserted to not be loaded. See
|
521 |
|
|
* tests/example/README.txt for more information on how the loading of the
|
522 |
|
|
* files is tested.
|
523 |
|
|
* @param $label
|
524 |
|
|
* (optional) A label to prepend to the assertion messages, to make them
|
525 |
|
|
* less ambiguous.
|
526 |
|
|
* @param $extensions
|
527 |
|
|
* (optional) The expected file extensions of $name. Defaults to
|
528 |
|
|
* array('js', 'css', 'php').
|
529 |
|
|
*/
|
530 |
|
|
function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) {
|
531 |
|
|
$label = ($label !== '' ? "$label: " : '');
|
532 |
|
|
|
533 |
|
|
// Test that the wrong files are not loaded...
|
534 |
|
|
$names = array(
|
535 |
|
|
'example_1' => FALSE,
|
536 |
|
|
'example_2' => FALSE,
|
537 |
|
|
'example_3' => FALSE,
|
538 |
|
|
'example_4' => FALSE,
|
539 |
|
|
);
|
540 |
|
|
// ...and the correct ones are.
|
541 |
|
|
$names[$name] = TRUE;
|
542 |
|
|
|
543 |
|
|
// Test for the specific HTML that the different file types appear as in the
|
544 |
|
|
// DOM.
|
545 |
|
|
$html = array(
|
546 |
|
|
'js' => array('<script type="text/javascript" src="', '"></script>'),
|
547 |
|
|
'css' => array('@import url("', '");'),
|
548 |
|
|
// PHP files do not get added to the DOM directly.
|
549 |
|
|
// @see _libraries_test_load()
|
550 |
|
|
'php' => array('<li>', '</li>'),
|
551 |
|
|
);
|
552 |
|
|
|
553 |
|
|
foreach ($names as $name => $expected) {
|
554 |
|
|
foreach ($extensions as $extension) {
|
555 |
3753f249
|
Assos Assos
|
$filepath = drupal_get_path('module', 'libraries') . "/tests/libraries/example/$name.$extension";
|
556 |
85ad3d82
|
Assos Assos
|
// JavaScript and CSS files appear as full URLs and with an appended
|
557 |
|
|
// query string.
|
558 |
|
|
if (in_array($extension, array('js', 'css'))) {
|
559 |
|
|
$filepath = url('', array('absolute' => TRUE)) . $filepath . '?' . variable_get('css_js_query_string');
|
560 |
|
|
}
|
561 |
|
|
$raw = $html[$extension][0] . $filepath . $html[$extension][1];
|
562 |
|
|
if ($expected) {
|
563 |
|
|
$this->assertRaw($raw, "$label$name.$extension found.");
|
564 |
|
|
}
|
565 |
|
|
else {
|
566 |
|
|
$this->assertNoRaw($raw, "$label$name.$extension not found.");
|
567 |
|
|
}
|
568 |
|
|
}
|
569 |
|
|
}
|
570 |
|
|
}
|
571 |
|
|
|
572 |
|
|
}
|