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, $overrides = array()) { $term = array( 'vid' => $vid, 'name' => $this->randomName(30), 'description' => $this->randomName(200), ); $term += $overrides; taxonomy_save_term($term); return (object)$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 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'); } } 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."); $keywords = calais_get_keywords($node->nid, $node->type, $vid, 0.002); $this->assertEqual(count($keywords[$vid]), 2, "Two terms were returned for overriden threshold."); } }