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