Commit b2930200 authored by Dries's avatar Dries

Issue #2256405 by blueminds: VocabularyUnitTest and VocabularyTest are misnamed.

parent 63dfd503
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\VocabularyTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Tests the taxonomy vocabulary interface.
*/
class VocabularyTest extends TaxonomyTestBase {
public static function getInfo() {
return array(
'name' => 'Taxonomy vocabulary interface',
'description' => 'Test the taxonomy vocabulary interface.',
'group' => '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/structure/taxonomy');
// Create a new vocabulary.
$this->clickLink(t('Add vocabulary'));
$edit = array();
$vid = drupal_strtolower($this->randomName());
$edit['name'] = $this->randomName();
$edit['description'] = $this->randomName();
$edit['vid'] = $vid;
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), 'Vocabulary created successfully.');
// Edit the vocabulary.
$this->drupalGet('admin/structure/taxonomy');
$this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');
$this->clickLink(t('Edit vocabulary'));
$edit = array();
$edit['name'] = $this->randomName();
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalGet('admin/structure/taxonomy');
$this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');
// Try to submit a vocabulary with a duplicate machine name.
$edit['vid'] = $vid;
$this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
$this->assertText(t('The machine-readable name is already in use. It must be unique.'));
// Try to submit an invalid machine name.
$edit['vid'] = '!&^%';
$this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
$this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
// Ensure that vocabulary titles are escaped properly.
$edit = array();
$edit['name'] = 'Don\'t Panic';
$edit['description'] = $this->randomName();
$edit['vid'] = 'don_t_panic';
$this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
$site_name = \Drupal::config('system.site')->get('name');
$this->assertTitle(t('Don\'t Panic | @site-name', array('@site-name' => $site_name)), 'The page title contains the escaped character.');
$this->assertNoTitle(t('Don&#039;t Panic | @site-name', array('@site-name' => $site_name)), 'The page title does not contain an encoded character.');
}
/**
* 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 = entity_load_multiple('taxonomy_vocabulary');
$edit = array();
foreach ($vocabularies as $key => $vocabulary) {
$weight = -$vocabulary->weight;
$vocabularies[$key]->weight = $weight;
$edit['vocabularies[' . $key . '][weight]'] = $weight;
}
// Saving the new weights via the interface.
$this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));
// Load the vocabularies from the database.
$this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
$new_vocabularies = entity_load_multiple('taxonomy_vocabulary');
// Check that the weights are saved in the database correctly.
foreach ($vocabularies as $key => $vocabulary) {
$this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, 'The vocabulary weight was changed.');
}
}
/**
* Test the vocabulary overview with no vocabularies.
*/
function testTaxonomyAdminNoVocabularies() {
// Delete all vocabularies.
$vocabularies = entity_load_multiple('taxonomy_vocabulary');
foreach ($vocabularies as $key => $vocabulary) {
$vocabulary->delete();
}
// Confirm that no vocabularies are found in the database.
$this->assertFalse(entity_load_multiple('taxonomy_vocabulary'), 'No vocabularies found.');
$this->drupalGet('admin/structure/taxonomy');
// Check the default message for no vocabularies.
$this->assertText(t('No vocabularies available.'));
}
/**
* Deleting a vocabulary.
*/
function testTaxonomyAdminDeletingVocabulary() {
// Create a vocabulary.
$vid = drupal_strtolower($this->randomName());
$edit = array(
'name' => $this->randomName(),
'vid' => $vid,
);
$this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
$this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
// Check the created vocabulary.
$this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
$vocabulary = entity_load('taxonomy_vocabulary', $vid);
$this->assertTrue($vocabulary, 'Vocabulary found.');
// Delete the vocabulary.
$this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id());
$this->clickLink(t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), '[confirm deletion] Asks for confirmation.');
$this->assertText(t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'), '[confirm deletion] Inform that all terms will be deleted.');
// Confirm deletion.
$this->drupalPostForm(NULL, NULL, t('Delete'));
$this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), 'Vocabulary deleted.');
$this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
$this->assertFalse(entity_load('taxonomy_vocabulary', $vid), 'Vocabulary not found.');
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\VocabularyUnitTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Tests for taxonomy vocabulary functions.
*/
class VocabularyUnitTest extends TaxonomyTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('field_test');
public static function getInfo() {
return array(
'name' => 'Taxonomy vocabularies',
'description' => 'Test loading, saving and deleting vocabularies.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp();
$admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
$this->drupalLogin($admin_user);
$this->vocabulary = $this->createVocabulary();
}
/**
* Test deleting a taxonomy that contains terms.
*/
function testTaxonomyVocabularyDeleteWithTerms() {
// Delete any existing vocabularies.
foreach (entity_load_multiple('taxonomy_vocabulary') as $vocabulary) {
$vocabulary->delete();
}
// Assert that there are no terms left.
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField(), 'There are no terms remaining.');
// 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]->id());
$terms[2]->save();
$terms[4]->parent = array($terms[1]->id(), $terms[2]->id());
$terms[4]->save();
// Assert that there are now 5 terms.
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField(), 'There are 5 terms found.');
$vocabulary->delete();
// Assert that there are no terms left.
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField(), 'All terms are deleted.');
}
/**
* Ensure that the vocabulary static reset works correctly.
*/
function testTaxonomyVocabularyLoadStaticReset() {
$original_vocabulary = entity_load('taxonomy_vocabulary', $this->vocabulary->id());
$this->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.');
$this->assertEqual($this->vocabulary->name, $original_vocabulary->name, 'Vocabulary loaded successfully.');
// Change the name and description.
$vocabulary = $original_vocabulary;
$vocabulary->name = $this->randomName();
$vocabulary->description = $this->randomName();
$vocabulary->save();
// Load the vocabulary.
$new_vocabulary = entity_load('taxonomy_vocabulary', $original_vocabulary->id());
$this->assertEqual($new_vocabulary->name, $vocabulary->name, 'The vocabulary was loaded.');
// Delete the vocabulary.
$this->vocabulary->delete();
$vocabularies = entity_load_multiple('taxonomy_vocabulary');
$this->assertTrue(!isset($vocabularies[$this->vocabulary->id()]), 'The vocabulary was deleted.');
}
/**
* Tests for loading multiple vocabularies.
*/
function testTaxonomyVocabularyLoadMultiple() {
// Delete any existing vocabularies.
foreach (entity_load_multiple('taxonomy_vocabulary') as $vocabulary) {
$vocabulary->delete();
}
// Create some vocabularies and assign weights.
$vocabulary1 = $this->createVocabulary();
$vocabulary1->weight = 0;
$vocabulary1->save();
$vocabulary2 = $this->createVocabulary();
$vocabulary2->weight = 1;
$vocabulary2->save();
$vocabulary3 = $this->createVocabulary();
$vocabulary3->weight = 2;
$vocabulary3->save();
// Fetch the names for all vocabularies, confirm that they are keyed by
// machine name.
$names = taxonomy_vocabulary_get_names();
$this->assertEqual($names[$vocabulary1->id()], $vocabulary1->id(), 'Vocabulary 1 name found.');
// Fetch the vocabularies with entity_load_multiple(), specifying IDs.
// Ensure they are returned in the same order as the original array.
$vocabularies = entity_load_multiple('taxonomy_vocabulary', array($vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()));
$loaded_order = array_keys($vocabularies);
$expected_order = array($vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id());
$this->assertIdentical($loaded_order, $expected_order);
// Test loading vocabularies by their properties.
$controller = $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary');
// Fetch vocabulary 1 by name.
$vocabulary = current($controller->loadByProperties(array('name' => $vocabulary1->name)));
$this->assertEqual($vocabulary->id(), $vocabulary1->id(), 'Vocabulary loaded successfully by name.');
// Fetch vocabulary 2 by name and ID.
$vocabulary = current($controller->loadByProperties(array(
'name' => $vocabulary2->name,
'vid' => $vocabulary2->id(),
)));
$this->assertEqual($vocabulary->id(), $vocabulary2->id(), 'Vocabulary loaded successfully by name and ID.');
}
/**
* Tests that machine name changes are properly reflected.
*/
function testTaxonomyVocabularyChangeMachineName() {
// Add a field instance to the vocabulary.
entity_create('field_config', array(
'name' => 'field_test',
'entity_type' => 'taxonomy_term',
'type' => 'test_field',
))->save();
entity_create('field_instance_config', array(
'field_name' => 'field_test',
'entity_type' => 'taxonomy_term',
'bundle' => $this->vocabulary->id(),
))->save();
// Change the machine name.
$old_name = $this->vocabulary->id();
$new_name = drupal_strtolower($this->randomName());
$this->vocabulary->vid = $new_name;
$this->vocabulary->save();
// Check that entity bundles are properly updated.
$info = entity_get_bundles('taxonomy_term');
$this->assertFalse(isset($info[$old_name]), 'The old bundle name does not appear in entity_get_bundles().');
$this->assertTrue(isset($info[$new_name]), 'The new bundle name appears in entity_get_bundles().');
// Check that the field instance is still attached to the vocabulary.
$this->assertTrue(field_info_instance('taxonomy_term', 'field_test', $new_name), 'The bundle name was updated correctly.');
}
/**
* 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_definition = array(
'name' => $this->field_name,
'entity_type' => 'taxonomy_term',
'type' => 'text',
'cardinality' => 4
);
entity_create('field_config', $this->field_definition)->save();
$this->instance_definition = array(
'field_name' => $this->field_name,
'entity_type' => 'taxonomy_term',
'bundle' => $this->vocabulary->id(),
'label' => $this->randomName() . '_label',
);
entity_create('field_instance_config', $this->instance_definition)->save();
require_once DRUPAL_ROOT . '/core/includes/install.inc';
module_uninstall(array('taxonomy'));
\Drupal::moduleHandler()->install(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.
$this->vocabulary->enforceIsNew();
$this->vocabulary->save();
entity_create('field_config', $this->field_definition)->save();
entity_create('field_instance_config', $this->instance_definition)->save();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment