View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Vocabulary;
class VocabularyUiTest extends TaxonomyTestBase {
protected $vocabulary;
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
]));
$this->vocabulary = $this
->createVocabulary();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('page_title_block');
}
public function testVocabularyInterface() {
$this
->drupalGet('admin/structure/taxonomy');
$this
->clickLink(t('Add vocabulary'));
$edit = [];
$vid = mb_strtolower($this
->randomMachineName());
$edit['name'] = $this
->randomMachineName();
$edit['description'] = $this
->randomMachineName();
$edit['vid'] = $vid;
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertRaw(t('Created new vocabulary %name.', [
'%name' => $edit['name'],
]), 'Vocabulary created successfully.');
$this
->drupalGet('admin/structure/taxonomy');
$this
->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
$this
->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
$this
->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', [
'taxonomy_vocabulary' => $edit['vid'],
])
->toString());
$this
->clickLink(t('Edit vocabulary'));
$edit = [];
$edit['name'] = $this
->randomMachineName();
$edit['description'] = $this
->randomMachineName();
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->drupalGet('admin/structure/taxonomy');
$this
->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
$this
->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
$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.'));
$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.'));
$edit = [];
$edit['name'] = 'Don\'t Panic';
$edit['description'] = $this
->randomMachineName();
$edit['vid'] = 'don_t_panic';
$this
->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
$site_name = $this
->config('system.site')
->get('name');
$this
->assertTitle("Don't Panic | {$site_name}");
}
public function testTaxonomyAdminChangingWeights() {
for ($i = 0; $i < 10; $i++) {
$this
->createVocabulary();
}
$vocabularies = Vocabulary::loadMultiple();
$edit = [];
foreach ($vocabularies as $key => $vocabulary) {
$weight = -$vocabulary
->get('weight');
$vocabularies[$key]
->set('weight', $weight);
$edit['vocabularies[' . $key . '][weight]'] = $weight;
}
$this
->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));
$this->container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary')
->resetCache();
$new_vocabularies = Vocabulary::loadMultiple();
foreach ($vocabularies as $key => $vocabulary) {
$this
->assertEqual($new_vocabularies[$key]
->get('weight'), $vocabularies[$key]
->get('weight'), 'The vocabulary weight was changed.');
}
}
public function testTaxonomyAdminNoVocabularies() {
$vocabularies = Vocabulary::loadMultiple();
foreach ($vocabularies as $key => $vocabulary) {
$vocabulary
->delete();
}
$this
->assertEmpty(Vocabulary::loadMultiple(), 'No vocabularies found.');
$this
->drupalGet('admin/structure/taxonomy');
$this
->assertText(t('No vocabularies available.'));
}
public function testTaxonomyAdminDeletingVocabulary() {
$vid = mb_strtolower($this
->randomMachineName());
$edit = [
'name' => $this
->randomMachineName(),
'vid' => $vid,
];
$this
->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
$this
->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
$this->container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary')
->resetCache();
$vocabulary = Vocabulary::load($vid);
$this
->assertNotEmpty($vocabulary, 'Vocabulary found.');
$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?', [
'%name' => $vocabulary
->label(),
]), '[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.');
$this
->drupalPostForm(NULL, NULL, t('Delete'));
$this
->assertRaw(t('Deleted vocabulary %name.', [
'%name' => $vocabulary
->label(),
]), 'Vocabulary deleted.');
$this->container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary')
->resetCache();
$this
->assertNull(Vocabulary::load($vid), 'Vocabulary not found.');
}
}