- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
<?php
2008-11-16 14:59:33 +00:00
/**
* @file
2011-02-04 18:42:22 +00:00
* Tests for taxonomy.module.
2008-11-16 14:59:33 +00:00
*/
2008-11-22 12:05:02 +00:00
/**
2010-08-05 23:53:39 +00:00
* Class with common helper methods.
*/
2008-11-22 12:05:02 +00:00
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();
2009-06-12 13:59:56 +00:00
$vocabulary->machine_name = drupal_strtolower($this->randomName());
2008-11-22 12:05:02 +00:00
$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.
*/
2009-06-12 13:59:56 +00:00
function createTerm($vocabulary) {
2008-11-22 12:05:02 +00:00
$term = new stdClass();
$term->name = $this->randomName();
2010-01-29 22:56:54 +00:00
$term->description = $this->randomName();
2010-09-28 03:30:37 +00:00
// Use the first available text format.
$term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
2009-06-12 13:59:56 +00:00
$term->vid = $vocabulary->vid;
2008-11-22 12:05:02 +00:00
taxonomy_term_save($term);
return $term;
}
}
/**
2010-08-05 23:53:39 +00:00
* Tests for the taxonomy vocabulary interface.
*/
2008-11-22 12:05:02 +00:00
class TaxonomyVocabularyFunctionalTest extends TaxonomyWebTestCase {
2008-11-16 14:59:33 +00:00
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2008-11-16 14:59:33 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Taxonomy vocabulary interface',
'description' => 'Test the taxonomy vocabulary interface.',
'group' => 'Taxonomy',
2008-11-16 14:59:33 +00:00
);
}
function setUp() {
parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
2008-11-22 12:05:02 +00:00
$this->drupalLogin($this->admin_user);
$this->vocabulary = $this->createVocabulary();
2008-11-16 14:59:33 +00:00
}
2008-09-24 02:59:42 +00:00
/**
2008-11-16 14:59:33 +00:00
* Create, edit and delete a vocabulary via the user interface.
2008-09-24 02:59:42 +00:00
*/
2008-11-16 14:59:33 +00:00
function testVocabularyInterface() {
// Visit the main taxonomy administration page.
2009-07-20 18:51:36 +00:00
$this->drupalGet('admin/structure/taxonomy');
2008-11-16 14:59:33 +00:00
// Create a new vocabulary.
$this->clickLink(t('Add vocabulary'));
$edit = array();
2009-06-20 18:32:04 +00:00
$machine_name = drupal_strtolower($this->randomName());
2008-11-16 14:59:33 +00:00
$edit['name'] = $this->randomName();
$edit['description'] = $this->randomName();
2009-06-20 18:32:04 +00:00
$edit['machine_name'] = $machine_name;
2008-11-16 14:59:33 +00:00
$this->drupalPost(NULL, $edit, t('Save'));
2010-08-05 23:53:39 +00:00
$this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), t('Vocabulary created successfully'));
2008-11-16 14:59:33 +00:00
// Edit the vocabulary.
2009-07-20 18:51:36 +00:00
$this->drupalGet('admin/structure/taxonomy');
2010-08-05 23:53:39 +00:00
$this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
2008-11-16 14:59:33 +00:00
$this->clickLink(t('edit vocabulary'));
$edit = array();
$edit['name'] = $this->randomName();
$this->drupalPost(NULL, $edit, t('Save'));
2009-07-20 18:51:36 +00:00
$this->drupalGet('admin/structure/taxonomy');
2010-08-05 23:53:39 +00:00
$this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
2009-06-12 13:59:56 +00:00
// Try to submit a vocabulary with a duplicate machine name.
2009-06-20 18:32:04 +00:00
$edit['machine_name'] = $machine_name;
2009-07-20 18:51:36 +00:00
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
2010-10-13 13:43:21 +00:00
$this->assertText(t('The machine-readable name is already in use. It must be unique.'));
2009-06-12 13:59:56 +00:00
// Try to submit an invalid machine name.
$edit['machine_name'] = '!&^%';
2009-07-20 18:51:36 +00:00
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
2009-06-12 13:59:56 +00:00
$this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
2008-11-16 14:59:33 +00:00
}
2008-11-22 12:05:02 +00:00
/**
* 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.
2012-01-18 03:37:45 +00:00
$vocabularies = taxonomy_vocabulary_load_multiple(FALSE);
2008-11-22 12:05:02 +00:00
$edit = array();
foreach ($vocabularies as $key => $vocabulary) {
$vocabulary->weight = -$vocabulary->weight;
$vocabularies[$key]->weight = $vocabulary->weight;
2009-05-24 17:39:35 +00:00
$edit[$key . '[weight]'] = $vocabulary->weight;
2008-11-22 12:05:02 +00:00
}
// Saving the new weights via the interface.
2009-11-08 11:19:02 +00:00
$this->drupalPost('admin/structure/taxonomy', $edit, t('Save'));
2008-11-22 12:05:02 +00:00
// Load the vocabularies from the database.
2012-01-18 03:37:45 +00:00
$new_vocabularies = taxonomy_vocabulary_load_multiple(FALSE);
2008-11-22 12:05:02 +00:00
// Check that the weights are saved in the database correctly.
foreach ($vocabularies as $key => $vocabulary) {
2010-08-05 23:53:39 +00:00
$this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, t('The vocabulary weight was changed.'));
2008-11-22 12:05:02 +00:00
}
}
/**
* Test the vocabulary overview with no vocabularies.
*/
function testTaxonomyAdminNoVocabularies() {
// Delete all vocabularies.
2012-01-18 03:37:45 +00:00
$vocabularies = taxonomy_vocabulary_load_multiple(FALSE);
2008-11-22 12:05:02 +00:00
foreach ($vocabularies as $key => $vocabulary) {
2009-06-12 13:59:56 +00:00
taxonomy_vocabulary_delete($key);
2008-11-22 12:05:02 +00:00
}
// Confirm that no vocabularies are found in the database.
2012-01-18 03:37:45 +00:00
$this->assertFalse(taxonomy_vocabulary_load_multiple(FALSE), t('No vocabularies found in the database'));
2009-07-20 18:51:36 +00:00
$this->drupalGet('admin/structure/taxonomy');
2008-11-22 12:05:02 +00:00
// Check the default message for no vocabularies.
2010-08-05 23:53:39 +00:00
$this->assertText(t('No vocabularies available.'), t('No vocabularies were found.'));
2008-11-22 12:05:02 +00:00
}
/**
* Deleting a vocabulary.
*/
function testTaxonomyAdminDeletingVocabulary() {
// Create a vocabulary.
$edit = array(
'name' => $this->randomName(),
2009-06-12 13:59:56 +00:00
'machine_name' => drupal_strtolower($this->randomName()),
2008-11-22 12:05:02 +00:00
);
2009-07-20 18:51:36 +00:00
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
2010-08-05 23:53:39 +00:00
$this->assertText(t('Created new vocabulary'), t('New vocabulary was created.'));
2008-11-22 12:05:02 +00:00
// Check the created vocabulary.
2012-01-18 03:37:45 +00:00
$vocabularies = taxonomy_vocabulary_load_multiple(FALSE);
2010-08-05 23:53:39 +00:00
$vid = $vocabularies[count($vocabularies)-1]->vid;
2009-08-25 21:53:48 +00:00
entity_get_controller('taxonomy_vocabulary')->resetCache();
2009-04-02 20:39:45 +00:00
$vocabulary = taxonomy_vocabulary_load($vid);
2010-08-05 23:53:39 +00:00
$this->assertTrue($vocabulary, t('Vocabulary found in database'));
2008-11-22 12:05:02 +00:00
// Delete the vocabulary.
$edit = array();
2010-03-25 11:53:25 +00:00
$this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/edit', $edit, t('Delete'));
2010-08-05 23:53:39 +00:00
$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.'));
2008-11-22 12:05:02 +00:00
// Confirm deletion.
$this->drupalPost(NULL, NULL, t('Delete'));
2010-08-05 23:53:39 +00:00
$this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), t('Vocabulary deleted'));
2009-08-25 21:53:48 +00:00
entity_get_controller('taxonomy_vocabulary')->resetCache();
2010-08-05 23:53:39 +00:00
$this->assertFalse(taxonomy_vocabulary_load($vid), t('Vocabulary is not found in the database'));
2008-11-22 12:05:02 +00:00
}
2008-11-16 14:59:33 +00:00
}
/**
* Tests for taxonomy vocabulary functions.
*/
2008-11-22 12:05:02 +00:00
class TaxonomyVocabularyUnitTest extends TaxonomyWebTestCase {
2008-11-16 14:59:33 +00:00
2009-05-24 17:39:35 +00:00
public static function getInfo() {
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Taxonomy vocabularies',
'description' => 'Test loading, saving and deleting vocabularies.',
'group' => 'Taxonomy',
2009-05-24 17:39:35 +00:00
);
}
2008-09-24 02:59:42 +00:00
function setUp() {
2010-10-13 05:19:26 +00:00
parent::setUp('taxonomy', 'field_test');
2008-11-16 14:59:33 +00:00
$admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
2008-09-24 02:59:42 +00:00
$this->drupalLogin($admin_user);
2008-11-22 12:05:02 +00:00
$this->vocabulary = $this->createVocabulary();
2008-09-24 02:59:42 +00:00
}
/**
* 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.
2012-01-18 03:37:45 +00:00
$vocabularies = taxonomy_vocabulary_load_multiple(FALSE);
2008-09-24 02:59:42 +00:00
$vid = count($vocabularies) + 1;
$vocabulary = taxonomy_vocabulary_load($vid);
// This should not return an object because no such vocabulary exists.
2010-08-05 23:53:39 +00:00
$this->assertTrue(empty($vocabulary), t('No object loaded.'));
2008-09-24 02:59:42 +00:00
// Create a new vocabulary.
2008-11-22 12:05:02 +00:00
$this->createVocabulary();
2008-09-24 02:59:42 +00:00
// 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);
2010-08-05 23:53:39 +00:00
$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.'));
2008-09-24 02:59:42 +00:00
}
2008-09-24 18:42:00 +00:00
2010-05-28 10:17:02 +00:00
/**
* Test deleting a taxonomy that contains terms.
*/
function testTaxonomyVocabularyDeleteWithTerms() {
// Delete any existing vocabularies.
2012-01-18 03:37:45 +00:00
foreach (taxonomy_vocabulary_load_multiple(FALSE) as $vocabulary) {
2010-05-28 10:17:02 +00:00
taxonomy_vocabulary_delete($vocabulary->vid);
}
// Assert that there are no terms left.
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
// Create a new vocabulary and add a few terms to it.
$vocabulary = $this->createVocabulary();
$terms = array();
for ($i = 0; $i < 5; $i++) {
$terms[$i] = $this->createTerm($vocabulary);
}
// Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2.
$terms[2]->parent = array($terms[1]->tid);
taxonomy_term_save($terms[2]);
$terms[4]->parent = array($terms[1]->tid, $terms[2]->tid);
taxonomy_term_save($terms[4]);
// Assert that there are now 5 terms.
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
taxonomy_vocabulary_delete($vocabulary->vid);
// Assert that there are no terms left.
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
}
2008-09-24 18:42:00 +00:00
/**
* Ensure that the vocabulary static reset works correctly.
*/
function testTaxonomyVocabularyLoadStaticReset() {
2009-04-02 20:39:45 +00:00
$original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
2010-08-05 23:53:39 +00:00
$this->assertTrue(is_object($original_vocabulary), t('Vocabulary loaded successfully'));
$this->assertEqual($this->vocabulary->name, $original_vocabulary->name, t('Vocabulary loaded successfully'));
2008-09-24 18:42:00 +00:00
// Change the name and description.
2008-11-05 12:47:23 +00:00
$vocabulary = $original_vocabulary;
$vocabulary->name = $this->randomName();
$vocabulary->description = $this->randomName();
taxonomy_vocabulary_save($vocabulary);
2008-09-24 18:42:00 +00:00
2009-04-02 20:39:45 +00:00
// Load the vocabulary.
$new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid);
2008-11-05 12:47:23 +00:00
$this->assertEqual($new_vocabulary->name, $vocabulary->name);
$this->assertEqual($new_vocabulary->name, $vocabulary->name);
2008-11-16 14:59:33 +00:00
// Delete the vocabulary.
taxonomy_vocabulary_delete($this->vocabulary->vid);
2012-01-18 03:37:45 +00:00
$vocabularies = taxonomy_vocabulary_load_multiple(FALSE);
2010-08-05 23:53:39 +00:00
$this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), t('The vocabulary was deleted'));
2008-09-24 18:42:00 +00:00
}
2009-03-30 05:18:49 +00:00
/**
* Tests for loading multiple vocabularies.
*/
function testTaxonomyVocabularyLoadMultiple() {
// Delete any existing vocabularies.
2012-01-18 03:37:45 +00:00
foreach (taxonomy_vocabulary_load_multiple(FALSE) as $vocabulary) {
2009-03-30 05:18:49 +00:00
taxonomy_vocabulary_delete($vocabulary->vid);
}
// Create some vocabularies and assign weights.
$vocabulary1 = $this->createVocabulary();
$vocabulary1->weight = 0;
taxonomy_vocabulary_save($vocabulary1);
$vocabulary2 = $this->createVocabulary();
$vocabulary2->weight = 1;
taxonomy_vocabulary_save($vocabulary2);
$vocabulary3 = $this->createVocabulary();
$vocabulary3->weight = 2;
taxonomy_vocabulary_save($vocabulary3);
2009-06-12 13:59:56 +00:00
// Fetch the names for all vocabularies, confirm that they are keyed by
// machine name.
$names = taxonomy_vocabulary_get_names();
2010-08-05 23:53:39 +00:00
$this->assertEqual($names[$vocabulary1->machine_name]->name, $vocabulary1->name, t('Vocabulary 1 name found.'));
2009-06-12 13:59:56 +00:00
2012-01-18 03:37:45 +00:00
// Fetch all of the vocabularies using taxonomy_vocabulary_load_multiple(FALSE).
2009-03-30 05:18:49 +00:00
// Confirm that the vocabularies are ordered by weight.
2012-01-18 03:37:45 +00:00
$vocabularies = taxonomy_vocabulary_load_multiple(FALSE);
2010-08-05 23:53:39 +00:00
$this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, t('Vocabulary was found in the vocabularies array.'));
$this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, t('Vocabulary was found in the vocabularies array.'));
$this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, t('Vocabulary was found in the vocabularies array.'));
2009-03-30 05:18:49 +00:00
// Fetch the vocabularies with taxonomy_vocabulary_load_multiple(), specifying IDs.
// Ensure they are returned in the same order as the original array.
$vocabularies = taxonomy_vocabulary_load_multiple(array($vocabulary3->vid, $vocabulary2->vid, $vocabulary1->vid));
2010-08-05 23:53:39 +00:00
$this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, t('Vocabulary loaded successfully by ID.'));
$this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, t('Vocabulary loaded successfully by ID.'));
$this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, t('Vocabulary loaded successfully by ID.'));
2009-03-30 05:18:49 +00:00
// Fetch vocabulary 1 by name.
2009-10-08 07:58:47 +00:00
$vocabulary = current(taxonomy_vocabulary_load_multiple(array(), array('name' => $vocabulary1->name)));
2010-08-05 23:53:39 +00:00
$this->assertTrue($vocabulary->vid == $vocabulary1->vid, t('Vocabulary loaded successfully by name.'));
2009-03-30 05:18:49 +00:00
// Fetch vocabulary 1 by name and ID.
2010-08-05 23:53:39 +00:00
$this->assertTrue(current(taxonomy_vocabulary_load_multiple(array($vocabulary1->vid), array('name' => $vocabulary1->name)))->vid == $vocabulary1->vid, t('Vocabulary loaded successfully by name and ID.'));
2009-03-30 05:18:49 +00:00
}
2010-10-13 05:19:26 +00:00
/**
* Tests that machine name changes are properly reflected.
*/
function testTaxonomyVocabularyChangeMachineName() {
// Add a field instance to the vocabulary.
$field = array(
'field_name' => 'field_test',
'type' => 'test_field',
);
field_create_field($field);
$instance = array(
'field_name' => 'field_test',
'entity_type' => 'taxonomy_term',
'bundle' => $this->vocabulary->machine_name,
);
field_create_instance($instance);
// Change the machine name.
2012-01-03 05:51:46 +00:00
$old_name = $this->vocabulary->machine_name;
2010-10-13 05:19:26 +00:00
$new_name = drupal_strtolower($this->randomName());
$this->vocabulary->machine_name = $new_name;
taxonomy_vocabulary_save($this->vocabulary);
2012-01-03 05:51:46 +00:00
// Check that entity bundles are properly updated.
$info = entity_get_info('taxonomy_term');
$this->assertFalse(isset($info['bundles'][$old_name]), t('The old bundle name does not appear in entity_get_info().'));
$this->assertTrue(isset($info['bundles'][$new_name]), t('The new bundle name appears in entity_get_info().'));
2010-10-13 05:19:26 +00:00
// Check that the field instance is still attached to the vocabulary.
$this->assertTrue(field_info_instance('taxonomy_term', 'field_test', $new_name), t('The bundle name was updated correctly.'));
}
2011-06-14 08:26:10 +00:00
/**
* Test uninstall and reinstall of the taxonomy module.
*/
function testUninstallReinstall() {
// Fields and field instances attached to taxonomy term bundles should be
// removed when the module is uninstalled.
$this->field_name = drupal_strtolower($this->randomName() . '_field_name');
$this->field = array('field_name' => $this->field_name, 'type' => 'text', 'cardinality' => 4);
$this->field = field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'taxonomy_term',
'bundle' => $this->vocabulary->machine_name,
'label' => $this->randomName() . '_label',
);
field_create_instance($this->instance);
module_disable(array('taxonomy'));
2011-08-30 06:12:26 +00:00
drupal_flush_all_caches();
2011-10-31 04:05:57 +00:00
require_once DRUPAL_ROOT . '/core/includes/install.inc';
2011-06-14 08:26:10 +00:00
drupal_uninstall_modules(array('taxonomy'));
module_enable(array('taxonomy'));
// Now create a vocabulary with the same name. All field instances
// connected to this vocabulary name should have been removed when the
// module was uninstalled. Creating a new field with the same name and
// an instance of this field on the same bundle name should be successful.
unset($this->vocabulary->vid);
taxonomy_vocabulary_save($this->vocabulary);
unset($this->field['id']);
field_create_field($this->field);
field_create_instance($this->instance);
}
2008-09-24 02:59:42 +00:00
}
2009-01-28 01:14:39 +00:00
/**
* Unit tests for taxonomy term functions.
*/
class TaxonomyTermUnitTest extends TaxonomyWebTestCase {
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2009-01-28 01:14:39 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Taxonomy term unit tests',
'description' => 'Unit tests for taxonomy term functions.',
'group' => 'Taxonomy',
2009-01-28 01:14:39 +00:00
);
}
2010-08-22 15:45:03 +00:00
function testTermDelete() {
$vocabulary = $this->createVocabulary();
$valid_term = $this->createTerm($vocabulary);
// Delete a valid term.
taxonomy_term_delete($valid_term->tid);
$terms = taxonomy_term_load_multiple(array(), array('vid' => $vocabulary->vid));
$this->assertTrue(empty($terms), 'Vocabulary is empty after deletion');
// Delete an invalid term. Should not throw any notices.
taxonomy_term_delete(42);
}
2009-01-28 01:14:39 +00:00
}
2010-12-07 05:20:08 +00:00
/**
* Test for legacy node bug.
*/
class TaxonomyLegacyTestCase extends TaxonomyWebTestCase {
public static function getInfo() {
return array(
'name' => 'Test for legacy node bug.',
'description' => 'Posts an article with a taxonomy term and a date prior to 1970.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp('taxonomy');
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access'));
$this->drupalLogin($this->admin_user);
}
/**
* Test taxonomy functionality with nodes prior to 1970.
*/
function testTaxonomyLegacyNode() {
// Posts an article with a taxonomy term and a date prior to 1970.
$langcode = LANGUAGE_NONE;
$edit = array();
$edit['title'] = $this->randomName();
$edit['date'] = '1969-01-01 00:00:00 -0500';
$edit["body[$langcode][0][value]"] = $this->randomName();
$edit["field_tags[$langcode]"] = $this->randomName();
$this->drupalPost('node/add/article', $edit, t('Save'));
// Checks that the node has been saved.
$node = $this->drupalGetNodeByTitle($edit['title']);
$this->assertEqual($node->created, strtotime($edit['date']), t('Legacy node was saved with the right date.'));
}
}
2008-11-16 14:59:33 +00:00
/**
* Tests for taxonomy term functions.
*/
2008-11-22 12:05:02 +00:00
class TaxonomyTermTestCase extends TaxonomyWebTestCase {
2008-11-16 14:59:33 +00:00
2009-03-31 01:49:55 +00:00
public static function getInfo() {
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Taxonomy term functions and forms.',
'description' => 'Test load, save and delete for taxonomy terms.',
2009-09-20 17:40:42 +00:00
'group' => 'Taxonomy',
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
);
}
2008-09-24 02:59:42 +00:00
function setUp() {
parent::setUp('taxonomy');
2008-11-16 14:59:33 +00:00
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
2008-11-22 12:05:02 +00:00
$this->drupalLogin($this->admin_user);
$this->vocabulary = $this->createVocabulary();
2009-10-08 07:58:47 +00:00
2010-02-11 03:29:22 +00:00
$field = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
2010-10-01 01:37:13 +00:00
'vocabulary' => $this->vocabulary->machine_name,
2010-02-11 03:29:22 +00:00
'parent' => 0,
),
),
),
);
field_create_field($field);
2009-10-08 07:58:47 +00:00
$this->instance = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'bundle' => 'article',
2010-03-27 05:52:50 +00:00
'entity_type' => 'node',
2009-10-08 07:58:47 +00:00
'widget' => array(
'type' => 'options_select',
),
'display' => array(
2010-05-23 19:10:23 +00:00
'default' => array(
2010-01-04 18:03:12 +00:00
'type' => 'taxonomy_term_reference_link',
2009-10-08 07:58:47 +00:00
),
),
);
field_create_instance($this->instance);
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
}
/**
2008-11-16 14:59:33 +00:00
* Test terms in a single and multiple hierarchy.
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
*/
2008-11-16 14:59:33 +00:00
function testTaxonomyTermHierarchy() {
// Create two taxonomy terms.
2009-06-12 13:59:56 +00:00
$term1 = $this->createTerm($this->vocabulary);
$term2 = $this->createTerm($this->vocabulary);
2008-11-16 14:59:33 +00:00
// Edit $term2, setting $term1 as parent.
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
$edit = array();
2009-10-14 20:22:04 +00:00
$edit['parent[]'] = array($term1->tid);
2008-11-16 14:59:33 +00:00
$this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
// Check the hierarchy.
2012-01-18 03:37:45 +00:00
$children = taxonomy_term_load_children($term1->tid);
$parents = taxonomy_term_load_parents($term2->tid);
2010-08-05 23:53:39 +00:00
$this->assertTrue(isset($children[$term2->tid]), t('Child found correctly.'));
$this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.'));
2008-11-16 14:59:33 +00:00
2010-03-26 12:15:14 +00:00
// Load and save a term, confirming that parents are still set.
$term = taxonomy_term_load($term2->tid);
taxonomy_term_save($term);
2012-01-18 03:37:45 +00:00
$parents = taxonomy_term_load_parents($term2->tid);
2010-08-05 23:53:39 +00:00
$this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.'));
2010-03-26 12:15:14 +00:00
2008-11-16 14:59:33 +00:00
// Create a third term and save this as a parent of term2.
2009-06-12 13:59:56 +00:00
$term3 = $this->createTerm($this->vocabulary);
2008-11-16 14:59:33 +00:00
$term2->parent = array($term1->tid, $term3->tid);
taxonomy_term_save($term2);
2012-01-18 03:37:45 +00:00
$parents = taxonomy_term_load_parents($term2->tid);
2010-08-05 23:53:39 +00:00
$this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), t('Both parents found successfully.'));
2008-09-11 19:47:40 +00:00
}
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
/**
2009-03-08 04:25:07 +00:00
* Test that hook_node_$op implementations work correctly.
2008-11-22 12:05:02 +00:00
*
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
* Save & edit a node and assert that taxonomy terms are saved/loaded properly.
*/
function testTaxonomyNode() {
2008-11-22 12:05:02 +00:00
// Create two taxonomy terms.
2009-06-12 13:59:56 +00:00
$term1 = $this->createTerm($this->vocabulary);
$term2 = $this->createTerm($this->vocabulary);
2008-11-16 14:59:33 +00:00
// Post an article.
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
$edit = array();
2009-12-02 19:26:23 +00:00
$langcode = LANGUAGE_NONE;
2010-01-09 21:54:01 +00:00
$edit["title"] = $this->randomName();
2009-08-22 00:58:55 +00:00
$edit["body[$langcode][0][value]"] = $this->randomName();
2010-08-05 23:53:39 +00:00
$edit[$this->instance['field_name'] . '[' . $langcode .'][]'] = $term1->tid;
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
$this->drupalPost('node/add/article', $edit, t('Save'));
2008-11-16 14:59:33 +00:00
// Check that the term is displayed when the node is viewed.
2010-01-09 21:54:01 +00:00
$node = $this->drupalGetNodeByTitle($edit["title"]);
2008-11-16 14:59:33 +00:00
$this->drupalGet('node/' . $node->nid);
2010-08-05 23:53:39 +00:00
$this->assertText($term1->name, t('Term is displayed when viewing the node.'));
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
2008-11-16 14:59:33 +00:00
// Edit the node with a different term.
2009-11-20 05:14:13 +00:00
$edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term2->tid;
2008-05-30 07:30:53 +00:00
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
2008-04-20 18:24:07 +00:00
2008-11-16 14:59:33 +00:00
$this->drupalGet('node/' . $node->nid);
2010-08-05 23:53:39 +00:00
$this->assertText($term2->name, t('Term is displayed when viewing the node.'));
2010-07-30 03:06:18 +00:00
2010-10-13 01:25:11 +00:00
// Preview the node.
2010-07-30 03:06:18 +00:00
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview'));
2010-08-05 23:53:39 +00:00
$this->assertNoUniqueText($term2->name, t('Term is displayed when previewing the node.'));
2010-07-30 03:06:18 +00:00
$this->drupalPost(NULL, NULL, t('Preview'));
2010-08-05 23:53:39 +00:00
$this->assertNoUniqueText($term2->name, t('Term is displayed when previewing the node again.'));
2008-09-11 19:47:40 +00:00
}
2008-11-09 00:58:03 +00:00
/**
* Test term creation with a free-tagging vocabulary from the node form.
*/
2010-04-30 12:52:10 +00:00
function testNodeTermCreationAndDeletion() {
2008-11-16 14:59:33 +00:00
// Enable tags in the vocabulary.
2009-10-08 07:58:47 +00:00
$instance = $this->instance;
$instance['widget'] = array('type' => 'taxonomy_autocomplete');
$instance['bundle'] = 'page';
field_create_instance($instance);
2008-11-09 00:58:03 +00:00
$terms = array(
2011-12-21 03:21:51 +00:00
'term1' => $this->randomName(),
'term2' => $this->randomName() . ', ' . $this->randomName(),
'term3' => $this->randomName(),
2008-11-09 00:58:03 +00:00
);
2009-10-08 07:58:47 +00:00
2008-11-09 00:58:03 +00:00
$edit = array();
2009-12-02 19:26:23 +00:00
$langcode = LANGUAGE_NONE;
2010-01-09 21:54:01 +00:00
$edit["title"] = $this->randomName();
2009-08-22 00:58:55 +00:00
$edit["body[$langcode][0][value]"] = $this->randomName();
2009-10-08 07:58:47 +00:00
// Insert the terms in a comma separated list. Vocabulary 1 is a
// free-tagging field created by the default profile.
2011-07-25 18:47:59 +00:00
$edit[$instance['field_name'] . "[$langcode]"] = drupal_implode_tags($terms);
2010-07-14 20:26:52 +00:00
// Preview and verify the terms appear but are not created.
$this->drupalPost('node/add/page', $edit, t('Preview'));
foreach ($terms as $term) {
2010-08-05 23:53:39 +00:00
$this->assertText($term, t('The term appears on the node preview'));
2010-07-14 20:26:52 +00:00
}
$tree = taxonomy_get_tree($this->vocabulary->vid);
2010-08-05 23:53:39 +00:00
$this->assertTrue(empty($tree), t('The terms are not created on preview.'));
2010-07-14 20:26:52 +00:00
// taxonomy.module does not maintain its static caches.
drupal_static_reset();
// Save, creating the terms.
2009-10-08 07:58:47 +00:00
$this->drupalPost('node/add/page', $edit, t('Save'));
2010-08-05 23:53:39 +00:00
$this->assertRaw(t('@type %title has been created.', array('@type' => t('Basic page'), '%title' => $edit["title"])), t('The node was created successfully'));
2008-11-09 00:58:03 +00:00
foreach ($terms as $term) {
2010-08-05 23:53:39 +00:00
$this->assertText($term, t('The term was saved and appears on the node page'));
2008-11-09 00:58:03 +00:00
}
2010-04-30 12:52:10 +00:00
// Get the created terms.
2011-12-21 03:21:51 +00:00
$term_objects = array();
foreach ($terms as $key => $term) {
2012-01-18 03:37:45 +00:00
$term_objects[$key] = taxonomy_term_load_multiple_by_name($term);
2011-12-21 03:21:51 +00:00
$term_objects[$key] = reset($term_objects[$key]);
}
2010-04-30 12:52:10 +00:00
2011-07-25 18:47:59 +00:00
// Delete term 1.
2011-12-21 03:21:51 +00:00
$this->drupalPost('taxonomy/term/' . $term_objects['term1']->tid . '/edit', array(), t('Delete'));
2010-04-30 12:52:10 +00:00
$this->drupalPost(NULL, NULL, t('Delete'));
2011-12-21 03:21:51 +00:00
$term_names = array($term_objects['term2']->name, $term_objects['term3']->name);
2010-04-30 12:52:10 +00:00
// Get the node.
$node = $this->drupalGetNodeByTitle($edit["title"]);
$this->drupalGet('node/' . $node->nid);
foreach ($term_names as $term_name) {
2011-12-21 03:21:51 +00:00
$this->assertText($term_name, t('The term %name appears on the node page after one term %deleted was deleted', array('%name' => $term_name, '%deleted' => $term_objects['term1']->name)));
2010-04-30 12:52:10 +00:00
}
2011-12-21 03:21:51 +00:00
$this->assertNoText($term_objects['term1']->name, t('The deleted term %name does not appear on the node page.', array('%name' => $term_objects['term1']->name)));
2010-10-13 01:25:11 +00:00
2011-12-05 12:17:24 +00:00
// Test autocomplete on term 2, which contains a comma.
// The term will be quoted, and the " will be encoded in unicode (\u0022).
2011-12-21 03:21:51 +00:00
$input = substr($term_objects['term2']->name, 0, 3);
2010-10-13 01:25:11 +00:00
$this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
2011-12-21 03:21:51 +00:00
$this->assertRaw('{"\u0022' . $term_objects['term2']->name . '\u0022":"' . $term_objects['term2']->name . '"}', t('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term2']->name)));
2011-07-25 18:47:59 +00:00
// Test autocomplete on term 3 - it is alphanumeric only, so no extra
// quoting.
2011-12-21 03:21:51 +00:00
$input = substr($term_objects['term3']->name, 0, 3);
2011-07-25 18:47:59 +00:00
$this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
2011-12-21 03:21:51 +00:00
$this->assertRaw('{"' . $term_objects['term3']->name . '":"' . $term_objects['term3']->name . '"}', t('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term3']->name)));
2008-11-09 00:58:03 +00:00
}
2008-09-11 19:47:40 +00:00
/**
2009-01-28 01:09:58 +00:00
* Save, edit and delete a term using the user interface.
2008-09-11 19:47:40 +00:00
*/
2009-01-28 01:09:58 +00:00
function testTermInterface() {
2008-09-11 19:47:40 +00:00
$edit = array(
'name' => $this->randomName(12),
2010-03-07 23:14:20 +00:00
'description[value]' => $this->randomName(100),
2008-09-11 19:47:40 +00:00
);
2008-11-16 14:59:33 +00:00
// Explicitly set the parents field to 'root', to ensure that
2008-11-13 08:13:56 +00:00
// taxonomy_form_term_submit() handles the invalid term ID correctly.
2009-10-14 20:22:04 +00:00
$edit['parent[]'] = array(0);
2008-09-11 19:47:40 +00:00
2008-11-16 14:59:33 +00:00
// Create the term to edit.
2010-03-25 11:53:25 +00:00
$this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
2008-09-11 19:47:40 +00:00
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($edit['name']);
2009-12-10 15:39:43 +00:00
$term = reset($terms);
2010-08-05 23:53:39 +00:00
$this->assertNotNull($term, t('Term found in database'));
2008-09-11 19:47:40 +00:00
// Submitting a term takes us to the add page; we need the List page.
2010-03-25 11:53:25 +00:00
$this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
2008-09-11 19:47:40 +00:00
// 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'));
2010-09-24 00:37:45 +00:00
$this->assertRaw($edit['name'], t('The randomly generated term name is present.'));
2010-08-05 23:53:39 +00:00
$this->assertText($edit['description[value]'], t('The randomly generated term description is present.'));
2008-09-11 19:47:40 +00:00
$edit = array(
'name' => $this->randomName(14),
2010-03-07 23:14:20 +00:00
'description[value]' => $this->randomName(102),
2008-09-11 19:47:40 +00:00
);
// Edit the term.
2009-06-03 19:34:53 +00:00
$this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
2008-09-11 19:47:40 +00:00
2010-04-06 05:44:07 +00:00
// Check that the term is still present at admin UI after edit.
$this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
2010-08-05 23:53:39 +00:00
$this->assertText($edit['name'], t('The randomly generated term name is present.'));
2010-04-06 05:44:07 +00:00
$this->assertLink(t('edit'));
2008-09-11 19:47:40 +00:00
// View the term and check that it is correct.
2009-06-03 19:34:53 +00:00
$this->drupalGet('taxonomy/term/' . $term->tid);
2010-08-05 23:53:39 +00:00
$this->assertText($edit['name'], t('The randomly generated term name is present.'));
$this->assertText($edit['description[value]'], t('The randomly generated term description is present.'));
2009-01-28 01:09:58 +00:00
2010-08-22 13:53:37 +00:00
// Did this page request display a 'term-listing-heading'?
2011-05-08 19:37:54 +00:00
$this->assertPattern('|class="taxonomy-term-description"|', 'Term page displayed the term description element.');
2010-08-22 13:53:37 +00:00
// Check that it does NOT show a description when description is blank.
$term->description = '';
taxonomy_term_save($term);
$this->drupalGet('taxonomy/term/' . $term->tid);
2011-05-08 19:37:54 +00:00
$this->assertNoPattern('|class="taxonomy-term-description"|', 'Term page did not display the term description when description was blank.');
2010-08-22 13:53:37 +00:00
2010-04-06 05:44:07 +00:00
// Check that the term feed page is working.
2009-11-18 18:58:01 +00:00
$this->drupalGet('taxonomy/term/' . $term->tid . '/feed');
2011-12-30 07:02:33 +00:00
// Check that the term edit page does not try to interpret additional path
// components as arguments for taxonomy_form_term().
$this->drupalGet('taxonomy/term/' . $term->tid . '/edit/' . $this->randomName());
2009-01-28 01:09:58 +00:00
// Delete the term.
2009-06-03 19:34:53 +00:00
$this->drupalPost('taxonomy/term/' . $term->tid . '/edit', array(), t('Delete'));
2009-01-28 01:09:58 +00:00
$this->drupalPost(NULL, NULL, t('Delete'));
// Assert that the term no longer exists.
2009-06-03 19:34:53 +00:00
$this->drupalGet('taxonomy/term/' . $term->tid);
2010-08-05 23:53:39 +00:00
$this->assertResponse(404, t('The taxonomy term page was not found'));
2008-09-11 19:47:40 +00:00
}
2009-06-03 19:34:53 +00:00
2009-10-24 04:23:51 +00:00
/**
* Save, edit and delete a term using the user interface.
*/
function testTermReorder() {
$this->createTerm($this->vocabulary);
$this->createTerm($this->vocabulary);
$this->createTerm($this->vocabulary);
// Fetch the created terms in the default alphabetical order, i.e. term1
// precedes term2 alphabetically, and term2 precedes term3.
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_treeparent');
drupal_static_reset('taxonomy_get_treeterms');
list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary->vid);
2010-03-25 11:53:25 +00:00
$this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
2010-07-31 13:01:50 +00:00
// Each term has four hidden fields, "tid:1:0[tid]", "tid:1:0[parent]",
// "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2,
// term3, term1 by setting weight property, make term3 a child of term2 by
// setting the parent and depth properties, and update all hidden fields.
2009-10-24 04:23:51 +00:00
$edit = array(
'tid:' . $term2->tid . ':0[tid]' => $term2->tid,
'tid:' . $term2->tid . ':0[parent]' => 0,
'tid:' . $term2->tid . ':0[depth]' => 0,
2010-07-31 13:01:50 +00:00
'tid:' . $term2->tid . ':0[weight]' => 0,
2009-10-24 04:23:51 +00:00
'tid:' . $term3->tid . ':0[tid]' => $term3->tid,
'tid:' . $term3->tid . ':0[parent]' => $term2->tid,
'tid:' . $term3->tid . ':0[depth]' => 1,
2010-07-31 13:01:50 +00:00
'tid:' . $term3->tid . ':0[weight]' => 1,
2009-10-24 04:23:51 +00:00
'tid:' . $term1->tid . ':0[tid]' => $term1->tid,
'tid:' . $term1->tid . ':0[parent]' => 0,
'tid:' . $term1->tid . ':0[depth]' => 0,
2010-07-31 13:01:50 +00:00
'tid:' . $term1->tid . ':0[weight]' => 2,
2009-10-24 04:23:51 +00:00
);
$this->drupalPost(NULL, $edit, t('Save'));
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_treeparent');
drupal_static_reset('taxonomy_get_treeterms');
$terms = taxonomy_get_tree($this->vocabulary->vid);
2010-08-05 23:53:39 +00:00
$this->assertEqual($terms[0]->tid, $term2->tid, t('Term 2 was moved above term 1.'));
$this->assertEqual($terms[1]->parents, array($term2->tid), t('Term 3 was made a child of term 2.'));
$this->assertEqual($terms[2]->tid, $term1->tid, t('Term 1 was moved below term 2.'));
2009-10-24 04:23:51 +00:00
2010-03-25 11:53:25 +00:00
$this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name, array(), t('Reset to alphabetical'));
2009-10-24 04:23:51 +00:00
// Submit confirmation form.
$this->drupalPost(NULL, array(), t('Reset to alphabetical'));
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_treeparent');
drupal_static_reset('taxonomy_get_treeterms');
$terms = taxonomy_get_tree($this->vocabulary->vid);
2010-08-05 23:53:39 +00:00
$this->assertEqual($terms[0]->tid, $term1->tid, t('Term 1 was moved to back above term 2.'));
$this->assertEqual($terms[1]->tid, $term2->tid, t('Term 2 was moved to back below term 1.'));
$this->assertEqual($terms[2]->tid, $term3->tid, t('Term 3 is still below term 2.'));
$this->assertEqual($terms[2]->parents, array($term2->tid), t('Term 3 is still a child of term 2.').var_export($terms[1]->tid,1));
2009-10-24 04:23:51 +00:00
}
2011-07-08 04:06:19 +00:00
/**
* Test saving a term with multiple parents through the UI.
*/
function testTermMultipleParentsInterface() {
// Add a new term to the vocabulary so that we can have multiple parents.
$parent = $this->createTerm($this->vocabulary);
// Add a new term with multiple parents.
$edit = array(
'name' => $this->randomName(12),
'description[value]' => $this->randomName(100),
'parent[]' => array(0, $parent->tid),
);
// Save the new term.
$this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
// Check that the term was successfully created.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($edit['name']);
2011-07-08 04:06:19 +00:00
$term = reset($terms);
$this->assertNotNull($term, t('Term found in database'));
$this->assertEqual($edit['name'], $term->name, t('Term name was successfully saved.'));
$this->assertEqual($edit['description[value]'], $term->description, t('Term description was successfully saved.'));
// Check that the parent tid is still there. The other parent (<root>) is
2012-01-18 03:37:45 +00:00
// not added by taxonomy_term_load_parents().
$parents = taxonomy_term_load_parents($term->tid);
2011-07-08 04:06:19 +00:00
$parent = reset($parents);
$this->assertEqual($edit['parent[]'][1], $parent->tid, t('Term parents were successfully saved.'));
}
2009-06-03 19:34:53 +00:00
/**
2012-01-18 03:37:45 +00:00
* Test taxonomy_term_load_multiple_by_name().
2009-06-03 19:34:53 +00:00
*/
function testTaxonomyGetTermByName() {
2009-06-12 13:59:56 +00:00
$term = $this->createTerm($this->vocabulary);
2009-06-03 19:34:53 +00:00
// Load the term with the exact name.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($term->name);
2010-08-05 23:53:39 +00:00
$this->assertTrue(isset($terms[$term->tid]), t('Term loaded using exact name.'));
2009-06-03 19:34:53 +00:00
// Load the term with space concatenated.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name(' ' . $term->name . ' ');
2010-08-05 23:53:39 +00:00
$this->assertTrue(isset($terms[$term->tid]), t('Term loaded with extra whitespace.'));
2009-06-03 19:34:53 +00:00
// Load the term with name uppercased.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name(strtoupper($term->name));
2010-08-05 23:53:39 +00:00
$this->assertTrue(isset($terms[$term->tid]), t('Term loaded with uppercased name.'));
2009-06-03 19:34:53 +00:00
// Load the term with name lowercased.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name(strtolower($term->name));
2010-08-05 23:53:39 +00:00
$this->assertTrue(isset($terms[$term->tid]), t('Term loaded with lowercased name.'));
2009-06-03 19:34:53 +00:00
// Try to load an invalid term name.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name('Banana');
2009-06-03 19:34:53 +00:00
$this->assertFalse($terms);
// Try to load the term using a substring of the name.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name(drupal_substr($term->name, 2));
2009-06-03 19:34:53 +00:00
$this->assertFalse($terms);
2012-01-08 14:02:21 +00:00
// Create a new term in a different vocabulary with the same name.
$new_vocabulary = $this->createVocabulary();
$new_term = new stdClass();
$new_term->name = $term->name;
$new_term->vid = $new_vocabulary->vid;
taxonomy_term_save($new_term);
// Load multiple terms with the same name.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($term->name);
2012-01-08 14:02:21 +00:00
$this->assertEqual(count($terms), 2, t('Two terms loaded with the same name.'));
// Load single term when restricted to one vocabulary.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($term->name, $this->vocabulary->machine_name);
2012-01-08 14:02:21 +00:00
$this->assertEqual(count($terms), 1, t('One term loaded when restricted by vocabulary.'));
$this->assertTrue(isset($terms[$term->tid]), t('Term loaded using exact name and vocabulary machine name.'));
// Create a new term with another name.
$term2 = $this->createTerm($this->vocabulary);
// Try to load a term by name that doesn't exist in this vocabulary but
// exists in another vocabulary.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($term2->name, $new_vocabulary->machine_name);
2012-01-08 14:02:21 +00:00
$this->assertFalse($terms, t('Invalid term name restricted by vocabulary machine name not loaded.'));
// Try to load terms filtering by a non-existing vocabulary.
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($term2->name, 'non_existing_vocabulary');
2012-01-08 14:02:21 +00:00
$this->assertEqual(count($terms), 0, t('No terms loaded when restricted by a non-existing vocabulary.'));
2009-06-03 19:34:53 +00:00
}
2008-09-15 20:48:10 +00:00
}
2008-12-05 22:18:46 +00:00
2011-12-08 03:52:37 +00:00
/**
* Tests the hook implementations that maintain the taxonomy index.
*/
class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase {
public static function getInfo() {
return array(
'name' => 'Taxonomy term index',
'description' => 'Tests the hook implementations that maintain the taxonomy index.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp('taxonomy');
// Create an administrative user.
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
$this->drupalLogin($this->admin_user);
// Create a vocabulary and add two term reference fields to article nodes.
$this->vocabulary = $this->createVocabulary();
$this->field_name_1 = drupal_strtolower($this->randomName());
$this->field_1 = array(
'field_name' => $this->field_name_1,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $this->vocabulary->machine_name,
'parent' => 0,
),
),
),
);
field_create_field($this->field_1);
$this->instance_1 = array(
'field_name' => $this->field_name_1,
'bundle' => 'article',
'entity_type' => 'node',
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($this->instance_1);
$this->field_name_2 = drupal_strtolower($this->randomName());
$this->field_2 = array(
'field_name' => $this->field_name_2,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $this->vocabulary->machine_name,
'parent' => 0,
),
),
),
);
field_create_field($this->field_2);
$this->instance_2 = array(
'field_name' => $this->field_name_2,
'bundle' => 'article',
'entity_type' => 'node',
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($this->instance_2);
}
/**
* Tests that the taxonomy index is maintained properly.
*/
function testTaxonomyIndex() {
// Create terms in the vocabulary.
$term_1 = $this->createTerm($this->vocabulary);
$term_2 = $this->createTerm($this->vocabulary);
// Post an article.
$edit = array();
$langcode = LANGUAGE_NONE;
$edit["title"] = $this->randomName();
$edit["body[$langcode][0][value]"] = $this->randomName();
$edit["{$this->field_name_1}[$langcode][]"] = $term_1->tid;
$edit["{$this->field_name_2}[$langcode][]"] = $term_1->tid;
$this->drupalPost('node/add/article', $edit, t('Save'));
// Check that the term is indexed, and only once.
$node = $this->drupalGetNodeByTitle($edit["title"]);
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_1->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 1 is indexed once.'));
// Update the article to change one term.
$edit["{$this->field_name_1}[$langcode][]"] = $term_2->tid;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Check that both terms are indexed.
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_1->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 1 is indexed.'));
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_2->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 2 is indexed.'));
// Update the article to change another term.
$edit["{$this->field_name_2}[$langcode][]"] = $term_2->tid;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Check that only one term is indexed.
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_1->tid,
))->fetchField();
$this->assertEqual(0, $index_count, t('Term 1 is not indexed.'));
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_2->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 2 is indexed once.'));
// Redo the above tests without interface.
$update_node = array(
'nid' => $node->nid,
'vid' => $node->vid,
'uid' => $node->uid,
'type' => $node->type,
'title' => $this->randomName(),
);
// Update the article with no term changed.
$updated_node = (object) $update_node;
node_save($updated_node);
// Check that the index was not changed.
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_1->tid,
))->fetchField();
$this->assertEqual(0, $index_count, t('Term 1 is not indexed.'));
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_2->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 2 is indexed once.'));
// Update the article to change one term.
$update_node[$this->field_name_1][$langcode] = array(array('tid' => $term_1->tid));
$updated_node = (object) $update_node;
node_save($updated_node);
// Check that both terms are indexed.
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_1->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 1 is indexed.'));
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_2->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 2 is indexed.'));
// Update the article to change another term.
$update_node[$this->field_name_2][$langcode] = array(array('tid' => $term_1->tid));
$updated_node = (object) $update_node;
node_save($updated_node);
// Check that only one term is indexed.
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_1->tid,
))->fetchField();
$this->assertEqual(1, $index_count, t('Term 1 is indexed once.'));
$index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
':nid' => $node->nid,
':tid' => $term_2->tid,
))->fetchField();
$this->assertEqual(0, $index_count, t('Term 2 is not indexed.'));
}
}
2008-12-05 22:18:46 +00:00
/**
* Test the taxonomy_term_load_multiple() function.
*/
class TaxonomyLoadMultipleUnitTest extends TaxonomyWebTestCase {
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2008-12-05 22:18:46 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Taxonomy term multiple loading',
'description' => 'Test the loading of multiple taxonomy terms at once',
'group' => 'Taxonomy',
2008-12-05 22:18:46 +00:00
);
}
function setUp() {
parent::setUp();
$this->taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
$this->drupalLogin($this->taxonomy_admin);
}
/**
* Create a vocabulary and some taxonomy terms, ensuring they're loaded
* correctly using taxonomy_term_load_multiple().
*/
function testTaxonomyTermMultipleLoad() {
// Create a vocabulary.
$vocabulary = $this->createVocabulary();
// Create five terms in the vocabulary.
$i = 0;
while ($i < 5) {
$i++;
2009-06-12 13:59:56 +00:00
$this->createTerm($vocabulary);
2008-12-05 22:18:46 +00:00
}
// Load the terms from the vocabulary.
$terms = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
$count = count($terms);
2010-08-05 23:53:39 +00:00
$this->assertTrue($count == 5, t('Correct number of terms were loaded. !count terms.', array('!count' => $count)));
2008-12-05 22:18:46 +00:00
// Load the same terms again by tid.
$terms2 = taxonomy_term_load_multiple(array_keys($terms));
2010-08-05 23:53:39 +00:00
$this->assertTrue($count == count($terms2), t('Five terms were loaded by tid'));
$this->assertEqual($terms, $terms2, t('Both arrays contain the same terms'));
2008-12-05 22:18:46 +00:00
// Load the terms by tid, with a condition on vid.
$terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vid' => $vocabulary->vid));
$this->assertEqual($terms2, $terms3);
// Remove one term from the array, then delete it.
$deleted = array_shift($terms3);
taxonomy_term_delete($deleted->tid);
2009-04-02 20:39:45 +00:00
$deleted_term = taxonomy_term_load($deleted->tid);
2008-12-05 22:18:46 +00:00
$this->assertFalse($deleted_term);
// Load terms from the vocabulary by vid.
2009-04-02 20:39:45 +00:00
$terms4 = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
2010-08-05 23:53:39 +00:00
$this->assertTrue(count($terms4 == 4), t('Correct number of terms were loaded.'));
2008-12-05 22:18:46 +00:00
$this->assertFalse(isset($terms4[$deleted->tid]));
2008-12-19 15:34:27 +00:00
// Create a single term and load it by name.
2009-06-12 13:59:56 +00:00
$term = $this->createTerm($vocabulary);
2008-12-19 15:34:27 +00:00
$loaded_terms = taxonomy_term_load_multiple(array(), array('name' => $term->name));
2010-08-05 23:53:39 +00:00
$this->assertEqual(count($loaded_terms), 1, t('One term was loaded'));
2008-12-19 15:34:27 +00:00
$loaded_term = reset($loaded_terms);
2010-08-05 23:53:39 +00:00
$this->assertEqual($term->tid, $loaded_term->tid, t('Term loaded by name successfully.'));
2008-12-05 22:18:46 +00:00
}
}
2009-01-22 18:25:59 +00:00
/**
* Tests for taxonomy hook invocation.
*/
2009-06-12 13:59:56 +00:00
class TaxonomyHooksTestCase extends TaxonomyWebTestCase {
2009-03-31 01:49:55 +00:00
public static function getInfo() {
2009-01-22 18:25:59 +00:00
return array(
2009-07-13 21:51:42 +00:00
'name' => 'Taxonomy term hooks',
'description' => 'Hooks for taxonomy term load/save/delete.',
2009-09-20 17:40:42 +00:00
'group' => 'Taxonomy',
2009-01-22 18:25:59 +00:00
);
}
function setUp() {
parent::setUp('taxonomy', 'taxonomy_test');
$taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
$this->drupalLogin($taxonomy_admin);
}
/**
* Test that hooks are run correctly on creating, editing and deleting a term.
*/
function testTaxonomyTermHooks() {
2009-06-12 13:59:56 +00:00
$vocabulary = $this->createVocabulary();
2009-01-22 18:25:59 +00:00
// Create a term with one antonym.
$edit = array(
'name' => $this->randomName(),
2009-08-03 20:19:29 +00:00
'antonym' => 'Long',
2009-01-22 18:25:59 +00:00
);
2010-03-25 11:53:25 +00:00
$this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save'));
2012-01-18 03:37:45 +00:00
$terms = taxonomy_term_load_multiple_by_name($edit['name']);
2009-12-10 15:39:43 +00:00
$term = reset($terms);
2010-08-05 23:53:39 +00:00
$this->assertEqual($term->antonym, $edit['antonym'], t('Antonym was loaded into the term object'));
2009-01-22 18:25:59 +00:00
// Update the term with a different antonym.
$edit = array(
'name' => $this->randomName(),
2009-08-03 20:19:29 +00:00
'antonym' => 'Short',
2009-01-22 18:25:59 +00:00
);
$this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
2009-04-02 20:39:45 +00:00
taxonomy_terms_static_reset();
$term = taxonomy_term_load($term->tid);
2010-08-05 23:53:39 +00:00
$this->assertEqual($edit['antonym'], $term->antonym, t('Antonym was successfully edited'));
2009-01-22 18:25:59 +00:00
// Delete the term.
taxonomy_term_delete($term->tid);
2009-08-03 20:19:29 +00:00
$antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
2010-08-05 23:53:39 +00:00
$this->assertFalse($antonym, t('The antonym were deleted from the database.'));
2009-01-22 18:25:59 +00:00
}
}
2009-07-31 07:43:33 +00:00
/**
* Tests for taxonomy term field and formatter.
*/
class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
protected $instance;
protected $vocabulary;
public static function getInfo() {
return array(
2010-01-04 18:03:12 +00:00
'name' => 'Taxonomy term reference field',
2009-09-20 17:40:42 +00:00
'description' => 'Test the creation of term fields.',
'group' => 'Taxonomy',
2009-07-31 07:43:33 +00:00
);
}
function setUp() {
parent::setUp('field_test');
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy'));
$this->drupalLogin($web_user);
$this->vocabulary = $this->createVocabulary();
2010-10-13 05:19:26 +00:00
// Setup a field and instance.
2009-07-31 07:43:33 +00:00
$this->field_name = drupal_strtolower($this->randomName());
$this->field = array(
'field_name' => $this->field_name,
2010-01-04 18:03:12 +00:00
'type' => 'taxonomy_term_reference',
2009-07-31 07:43:33 +00:00
'settings' => array(
'allowed_values' => array(
array(
2010-10-01 01:37:13 +00:00
'vocabulary' => $this->vocabulary->machine_name,
2009-07-31 07:43:33 +00:00
'parent' => '0',
),
),
)
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
2010-03-27 05:52:50 +00:00
'entity_type' => 'test_entity',
2009-11-20 23:29:28 +00:00
'bundle' => 'test_bundle',
2009-07-31 07:43:33 +00:00
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'full' => array(
2010-01-04 18:03:12 +00:00
'type' => 'taxonomy_term_reference_link',
2009-07-31 07:43:33 +00:00
),
),
);
field_create_instance($this->instance);
2010-10-13 05:19:26 +00:00
}
2009-07-31 07:43:33 +00:00
2010-10-13 05:19:26 +00:00
/**
* Test term field validation.
*/
function testTaxonomyTermFieldValidation() {
2009-07-31 07:43:33 +00:00
// Test valid and invalid values with field_attach_validate().
2009-12-02 19:26:23 +00:00
$langcode = LANGUAGE_NONE;
2009-11-20 23:29:28 +00:00
$entity = field_test_create_stub_entity();
2009-07-31 07:43:33 +00:00
$term = $this->createTerm($this->vocabulary);
2009-11-27 07:10:51 +00:00
$entity->{$this->field_name}[$langcode][0]['tid'] = $term->tid;
2009-07-31 07:43:33 +00:00
try {
2009-11-27 07:10:51 +00:00
field_attach_validate('test_entity', $entity);
2010-08-05 23:53:39 +00:00
$this->pass(t('Correct term does not cause validation error'));
2009-07-31 07:43:33 +00:00
}
catch (FieldValidationException $e) {
2010-08-05 23:53:39 +00:00
$this->fail(t('Correct term does not cause validation error'));
2009-07-31 07:43:33 +00:00
}
2009-11-20 23:29:28 +00:00
$entity = field_test_create_stub_entity();
2009-07-31 07:43:33 +00:00
$bad_term = $this->createTerm($this->createVocabulary());
2009-11-27 07:10:51 +00:00
$entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->tid;
2009-07-31 07:43:33 +00:00
try {
field_attach_validate('test_entity', $entity);
2010-08-05 23:53:39 +00:00
$this->fail(t('Wrong term causes validation error'));
2009-07-31 07:43:33 +00:00
}
catch (FieldValidationException $e) {
2010-08-05 23:53:39 +00:00
$this->pass(t('Wrong term causes validation error'));
2009-07-31 07:43:33 +00:00
}
}
/**
* Test widgets.
*/
function testTaxonomyTermFieldWidgets() {
// Create a term in the vocabulary.
$term = $this->createTerm($this->vocabulary);
// Display creation form.
2009-12-02 19:26:23 +00:00
$langcode = LANGUAGE_NONE;
2009-07-31 07:43:33 +00:00
$this->drupalGet('test-entity/add/test-bundle');
2010-08-05 23:53:39 +00:00
$this->assertFieldByName("{$this->field_name}[$langcode]", '', t('Widget is displayed'));
2009-07-31 07:43:33 +00:00
// Submit with some value.
$edit = array(
2009-11-20 05:14:13 +00:00
"{$this->field_name}[$langcode]" => array($term->tid),
2009-07-31 07:43:33 +00:00
);
$this->drupalPost(NULL, $edit, t('Save'));
2010-09-24 00:37:45 +00:00
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
2009-07-31 07:43:33 +00:00
$id = $match[1];
2010-08-05 23:53:39 +00:00
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
2009-07-31 07:43:33 +00:00
// Display the object.
2009-10-16 03:47:14 +00:00
$entity = field_test_entity_test_load($id);
2009-10-16 03:21:23 +00:00
$entities = array($id => $entity);
2010-10-13 05:19:26 +00:00
field_attach_prepare_view('test_entity', $entities, 'full');
$entity->content = field_attach_view('test_entity', $entity, 'full');
2009-07-31 07:43:33 +00:00
$this->content = drupal_render($entity->content);
2010-08-05 23:53:39 +00:00
$this->assertText($term->name, t('Term name is displayed'));
2009-07-31 07:43:33 +00:00
}
2010-10-13 05:19:26 +00:00
/**
* Tests that vocabulary machine name changes are mirrored in field definitions.
*/
function testTaxonomyTermFieldChangeMachineName() {
// Add several entries in the 'allowed_values' setting, to make sure that
// they all get updated.
$this->field['settings']['allowed_values'] = array(
array(
'vocabulary' => $this->vocabulary->machine_name,
'parent' => '0',
),
array(
'vocabulary' => $this->vocabulary->machine_name,
'parent' => '0',
),
array(
'vocabulary' => 'foo',
'parent' => '0',
),
);
field_update_field($this->field);
// Change the machine name.
$new_name = drupal_strtolower($this->randomName());
$this->vocabulary->machine_name = $new_name;
taxonomy_vocabulary_save($this->vocabulary);
// Check that the field instance is still attached to the vocabulary.
$field = field_info_field($this->field_name);
$allowed_values = $field['settings']['allowed_values'];
$this->assertEqual($allowed_values[0]['vocabulary'], $new_name, t('Index 0: Machine name was updated correctly.'));
$this->assertEqual($allowed_values[1]['vocabulary'], $new_name, t('Index 1: Machine name was updated correctly.'));
$this->assertEqual($allowed_values[2]['vocabulary'], 'foo', t('Index 2: Machine name was left untouched.'));
}
2009-07-31 07:43:33 +00:00
}
2010-01-29 22:56:54 +00:00
/**
* Test taxonomy token replacement in strings.
*/
class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase {
public static function getInfo() {
return array(
'name' => 'Taxonomy token replacement',
'description' => 'Generates text using placeholders for dummy content to check taxonomy token replacement.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
$this->drupalLogin($this->admin_user);
$this->vocabulary = $this->createVocabulary();
$this->langcode = LANGUAGE_NONE;
2010-02-11 03:29:22 +00:00
$field = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
2010-10-01 01:37:13 +00:00
'vocabulary' => $this->vocabulary->machine_name,
2010-02-11 03:29:22 +00:00
'parent' => 0,
),
),
),
);
field_create_field($field);
2010-01-29 22:56:54 +00:00
$this->instance = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'bundle' => 'article',
2010-03-27 05:52:50 +00:00
'entity_type' => 'node',
2010-01-29 22:56:54 +00:00
'widget' => array(
'type' => 'options_select',
),
'display' => array(
2010-05-23 19:10:23 +00:00
'default' => array(
2010-01-29 22:56:54 +00:00
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($this->instance);
}
/**
* Creates some terms and a node, then tests the tokens generated from them.
*/
function testTaxonomyTokenReplacement() {
2010-04-20 09:48:06 +00:00
global $language;
2010-01-29 22:56:54 +00:00
// Create two taxonomy terms.
$term1 = $this->createTerm($this->vocabulary);
$term2 = $this->createTerm($this->vocabulary);
// Edit $term2, setting $term1 as parent.
$edit = array();
2010-04-20 09:48:06 +00:00
$edit['name'] = '<blink>Blinking Text</blink>';
2010-01-29 22:56:54 +00:00
$edit['parent[]'] = array($term1->tid);
$this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
// Create node with term2.
$edit = array();
$node = $this->drupalCreateNode(array('type' => 'article'));
$edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid;
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
2010-08-03 02:17:18 +00:00
// Generate and test sanitized tokens for term1.
$tests = array();
$tests['[term:tid]'] = $term1->tid;
$tests['[term:name]'] = check_plain($term1->name);
$tests['[term:description]'] = check_markup($term1->description, $term1->format);
$tests['[term:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE));
$tests['[term:node-count]'] = 0;
$tests['[term:parent:name]'] = '[term:parent:name]';
$tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name);
2010-10-13 01:25:11 +00:00
2010-08-03 02:17:18 +00:00
foreach ($tests as $input => $expected) {
$output = token_replace($input, array('term' => $term1), array('language' => $language));
2011-03-01 01:36:37 +00:00
$this->assertEqual($output, $expected, t('Sanitized taxonomy term token %token replaced.', array('%token' => $input)));
2010-08-03 02:17:18 +00:00
}
// Generate and test sanitized tokens for term2.
2010-04-20 09:48:06 +00:00
$tests = array();
$tests['[term:tid]'] = $term2->tid;
$tests['[term:name]'] = check_plain($term2->name);
$tests['[term:description]'] = check_markup($term2->description, $term2->format);
$tests['[term:url]'] = url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE));
$tests['[term:node-count]'] = 1;
$tests['[term:parent:name]'] = check_plain($term1->name);
2010-08-08 02:06:56 +00:00
$tests['[term:parent:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE));
$tests['[term:parent:parent:name]'] = '[term:parent:parent:name]';
2010-04-20 09:48:06 +00:00
$tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name);
// Test to make sure that we generated something for each token.
2010-08-05 23:53:39 +00:00
$this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.'));
2010-04-20 09:48:06 +00:00
foreach ($tests as $input => $expected) {
$output = token_replace($input, array('term' => $term2), array('language' => $language));
2011-03-01 01:36:37 +00:00
$this->assertEqual($output, $expected, t('Sanitized taxonomy term token %token replaced.', array('%token' => $input)));
2010-04-20 09:48:06 +00:00
}
// Generate and test unsanitized tokens.
$tests['[term:name]'] = $term2->name;
$tests['[term:description]'] = $term2->description;
$tests['[term:parent:name]'] = $term1->name;
$tests['[term:vocabulary:name]'] = $this->vocabulary->name;
2010-01-29 22:56:54 +00:00
2010-04-20 09:48:06 +00:00
foreach ($tests as $input => $expected) {
$output = token_replace($input, array('term' => $term2), array('language' => $language, 'sanitize' => FALSE));
2011-03-01 01:36:37 +00:00
$this->assertEqual($output, $expected, t('Unsanitized taxonomy term token %token replaced.', array('%token' => $input)));
2010-04-20 09:48:06 +00:00
}
// Generate and test sanitized tokens.
$tests = array();
$tests['[vocabulary:vid]'] = $this->vocabulary->vid;
$tests['[vocabulary:name]'] = check_plain($this->vocabulary->name);
$tests['[vocabulary:description]'] = filter_xss($this->vocabulary->description);
$tests['[vocabulary:node-count]'] = 1;
$tests['[vocabulary:term-count]'] = 2;
2010-01-29 22:56:54 +00:00
2010-04-20 09:48:06 +00:00
// Test to make sure that we generated something for each token.
2010-08-05 23:53:39 +00:00
$this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.'));
2010-04-20 09:48:06 +00:00
foreach ($tests as $input => $expected) {
$output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language));
2011-03-01 01:36:37 +00:00
$this->assertEqual($output, $expected, t('Sanitized taxonomy vocabulary token %token replaced.', array('%token' => $input)));
2010-04-20 09:48:06 +00:00
}
// Generate and test unsanitized tokens.
$tests['[vocabulary:name]'] = $this->vocabulary->name;
$tests['[vocabulary:description]'] = $this->vocabulary->description;
foreach ($tests as $input => $expected) {
$output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language, 'sanitize' => FALSE));
2011-03-01 01:36:37 +00:00
$this->assertEqual($output, $expected, t('Unsanitized taxonomy vocabulary token %token replaced.', array('%token' => $input)));
2010-04-20 09:48:06 +00:00
}
2010-01-29 22:56:54 +00:00
}
}
2010-11-20 09:31:08 +00:00
/**
* Tests for verifying that taxonomy pages use the correct theme.
*/
class TaxonomyThemeTestCase extends TaxonomyWebTestCase {
public static function getInfo() {
return array(
'name' => 'Taxonomy theme switching',
'description' => 'Verifies that various taxonomy pages use the expected theme.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp();
// Make sure we are using distinct default and administrative themes for
// the duration of these tests.
variable_set('theme_default', 'bartik');
variable_set('admin_theme', 'seven');
// Create and log in as a user who has permission to add and edit taxonomy
2010-11-27 20:25:44 +00:00
// terms and view the administrative theme.
$admin_user = $this->drupalCreateUser(array('administer taxonomy', 'view the administration theme'));
2010-11-20 09:31:08 +00:00
$this->drupalLogin($admin_user);
}
/**
* Test the theme used when adding, viewing and editing taxonomy terms.
*/
function testTaxonomyTermThemes() {
// Adding a term to a vocabulary is considered an administrative action and
// should use the administrative theme.
$vocabulary = $this->createVocabulary();
$this->drupalGet('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add');
$this->assertRaw('seven/style.css', t("The administrative theme's CSS appears on the page for adding a taxonomy term."));
// Viewing a taxonomy term should use the default theme.
$term = $this->createTerm($vocabulary);
$this->drupalGet('taxonomy/term/' . $term->tid);
$this->assertRaw('bartik/css/style.css', t("The default theme's CSS appears on the page for viewing a taxonomy term."));
// Editing a taxonomy term should use the same theme as adding one.
$this->drupalGet('taxonomy/term/' . $term->tid . '/edit');
$this->assertRaw('seven/style.css', t("The administrative theme's CSS appears on the page for editing a taxonomy term."));
}
}