t('Calais API Test'), 'description' => t('Verify that a Calais request returns expected results.'), 'group' => t('Calais') ); } function testAnalyze() { $body = filter_xss($this->get_test_content()); $date = format_date(time(), 'custom', 'r'); $calais = new Calais(array('host' => 'api1.opencalais.com')); $results = $calais->analyzeXML('Calais API Test', $body, $date); $this->assertCalaisResults($results); $this->arrayContains($results->CalaisDocumentCategory, 'Technology', 'Technology is in Document Category'); $this->arrayContains($results->TVShow, '60 Minutes', '60 Minutes is a TVShow'); $this->assertRelevance($results->TVShow, '60 Minutes', 0.052); } // http://voices.washingtonpost.com/44/2008/03/31/three_turning_points_on_the_ro.html function get_test_content() { $str = << t('SemanticProxy Test'), 'description' => t('Verify that a SemanticProxy request returns expected results.'), 'group' => t('Calais') ); } function testAnalyze() { $sp = new SemanticProxy(); $results = $sp->analyze('http://voices.washingtonpost.com/44/2008/03/31/three_turning_points_on_the_ro.html'); drupal_set_message($sp->rdf); $this->assertCalaisResults($results); $this->arrayContains($results->CalaisDocumentCategory, 'Politics', 'Politics is in Document Category'); } } class CalaisApiTestCase extends DrupalWebTestCase { function setUp() { // Get the configured API key before generating a new $db_prefix $api_key = variable_get('calais_api_key', ''); parent::setUp('calais_api'); variable_set('calais_api_key', $api_key); } function tearDown() { parent::tearDown(); } function assertCalaisResults($results) { $this->assertFalse(empty($results), "Results were populated"); $this->arrayContains($results->City, 'Denver', 'Denver is a City'); $this->arrayContains($results->City, 'Philadelphia', 'Philadelphia is a City'); $this->arrayContains($results->City, 'Chicago', 'Chicago is a City'); $this->arrayContains($results->Company, 'CBS', 'CBS is a Company'); $this->arrayContains($results->Person, 'Barack Obama', 'Barack Obama is a Person'); $this->arrayContains($results->Person, 'Hillary Clinton', 'Hillary Clinton is a Person'); $this->arrayContains($results->Person, 'Howard Dean', 'Howard Dean is a Person'); $this->arrayContains($results->Person, 'Al Gore', 'Al Gore is a Person'); $this->arrayContains($results->PublishedMedium, 'The Wall Street Journal', 'The Wall Street Journal is a PublishedMedium'); $this->arrayContains($results->ProvinceOrState, 'Indiana', 'Indiana is a ProvinceOrState'); $this->arrayContains($results->ProvinceOrState, 'Ohio', 'Ohio is a ProvinceOrState'); $this->arrayContains($results->ProvinceOrState, 'Florida', 'Florida is a ProvinceOrState'); $this->arrayContains($results->ProvinceOrState, 'North Carolina', 'North Carolina is a ProvinceOrState'); $this->arrayContains($results->ProvinceOrState, 'Iowa', 'Iowa is a ProvinceOrState'); $this->arrayContains($results->ProvinceOrState, 'Pennsylvania', 'Pennsylvania is a ProvinceOrState'); $this->arrayContains($results->ProvinceOrState, 'Michigan', 'Michigan is a ProvinceOrState'); $this->arrayContains($results->ProvinceOrState, 'Vermont', 'Vermont is a ProvinceOrState'); $this->arrayContains($results->EventsFacts, 'Person Career', 'Person Career is an Event/Fact'); $this->arrayContains($results->EventsFacts, 'Quotation', 'Quotation is an Event/Fact'); $this->assertFalse(property_exists($results, 'Geo'), 'GEO data handled correctly'); $this->assertRelevance($results->Person, 'Barack Obama', 0.797); $this->assertRelevance($results->ProvinceOrState, 'Indiana', 0.566); $this->assertResolvedName($results->Company, 'CBS', 'Viacom International Inc', 'company', 'CBS disambiguates to Viacom International Inc'); $this->assertResolvedData($results->Company, 'CBS', array('ticker' => 'CBS')); $this->assertResolvedData($results->ProvinceOrState, 'Indiana', array('latitude' => '40.0066019668', 'longitude' => '-86.2913991487')); $this->assertResolvedData($results->ProvinceOrState, 'North Carolina', array('latitude' => '35.4833648675', 'longitude' => '-79.4002284439')); } function arrayContains($haystack, $needle, $message) { if(!isset($haystack)) { $this->fail("No results returned when looking for $needle"); } $this->assertTrue($haystack->has_term_value($needle), t($message)); } function assertResolvedName($entitySet, $entity, $resolved, $type, $message) { $term = $entitySet->get_term_by_value($entity); $this->assertTrue($term, "'$entity' was found for disambiguation"); $this->assertEqual($term->resolved_name, $resolved, $message); $this->assertEqual($term->resolved_type, $type, "Resolved Type '$term->resolved_type' equals '$type'"); } function assertResolvedData($entitySet, $entity, $data) { $term = $entitySet->get_term_by_value($entity); $extra = $term->extra; foreach ($data as $key => $val) { $this->assertEqual($extra[$key], $val, "Resolved '$key' for $entity is {$extra[$key]}"); } } function assertRelevance($entitySet, $value, $score) { $term = $entitySet->get_term_by_value($value); $this->assertEqual($term->relevance, $score, "Relevance score for $value is {$term->relevance}"); } }