VocabularyTest.php 6.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php

/**
 * @file
 * Definition of Drupal\taxonomy\Tests\VocabularyTest.
 */

namespace Drupal\taxonomy\Tests;

/**
 * Tests the taxonomy vocabulary interface.
 */
class VocabularyTest extends TaxonomyTestBase {

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy vocabulary interface',
      'description' => 'Test the taxonomy vocabulary interface.',
      'group' => 'Taxonomy',
    );
  }

  function setUp() {
    parent::setUp();
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
    $this->drupalLogin($this->admin_user);
    $this->vocabulary = $this->createVocabulary();
  }

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

    // Create a new vocabulary.
    $this->clickLink(t('Add vocabulary'));
    $edit = array();
40
    $vid = drupal_strtolower($this->randomName());
41 42
    $edit['name'] = $this->randomName();
    $edit['description'] = $this->randomName();
43
    $edit['vid'] = $vid;
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), 'Vocabulary created successfully.');

    // Edit the vocabulary.
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');
    $this->clickLink(t('edit vocabulary'));
    $edit = array();
    $edit['name'] = $this->randomName();
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');

    // Try to submit a vocabulary with a duplicate machine name.
58
    $edit['vid'] = $vid;
59 60 61 62
    $this->drupalPost('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.
63
    $edit['vid'] = '!&^%';
64 65
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
66 67 68 69 70

    // Ensure that vocabulary titles are escaped properly.
    $edit = array();
    $edit['name'] = 'Don\'t Panic';
    $edit['description'] = $this->randomName();
71
    $edit['vid'] = 'don_t_panic';
72 73 74 75 76
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));

    $site_name = config('system.site')->get('name');
    $this->assertTitle(t('Don\'t Panic | @site-name', array('@site-name' => $site_name)));
    $this->assertNoTitle(t('Don&#039;t Panic | @site-name', array('@site-name' => $site_name)));
77 78 79 80 81 82 83 84 85 86 87
  }

  /**
   * Changing weights on the vocabulary overview with two or more vocabularies.
   */
  function testTaxonomyAdminChangingWeights() {
    // Create some vocabularies.
    for ($i = 0; $i < 10; $i++) {
      $this->createVocabulary();
    }
    // Get all vocabularies and change their weights.
88
    $vocabularies = taxonomy_vocabulary_load_multiple();
89 90
    $edit = array();
    foreach ($vocabularies as $key => $vocabulary) {
91 92
      $weight = -$vocabulary->weight;
      $vocabularies[$key]->weight = $weight;
93
      $edit['vocabularies[' . $key . '][weight]'] = $weight;
94 95 96 97 98
    }
    // Saving the new weights via the interface.
    $this->drupalPost('admin/structure/taxonomy', $edit, t('Save'));

    // Load the vocabularies from the database.
99
    $this->container->get('plugin.manager.entity')->getStorageController('taxonomy_vocabulary')->resetCache();
100
    $new_vocabularies = taxonomy_vocabulary_load_multiple();
101
    taxonomy_vocabulary_sort($new_vocabularies);
102 103 104 105 106 107 108 109 110 111 112 113

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

  /**
   * Test the vocabulary overview with no vocabularies.
   */
  function testTaxonomyAdminNoVocabularies() {
    // Delete all vocabularies.
114
    $vocabularies = taxonomy_vocabulary_load_multiple();
115
    foreach ($vocabularies as $key => $vocabulary) {
116
      $vocabulary->delete();
117 118
    }
    // Confirm that no vocabularies are found in the database.
119
    $this->assertFalse(taxonomy_vocabulary_load_multiple(), 'No vocabularies found.');
120 121
    $this->drupalGet('admin/structure/taxonomy');
    // Check the default message for no vocabularies.
122
    $this->assertText(t('No vocabularies available.'));
123 124 125 126 127 128 129
  }

  /**
   * Deleting a vocabulary.
   */
  function testTaxonomyAdminDeletingVocabulary() {
    // Create a vocabulary.
130
    $vid = drupal_strtolower($this->randomName());
131 132
    $edit = array(
      'name' => $this->randomName(),
133
      'vid' => $vid,
134 135 136 137 138
    );
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');

    // Check the created vocabulary.
139
    $this->container->get('plugin.manager.entity')->getStorageController('taxonomy_vocabulary')->resetCache();
140
    $vocabulary = taxonomy_vocabulary_load($vid);
141
    $this->assertTrue($vocabulary, 'Vocabulary found.');
142 143 144

    // Delete the vocabulary.
    $edit = array();
145
    $this->drupalPost('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/edit', $edit, t('Delete'));
146 147 148 149 150 151
    $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), '[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.');

    // Confirm deletion.
    $this->drupalPost(NULL, NULL, t('Delete'));
    $this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), 'Vocabulary deleted.');
152
    $this->container->get('plugin.manager.entity')->getStorageController('taxonomy_vocabulary')->resetCache();
153
    $this->assertFalse(taxonomy_vocabulary_load($vid), t('Vocabulary not found.'));
154 155
  }
}