annotate core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\taxonomy\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Url;
Chris@0 6 use Drupal\taxonomy\Entity\Vocabulary;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the taxonomy vocabulary interface.
Chris@0 10 *
Chris@0 11 * @group taxonomy
Chris@0 12 */
Chris@0 13 class VocabularyUiTest extends TaxonomyTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * The vocabulary used for creating terms.
Chris@0 17 *
Chris@0 18 * @var \Drupal\taxonomy\VocabularyInterface
Chris@0 19 */
Chris@0 20 protected $vocabulary;
Chris@0 21
Chris@0 22 protected function setUp() {
Chris@0 23 parent::setUp();
Chris@0 24 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
Chris@0 25 $this->vocabulary = $this->createVocabulary();
Chris@0 26 $this->drupalPlaceBlock('local_actions_block');
Chris@0 27 $this->drupalPlaceBlock('page_title_block');
Chris@0 28 }
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Create, edit and delete a vocabulary via the user interface.
Chris@0 32 */
Chris@0 33 public function testVocabularyInterface() {
Chris@0 34 // Visit the main taxonomy administration page.
Chris@0 35 $this->drupalGet('admin/structure/taxonomy');
Chris@0 36
Chris@0 37 // Create a new vocabulary.
Chris@0 38 $this->clickLink(t('Add vocabulary'));
Chris@0 39 $edit = [];
Chris@17 40 $vid = mb_strtolower($this->randomMachineName());
Chris@0 41 $edit['name'] = $this->randomMachineName();
Chris@0 42 $edit['description'] = $this->randomMachineName();
Chris@0 43 $edit['vid'] = $vid;
Chris@0 44 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 45 $this->assertRaw(t('Created new vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary created successfully.');
Chris@0 46
Chris@0 47 // Edit the vocabulary.
Chris@0 48 $this->drupalGet('admin/structure/taxonomy');
Chris@0 49 $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
Chris@0 50 $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
Chris@0 51 $this->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $edit['vid']])->toString());
Chris@0 52 $this->clickLink(t('Edit vocabulary'));
Chris@0 53 $edit = [];
Chris@0 54 $edit['name'] = $this->randomMachineName();
Chris@0 55 $edit['description'] = $this->randomMachineName();
Chris@0 56 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 57 $this->drupalGet('admin/structure/taxonomy');
Chris@0 58 $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
Chris@0 59 $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
Chris@0 60
Chris@0 61 // Try to submit a vocabulary with a duplicate machine name.
Chris@0 62 $edit['vid'] = $vid;
Chris@0 63 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
Chris@0 64 $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
Chris@0 65
Chris@0 66 // Try to submit an invalid machine name.
Chris@0 67 $edit['vid'] = '!&^%';
Chris@0 68 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
Chris@0 69 $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
Chris@0 70
Chris@0 71 // Ensure that vocabulary titles are escaped properly.
Chris@0 72 $edit = [];
Chris@0 73 $edit['name'] = 'Don\'t Panic';
Chris@0 74 $edit['description'] = $this->randomMachineName();
Chris@0 75 $edit['vid'] = 'don_t_panic';
Chris@0 76 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
Chris@0 77
Chris@0 78 $site_name = $this->config('system.site')->get('name');
Chris@0 79 $this->assertTitle(t("Don't Panic | @site-name", ['@site-name' => $site_name]), 'The page title contains the escaped character.');
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Changing weights on the vocabulary overview with two or more vocabularies.
Chris@0 84 */
Chris@0 85 public function testTaxonomyAdminChangingWeights() {
Chris@0 86 // Create some vocabularies.
Chris@0 87 for ($i = 0; $i < 10; $i++) {
Chris@0 88 $this->createVocabulary();
Chris@0 89 }
Chris@0 90 // Get all vocabularies and change their weights.
Chris@0 91 $vocabularies = Vocabulary::loadMultiple();
Chris@0 92 $edit = [];
Chris@0 93 foreach ($vocabularies as $key => $vocabulary) {
Chris@0 94 $weight = -$vocabulary->get('weight');
Chris@0 95 $vocabularies[$key]->set('weight', $weight);
Chris@0 96 $edit['vocabularies[' . $key . '][weight]'] = $weight;
Chris@0 97 }
Chris@0 98 // Saving the new weights via the interface.
Chris@0 99 $this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));
Chris@0 100
Chris@0 101 // Load the vocabularies from the database.
Chris@0 102 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
Chris@0 103 $new_vocabularies = Vocabulary::loadMultiple();
Chris@0 104
Chris@0 105 // Check that the weights are saved in the database correctly.
Chris@0 106 foreach ($vocabularies as $key => $vocabulary) {
Chris@0 107 $this->assertEqual($new_vocabularies[$key]->get('weight'), $vocabularies[$key]->get('weight'), 'The vocabulary weight was changed.');
Chris@0 108 }
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Test the vocabulary overview with no vocabularies.
Chris@0 113 */
Chris@0 114 public function testTaxonomyAdminNoVocabularies() {
Chris@0 115 // Delete all vocabularies.
Chris@0 116 $vocabularies = Vocabulary::loadMultiple();
Chris@0 117 foreach ($vocabularies as $key => $vocabulary) {
Chris@0 118 $vocabulary->delete();
Chris@0 119 }
Chris@0 120 // Confirm that no vocabularies are found in the database.
Chris@0 121 $this->assertFalse(Vocabulary::loadMultiple(), 'No vocabularies found.');
Chris@0 122 $this->drupalGet('admin/structure/taxonomy');
Chris@0 123 // Check the default message for no vocabularies.
Chris@0 124 $this->assertText(t('No vocabularies available.'));
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * Deleting a vocabulary.
Chris@0 129 */
Chris@0 130 public function testTaxonomyAdminDeletingVocabulary() {
Chris@0 131 // Create a vocabulary.
Chris@17 132 $vid = mb_strtolower($this->randomMachineName());
Chris@0 133 $edit = [
Chris@0 134 'name' => $this->randomMachineName(),
Chris@0 135 'vid' => $vid,
Chris@0 136 ];
Chris@0 137 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
Chris@0 138 $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
Chris@0 139
Chris@0 140 // Check the created vocabulary.
Chris@0 141 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
Chris@0 142 $vocabulary = Vocabulary::load($vid);
Chris@0 143 $this->assertTrue($vocabulary, 'Vocabulary found.');
Chris@0 144
Chris@0 145 // Delete the vocabulary.
Chris@0 146 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id());
Chris@0 147 $this->clickLink(t('Delete'));
Chris@0 148 $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', ['%name' => $vocabulary->label()]), '[confirm deletion] Asks for confirmation.');
Chris@0 149 $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.');
Chris@0 150
Chris@0 151 // Confirm deletion.
Chris@0 152 $this->drupalPostForm(NULL, NULL, t('Delete'));
Chris@0 153 $this->assertRaw(t('Deleted vocabulary %name.', ['%name' => $vocabulary->label()]), 'Vocabulary deleted.');
Chris@0 154 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
Chris@0 155 $this->assertFalse(Vocabulary::load($vid), 'Vocabulary not found.');
Chris@0 156 }
Chris@0 157
Chris@0 158 }