drupalCreateUser(array('administer taxonomy')); $this->drupalLogin($admin_user); } function tearDown() { parent::tearDown(); } function createVocabulary() { $vocab = array(); $vocab['name'] = $this->randomName(20); taxonomy_save_vocabulary($vocab); return $vocab; } function createTerm($vid) { $term = array(); $term['vid'] = $vid; $term['name'] = $this->randomName(30); $term['description'] = $this->randomName(200); taxonomy_save_term($term); return $term; } } class CalaisPopulateTermGuidTestCase extends CalaisTestCase { function getInfo() { return array( 'name' => t('Populate Existing Term GUID'), 'description' => t('Test populating pre-existing taxonomy terms with a Calais GUID.'), 'group' => t('Calais') ); } // Test filling in an existing terms guid function testPopulateTermGuid() { $vocab = $this->createVocabulary(); $term = $this->createTerm($vocab['vid']); $guid = db_result(db_query('SELECT guid FROM {term_data} WHERE tid = %d', $term['tid'])); $this->assertNull($guid, 'Initial term guid should be NULL'); $calais_term = new CalaisTerm($this->randomName(100), $term['name']); calais_populate_term_guid($term['vid'], $calais_term); $guid = db_result(db_query('SELECT guid FROM {term_data} WHERE tid = %d', $term['tid'])); $this->assertEqual($guid, $calais_term->guid, 'Term guid matches CalaisTerm'); } } class CalaisAssociateTermTestCase extends CalaisTestCase { function getInfo() { return array( 'name' => t('Associate Calais Term with Node'), 'description' => t('Test associating new Calais Terms with an existing node.'), 'group' => t('Calais') ); } // Associating new calais terms with an existing node function testAssociateTerms() { $vocab = $this->createVocabulary(); $vid = $vocab['vid']; $node = $this->drupalCreateNode(); $calais_term = new CalaisTerm($this->randomName(100), $this->randomName(30), 0.5); $terms = calais_get_keywords($node->nid, $node->type, $vid); $this->assertTrue(empty($terms[$vid]), 'No Calais Terms Exist'); calais_associate_term($vid, $calais_term, $node); $terms = calais_get_keywords($node->nid, $node->type, $vid); $this->assertTrue(count($terms[$vid]) == 1, 'One Calais Term Exists'); $this->assertEqual($terms[$vid][0], $calais_term->value, 'Calais Term name is correct'); $relevance = db_result(db_query('SELECT relevance FROM {calais_term_node} WHERE nid = %d', $node->nid)); $this->assertEqual($relevance, 0.5, 'Calais Term relevance is correct'); } } class CalaisAssignTermsToNodeTestCase extends CalaisTestCase { function getInfo() { return array( 'name' => t('Assign Taxonomy Term to Node'), 'description' => t('Test associating Taxonomy Terms with an existing node.'), 'group' => t('Calais') ); } // Associating new calais terms with an existing node function testAssignToNode() { $vocab = $this->createVocabulary(); $vid = $vocab['vid']; $node = $this->drupalCreateNode(); $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'); calais_assign_to_node($vid, $calais_term, $node); $terms = taxonomy_get_term_by_name($calais_term->value); $this->assertTrue(count($terms) == 1, 'Taxonomy Term was created'); $node_terms = taxonomy_node_get_terms_by_vocabulary($node, $vid); $this->assertTrue(count($node_terms) == 1, 'One Node Taxonomy Term Exists'); $this->assertEqual($terms[0]->name, $calais_term->value, 'Taxonomy Term name is correct'); $this->assertEqual($terms[0]->guid, $calais_term->guid, 'Taxonomy Term guid is correct'); } } 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[] = new CalaisTerm($this->randomName(100), 'Low Relevance', 0.001); $calais_terms[] = new CalaisTerm($this->randomName(100), 'Mid Relevance', 0.5); $calais_terms[] = 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."); 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."); } }