function VocabularyCrudTest::testTaxonomyVocabularyLoadMultiple

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php \Drupal\Tests\taxonomy\Kernel\VocabularyCrudTest::testTaxonomyVocabularyLoadMultiple()
  2. 8.9.x core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php \Drupal\Tests\taxonomy\Kernel\VocabularyCrudTest::testTaxonomyVocabularyLoadMultiple()
  3. 11.x core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php \Drupal\Tests\taxonomy\Kernel\VocabularyCrudTest::testTaxonomyVocabularyLoadMultiple()

Tests for loading multiple vocabularies.

File

core/modules/taxonomy/tests/src/Kernel/VocabularyCrudTest.php, line 77

Class

VocabularyCrudTest
Tests loading, saving and deleting vocabularies.

Namespace

Drupal\Tests\taxonomy\Kernel

Code

public function testTaxonomyVocabularyLoadMultiple() : void {
  // Ensure there are no vocabularies.
  $this->assertEmpty(Vocabulary::loadMultiple());
  // Create some vocabularies and assign weights.
  $vocabulary1 = $this->createVocabulary();
  $vocabulary1->set('weight', 0);
  $vocabulary1->save();
  $vocabulary2 = $this->createVocabulary();
  $vocabulary2->set('weight', 1);
  $vocabulary2->save();
  $vocabulary3 = $this->createVocabulary();
  $vocabulary3->set('weight', 2);
  $vocabulary3->save();
  // Check if third party settings exist.
  $this->assertEquals('bar', $vocabulary1->getThirdPartySetting('taxonomy_crud', 'foo'));
  $this->assertEquals('bar', $vocabulary2->getThirdPartySetting('taxonomy_crud', 'foo'));
  $this->assertEquals('bar', $vocabulary3->getThirdPartySetting('taxonomy_crud', 'foo'));
  // Fetch the vocabularies with Vocabulary::loadMultiple(), specifying IDs.
  // Ensure they are returned in the same order as the original array.
  $vocabularies = Vocabulary::loadMultiple([
    $vocabulary3->id(),
    $vocabulary2->id(),
    $vocabulary1->id(),
  ]);
  $loaded_order = array_keys($vocabularies);
  $expected_order = [
    $vocabulary3->id(),
    $vocabulary2->id(),
    $vocabulary1->id(),
  ];
  $this->assertSame($expected_order, $loaded_order);
  // Test loading vocabularies by their properties.
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('taxonomy_vocabulary');
  // Fetch vocabulary 1 by name.
  $vocabulary = current($storage->loadByProperties([
    'name' => $vocabulary1->label(),
  ]));
  $this->assertEquals($vocabulary1->id(), $vocabulary->id());
  // Fetch vocabulary 2 by name and ID.
  $vocabulary = current($storage->loadByProperties([
    'name' => $vocabulary2->label(),
    'vid' => $vocabulary2->id(),
  ]));
  $this->assertEquals($vocabulary2->id(), $vocabulary->id());
}

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