PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/taxonomy/src/Tests/VocabularyUiTest.php

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