function VocabularyUiTest::testTaxonomyAdminChangingWeights

Same name and namespace in other branches
  1. 8.9.x core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php \Drupal\Tests\taxonomy\Functional\VocabularyUiTest::testTaxonomyAdminChangingWeights()
  2. 10 core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php \Drupal\Tests\taxonomy\Functional\VocabularyUiTest::testTaxonomyAdminChangingWeights()
  3. 11.x 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 96

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->drupalGet('admin/structure/taxonomy');
    $this->submitForm($edit, '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->assertEquals($new_vocabularies[$key]->get('weight'), $vocabularies[$key]->get('weight'), 'The vocabulary weight was changed.');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.