drupal/modules/taxonomy/taxonomy.test

429 lines
16 KiB
Plaintext

<?php
// $Id$
/**
* @file
* Tests for Taxonomy module.
*/
/**
* Class with common helper methods.
*/
class TaxonomyWebTestCase extends DrupalWebTestCase {
/**
* Returns a new vocabulary with random properties.
*/
function createVocabulary() {
// Create a vocabulary.
$vocabulary = new stdClass();
$vocabulary->name = $this->randomName();
$vocabulary->description = $this->randomName();
$vocabulary->help = '';
$vocabulary->nodes = array('article' => 'article');
$vocabulary->weight = mt_rand(0, 10);
taxonomy_vocabulary_save($vocabulary);
return $vocabulary;
}
/**
* Returns a new term with random properties in vocabulary $vid.
*/
function createTerm($vid) {
$term = new stdClass();
$term->name = $this->randomName();
$term->vid = $vid;
taxonomy_term_save($term);
return $term;
}
}
/**
* Tests for the taxonomy vocabulary interface.
*/
class TaxonomyVocabularyFunctionalTest extends TaxonomyWebTestCase {
function getInfo() {
return array(
'name' => t('Taxonomy vocabulary interface'),
'description' => t('Test the taxonomy vocabulary interface.'),
'group' => t('Taxonomy'),
);
}
function setUp() {
parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
$this->drupalLogin($this->admin_user);
$this->vocabulary = $this->createVocabulary();
}
/**
* Create, edit and delete a vocabulary via the user interface.
*/
function testVocabularyInterface() {
// Visit the main taxonomy administration page.
$this->drupalGet('admin/content/taxonomy');
// Create a new vocabulary.
$this->clickLink(t('Add vocabulary'));
$edit = array();
$edit['name'] = $this->randomName();
$edit['description'] = $this->randomName();
$edit['help'] = $this->randomName();
$edit['nodes[article]'] = 'article';
$edit['tags'] = 1;
$edit['multiple'] = 1;
$edit['required'] = 1;
$edit['weight'] = 0;
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), t('Vocabulary created successfully'));
// Edit the vocabulary.
$this->drupalGet('admin/content/taxonomy');
$this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
$this->clickLink(t('edit vocabulary'));
$edit = array();
$edit['name'] = $this->randomName();
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(t('Updated vocabulary %name.', array('%name' => $edit['name'])));
$this->drupalGet('admin/content/taxonomy');
$this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
}
/**
* Changing weights on the vocabulary overview with two or more vocabularies.
*/
function testTaxonomyAdminChangingWeights() {
// Create some vocabularies.
for ($i = 0; $i < 10; $i++) {
$this->createVocabulary();
}
// Get all vocabularies and change their weights.
$vocabularies = taxonomy_get_vocabularies();
$edit = array();
foreach ($vocabularies as $key => $vocabulary) {
$vocabulary->weight = -$vocabulary->weight;
$vocabularies[$key]->weight = $vocabulary->weight;
$edit[$key .'[weight]'] = $vocabulary->weight;
}
// Saving the new weights via the interface.
$this->drupalPost('admin/content/taxonomy/', $edit, t('Save'));
// Load the vocabularies from the database.
$new_vocabularies = taxonomy_get_vocabularies();
// Check that the weights are saved in the database correctly.
foreach ($vocabularies as $key => $vocabulary) {
$this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, t('The vocabulary weight was changed.'));
}
}
/**
* Test the vocabulary overview with no vocabularies.
*/
function testTaxonomyAdminNoVocabularies() {
// Delete all vocabularies.
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $key => $vocabulary) {
$edit = array();
$this->drupalPost('admin/content/taxonomy/' . $vocabulary->vid, $edit, t('Delete'));
// Submit the confirm form for deletion.
$this->drupalPost(NULL, NULL, t('Delete'));
}
// Confirm that no vocabularies are found in the database.
$this->assertFalse(taxonomy_get_vocabularies(), t('No vocabularies found in the database'));
// Check the default message for no vocabularies.
$this->assertText(t('No vocabularies available.'), t('No vocabularies were found.'));
}
/**
* Deleting a vocabulary.
*/
function testTaxonomyAdminDeletingVocabulary() {
// Create a vocabulary.
$edit = array(
'name' => $this->randomName(),
'nodes[article]' => 'article',
);
$this->drupalPost('admin/content/taxonomy/add', $edit, t('Save'));
$this->assertText(t('Created new vocabulary'), t('New vocabulary was created.'));
// Check the created vocabulary.
$vocabularies = taxonomy_get_vocabularies();
$vid = $vocabularies[count($vocabularies)-1]->vid;
$vocabulary = taxonomy_vocabulary_load($vid, TRUE);
$this->assertTrue($vocabulary, t('Vocabulary found in database'));
// Delete the vocabulary.
$edit = array();
$this->drupalPost('admin/content/taxonomy/' .$vid, $edit, t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), t('[confirm deletion] Asks for confirmation.'));
$this->assertText(t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'), t('[confirm deletion] Inform that all terms will be deleted.'));
// Confirm deletion.
$this->drupalPost(NULL, NULL, t('Delete'));
$this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), t('Vocabulary deleted'));
$this->assertFalse(taxonomy_vocabulary_load($vid, TRUE), t('Vocabulary is not found in the database'));
}
}
/**
* Tests for taxonomy vocabulary functions.
*/
class TaxonomyVocabularyUnitTest extends TaxonomyWebTestCase {
function getInfo() {
return array(
'name' => t('Taxonomy vocabularies'),
'description' => t('Test loading, saving and deleting vocabularies.'),
'group' => t('Taxonomy'),
);
}
function setUp() {
parent::setUp('taxonomy');
$admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
$this->drupalLogin($admin_user);
$this->vocabulary = $this->createVocabulary();
}
/**
* Ensure that when an invalid vocabulary vid is loaded, it is possible
* to load the same vid successfully if it subsequently becomes valid.
*/
function testTaxonomyVocabularyLoadReturnFalse() {
// Load a vocabulary that doesn't exist.
$vocabularies = taxonomy_get_vocabularies();
$vid = count($vocabularies) + 1;
$vocabulary = taxonomy_vocabulary_load($vid);
// This should not return an object because no such vocabulary exists.
$this->assertTrue(empty($vocabulary), t('No object loaded.'));
// Create a new vocabulary.
$this->createVocabulary();
// Load the vocabulary with the same $vid from earlier.
// This should return a vocabulary object since it now matches a real vid.
$vocabulary = taxonomy_vocabulary_load($vid);
$this->assertTrue(!empty($vocabulary) && is_object($vocabulary), t('Vocabulary is an object'));
$this->assertTrue($vocabulary->vid == $vid, t('Valid vocabulary vid is the same as our previously invalid one.'));
}
/**
* Ensure that the vocabulary static reset works correctly.
*/
function testTaxonomyVocabularyLoadStaticReset() {
$original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
$this->assertTrue(is_object($original_vocabulary), t('Vocabulary loaded successfully'));
$this->assertEqual($this->vocabulary->name, $original_vocabulary->name, t('Vocabulary loaded successfully'));
// Change the name and description.
$vocabulary = $original_vocabulary;
$vocabulary->name = $this->randomName();
$vocabulary->description = $this->randomName();
taxonomy_vocabulary_save($vocabulary);
// Load the vocabulary with $reset TRUE.
$new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid, TRUE);
$this->assertEqual($new_vocabulary->name, $vocabulary->name);
$this->assertEqual($new_vocabulary->name, $vocabulary->name);
// Delete the vocabulary.
taxonomy_vocabulary_delete($this->vocabulary->vid);
$vocabularies = taxonomy_get_vocabularies();
$this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), t('The vocabulary was deleted'));
}
}
/**
* Tests for taxonomy term functions.
*/
class TaxonomyTermTestCase extends TaxonomyWebTestCase {
function getInfo() {
return array(
'name' => t('Taxonomy term functions and forms.'),
'description' => t('Test load, save and delete for taxonomy terms.'),
'group' => t('Taxonomy')
);
}
function setUp() {
parent::setUp('taxonomy');
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
$this->drupalLogin($this->admin_user);
$this->vocabulary = $this->createVocabulary();
}
/**
* Test related terms.
*/
function testTaxonomyTermRelations() {
// Create two taxonomy terms.
$term1 = $this->createTerm($this->vocabulary->vid);
$term2 = $this->createTerm($this->vocabulary->vid);
// Edit $term1 and add $term2 as a relationship.
$edit = array();
$edit['relations[]'] = $term2->tid;
$this->drupalPost('taxonomy/term/' . $term1->tid . '/edit', $edit, t('Save'));
$related = taxonomy_get_related($term1->tid);
$this->assertTrue(isset($related[$term2->tid]), t('Related term was found'));
}
/**
* Test synonyms.
*/
function testTaxonomySynonyms() {
// Create a taxonomy term with one synonym.
$term = $this->createTerm($this->vocabulary->vid);
$term->synonyms = $this->randomName();
taxonomy_term_save($term);
// Fetch the synonyms.
$synonyms = taxonomy_get_synonyms($term->tid);
$count = count($synonyms);
$this->assertEqual($count, 1, t('@count synonyms were found.', array('@count' => $count)));
// Fetch the term using the synonyms.
$returned_term = taxonomy_get_synonym_root($synonyms[0]);
$this->assertEqual($term->tid, $returned_term->tid, t('Term ID returned correctly'));
}
/**
* Test terms in a single and multiple hierarchy.
*/
function testTaxonomyTermHierarchy() {
// Create two taxonomy terms.
$term1 = $this->createTerm($this->vocabulary->vid);
$term2 = $this->createTerm($this->vocabulary->vid);
// Edit $term2, setting $term1 as parent.
$edit = array();
$edit['parent[]'] = $term1->tid;
$this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
// Check the hierarchy.
$children = taxonomy_get_children($term1->tid);
$parents = taxonomy_get_parents($term2->tid);
$this->assertTrue(isset($children[$term2->tid]), t('Child found correctly.'));
$this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.'));
// Create a third term and save this as a parent of term2.
$term3 = $this->createTerm($this->vocabulary->vid);
$term2->parent = array($term1->tid, $term3->tid);
taxonomy_term_save($term2);
$parents = taxonomy_get_parents($term2->tid);
$this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), t('Both parents found successfully.'));
}
/**
* Test that hook_nodeapi_$op implementations work correctly.
*
* Save & edit a node and assert that taxonomy terms are saved/loaded properly.
*/
function testTaxonomyNode() {
// Create two taxonomy terms.
$term1 = $this->createTerm($this->vocabulary->vid);
$term2 = $this->createTerm($this->vocabulary->vid);
// Post an article.
$edit = array();
$edit['title'] = $this->randomName();
$edit['body'] = $this->randomName();
$edit['taxonomy[' . $this->vocabulary->vid . ']'] = $term1->tid;
$this->drupalPost('node/add/article', $edit, t('Save'));
// Check that the term is displayed when the node is viewed.
$node = node_load(array('title' => $edit['title']));
$this->drupalGet('node/' . $node->nid);
$this->assertText($term1->name, t('Term is displayed when viewing the node.'));
// Edit the node with a different term.
$edit['taxonomy[' . $this->vocabulary->vid . ']'] = $term2->tid;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->drupalGet('node/' . $node->nid);
$this->assertText($term2->name, t('Term is displayed when viewing the node.'));
// Delete node through browser.
$this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
$this->drupalGet('node/' . $node->nid);
$this->assertNoText($term2->name, t('Checking if node exists'));
// Checking database fields.
$result = db_query('SELECT * FROM {term_node} WHERE nid = :nid', array(':nid' => $node->nid))->fetch();
$this->assertTrue(empty($result), t('Term/node relationships are no longer in the database table.'));
}
/**
* Test term creation with a free-tagging vocabulary from the node form.
*/
function testNodeTermCreation() {
// Enable tags in the vocabulary.
$this->vocabulary->tags = 1;
taxonomy_vocabulary_save($this->vocabulary);
$terms = array(
$this->randomName(),
$this->randomName(),
$this->randomName(),
);
$edit = array();
$edit['title'] = $this->randomName();
// Insert the terms in a comma separated list. Vocabulary 1 is a
// free-tagging field created by the default profile.
$edit['taxonomy[tags][' . $this->vocabulary->vid .']'] = implode(', ', $terms);
$edit['body'] = $this->randomName();
$this->drupalPost('node/add/article', $edit, t('Save'));
$this->assertRaw(t('@type %title has been created.', array('@type' => t('Article'), '%title' => $edit['title'])), t('The node was created successfully'));
foreach ($terms as $term) {
$this->assertText($term, t('The term was saved and appears on the node page'));
}
}
/**
* Save and edit a term and assert that the name and description are correct.
*/
function testTermEdit() {
$edit = array(
'name' => $this->randomName(12),
'description' => $this->randomName(100),
);
// Explicitly set the parents field to 'root', to ensure that
// taxonomy_form_term_submit() handles the invalid term ID correctly.
$edit['parent[]'] = 0;
// Create the term to edit.
$this->drupalPost('admin/content/taxonomy/' . $this->vocabulary->vid . '/add', $edit, t('Save'));
$term = taxonomy_get_term_by_name($edit['name']);
$this->assertNotNull($term, t('Term found in database'));
// Submitting a term takes us to the add page; we need the List page.
$this->drupalGet('admin/content/taxonomy/' . $this->vocabulary->vid . '/list');
// Test edit link as accessed from Taxonomy administration pages.
// Because Simpletest creates its own database when running tests, we know
// the first edit link found on the listing page is to our term.
$this->clickLink(t('edit'));
// This failed inexplicably with assertText, so used assertRaw. @TODO: Why?
$this->assertText($edit['name'], t('The randomly generated term name is present.'));
$this->assertText($edit['description'], t('The randomly generated term description is present.'));
$edit = array(
'name' => $this->randomName(14),
'description' => $this->randomName(102),
);
// Edit the term.
$this->drupalPost('taxonomy/term/' . $term[0]->tid . '/edit', $edit, t('Save'));
// View the term and check that it is correct.
$this->drupalGet('taxonomy/term/' . $term[0]->tid);
$this->assertText($edit['name'], t('The randomly generated term name is present.'));
$this->assertText($edit['description'], t('The randomly generated term description is present.'));
}
}