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