Commit 202ad58e authored by catch's avatar catch

Issue #2473681 by Manuel Garcia, yogeshmpawar, claudiu.cristea, andypost,...

Issue #2473681 by Manuel Garcia, yogeshmpawar, claudiu.cristea, andypost, naveenvalecha: Convert VocabularyCrudTest test to kernel test
parent 987c9896
<?php <?php
namespace Drupal\Tests\taxonomy\Functional; namespace Drupal\Tests\taxonomy\Kernel;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/** /**
* Tests loading, saving and deleting vocabularies. * Tests loading, saving and deleting vocabularies.
* *
* @group taxonomy * @group taxonomy
*/ */
class VocabularyCrudTest extends TaxonomyTestBase { class VocabularyCrudTest extends KernelTestBase {
use TaxonomyTestTrait;
/** /**
* Modules to enable. * {@inheritdoc}
*
* @var array
*/ */
public static $modules = ['field_test', 'taxonomy_crud']; protected static $modules = [
'field',
'filter',
'system',
'taxonomy',
'taxonomy_crud',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->installSchema('user', ['users_data']);
$admin_user = $this->drupalCreateUser(['create article content', 'administer taxonomy']); $this->installEntitySchema('taxonomy_term');
$this->drupalLogin($admin_user);
$this->vocabulary = $this->createVocabulary();
} }
/** /**
* Test deleting a taxonomy that contains terms. * Test deleting a taxonomy that contains terms.
*/ */
public function testTaxonomyVocabularyDeleteWithTerms() { public function testTaxonomyVocabularyDeleteWithTerms() {
// Delete any existing vocabularies. $vocabulary = $this->createVocabulary();
foreach (Vocabulary::loadMultiple() as $vocabulary) {
$vocabulary->delete();
}
$query = \Drupal::entityQuery('taxonomy_term')->count(); $query = \Drupal::entityQuery('taxonomy_term')->count();
// Assert that there are no terms left. // Assert that there are no terms left.
$this->assertEqual(0, $query->execute(), 'There are no terms remaining.'); $this->assertEquals(0, $query->execute());
$terms = []; $terms = [];
for ($i = 0; $i < 5; $i++) { for ($i = 0; $i < 5; $i++) {
...@@ -53,47 +61,20 @@ public function testTaxonomyVocabularyDeleteWithTerms() { ...@@ -53,47 +61,20 @@ public function testTaxonomyVocabularyDeleteWithTerms() {
$terms[4]->save(); $terms[4]->save();
// Assert that there are now 5 terms. // Assert that there are now 5 terms.
$this->assertEqual(5, $query->execute(), 'There are 5 terms found.'); $this->assertEquals(5, $query->execute());
$vocabulary->delete(); $vocabulary->delete();
// Assert that there are no terms left. // Assert that there are no terms left.
$this->assertEqual(0, $query->execute(), 'All terms are deleted.'); $this->assertEquals(0, $query->execute());
}
/**
* Ensure that the vocabulary static reset works correctly.
*/
public 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.');
// Change the name and description.
$vocabulary = $original_vocabulary;
$vocabulary->set('name', $this->randomMachineName());
$vocabulary->set('description', $this->randomMachineName());
$vocabulary->save();
// Load the vocabulary.
$new_vocabulary = Vocabulary::load($original_vocabulary->id());
$this->assertEqual($new_vocabulary->label(), $vocabulary->label(), 'The vocabulary was loaded.');
// Delete the vocabulary.
$this->vocabulary->delete();
$vocabularies = Vocabulary::loadMultiple();
$this->assertTrue(!isset($vocabularies[$this->vocabulary->id()]), 'The vocabulary was deleted.');
} }
/** /**
* Tests for loading multiple vocabularies. * Tests for loading multiple vocabularies.
*/ */
public function testTaxonomyVocabularyLoadMultiple() { public function testTaxonomyVocabularyLoadMultiple() {
// Ensure there are no vocabularies.
// Delete any existing vocabularies. $this->assertEmpty(Vocabulary::loadMultiple());
foreach (Vocabulary::loadMultiple() as $vocabulary) {
$vocabulary->delete();
}
// Create some vocabularies and assign weights. // Create some vocabularies and assign weights.
$vocabulary1 = $this->createVocabulary(); $vocabulary1 = $this->createVocabulary();
...@@ -107,40 +88,49 @@ public function testTaxonomyVocabularyLoadMultiple() { ...@@ -107,40 +88,49 @@ public function testTaxonomyVocabularyLoadMultiple() {
$vocabulary3->save(); $vocabulary3->save();
// Check if third party settings exist. // Check if third party settings exist.
$this->assertEqual('bar', $vocabulary1->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); $this->assertEquals('bar', $vocabulary1->getThirdPartySetting('taxonomy_crud', 'foo'));
$this->assertEqual('bar', $vocabulary2->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); $this->assertEquals('bar', $vocabulary2->getThirdPartySetting('taxonomy_crud', 'foo'));
$this->assertEqual('bar', $vocabulary3->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); $this->assertEquals('bar', $vocabulary3->getThirdPartySetting('taxonomy_crud', 'foo'));
// Fetch the names for all vocabularies, confirm that they are keyed by // Fetch the names for all vocabularies, confirm that they are keyed by
// machine name. // machine name.
$names = taxonomy_vocabulary_get_names(); $names = taxonomy_vocabulary_get_names();
$this->assertEqual($names[$vocabulary1->id()], $vocabulary1->id(), 'Vocabulary 1 name found.'); $this->assertEquals($vocabulary1->id(), $names[$vocabulary1->id()]);
// Fetch the vocabularies with entity_load_multiple(), specifying IDs. // Fetch the vocabularies with Vocabulary::loadMultiple(), specifying IDs.
// Ensure they are returned in the same order as the original array. // Ensure they are returned in the same order as the original array.
$vocabularies = Vocabulary::loadMultiple([$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()]); $vocabularies = Vocabulary::loadMultiple([
$vocabulary3->id(),
$vocabulary2->id(),
$vocabulary1->id(),
]);
$loaded_order = array_keys($vocabularies); $loaded_order = array_keys($vocabularies);
$expected_order = [$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()]; $expected_order = [
$this->assertIdentical($loaded_order, $expected_order); $vocabulary3->id(),
$vocabulary2->id(),
$vocabulary1->id(),
];
$this->assertSame($expected_order, $loaded_order);
// Test loading vocabularies by their properties. // Test loading vocabularies by their properties.
$controller = $this->container->get('entity_type.manager')->getStorage('taxonomy_vocabulary'); $storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_vocabulary');
// Fetch vocabulary 1 by name. // Fetch vocabulary 1 by name.
$vocabulary = current($controller->loadByProperties(['name' => $vocabulary1->label()])); $vocabulary = current($storage->loadByProperties(['name' => $vocabulary1->label()]));
$this->assertEqual($vocabulary->id(), $vocabulary1->id(), 'Vocabulary loaded successfully by name.'); $this->assertEquals($vocabulary1->id(), $vocabulary->id());
// Fetch vocabulary 2 by name and ID. // Fetch vocabulary 2 by name and ID.
$vocabulary = current($controller->loadByProperties([ $vocabulary = current($storage->loadByProperties([
'name' => $vocabulary2->label(), 'name' => $vocabulary2->label(),
'vid' => $vocabulary2->id(), 'vid' => $vocabulary2->id(),
])); ]));
$this->assertEqual($vocabulary->id(), $vocabulary2->id(), 'Vocabulary loaded successfully by name and ID.'); $this->assertEquals($vocabulary2->id(), $vocabulary->id());
} }
/** /**
* Test uninstall and reinstall of the taxonomy module. * Test uninstall and reinstall of the taxonomy module.
*/ */
public function testUninstallReinstall() { public function testUninstallReinstall() {
$vocabulary = $this->createVocabulary();
// Field storages and fields attached to taxonomy term bundles should be // Field storages and fields attached to taxonomy term bundles should be
// removed when the module is uninstalled. // removed when the module is uninstalled.
$field_name = mb_strtolower($this->randomMachineName() . '_field_name'); $field_name = mb_strtolower($this->randomMachineName() . '_field_name');
...@@ -154,7 +144,7 @@ public function testUninstallReinstall() { ...@@ -154,7 +144,7 @@ public function testUninstallReinstall() {
$field_definition = [ $field_definition = [
'field_name' => $field_name, 'field_name' => $field_name,
'entity_type' => 'taxonomy_term', 'entity_type' => 'taxonomy_term',
'bundle' => $this->vocabulary->id(), 'bundle' => $vocabulary->id(),
'label' => $this->randomMachineName() . '_label', 'label' => $this->randomMachineName() . '_label',
]; ];
FieldConfig::create($field_definition)->save(); FieldConfig::create($field_definition)->save();
...@@ -162,18 +152,16 @@ public function testUninstallReinstall() { ...@@ -162,18 +152,16 @@ public function testUninstallReinstall() {
// Remove the third party setting from the memory copy of the vocabulary. // Remove the third party setting from the memory copy of the vocabulary.
// We keep this invalid copy around while the taxonomy module is not even // We keep this invalid copy around while the taxonomy module is not even
// installed for testing below. // installed for testing below.
$this->vocabulary->unsetThirdPartySetting('taxonomy_crud', 'foo'); $vocabulary->unsetThirdPartySetting('taxonomy_crud', 'foo');
require_once $this->root . '/core/includes/install.inc';
$this->container->get('module_installer')->uninstall(['taxonomy']); $this->container->get('module_installer')->uninstall(['taxonomy']);
$this->container->get('module_installer')->install(['taxonomy']); $this->container->get('module_installer')->install(['taxonomy']);
// Now create a vocabulary with the same name. All fields // Now create a vocabulary with the same name. All fields connected to this
// connected to this vocabulary name should have been removed when the // vocabulary name should have been removed when the module was uninstalled.
// module was uninstalled. Creating a new field with the same name and // Creating a new field with the same name and an instance of this field on
// an instance of this field on the same bundle name should be successful. // the same bundle name should be successful.
$this->vocabulary->enforceIsNew(); $vocabulary->enforceIsNew()->save();
$this->vocabulary->save();
FieldStorageConfig::create($storage_definition)->save(); FieldStorageConfig::create($storage_definition)->save();
FieldConfig::create($field_definition)->save(); FieldConfig::create($field_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