-
Notifications
You must be signed in to change notification settings - Fork 1
/
calais.test
450 lines (362 loc) · 17.9 KB
/
calais.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
<?php
class CalaisTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('calais_api', 'calais', 'rdf');
// Create and login user
$admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer calais', 'access calais'));
$this->drupalLogin($admin_user);
}
function tearDown() {
parent::tearDown();
}
function createVocabulary() {
$vocab = array();
$vocab['name'] = $this->randomName(20);
$vocab['nodes'] = array('page' => TRUE);
taxonomy_save_vocabulary($vocab);
return $vocab;
}
function createTerm($vid, $overrides = array()) {
$term = array(
'vid' => $vid,
'name' => $this->randomName(30),
'description' => $this->randomName(200),
);
$term += $overrides;
taxonomy_save_term($term);
return (object)$term;
}
}
class CalaisGetTermTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Calais Get Term'),
'description' => t('Test various finds for local Calais Terms.'),
'group' => t('Calais')
);
}
function testGetTerm() {
$vocab = $this->createVocabulary();
$calais_term = new CalaisTerm($this->randomName(100), 'CalaisGetTerm');
$found = calais_get_term($vocab['vid'], $calais_term);
$this->assertFalse($found, 'FALSE returned for nonexistent Calais term.');
db_query("INSERT INTO {calais_term} (vid, name) VALUES (%d, '%s')", $vocab['vid'], $calais_term->name);
$tid = db_last_insert_id('calais_term', 'tid');
$found = calais_get_term($vocab['vid'], $calais_term);
$this->assertTrue($found, 'Calais term found by name');
$this->assertEqual($found->name, $calais_term->name, 'Calais term found by name matches');
$calais_term->value = $this->randomName(100); // Make sure it can't be found by name on a failure
db_query("UPDATE {calais_term} SET guid = '%s' WHERE tid = %d", $calais_term->guid, $tid);
$found = calais_get_term($vocab['vid'], $calais_term);
$this->assertTrue($found, 'Calais term found by guid');
$this->assertEqual($found->guid, $calais_term->guid, 'Calais term found by guid matches');
// Test getting resolved company data
db_query("INSERT INTO {calais_term_data_company} (tid, ticker, score) VALUES (%d, '%s', %f)", $tid, 'TRI', 0.999);
db_query("UPDATE {calais_term} SET resolved_type = '%s' WHERE tid = %d", 'company', $tid);
$found = calais_get_term($vocab['vid'], $calais_term);
$this->assertTrue($found, 'Calais term & data found by guid');
$this->assertEqual(count($found->extra), 4, 'Calais resolved data has 4 values');
$this->assertEqual($found->extra['ticker'], 'TRI', 'Calais term ticker is TRI');
$this->assertEqual($found->extra['score'], 0.999, 'Calais term score is 0.999');
$found = calais_get_term($vocab['vid'], $tid);
$this->assertTrue($found, 'Calais term found by tid');
}
}
class CalaisSaveTermTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Calais Save Term'),
'description' => t('Test various saves for local Calais Terms.'),
'group' => t('Calais')
);
}
function testSaveTerm() {
$vocab = $this->createVocabulary();
$vid = $vocab['vid'];
$calais_term = new CalaisTerm($this->randomName(100), 'CalaisSaveTerm');
$calais_term->vid = $vid;
calais_save_term($calais_term);
$this->assertNotNull($calais_term->tid, 'Term ID set for new Calais term.');
$found = calais_get_term($vid, $calais_term->tid);
$this->assertEqual($found->guid, $calais_term->guid, 'Inserted Calais term found');
$this->assertNull($found->resolved_guid, 'Calais term resolved guid is null');
$new_guid = $this->randomName(100);
$calais_term->resolved_guid = $new_guid;
calais_save_term($calais_term);
$found = calais_get_term($vid, $calais_term->tid);
$this->assertEqual($found->resolved_guid, $new_guid, 'Calais term resolved guid updated');
$calais_term->resolved_type = 'geo';
$calais_term->extra = array(
'latitude' => 44.4444,
'longitude' => -88.888,
);
calais_save_term($calais_term);
$found = calais_get_term($vid, $calais_term->tid);
$this->assertEqual(count($found->extra), 7, 'Calais resolved data has 7 values');
$this->assertEqual($found->extra['latitude'], 44.4444, 'Calais term latitude is 44.4444');
$this->assertEqual($found->extra['longitude'], -88.888, 'Calais term longitude is -88.888');
$calais_term->extra['containedbycountry'] = 'USA';
calais_save_term($calais_term);
$found = calais_get_term($vid, $calais_term->tid);
$this->assertEqual(count($found->extra), 7, 'Calais resolved data has 7 values');
$this->assertEqual($found->extra['containedbycountry'], 'USA', 'Calais term containedbycountry is USA');
}
}
class CalaisGetKeywordsTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Get Keywords'),
'description' => t('Test looking up locally cached Calais keywords with threshold filtering.'),
'group' => t('Calais')
);
}
// Associating new calais terms with an existing node
function testGetKeywords() {
$vocab = $this->createVocabulary();
$vid = $vocab['vid'];
$node = $this->drupalCreateNode();
$calais_terms = array();
$calais_terms[0] = new CalaisTerm($this->randomName(100), 'Low Relevance', 0.001);
$calais_terms[1] = new CalaisTerm($this->randomName(100), 'Mid Relevance', 0.5);
$calais_terms[2] = new CalaisTerm($this->randomName(100), 'High Relevance', 1.0);
foreach ($calais_terms as $calais_term) {
calais_associate_term($vid, $calais_term, $node);
}
$type = drupal_strtolower($node->type);
$thres_key = "calais_threshold_{$type}";
variable_set($thres_key, 0.0);
$keywords = calais_get_keywords($node->nid, $node->type, $vid);
$this->assertEqual(count($keywords[$vid]), 3, "Three terms were returned for no threshold.");
$this->assertEqual($keywords[$vid][0]->guid, $calais_terms[2]->guid, "Correct 1st ordering");
$this->assertEqual($keywords[$vid][1]->guid, $calais_terms[1]->guid, "Correct 2nd ordering");
$this->assertEqual($keywords[$vid][2]->guid, $calais_terms[0]->guid, "Correct 3rd ordering");
variable_set($thres_key, 0.499);
$keywords = calais_get_keywords($node->nid, $node->type, $vid);
$this->assertEqual(count($keywords[$vid]), 2, "Two terms were returned for < 0.001.");
variable_set($thres_key, 0.500);
$keywords = calais_get_keywords($node->nid, $node->type, $vid);
$this->assertEqual(count($keywords[$vid]), 2, "Two terms were returned for < 0.001.");
variable_set($thres_key, 0.5001);
$keywords = calais_get_keywords($node->nid, $node->type, $vid);
$this->assertEqual(count($keywords[$vid]), 1, "One term was returned for > 0.001.");
variable_set($thres_key, 1.0);
$keywords = calais_get_keywords($node->nid, $node->type, $vid);
$this->assertEqual(count($keywords[$vid]), 1, "One term was returned for max threshold.");
$keywords = calais_get_keywords($node->nid, $node->type, $vid, 0.002);
$this->assertEqual(count($keywords[$vid]), 2, "Two terms were returned for overriden threshold.");
$this->assertEqual($keywords[$vid][0]->guid, $calais_terms[2]->guid, "Correct 1st ordering");
}
}
class CalaisApplyToNodeTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Apply Calais Entity'),
'description' => t('Apply a Calais Entity and Taxonomy to a Node.'),
'group' => t('Calais')
);
}
// Test filling in an existing terms guid
function testApply() {
$vocab = $this->createVocabulary();
$node = $this->drupalCreateNode();
// Test no Tax Term exists
$calais_term1 = new CalaisTerm($this->randomName(100), $this->randomName(25));
calais_save_term($calais_term1);
$terms = taxonomy_get_term_by_name($calais_term1->name);
$this->assertTrue(empty($terms), 'No term exists yet');
$this->assertNull($calais_term1->tdid, 'CalaisTerm tdid is null');
calais_apply_to_node($vocab['vid'], $calais_term1, $node);
$terms = taxonomy_node_get_terms($node);
$this->assertEqual(count($terms), 1, 'Term found that matches CalaisTerm');
$found = calais_get_term($vocab['vid'], $calais_term1->tid);
$this->assertEqual(reset($terms)->tid, $found->tdid, 'Term ID matches CalaisTerm tdid');
// Test Tax Term exists
$node2 = $this->drupalCreateNode();
$term = $this->createTerm($vocab['vid']);
$calais_term2 = new CalaisTerm($this->randomName(100), $term->name);
calais_save_term($calais_term2);
$this->assertNull($calais_term2->tdid, 'CalaisTerm tdid is null');
calais_apply_to_node($vocab['vid'], $calais_term2, $node2);
$this->assertEqual($term->tid, $calais_term2->tdid, 'Term ID set on CalaisTerm');
$terms = taxonomy_node_get_terms($node2);
$this->assertEqual(count($terms), 1, 'Term found for node');
$found = calais_get_term($vocab['vid'], $calais_term2->tid);
$this->assertEqual(reset($terms)->tid, $found->tdid, 'Term ID matches CalaisTerm tdid');
}
}
class CalaisHookTaxonomyTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Hook Taxonomy'),
'description' => t('Test that hook_taxonomy effecitively cleans up.'),
'group' => t('Calais')
);
}
function testHookTaxonomy() {
$vocab = $this->createVocabulary();
$node = $this->drupalCreateNode();
$term = $this->createTerm($vocab['vid']);
$calais_term = new CalaisTerm($this->randomName(100), $term->name);
calais_save_term($calais_term1);
calais_apply_to_node($vocab['vid'], $calais_term, $node);
$terms = taxonomy_node_get_terms($node);
$this->assertEqual(count($terms), 1, 'Term found for node');
$this->assertEqual(reset($terms)->tid, $calais_term->tdid, 'CalaisTerm tdid matches Term ID');
taxonomy_del_term($term->tid);
$found = calais_get_term($vocab['vid'], $calais_term->tid);
$this->assertNull($found->tdid, 'CalaisTerm tdid is null');
}
}
class CalaisFormSubmitTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Submit Calais Form'),
'description' => t('Test submitting the Calais tab form.'),
'group' => t('Calais')
);
}
// Associating new calais terms with an existing node
function testFormSubmit() {
$vocab = $this->createVocabulary();
$vid = $vocab['vid'];
$node = $this->drupalCreateNode();
variable_set("calais_node_{$node->type}_request", CALAIS_REQUEST_AUTO);
variable_set("calais_node_{$node->type}_process", CALAIS_PROCESS_AUTO);
variable_set('calais_vocabulary_names', array($vocab['name'] => $vocab['vid']));
variable_set('calais_applied_entities_global', array($vocab['name'] => $vocab['name']));
$calais_terms = array();
$calais_terms[0] = new CalaisTerm($this->randomName(100), "TestTerm1", 1);
$calais_terms[1] = new CalaisTerm($this->randomName(100), "TestTerm2", 1);
$calais_terms[2] = new CalaisTerm($this->randomName(100), "TestTerm3", 1);
foreach ($calais_terms as $calais_term) {
calais_associate_term($vid, $calais_term, $node);
$found = calais_get_term($vid, $calais_term->tid);
$this->assertNull($found->tdid, 'Taxonomy Term ID is not set');
}
$form["taxonomy[tags][$vid]"] = "{$calais_terms[0]->name}, {$calais_terms[1]->name}";
// Test validation error on non "tags" vocabulary
$html = $this->drupalPost("node/{$node->nid}/calais", $form, t('Save'));
$error = t('The @name vocabulary can not be modified in this way.', array('@name' => $vocab['name']));
$this->assertText($error);
$vocab['tags'] = TRUE;
taxonomy_save_vocabulary($vocab);
$html = $this->drupalPost("node/{$node->nid}/calais", $form, t('Save'));
$this->assertNoText($error);
// //$node_terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
$node_terms = taxonomy_node_get_terms($node);
$this->assertEqual(count($node_terms), 2, 'Two Node Taxonomy Terms Exists');
$term1 = reset($node_terms);
$calais_term1 = calais_get_node_term_by_name($node->nid, $term1->name);
$this->assertEqual($term1->tid, $calais_term1->tdid, 'Calais Term 1 has Tax Term ID set');
$term2 = reset($node_terms);
$calais_term2 = calais_get_node_term_by_name($node->nid, $term2->name);
$this->assertEqual($term2->tid, $calais_term2->tdid, 'Calais Term 2 has Tax Term ID set');
}
}
class CalaisLinkedDataUriTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Linked Data URIs'),
'description' => t('Test the fetching and storage/lookup of Linked Open Data URIs.'),
'group' => t('Calais')
);
}
function setUp() {
// Get the configured API key before generating a new $db_prefix
$api_key = variable_get('calais_api_key', '');
parent::setUp();
// Make sure Calais will process nodes of type page
variable_set('calais_api_key', $api_key);
variable_set("calais_node_page_request", CALAIS_REQUEST_AUTO);
variable_set("calais_node_page_process", CALAIS_PROCESS_AUTO);
variable_set('calais_lookup_loduri', TRUE);
variable_set('calais_applied_entities_global', array('Company' => 'Company'));
$vocabs = variable_get('calais_vocabulary_names', array());
$company = (array)taxonomy_vocabulary_load($vocabs['Company']);
$company['nodes'] = array('page' => TRUE);
taxonomy_save_vocabulary($company);
}
// Test the retrieval and lookup of linked data uris
function testLookupURIs() {
$node = new stdClass;
$node->uid = 1;
$node->status = 1;
$node->type = 'page';
$node->title = 'Test Node for Linked Data URIs';
$node->body = 'There is one company that I would like to talk about, and that company is Microsoft';
node_save($node);
$node = node_load($node->nid);
$this->assertTrue(!empty($node->taxonomy), "Node has taxonomy terms populated by Calais");
$term = array_shift($node->taxonomy);
$this->assertIdentical('Microsoft', $term->name, "Term is not Microsoft");
$calais_term = calais_get_term_by_taxonomy($term->tid);
$this->assertIdentical('http://cb.semsol.org/company/microsoft#self', $calais_term->uris['cb.semsol.org'], "CrunchBase URI is not Micrsoft");
$this->assertIdentical('http://dbpedia.org/resource/Microsoft', $calais_term->uris['dbpedia.org'], "DBpedia URI is not Micrsoft");
$this->assertIdentical('http://rdf.freebase.com/ns/guid.9202a8c04000641f8000000000026344', $calais_term->uris['rdf.freebase.com'], "Freebase URI is not Micrsoft");
}
}
/*
class CalaisTaxonomyTermLookupTestCase extends CalaisTestCase {
function getInfo() {
return array(
'name' => t('Taxonomy Term Lookup'),
'description' => t('Test finding a Taxonomy Terms fpr a Calais suggestion. This test the fall back from a GUID lookup, to a name lookup, to creation.'),
'group' => t('Calais')
);
}
// Associating new calais terms with an existing node
function testLookupTerm() {
$vocab = $this->createVocabulary();
$vid = $vocab['vid'];
$calais_term = new CalaisTerm($this->randomName(100), $this->randomName(20), 0.5);
$current_terms = taxonomy_get_term_by_name($calais_term->value);
$this->assertTrue(empty($current_terms), 'No Taxonomy Term Exist');
// Find for term that does not exist
$tid = calais_get_taxonomy_term($vid, $calais_term);
$foundterm = taxonomy_get_term($tid);
$this->assertTrue($foundterm, 'Taxonomy Term was created');
$this->assertEqual($foundterm->name, $calais_term->value, 'Taxonomy Term value was setup correctly');
// FInd for term that exists and has GUID set
$term = $this->createTerm($vid, array('guid' => $this->randomName(100)));
$calais_term = new CalaisTerm($term->guid, $term->name, 0.5);
$tid = calais_get_taxonomy_term($vid, $calais_term);
$this->assertEqual($tid, $term->tid, 'Correct Taxonomy Term was found by GUID');
$foundterm = taxonomy_get_term($tid);
$this->assertEqual($foundterm->name, $calais_term->value, 'Taxonomy Term value matches');
$this->assertEqual($foundterm->guid, $calais_term->guid, 'Taxonomy Term guid matches');
// Find for term that exists by name but does not have GUID set (null), GUID will get set
$term = $this->createTerm($vid);
$calais_term = new CalaisTerm($this->randomName(100), $term->name, 0.5);
$tid = calais_get_taxonomy_term($vid, $calais_term);
$this->assertEqual($tid, $term->tid, 'Correct Taxonomy Term was found by NAME');
$foundterm = taxonomy_get_term($tid);
$this->assertEqual($foundterm->name, $calais_term->value, 'Taxonomy Term value matches');
$this->assertEqual($foundterm->guid, $calais_term->guid, 'Taxonomy Term guid was set and matches');
// Find for term that exists by name but does not have GUID set (blank), GUID will get set
$term = $this->createTerm($vid, array('guid' => ''));
$calais_term = new CalaisTerm($this->randomName(100), $term->name, 0.5);
$tid = calais_get_taxonomy_term($vid, $calais_term);
$this->assertEqual($tid, $term->tid, 'Correct Taxonomy Term was found by NAME');
$foundterm = taxonomy_get_term($tid);
$this->assertEqual($foundterm->name, $calais_term->value, 'Taxonomy Term value matches');
$this->assertEqual($foundterm->guid, $calais_term->guid, 'Taxonomy Term guid was set and matches');
// Find for term that exists by name with no GUID. GUID should not get set
$term = $this->createTerm($vid);
//dvm(taxonomy_get_term($term->tid));
$calais_term = new CalaisTerm(NULL, $term->name, 0.5);
$tid = calais_get_taxonomy_term($vid, $calais_term);
$this->assertEqual($tid, $term->tid, 'Correct Taxonomy Term was found by NAME');
$foundterm = taxonomy_get_term($tid);
$this->assertEqual($foundterm->name, $calais_term->value, 'Taxonomy Term value matches');
$this->assertNull($foundterm->guid, 'Taxonomy Term guid was not set');
// Find for term that exists by name with diff GUID. GUID should not get overwritten
$term = $this->createTerm($vid, array('guid' => 'XxXxXxX'));
$calais_term = new CalaisTerm(NULL, $term->name, 0.5);
$tid = calais_get_taxonomy_term($vid, $calais_term);
$this->assertEqual($tid, $term->tid, 'Correct Taxonomy Term was found by NAME');
$foundterm = taxonomy_get_term($tid);
$this->assertEqual($foundterm->name, $calais_term->value, 'Taxonomy Term value matches');
$this->assertEqual($foundterm->guid, 'XxXxXxX', 'Taxonomy Term guid was not overwritten');
}
}
*/