View source
<?php
namespace Drupal\taxonomy\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\taxonomy\Entity\Vocabulary;
class VocabularyCrudTest extends TaxonomyTestBase {
public static $modules = array(
'field_test',
'taxonomy_crud',
);
protected function setUp() {
parent::setUp();
$admin_user = $this
->drupalCreateUser(array(
'create article content',
'administer taxonomy',
));
$this
->drupalLogin($admin_user);
$this->vocabulary = $this
->createVocabulary();
}
function testTaxonomyVocabularyDeleteWithTerms() {
foreach (Vocabulary::loadMultiple() as $vocabulary) {
$vocabulary
->delete();
}
$query = \Drupal::entityQuery('taxonomy_term')
->count();
$this
->assertEqual(0, $query
->execute(), 'There are no terms remaining.');
$terms = array();
for ($i = 0; $i < 5; $i++) {
$terms[$i] = $this
->createTerm($vocabulary);
}
$terms[2]->parent = array(
$terms[1]
->id(),
);
$terms[2]
->save();
$terms[4]->parent = array(
$terms[1]
->id(),
$terms[2]
->id(),
);
$terms[4]
->save();
$this
->assertEqual(5, $query
->execute(), 'There are 5 terms found.');
$vocabulary
->delete();
$this
->assertEqual(0, $query
->execute(), 'All terms are deleted.');
}
function testTaxonomyVocabularyLoadStaticReset() {
$original_vocabulary = Vocabulary::load($this->vocabulary
->id());
$this
->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.');
$this
->assertEqual($this->vocabulary
->label(), $original_vocabulary
->label(), 'Vocabulary loaded successfully.');
$vocabulary = $original_vocabulary;
$vocabulary
->set('name', $this
->randomMachineName());
$vocabulary
->set('description', $this
->randomMachineName());
$vocabulary
->save();
$new_vocabulary = Vocabulary::load($original_vocabulary
->id());
$this
->assertEqual($new_vocabulary
->label(), $vocabulary
->label(), 'The vocabulary was loaded.');
$this->vocabulary
->delete();
$vocabularies = Vocabulary::loadMultiple();
$this
->assertTrue(!isset($vocabularies[$this->vocabulary
->id()]), 'The vocabulary was deleted.');
}
function testTaxonomyVocabularyLoadMultiple() {
foreach (Vocabulary::loadMultiple() as $vocabulary) {
$vocabulary
->delete();
}
$vocabulary1 = $this
->createVocabulary();
$vocabulary1
->set('weight', 0);
$vocabulary1
->save();
$vocabulary2 = $this
->createVocabulary();
$vocabulary2
->set('weight', 1);
$vocabulary2
->save();
$vocabulary3 = $this
->createVocabulary();
$vocabulary3
->set('weight', 2);
$vocabulary3
->save();
$this
->assertEqual('bar', $vocabulary1
->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
$this
->assertEqual('bar', $vocabulary2
->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
$this
->assertEqual('bar', $vocabulary3
->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
$names = taxonomy_vocabulary_get_names();
$this
->assertEqual($names[$vocabulary1
->id()], $vocabulary1
->id(), 'Vocabulary 1 name found.');
$vocabularies = Vocabulary::loadMultiple(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);
$controller = $this->container
->get('entity.manager')
->getStorage('taxonomy_vocabulary');
$vocabulary = current($controller
->loadByProperties(array(
'name' => $vocabulary1
->label(),
)));
$this
->assertEqual($vocabulary
->id(), $vocabulary1
->id(), 'Vocabulary loaded successfully by name.');
$vocabulary = current($controller
->loadByProperties(array(
'name' => $vocabulary2
->label(),
'vid' => $vocabulary2
->id(),
)));
$this
->assertEqual($vocabulary
->id(), $vocabulary2
->id(), 'Vocabulary loaded successfully by name and ID.');
}
function testUninstallReinstall() {
$field_name = Unicode::strtolower($this
->randomMachineName() . '_field_name');
$storage_definition = array(
'field_name' => $field_name,
'entity_type' => 'taxonomy_term',
'type' => 'text',
'cardinality' => 4,
);
entity_create('field_storage_config', $storage_definition)
->save();
$field_definition = array(
'field_name' => $field_name,
'entity_type' => 'taxonomy_term',
'bundle' => $this->vocabulary
->id(),
'label' => $this
->randomMachineName() . '_label',
);
entity_create('field_config', $field_definition)
->save();
$this->vocabulary
->unsetThirdPartySetting('taxonomy_crud', 'foo');
require_once \Drupal::root() . '/core/includes/install.inc';
$this->container
->get('module_installer')
->uninstall(array(
'taxonomy',
));
$this->container
->get('module_installer')
->install(array(
'taxonomy',
));
$this->vocabulary
->enforceIsNew();
$this->vocabulary
->save();
entity_create('field_storage_config', $storage_definition)
->save();
entity_create('field_config', $field_definition)
->save();
}
}