MigrationTestTrait.php in Workbench Moderation to Content Moderation 8.2
File
tests/src/Kernel/MigrationTestTrait.php
View source
<?php
namespace Drupal\Tests\wbm2cm\Kernel;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessage;
trait MigrationTestTrait {
use ContentGenerationTrait;
protected $storage;
protected function createRevisions($number = 50) {
$keys = $this->storage
->getEntityType()
->getKeys();
$values = [
$keys['bundle'] => $this
->randomBundle(),
$keys['label'] => $this
->randomMachineName(),
];
$entity = $this->storage
->create($values);
$expectations = $this
->populate($entity, $this
->generateRevisionMatrix($number));
return $expectations;
}
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;
}
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;
}
protected function randomBundle() {
$entity_type_id = $this->storage
->getEntityType()
->getBundleEntityType();
return $this
->randomEntity($entity_type_id);
}
protected abstract function prepareDatabase();
}