You are here

trait MigrationTestTrait in Workbench Moderation to Content Moderation 8.2

Hierarchy

2 files declare their use of MigrationTestTrait
TestBase.php in tests/src/Functional/TestBase.php
TestBase.php in tests/src/Kernel/Migration/TestBase.php

File

tests/src/Kernel/MigrationTestTrait.php, line 9

Namespace

Drupal\Tests\wbm2cm\Kernel
View source
trait MigrationTestTrait {
  use ContentGenerationTrait;

  /**
   * The storage handler for the entity type under test.
   *
   * This is expected to be set by consumers' setUp() methods.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * Creates a set of revisions of a single entity.
   *
   * Each revision will be translated into every available language, and each
   * translation will be assigned a randomly chosen moderation state.
   *
   * @param int $number
   *   (optional) How many revisions to create. Defaults to 50.
   *
   * @return array
   *   An array of arrays, keyed by revision ID. Each inner array is a set of
   *   assigned moderation states, keyed by translation language.
   */
  protected function createRevisions($number = 50) {
    $keys = $this->storage
      ->getEntityType()
      ->getKeys();
    $values = [
      $keys['bundle'] => $this
        ->randomBundle(),
      $keys['label'] => $this
        ->randomMachineName(),
    ];

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->storage
      ->create($values);
    $expectations = $this
      ->populate($entity, $this
      ->generateRevisionMatrix($number));
    return $expectations;
  }

  /**
   * Translates an entity into every available language.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity to translate.
   *
   * @return string[]
   *   Every language into which the entity has been translated.
   */
  protected function translate(ContentEntityInterface $entity) {
    $label_key = $entity
      ->getEntityType()
      ->getKey('label');
    $translation_languages = array_keys($entity
      ->getTranslationLanguages());
    $needed_languages = $this->container
      ->get('entity_type.manager')
      ->getStorage('configurable_language')
      ->getQuery()
      ->condition('locked', FALSE)
      ->condition('id', $translation_languages, 'NOT IN')
      ->execute();
    foreach ($needed_languages as $language) {
      $entity
        ->addTranslation($language)
        ->getTranslation($language)
        ->set($label_key, $this
        ->randomMachineName());
      array_push($translation_languages, $language);
    }
    return $translation_languages;
  }

  /**
   * Executes a migration step for a particular entity type.
   *
   * @param string $entity_type_id
   *   The entity type ID.
   * @param string $step
   *   Which step to execute. Can be one of 'save', 'clear', or 'restore'.
   *
   * @return \Drupal\migrate\Plugin\MigrationInterface
   *   The executed migration.
   */
  protected function execute($entity_type_id, $step) {
    $migration_id = "wbm2cm_{$step}:{$entity_type_id}";
    $migration = $this->container
      ->get('plugin.manager.migration')
      ->createInstance($migration_id);
    $executable = new MigrateExecutable($migration, new MigrateMessage());
    $executable
      ->import();
    return $migration;
  }

  /**
   * Returns a randomly chosen bundle ID of the entity type under test.
   *
   * @return mixed
   *   A randomly chosen bundle ID.
   */
  protected function randomBundle() {
    $entity_type_id = $this->storage
      ->getEntityType()
      ->getBundleEntityType();
    return $this
      ->randomEntity($entity_type_id);
  }
  protected abstract function prepareDatabase();

}

Members

Namesort descending Modifiers Type Description Overrides
ContentGenerationTrait::generateFieldData protected function
ContentGenerationTrait::generateRevision protected function
ContentGenerationTrait::generateRevisionMatrix protected function
ContentGenerationTrait::getFieldData protected function
ContentGenerationTrait::getRevisionMatrix protected function
ContentGenerationTrait::getRevisions protected function
ContentGenerationTrait::pokeHoles protected function
ContentGenerationTrait::populate protected function
ContentGenerationTrait::query protected function
ContentGenerationTrait::randomEntity protected function
MigrationTestTrait::$storage protected property The storage handler for the entity type under test.
MigrationTestTrait::createRevisions protected function Creates a set of revisions of a single entity.
MigrationTestTrait::execute protected function Executes a migration step for a particular entity type.
MigrationTestTrait::prepareDatabase abstract protected function 2
MigrationTestTrait::randomBundle protected function Returns a randomly chosen bundle ID of the entity type under test.
MigrationTestTrait::translate protected function Translates an entity into every available language. 1