-
Notifications
You must be signed in to change notification settings - Fork 2
/
vinculum.test
474 lines (401 loc) · 16.2 KB
/
vinculum.test
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/**
* @file
* Tests for the vinculum module.
*/
/**
* Test that the list of handlers is loaded properly.
*/
class VinculumLoadHandlersTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'API: Load handlers',
'description' => 'Test the registration of vinculum handlers.',
'group' => 'Vinculum',
);
}
function setUp() {
parent::setUp('vinculum', 'vinculum_dummy_handler');
}
/**
* Test the module-weight listing helper.
*/
function testGetModuleWeights() {
$expected_weights = array(
'vinculum' => 0,
'vinculum_dummy_handler' => 0,
);
$weights = _vinculum_get_module_weights(array('vinculum', 'vinculum_dummy_handler'));
$this->assertTrue(count($weights) == 2, t('@count weights reported.', array('@count' => count($weights))));
$this->assertEqual($weights, $expected_weights, t('Helper reports correct weights for the modules.'));
}
/**
* Enable a dummy handler and check the hooks provide the proper handler
* information.
*/
function testLoadHandler() {
// With the dummy handler enabled, the protocol-list should show a single
// handler:
$expected_handler = (object) array(
'protocol' => t('Dummy handler'),
'module' => 'vinculum_dummy_handler',
'weight' => 0,
);
$handlers = vinculum_load_all_handlers(TRUE);
$this->assertTrue(count($handlers) == 1, t('@count handler loaded.', array('@count' => count($handlers))));
$this->assertEqual(array_shift($handlers), $expected_handler, t('Handler has correct information.'));
}
/**
* Check that the vinculum_load_all_handlers() helper will always provide the
* handlers ordered by module-weight, and that the vinculum_set_weight()
* helper will correctly reset the system module-weight caches.
*/
function testResetModuleWeights() {
// Enable the second handler.
module_enable(array('vinculum_dummy_handler2'));
// With both dummy handlers enabled, the protocol-list should show both
// handlers, ordered by filename
$expected_handlers = array();
$expected_handlers['vinculum_dummy_handler'] = (object) array(
'protocol' => t('Dummy handler'),
'module' => 'vinculum_dummy_handler',
'weight' => 0,
);
$expected_handlers['vinculum_dummy_handler2'] = (object) array(
'protocol' => t('Dummy handler 2'),
'module' => 'vinculum_dummy_handler2',
'weight' => 0,
);
$handlers = vinculum_load_all_handlers(TRUE);
$this->assertTrue(count($handlers) == 2, t('@count handlers loaded.', array('@count' => count($handlers))));
$this->assertEqual($handlers, $expected_handlers, t('Unweighted handlers are sorted correctly (by module name).'));
// Give the first dummy handler a high weight.
$weights = array(
'vinculum_dummy_handler' => 5,
);
vinculum_set_weights($weights);
$expected_handlers = array();
$expected_handlers['vinculum_dummy_handler2'] = (object) array(
'protocol' => t('Dummy handler 2'),
'module' => 'vinculum_dummy_handler2',
'weight' => 0,
);
$expected_handlers['vinculum_dummy_handler'] = (object) array(
'protocol' => t('Dummy handler'),
'module' => 'vinculum_dummy_handler',
'weight' => 5,
);
$handlers = vinculum_load_all_handlers(TRUE);
$this->assertTrue(count($handlers) == 2, t('@count handlers loaded.', array('@count' => count($handlers))));
$this->assertEqual($handlers, $expected_handlers, t('Weighted handlers are sorted correctly, with the higher-weight module coming second.'));
}
}
/**
* Test that node-saving triggers the vinculum protocol.
*/
class VinculumSendTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'API: Send vinculums',
'description' => 'Test that vinculums are sent when a node is saved.',
'group' => 'Vinculum',
);
}
function setUp() {
parent::setUp('vinculum', 'vinculum_dummy_handler');
// Create a dummy content-type
$type = array(
'type' => 'vinculum_dummy_contenttype',
'name' => st('Vinculum dummy content-type'),
'base' => 'node_content',
'description' => st("A dummy content-type, used for testing the Vinculum module. This content-type should not be visible."),
'custom' => 1,
'modified' => 1,
'locked' => 0,
);
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
}
/**
* Test the extraction of text from a node field.
*/
function testFieldExtraction() {
$source_text = "Example plain text which suggests the links http://example.com/ and http://foo.example.com/bar/x.html";
// Dummy node, not to be used with drupalCreateNode().
$node = new stdClass;
$node->type = 'vinculum_dummy_contenttype';
$node->body = array(
LANGUAGE_NONE => array(
0 => array(
'value' => $source_text,
'format' => 'plain_text',
),
),
);
// The expected text is the source text, wrapped in <p> tags. with the links wrapped in <a> tags.
$expected = '<p>Example plain text which suggests the links <a href="http://example.com/">http://example.com/</a> and <a href="http://foo.example.com/bar/x.html">http://foo.example.com/bar/x.html</a></p>';
$extract = trim(_vinculum_extract_text($node));
$this->assertEqual($expected, $extract, t('Helper correctly extracts the text from the field.'));
}
/**
* Test the discovery of URLs within text.
*/
function testUrlExtraction() {
$text = "
This is a section of test text which <a href='http://example.com/'>links
to example</a> and <a href='http://foo.example.com/bar/x.html'>links to
another example</a>.";
$expected_links = array(
'http://example.com/',
'http://foo.example.com/bar/x.html',
);
$links = _vinculum_extract_urls($text);
$this->assertTrue(count($links) == 2, t('@count links reported.', array('@count' => count($links))));
$this->assertEqual($links, $expected_links, t('Helper reports correct links for the sample text.'));
}
/**
* Test that the helper to send a single vinculum is processed.
*/
function testSendSingleVinculum() {
$node = $this->drupalCreateNode();
$source = "node/{$node->nid}";
$target = "http://example.com/";
$handler_result = vinculum_send_single_vinculum($source, $target, $node);
$this->assertTrue($handler_result, t('The dummy vinculum handler reports success.'));
$vinculum_is_recorded = vinculum_is_sent($node->nid, $target);
$this->assertTrue($vinculum_is_recorded, t('The vinculum has been tracked in the DB.'));
}
/**
* Test that vinculums are attempted when a node is saved.
*/
function testSendVinculums() {
$text = "
This is a section of test text which <a href='http://example.com/'>links
to example</a> and <a href='http://foo.example.com/bar/x.html'>links to
another example</a>.";
$settings = array(
'type' => 'vinculum_dummy_contenttype',
'title' => 'Example node used for testing the vinculum module.',
'body' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => $text,
'format' => filter_default_format(),
),
),
),
);
$node = $this->drupalCreateNode($settings);
// After the node is saved, a record should be added to the
// {node_vinculum_sent table recording 'vinculum_dummy_handler' as the
// handler which successfully sent the vinculum.
$target = 'http://example.com/';
$success = vinculum_is_sent($node->nid, $target);
$this->assertTrue($success, t('The link to @target has been successfully recorded.', array('@target' => $target)));
$target = 'http://foo.example.com/bar/x.html';
$success = vinculum_is_sent($node->nid, $target);
$this->assertTrue($success, t('The link to @target has been successfully recorded.', array('@target' => $target)));
}
}
/**
* Test that node-saving triggers the vinculum protocol.
*/
class VinculumTestSuiteTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'API: Test whole suite',
'description' => 'Test the full pingback/trackback process with dummy test endpoints.',
'group' => 'Vinculum',
);
}
function setUp() {
parent::setUp('vinculum', 'vinculum_pingback', 'vinculum_trackback', 'vinculum_trackback_autodiscovery', 'vinculum_dummy_endpoints');
// Create a dummy content-type
$type_machine_name = 'vinculum_dummy_contenttype';
$type = array(
'type' => $type_machine_name,
'name' => st('Vinculum dummy content-type'),
'base' => 'node_content',
'description' => st("A dummy content-type, used for testing the Vinculum module. This content-type should not be visible."),
'custom' => 1,
'modified' => 1,
'locked' => 0,
);
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
// Allow vinculums to be sent for this content-type.
variable_set("vinculum_send_{$type_machine_name}", TRUE);
}
/**
* Test the general setup.
*/
function testSetup() {
// Check that the helper functions can properly extract the URLs from the node.
$url = url('vinculum_dummy_endpoints/no_vinculum_support', array('absolute' => TRUE));
// $text = "
// This is a section of test text which <a href='{$url}'>links to a test
// endpoint</a>.";
$text = "This is a section of test text which references {$url}.";
// Check that the correct URL is extracted from the text.
$links = _vinculum_extract_urls($text);
$expected = array($url);
$this->assertEqual($links, $expected, t('Links can be extracted from the text.'));
$settings = array(
'type' => 'vinculum_dummy_contenttype',
'title' => 'Example node used for testing the vinculum module.',
'body' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => $text,
'format' => filter_default_format(),
),
),
),
);
$node = $this->drupalCreateNode($settings);
// Check that the correct URL is extracted from the node.
$node = node_load($node->nid);
$links = vinculum_get_external_links($node);
$expected = array($url);
$this->assertEqual($links, $expected, t('Links can be extracted from the node.'));
}
/**
* Support for an endpoint with no vinculum support.
*/
function testNoVinculumSupport() {
// Create a node linking to vinculum_dummy_endpoints/no_vinculum_support.
// The vinculum should report failure.
$url = url('vinculum_dummy_endpoints/no_vinculum_support', array('absolute' => TRUE));
$text = "
This is a section of test text which <a href='{$url}'>links to a test
endpoint</a>.";
$settings = array(
'type' => 'vinculum_dummy_contenttype',
'title' => 'Example node used for testing the vinculum module.',
'body' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => $text,
'format' => filter_default_format(),
),
),
),
);
$node = $this->drupalCreateNode($settings);
// This should change the {node_vinculum_sent} table: a new entry should be
// added with the nid/URL, but handler should be NULL.
$result = db_select('node_vinculum_sent', 'nls')
->fields('nls', array('handler'))
->condition('nls.nid', $node->nid)
->condition('nls.url', $url)
->execute()
->fetchObject();
$this->assertNull($result->handler, t("When vinculums aren't supported, a NULL entry is recorded in {node_vinculum_sent}."));
}
//
// /**
// * Support for Pingback only.
// */
// function testPingbackOnlySupport() {
// // Create a node linking to vinculum_dummy_endpoints/no_vinculum_support.
// // The vinculum should report failure.
//
// $url = url('vinculum_dummy_endpoints/pingback_only', array('absolute' => TRUE));
// $text = "
// This is a section of test text which <a href='{$url}'>links to a test
// endpoint</a>.";
//
// $settings = array(
// 'type' => 'vinculum_dummy_contenttype',
// 'title' => 'Example node used for testing the vinculum module.',
// 'body' => array(
// LANGUAGE_NONE => array(
// 0 => array(
// 'value' => $text,
// 'format' => filter_default_format(),
// ),
// ),
// ),
// );
// $node = $this->drupalCreateNode($settings);
//
// // This should change the {node_vinculum_sent} table: a new entry should be
// // added with the nid/URL, with the handler set to 'vinculum_pingback'.
// $result = db_select('node_vinculum_sent', 'nls')
// ->fields('nls', array('handler'))
// ->condition('nls.nid', $node->nid)
// ->condition('nls.url', $url)
// ->execute()
// ->fetchAll();
// $this->assertEqual($result->handler, 'vinculum_pingback', t("When an endpoint supports only pingback, an entry is recorded in {node_vinculum_sent} with the handler 'vinculum_pingback'."));
// }
/**
* Support for Trackback only.
*/
function testTrackbackOnlySupport() {
// The target for this test is vinculum_dummy_endpoints/trackback_only
$url = url('vinculum_dummy_endpoints/trackback_only', array('absolute' => TRUE));
// The endpoint at $url should support auto-discovery of trackbacks.
$trackback_endpoint_url = vinculum_trackback_autodiscovery_autodetect_trackback_support($url);
$expected = url(VINCULUM_TEST_TRACKBACK_ENDPOINT, array('absolute' => TRUE));
$this->assertEqual($trackback_endpoint_url, $expected, t('Correct endpoint auto-discovered in the test URL at %url', array('%url', $url)));
// Create a node linking to vinculum_dummy_endpoints/no_vinculum_support.
// The vinculum should report failure.
$text = "
This is a section of test text which <a href='{$url}'>links to a test
endpoint</a>.";
$settings = array(
'type' => 'vinculum_dummy_contenttype',
'title' => 'Example node used for testing the vinculum module.',
'body' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => $text,
'format' => filter_default_format(),
),
),
),
);
$node = $this->drupalCreateNode($settings);
// This should change the {node_vinculum_sent} table: a new entry should be
// added with the nid/URL, with the handler set to 'vinculum_pingback'.
$result = db_select('node_vinculum_sent', 'nls')
->fields('nls', array('handler'))
->condition('nls.nid', $node->nid)
->condition('nls.url', $url)
->execute()
->fetchObject();
$this->assertEqual($result->handler, 'vinculum_trackback_autodiscovery', t("When an endpoint supports only trackback, an entry is recorded in {node_vinculum_sent} with the handler 'vinculum_trackback_autodiscovery'."));
// The endpoint should add an entry into the variables table for
// vinculum_testing_last_trackback.
$expected = array(
'url' => url("node/{$node->nid}", array('absolute' => TRUE)),
'title' => $node->title,
'blog_name' => variable_get('site_name', 'Drupal'),
'excerpt' => NULL,
);
$this->refreshVariables();
$result = variable_get('vinculum_testing_last_trackback', FALSE);
$this->verbose('<h1>Expected results</h1>'
. '<code>' . str_replace("\n", "<br />\n", var_export($expected, TRUE)) . '</code>'
. '<hr />'
. '<h1>Actual results</h1>'
. '<code>' . str_replace("\n", "<br />\n", var_export($result, TRUE)) . '</code>');
$this->assertEqual($result, $expected, t('The correct data was sent to the Trackback endpoint.'));
// Debug: Dump the watchdog table.
// $x = db_select('watchdog', 'w')
// ->fields('w')
// ->execute()
// ->fetchAll();
// $out = '';
// foreach($x as $row) {
// $out .= '<strong>';
// $out .= t($row->message, unserialize($row->variables));
// $out .= '</strong><br />';
// $out .= str_replace("\n", "<br />\n", var_export($row, TRUE)) . '<hr />';
// }
// $this->verbose($out);
}
}