function VocabularyUnitTest::testTaxonomyVocabularyLoadMultiple

Tests for loading multiple vocabularies.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php, line 100
Definition of Drupal\taxonomy\Tests\VocabularyUnitTest.

Class

VocabularyUnitTest
Tests for taxonomy vocabulary functions.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyVocabularyLoadMultiple() {

  // Delete any existing vocabularies.
  foreach (taxonomy_vocabulary_load_multiple() as $vocabulary) {
    $vocabulary
      ->delete();
  }

  // Create some vocabularies and assign weights.
  $vocabulary1 = $this
    ->createVocabulary();
  $vocabulary1->weight = 0;
  $vocabulary1
    ->save();
  $vocabulary2 = $this
    ->createVocabulary();
  $vocabulary2->weight = 1;
  $vocabulary2
    ->save();
  $vocabulary3 = $this
    ->createVocabulary();
  $vocabulary3->weight = 2;
  $vocabulary3
    ->save();

  // Fetch the names for all vocabularies, confirm that they are keyed by
  // machine name.
  $names = taxonomy_vocabulary_get_names();
  $this
    ->assertEqual($names[$vocabulary1
    ->id()], $vocabulary1
    ->id(), 'Vocabulary 1 name found.');

  // Fetch all of the vocabularies using taxonomy_vocabulary_load_multiple().
  // Confirm that the vocabularies are ordered by weight.
  $vocabularies = taxonomy_vocabulary_load_multiple();
  taxonomy_vocabulary_sort($vocabularies);
  $this
    ->assertEqual(array_shift($vocabularies)
    ->id(), $vocabulary1
    ->id(), 'Vocabulary was found in the vocabularies array.');
  $this
    ->assertEqual(array_shift($vocabularies)
    ->id(), $vocabulary2
    ->id(), 'Vocabulary was found in the vocabularies array.');
  $this
    ->assertEqual(array_shift($vocabularies)
    ->id(), $vocabulary3
    ->id(), 'Vocabulary was found in the vocabularies array.');

  // Fetch the vocabularies with taxonomy_vocabulary_load_multiple(), specifying IDs.
  // Ensure they are returned in the same order as the original array.
  $vocabularies = taxonomy_vocabulary_load_multiple(array(
    $vocabulary3
      ->id(),
    $vocabulary2
      ->id(),
    $vocabulary1
      ->id(),
  ));
  $this
    ->assertEqual(array_shift($vocabularies)
    ->id(), $vocabulary3
    ->id(), 'Vocabulary loaded successfully by ID.');
  $this
    ->assertEqual(array_shift($vocabularies)
    ->id(), $vocabulary2
    ->id(), 'Vocabulary loaded successfully by ID.');
  $this
    ->assertEqual(array_shift($vocabularies)
    ->id(), $vocabulary1
    ->id(), 'Vocabulary loaded successfully by ID.');

  // Test loading vocabularies by their properties.
  $controller = $this->container
    ->get('plugin.manager.entity')
    ->getStorageController('taxonomy_vocabulary');

  // Fetch vocabulary 1 by name.
  $vocabulary = current($controller
    ->loadByProperties(array(
    'name' => $vocabulary1->name,
  )));
  $this
    ->assertEqual($vocabulary
    ->id(), $vocabulary1
    ->id(), 'Vocabulary loaded successfully by name.');

  // Fetch vocabulary 2 by name and ID.
  $vocabulary = current($controller
    ->loadByProperties(array(
    'name' => $vocabulary2->name,
    'vid' => $vocabulary2
      ->id(),
  )));
  $this
    ->assertEqual($vocabulary
    ->id(), $vocabulary2
    ->id(), 'Vocabulary loaded successfully by name and ID.');
}