VocabularyUiTest.php 6.37 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\taxonomy\Functional;
4

5
use Drupal\Core\Url;
6 7
use Drupal\taxonomy\Entity\Vocabulary;

8 9
/**
 * Tests the taxonomy vocabulary interface.
10 11
 *
 * @group taxonomy
12
 */
13
class VocabularyUiTest extends TaxonomyTestBase {
14

15 16 17 18 19 20 21
  /**
   * The vocabulary used for creating terms.
   *
   * @var \Drupal\taxonomy\VocabularyInterface
   */
  protected $vocabulary;

22 23 24 25 26
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

27
  protected function setUp(): void {
28
    parent::setUp();
29
    $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
30
    $this->vocabulary = $this->createVocabulary();
31
    $this->drupalPlaceBlock('local_actions_block');
32
    $this->drupalPlaceBlock('page_title_block');
33 34 35 36 37
  }

  /**
   * Create, edit and delete a vocabulary via the user interface.
   */
38
  public function testVocabularyInterface() {
39 40 41 42 43
    // Visit the main taxonomy administration page.
    $this->drupalGet('admin/structure/taxonomy');

    // Create a new vocabulary.
    $this->clickLink(t('Add vocabulary'));
44
    $edit = [];
45
    $vid = mb_strtolower($this->randomMachineName());
46 47
    $edit['name'] = $this->randomMachineName();
    $edit['description'] = $this->randomMachineName();
48 49
    $edit['vid'] = $vid;
    $this->drupalPostForm(NULL, $edit, t('Save'));
50
    $this->assertRaw(t('Created new vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary created successfully.');
51 52 53

    // Edit the vocabulary.
    $this->drupalGet('admin/structure/taxonomy');
54 55
    $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
    $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
56
    $this->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $edit['vid']])->toString());
57
    $this->clickLink(t('Edit vocabulary'));
58
    $edit = [];
59
    $edit['name'] = $this->randomMachineName();
60
    $edit['description'] = $this->randomMachineName();
61 62
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->drupalGet('admin/structure/taxonomy');
63 64
    $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
    $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
65 66 67 68 69 70 71 72 73 74 75 76

    // Try to submit a vocabulary with a duplicate machine name.
    $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.'));

    // Try to submit an invalid machine name.
    $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.'));

    // Ensure that vocabulary titles are escaped properly.
77
    $edit = [];
78
    $edit['name'] = 'Don\'t Panic';
79
    $edit['description'] = $this->randomMachineName();
80 81 82
    $edit['vid'] = 'don_t_panic';
    $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));

83
    $site_name = $this->config('system.site')->get('name');
84
    $this->assertTitle("Don't Panic | $site_name");
85 86 87 88 89
  }

  /**
   * Changing weights on the vocabulary overview with two or more vocabularies.
   */
90
  public function testTaxonomyAdminChangingWeights() {
91 92 93 94 95
    // Create some vocabularies.
    for ($i = 0; $i < 10; $i++) {
      $this->createVocabulary();
    }
    // Get all vocabularies and change their weights.
96
    $vocabularies = Vocabulary::loadMultiple();
97
    $edit = [];
98
    foreach ($vocabularies as $key => $vocabulary) {
99 100
      $weight = -$vocabulary->get('weight');
      $vocabularies[$key]->set('weight', $weight);
101 102 103 104 105 106
      $edit['vocabularies[' . $key . '][weight]'] = $weight;
    }
    // Saving the new weights via the interface.
    $this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));

    // Load the vocabularies from the database.
107
    $this->container->get('entity_type.manager')->getStorage('taxonomy_vocabulary')->resetCache();
108
    $new_vocabularies = Vocabulary::loadMultiple();
109 110 111

    // Check that the weights are saved in the database correctly.
    foreach ($vocabularies as $key => $vocabulary) {
112
      $this->assertEqual($new_vocabularies[$key]->get('weight'), $vocabularies[$key]->get('weight'), 'The vocabulary weight was changed.');
113 114 115 116 117 118
    }
  }

  /**
   * Test the vocabulary overview with no vocabularies.
   */
119
  public function testTaxonomyAdminNoVocabularies() {
120
    // Delete all vocabularies.
121
    $vocabularies = Vocabulary::loadMultiple();
122 123 124 125
    foreach ($vocabularies as $key => $vocabulary) {
      $vocabulary->delete();
    }
    // Confirm that no vocabularies are found in the database.
126
    $this->assertEmpty(Vocabulary::loadMultiple(), 'No vocabularies found.');
127 128 129 130 131 132 133 134
    $this->drupalGet('admin/structure/taxonomy');
    // Check the default message for no vocabularies.
    $this->assertText(t('No vocabularies available.'));
  }

  /**
   * Deleting a vocabulary.
   */
135
  public function testTaxonomyAdminDeletingVocabulary() {
136
    // Create a vocabulary.
137
    $vid = mb_strtolower($this->randomMachineName());
138
    $edit = [
139
      'name' => $this->randomMachineName(),
140
      'vid' => $vid,
141
    ];
142 143 144 145
    $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');

    // Check the created vocabulary.
146
    $this->container->get('entity_type.manager')->getStorage('taxonomy_vocabulary')->resetCache();
147
    $vocabulary = Vocabulary::load($vid);
148
    $this->assertNotEmpty($vocabulary, 'Vocabulary found.');
149 150 151 152

    // Delete the vocabulary.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id());
    $this->clickLink(t('Delete'));
153
    $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', ['%name' => $vocabulary->label()]), '[confirm deletion] Asks for confirmation.');
154 155 156 157
    $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.');

    // Confirm deletion.
    $this->drupalPostForm(NULL, NULL, t('Delete'));
158
    $this->assertRaw(t('Deleted vocabulary %name.', ['%name' => $vocabulary->label()]), 'Vocabulary deleted.');
159
    $this->container->get('entity_type.manager')->getStorage('taxonomy_vocabulary')->resetCache();
160
    $this->assertNull(Vocabulary::load($vid), 'Vocabulary not found.');
161
  }
162

163
}