View source
<?php
namespace Drupal\Tests\taxonomy\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class VocabularyCrudTest extends KernelTestBase {
use TaxonomyTestTrait;
protected static $modules = [
'field',
'filter',
'system',
'taxonomy',
'taxonomy_crud',
'text',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('user', [
'users_data',
]);
$this
->installEntitySchema('taxonomy_term');
}
public function testTaxonomyVocabularyDeleteWithTerms() {
$vocabulary = $this
->createVocabulary();
$query = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE)
->count();
$this
->assertEquals(0, $query
->execute());
$terms = [];
for ($i = 0; $i < 5; $i++) {
$terms[$i] = $this
->createTerm($vocabulary);
}
$terms[2]->parent = [
$terms[1]
->id(),
];
$terms[2]
->save();
$terms[4]->parent = [
$terms[1]
->id(),
$terms[2]
->id(),
];
$terms[4]
->save();
$this
->assertEquals(5, $query
->execute());
$vocabulary
->delete();
$this
->assertEquals(0, $query
->execute());
}
public function testTaxonomyVocabularyLoadMultiple() {
$this
->assertEmpty(Vocabulary::loadMultiple());
$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
->assertEquals('bar', $vocabulary1
->getThirdPartySetting('taxonomy_crud', 'foo'));
$this
->assertEquals('bar', $vocabulary2
->getThirdPartySetting('taxonomy_crud', 'foo'));
$this
->assertEquals('bar', $vocabulary3
->getThirdPartySetting('taxonomy_crud', 'foo'));
$vocabularies = Vocabulary::loadMultiple([
$vocabulary3
->id(),
$vocabulary2
->id(),
$vocabulary1
->id(),
]);
$loaded_order = array_keys($vocabularies);
$expected_order = [
$vocabulary3
->id(),
$vocabulary2
->id(),
$vocabulary1
->id(),
];
$this
->assertSame($expected_order, $loaded_order);
$storage = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary');
$vocabulary = current($storage
->loadByProperties([
'name' => $vocabulary1
->label(),
]));
$this
->assertEquals($vocabulary1
->id(), $vocabulary
->id());
$vocabulary = current($storage
->loadByProperties([
'name' => $vocabulary2
->label(),
'vid' => $vocabulary2
->id(),
]));
$this
->assertEquals($vocabulary2
->id(), $vocabulary
->id());
}
public function testUninstallReinstall() {
$vocabulary = $this
->createVocabulary();
$field_name = mb_strtolower($this
->randomMachineName() . '_field_name');
$storage_definition = [
'field_name' => $field_name,
'entity_type' => 'taxonomy_term',
'type' => 'text',
'cardinality' => 4,
];
FieldStorageConfig::create($storage_definition)
->save();
$field_definition = [
'field_name' => $field_name,
'entity_type' => 'taxonomy_term',
'bundle' => $vocabulary
->id(),
'label' => $this
->randomMachineName() . '_label',
];
FieldConfig::create($field_definition)
->save();
$vocabulary
->unsetThirdPartySetting('taxonomy_crud', 'foo');
$this->container
->get('module_installer')
->uninstall([
'taxonomy',
]);
$this->container
->get('module_installer')
->install([
'taxonomy',
]);
$vocabulary
->enforceIsNew()
->save();
FieldStorageConfig::create($storage_definition)
->save();
FieldConfig::create($field_definition)
->save();
}
}