You are here

protected function ScannerHelperTrait::createVocabulary in Search and Replace Scanner 8

Create a vocabulary.

Parameters

array $values: Items passed to the vocabulary. If the 'vid' item is not present it will be automatically generated. If the 'name' item is not present the 'vid' will be used.

Return value

\Drupal\taxonomy\VocabularyInterface|\Drupal\Core\Entity\EntityInterface A fully formatted vocabulary object.

Throws

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Functional/ScannerHelperTrait.php, line 66

Class

ScannerHelperTrait
Misc helper functions for the automated tests.

Namespace

Drupal\Tests\scanner\Functional

Code

protected function createVocabulary(array $values = []) {

  // Find a non-existent random type name.
  if (!isset($values['vid'])) {
    do {
      $id = strtolower($this
        ->randomMachineName(8));
    } while (Vocabulary::load($id));
  }
  else {
    $id = $values['vid'];
  }
  $values += [
    'vid' => $id,
    'name' => $id,
  ];
  $vocab = Vocabulary::create($values);
  $status = $vocab
    ->save();
  if ($this instanceof \PHPUnit_Framework_TestCase) {
    $this
      ->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', [
      '%type' => $vocab
        ->id(),
    ]))
      ->__toString());
  }
  else {
    $this
      ->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', [
      '%type' => $vocab
        ->id(),
    ]))
      ->__toString());
  }
  return $vocab;
}