You are here

public function VocabularyUiTest::testTaxonomyAdminChangingWeights in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php \Drupal\Tests\taxonomy\Functional\VocabularyUiTest::testTaxonomyAdminChangingWeights()

Changing weights on the vocabulary overview with two or more vocabularies.

File

core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php, line 90

Class

VocabularyUiTest
Tests the taxonomy vocabulary interface.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTaxonomyAdminChangingWeights() {

  // Create some vocabularies.
  for ($i = 0; $i < 10; $i++) {
    $this
      ->createVocabulary();
  }

  // Get all vocabularies and change their weights.
  $vocabularies = Vocabulary::loadMultiple();
  $edit = [];
  foreach ($vocabularies as $key => $vocabulary) {
    $weight = -$vocabulary
      ->get('weight');
    $vocabularies[$key]
      ->set('weight', $weight);
    $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.
  $this->container
    ->get('entity_type.manager')
    ->getStorage('taxonomy_vocabulary')
    ->resetCache();
  $new_vocabularies = Vocabulary::loadMultiple();

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