Révision 7e72b748
Ajouté par Assos Assos il y a plus de 6 ans
drupal7/sites/all/modules/ctools/tests/context.test | ||
---|---|---|
1 | 1 |
<?php |
2 | 2 |
|
3 |
/** |
|
4 |
* Test the keyword substitution functionality. |
|
5 |
*/ |
|
6 |
class CtoolsContextIDTestCase extends DrupalWebTestCase { |
|
7 |
|
|
8 |
/** |
|
9 |
* {@inheritDoc} |
|
10 |
*/ |
|
11 |
public static function getInfo() { |
|
12 |
return array( |
|
13 |
'name' => 'Context IDs', |
|
14 |
'description' => 'Verify that Context IDs work properly.', |
|
15 |
'group' => 'ctools', |
|
16 |
'dependencies' => array('ctools'), |
|
17 |
); |
|
18 |
} |
|
19 |
|
|
20 |
/** |
|
21 |
* {@inheritDoc} |
|
22 |
*/ |
|
23 |
protected function setUp(array $modules = array()) { |
|
24 |
$modules[] = 'ctools'; |
|
25 |
parent::setUp($modules); |
|
26 |
ctools_include('context'); |
|
27 |
} |
|
28 |
|
|
29 |
private function getTestContexts() { |
|
30 |
$test_objects = array( |
|
31 |
array(), |
|
32 |
array( |
|
33 |
'name' => 'foo_bar', |
|
34 |
), |
|
35 |
array( |
|
36 |
'id' => 25, |
|
37 |
), |
|
38 |
array( |
|
39 |
'id' => 5, |
|
40 |
'name' => NULL, |
|
41 |
), |
|
42 |
array( |
|
43 |
'id' => 'a', |
|
44 |
'name' => 'foo_bar', |
|
45 |
), |
|
46 |
array( |
|
47 |
'id' => 'z', |
|
48 |
'name' => 'baz', |
|
49 |
), |
|
50 |
array( |
|
51 |
'id' => 15, |
|
52 |
'name' => 'baz', |
|
53 |
), |
|
54 |
array( |
|
55 |
'id' => 'z', |
|
56 |
'name' => 'foo', |
|
57 |
), |
|
58 |
array( |
|
59 |
'id' => 1, |
|
60 |
'name' => 'foo', |
|
61 |
), |
|
62 |
array( |
|
63 |
'id' => 47, |
|
64 |
'name' => 'bar', |
|
65 |
), |
|
66 |
array( |
|
67 |
'id' => 99, |
|
68 |
'name' => 'bar', |
|
69 |
), |
|
70 |
); |
|
71 |
return $test_objects; |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* Test ctools_context_id where the context only has an id. |
|
76 |
*/ |
|
77 |
public function testContextId() { |
|
78 |
$context = array(); |
|
79 |
$expected = 'context__1'; |
|
80 |
$actual = ctools_context_id($context); |
|
81 |
$this->assertEqual($actual, $expected, 'Empty context has id ' . $expected); |
|
82 |
|
|
83 |
$context = array('id' => 4); |
|
84 |
$expected = 'context__4'; |
|
85 |
$actual = ctools_context_id($context); |
|
86 |
$this->assertEqual($actual, $expected, 'Context 4 has id ' . $expected); |
|
87 |
|
|
88 |
$context = array('id' => 'a'); |
|
89 |
$expected = 'context__a'; |
|
90 |
$actual = ctools_context_id($context); |
|
91 |
$this->assertEqual($actual, $expected, 'Context "a" has id ' . $expected); |
|
92 |
} |
|
93 |
|
|
94 |
/** |
|
95 |
* Test ctools_context_id where the context has an id and a name. |
|
96 |
*/ |
|
97 |
public function testContextIdName() { |
|
98 |
$context = array('id' => '512', 'name' => 'foo'); |
|
99 |
$expected = 'context_foo_512'; |
|
100 |
$this->assertEqual(ctools_context_id($context), $expected, 'Context "512"/"foo" has id ' . $expected); |
|
101 |
|
|
102 |
$context = array('id' => '512', 'name' => 'foo_bar'); |
|
103 |
$expected = 'context_foo_bar_512'; |
|
104 |
$this->assertEqual(ctools_context_id($context), $expected, 'Context "512"/"foo_bar" has id ' . $expected); |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* Test ctools_context_id where the context has an id & name, and a type is specified. |
|
109 |
*/ |
|
110 |
public function testContextIdNameType() { |
|
111 |
$type = 'sort'; |
|
112 |
$context = array('id' => '512', 'name' => 'foo'); |
|
113 |
$expected = 'sort_foo_512'; |
|
114 |
$this->assertEqual(ctools_context_id($context, $type), $expected, 'Context "512" has id ' . $expected); |
|
115 |
|
|
116 |
$type = NULL; |
|
117 |
$context = array('id' => '512', 'name' => 'foo'); |
|
118 |
$expected = '_foo_512'; |
|
119 |
$this->assertEqual(ctools_context_id($context, $type), $expected, 'Context "512" has id ' . $expected); |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Test ctools_context_next_id in various scenarios. |
|
124 |
*/ |
|
125 |
public function testNextContextId() { |
|
126 |
$test_objects = $this->getTestContexts(); |
|
127 |
|
|
128 |
// If no object list or name is given? |
|
129 |
$value = ctools_context_next_id(NULL, NULL); |
|
130 |
$expected = 1; |
|
131 |
$this->assertEqual($value, $expected, 'NULL objects have next id ' . $expected); |
|
132 |
|
|
133 |
// If no object list is given? |
|
134 |
$value = ctools_context_next_id(NULL, 'bar'); |
|
135 |
$expected = 1; |
|
136 |
$this->assertEqual($value, $expected, 'NULL objects have next id ' . $expected); |
|
137 |
|
|
138 |
// If no object list is given (another way)? |
|
139 |
$value = ctools_context_next_id(array(), 'bar'); |
|
140 |
$expected = 1; |
|
141 |
$this->assertEqual($value, $expected, 'Empty objects have next id ' . $expected); |
|
142 |
|
|
143 |
// The name is empty... which is just another name. |
|
144 |
$value = ctools_context_next_id($test_objects, ''); |
|
145 |
$expected = 1; |
|
146 |
$this->assertEqual($value, $expected, 'Unnamed objects have next id ' . $expected . ' (got ' . $value . ')'); |
|
147 |
|
|
148 |
// The value of the id key is not a number. |
|
149 |
$value = ctools_context_next_id($test_objects, 'foo_bar'); |
|
150 |
$expected = 1; |
|
151 |
$this->assertEqual($value, $expected, 'Objects with non-integer ids are ignored ' . $expected); |
|
152 |
|
|
153 |
// One object's id is not a number (ignore) but another is valid. |
|
154 |
$value = ctools_context_next_id($test_objects, 'baz'); |
|
155 |
$expected = 16; |
|
156 |
$this->assertEqual($value, $expected, 'Baz\'s objects have next id ' . $expected); |
|
157 |
|
|
158 |
// An expected case: there is one match and the id is numeric. |
|
159 |
$value = ctools_context_next_id($test_objects, 'foo'); |
|
160 |
$expected = 2; |
|
161 |
$this->assertEqual($value, $expected, 'Foo\'s objects have next id ' . $expected); |
|
162 |
|
|
163 |
// Another expected case: there are multiple numeric IDs |
|
164 |
$value = ctools_context_next_id($test_objects, 'bar'); |
|
165 |
$expected = 100; |
|
166 |
$this->assertEqual($value, $expected, 'Bar\'s objects have next id ' . $expected); |
|
167 |
} |
|
168 |
|
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 |
/** |
|
173 |
* Test the keyword substitution functionality. |
|
174 |
*/ |
|
3 | 175 |
class CtoolsContextKeywordsSubstitutionTestCase extends DrupalWebTestCase { |
176 |
|
|
177 |
/** |
|
178 |
* {@inheritDoc} |
|
179 |
*/ |
|
4 | 180 |
public static function getInfo() { |
5 | 181 |
return array( |
6 | 182 |
'name' => 'Keywords substitution', |
7 | 183 |
'description' => 'Verify that keywords are properly replaced with data.', |
8 | 184 |
'group' => 'ctools', |
185 |
'dependencies' => array('ctools'), |
|
9 | 186 |
); |
10 | 187 |
} |
11 | 188 |
|
12 |
public function setUp() { |
|
13 |
parent::setUp('ctools'); |
|
189 |
/** |
|
190 |
* {@inheritDoc} |
|
191 |
*/ |
|
192 |
protected function setUp(array $modules = array()) { |
|
193 |
$modules[] = 'ctools'; |
|
194 |
parent::setUp($modules); |
|
14 | 195 |
|
15 | 196 |
ctools_include('context'); |
16 | 197 |
} |
17 | 198 |
|
199 |
/** |
|
200 |
* Test the keyword substitution. |
|
201 |
*/ |
|
18 | 202 |
public function testKeywordsSubstitution() { |
19 | 203 |
// Create node context for substitution. |
20 | 204 |
$node = $this->drupalCreateNode(); |
... | ... | |
23 | 207 |
|
24 | 208 |
// Run tests on some edge cases. |
25 | 209 |
$checks = array( |
26 |
'%node:changed:raw:' => array( |
|
210 |
array( |
|
211 |
'%node:changed:raw:', |
|
212 |
array('%title' => ''), |
|
27 | 213 |
"{$node->changed}:", |
28 | 214 |
t('Multi-level token has been replaced. Colon left untouched.'), |
29 | 215 |
), |
30 |
'%node:title' => array( |
|
216 |
array( |
|
217 |
'%node:title', |
|
218 |
array('%title' => ''), |
|
31 | 219 |
"{$node->title}", |
32 | 220 |
t('Keyword and converter have been replaced.'), |
33 | 221 |
), |
34 |
'%%node:title' => array( |
|
222 |
array( |
|
223 |
'%%node:title', |
|
224 |
array('%title' => ''), |
|
35 | 225 |
"%node:title", |
36 | 226 |
t('Keyword after escaped percent sign left untouched.'), |
37 | 227 |
), |
38 |
'%node:title%node:nid' => array( |
|
228 |
array( |
|
229 |
'%node:title%node:nid', |
|
230 |
array('%title' => ''), |
|
39 | 231 |
"{$node->title}{$node->nid}", |
40 | 232 |
t('Multiple substitutions have been replaced.'), |
41 | 233 |
), |
42 |
'%node:title:' => array( |
|
234 |
array( |
|
235 |
'%node:title:', |
|
236 |
array('%title' => ''), |
|
43 | 237 |
"{$node->title}:", |
44 | 238 |
t('Colon after keyword and converter left untouched.'), |
45 | 239 |
), |
46 |
'%node:title%%' => array( |
|
240 |
array( |
|
241 |
'%node:title%%', |
|
242 |
array('%title' => ''), |
|
47 | 243 |
"{$node->title}%", |
48 | 244 |
t('Escaped percent sign after keyword and converter left untouched.'), |
49 | 245 |
), |
50 |
'%%%node:title' => array( |
|
246 |
array( |
|
247 |
'%%%node:title', |
|
248 |
array('%title' => ''), |
|
51 | 249 |
"%{$node->title}", |
52 | 250 |
t('Keyword after escaped and unescaped percent sign has been replaced.'), |
53 | 251 |
), |
54 |
'%%foo:bar' => array( |
|
252 |
array( |
|
253 |
'%%foo:bar', |
|
254 |
array('%title' => ''), |
|
55 | 255 |
"%foo:bar", |
56 | 256 |
t('Non-existant context ignored.'), |
57 | 257 |
), |
58 |
'There was about 20%-30% difference in price.' => array( |
|
258 |
array( |
|
259 |
'There was about 20%-30% difference in price.', |
|
260 |
array('%title' => ''), |
|
59 | 261 |
'There was about 20%-30% difference in price.', |
60 | 262 |
t('Non-keyword percent sign left untouched.'), |
61 | 263 |
), |
62 |
'href="my%20file%2dname.pdf"' => array( |
|
264 |
array( |
|
265 |
'href="my%20file%2dname.pdf"', |
|
266 |
array('%title' => ''), |
|
63 | 267 |
'href="my%20file%2dname.pdf"', |
64 | 268 |
t('HTTP URL escape left untouched.'), |
65 | 269 |
), |
66 |
'href="my%a0file%fdname.pdf"' => array( |
|
270 |
array( |
|
271 |
'href="my%a0file%fdname.pdf"', |
|
272 |
array('%title' => ''), |
|
67 | 273 |
'href="my%a0file%fdname.pdf"', |
68 | 274 |
t('HTTP URL escape (high-chars) left untouched.'), |
69 | 275 |
), |
70 |
'<a href="http://www.example.com/here%20is%20a%20pdf.pdf">Click here!</a>' => array( |
|
276 |
array( |
|
277 |
'<a href="http://www.example.com/here%20is%20a%20pdf.pdf">Click here!</a>', |
|
278 |
array('%title' => ''), |
|
71 | 279 |
'<a href="http://www.example.com/here%20is%20a%20pdf.pdf">Click here!</a>', |
72 | 280 |
t('HTTP URL escape percent sign left untouched in HTML.'), |
73 | 281 |
), |
74 |
'SELECT * FROM {table} WHERE field = "%s"' => array( |
|
282 |
array( |
|
283 |
'SELECT * FROM {table} WHERE field = "%s"', |
|
284 |
array('%title' => ''), |
|
75 | 285 |
'SELECT * FROM {table} WHERE field = "%s"', |
76 | 286 |
t('SQL percent sign left untouched.'), |
77 | 287 |
), |
288 |
array( |
|
289 |
'%title', |
|
290 |
array('%title' => 'foobar'), |
|
291 |
'foobar', |
|
292 |
t('String value in $keywords array is returned.'), |
|
293 |
), |
|
294 |
array( |
|
295 |
'%title', |
|
296 |
array('%title' => ''), |
|
297 |
'', |
|
298 |
t('Empty string value in $keywords array returns empty string.'), |
|
299 |
), |
|
300 |
array( |
|
301 |
'%title', |
|
302 |
array('%title' => NULL), |
|
303 |
'', |
|
304 |
t('NULL value in $keywords array returns empty string.'), |
|
305 |
), |
|
306 |
array( |
|
307 |
'%title', |
|
308 |
array('%title' => FALSE), |
|
309 |
'', |
|
310 |
t('FALSE value in $keywords array returns empty string.'), |
|
311 |
), |
|
312 |
array( |
|
313 |
'%title', |
|
314 |
array('%title' => 11), |
|
315 |
'11', |
|
316 |
t('Integer value in $keywords array returns string representation of the integer.'), |
|
317 |
), |
|
318 |
array( |
|
319 |
'%title', |
|
320 |
array('%title' => 'substring %title'), |
|
321 |
'substring %title', |
|
322 |
t('Input value as substring in $keywords array left untouched.'), |
|
323 |
), |
|
78 | 324 |
); |
79 |
foreach ($checks as $string => $expectations) {
|
|
80 |
list($expected_result, $message) = $expectations;
|
|
81 |
$actual_result = ctools_context_keyword_substitute($string, array(), $contexts);
|
|
325 |
foreach ($checks as $check) {
|
|
326 |
list($string, $keywords, $expected_result, $message) = $check;
|
|
327 |
$actual_result = ctools_context_keyword_substitute($string, $keywords, $contexts);
|
|
82 | 328 |
$this->assertEqual($actual_result, $expected_result, $message); |
83 | 329 |
} |
84 | 330 |
} |
85 | 331 |
|
86 | 332 |
} |
333 |
|
|
334 |
/** |
|
335 |
* Test the context classes. |
|
336 |
*/ |
|
337 |
class CtoolsContextUnitTestCase extends DrupalUnitTestCase { |
|
338 |
|
|
339 |
/** |
|
340 |
* {@inheritDoc} |
|
341 |
*/ |
|
342 |
public static function getInfo() { |
|
343 |
return array( |
|
344 |
'name' => 'Context unit tests', |
|
345 |
'description' => 'Verifies that context classes behave correctly', |
|
346 |
'group' => 'ctools', |
|
347 |
'dependencies' => array('ctools'), |
|
348 |
); |
|
349 |
} |
|
350 |
|
|
351 |
/** |
|
352 |
* {@inheritDoc} |
|
353 |
*/ |
|
354 |
protected function setUp() { |
|
355 |
parent::setUp(); |
|
356 |
require_once __DIR__ . '/../includes/context.inc'; |
|
357 |
} |
|
358 |
|
|
359 |
/** |
|
360 |
* Tests that contexts have the correct required property value. |
|
361 |
*/ |
|
362 |
public function testOptionalRequiredContext() { |
|
363 |
$required_context = new ctools_context_required('test1'); |
|
364 |
$this->assertTrue($required_context->required); |
|
365 |
|
|
366 |
$optional_context = new ctools_context_optional('test2'); |
|
367 |
$this->assertFalse($optional_context->required); |
|
368 |
} |
|
369 |
|
|
370 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules