function VocabularyUnitTest::testTaxonomyVocabularyLoadReturnFalse

Ensure that when an invalid vocabulary vid is loaded, it is possible to load the same vid successfully if it subsequently becomes valid.

File

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

Class

VocabularyUnitTest
Tests for taxonomy vocabulary functions.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyVocabularyLoadReturnFalse() {

  // Load a vocabulary that doesn't exist.
  $vocabularies = taxonomy_vocabulary_load_multiple();
  $vid = count($vocabularies) + 1;
  $vocabulary = taxonomy_vocabulary_load($vid);

  // This should not return an object because no such vocabulary exists.
  $this
    ->assertTrue(empty($vocabulary), 'No object loaded.');

  // Create a new vocabulary.
  $this
    ->createVocabulary();

  // Load the vocabulary with the same $vid from earlier.
  // This should return a vocabulary object since it now matches a real vid.
  $vocabulary = taxonomy_vocabulary_load($vid);
  $this
    ->assertTrue(!empty($vocabulary) && is_object($vocabulary), 'Vocabulary is an object.');
  $this
    ->assertEqual($vocabulary->vid, $vid, 'Valid vocabulary vid is the same as our previously invalid one.');
}