View source
<?php
namespace Drupal\Tests\eck\Kernel\Migrate\d7;
use Drupal\eck\Entity\EckEntity;
use Drupal\eck\Entity\EckEntityBundle;
use Drupal\eck\Entity\EckEntityType;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldConfigInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
abstract class MigrateEckTestBase extends MigrateDrupal7TestBase {
protected $entityTypeManager;
protected function setUp() {
parent::setUp();
$this->entityTypeManager = \Drupal::entityTypeManager();
}
protected function getFixtureFilePath() {
return __DIR__ . '/../../../../fixtures/drupal7.php';
}
protected function migrateFields() {
$this
->executeMigration('d7_field');
$this
->migrateContentTypes();
$this
->executeMigrations([
'd7_field_instance',
]);
}
public function assertEckEntityType(array $type) {
$entity = eckEntityType::load($type['id']);
$this
->assertInstanceOf(eckEntityType::class, $entity);
$this
->assertSame($type['label'], $entity
->label());
$this
->assertSame($type['langcode'], $entity
->language()
->getId());
}
public function assertEckBundle(array $bundle) {
$entity = eckEntityBundle::load($bundle['type']);
$this
->assertInstanceOf(eckEntityBundle::class, $entity);
$this
->assertSame($bundle['name'], $entity->name);
$this
->assertSame($bundle['description'], $entity->description);
$this
->assertSame($bundle['langcode'], $entity
->language()
->getId());
}
public function assertEck(array $eck) {
$message = "Failure for eck entity type '" . $eck['type'] . "' with id of '" . $eck['id'] . "'";
$entity = $this->entityTypeManager
->getStorage($eck['type'])
->load($eck['id']);
$this
->assertInstanceOf(EckEntity::class, $entity, $message);
$this
->assertSame($eck['label'], $entity
->label(), $message);
$this
->assertSame($eck['bundle'], $entity
->bundle(), $message);
$this
->assertSame($eck['langcode'], $entity
->language()
->getId(), $message);
foreach ($eck['fields'] as $name => $value) {
$this
->assertSame($value, $entity
->get($name)
->getValue(), $message);
}
if (!empty($eck['translations'])) {
foreach ($eck['translations'] as $language => $translation_data) {
$this
->assertTrue($entity
->hasTranslation($language));
$translation = $entity
->getTranslation($language);
foreach ($translation_data['fields'] as $name => $value) {
$this
->assertSame($value, $translation
->get($name)
->getValue(), $message);
}
}
}
}
protected function assertFieldStorage($id, $expected_type, $expected_translatable, $expected_cardinality) {
list($expected_entity_type, $expected_name) = explode('.', $id);
$field = FieldStorageConfig::load($id);
$this
->assertInstanceOf(FieldStorageConfigInterface::class, $field);
$this
->assertEquals($expected_name, $field
->getName());
$this
->assertEquals($expected_type, $field
->getType());
$this
->assertEquals($expected_translatable, $field
->isTranslatable());
$this
->assertEquals($expected_entity_type, $field
->getTargetEntityTypeId());
if ($expected_cardinality === 1) {
$this
->assertFalse($field
->isMultiple());
}
else {
$this
->assertTrue($field
->isMultiple());
}
$this
->assertEquals($expected_cardinality, $field
->getCardinality());
}
protected function assertFieldInstance($id, $expected_label, $expected_field_type, $is_required, $expected_translatable) {
list($expected_entity_type, $expected_bundle, $expected_name) = explode('.', $id);
$field = FieldConfig::load($id);
$this
->assertInstanceOf(FieldConfigInterface::class, $field);
$this
->assertEquals($expected_label, $field
->label());
$this
->assertEquals($expected_field_type, $field
->getType());
$this
->assertEquals($expected_entity_type, $field
->getTargetEntityTypeId());
$this
->assertEquals($expected_bundle, $field
->getTargetBundle());
$this
->assertEquals($expected_name, $field
->getName());
$this
->assertEquals($is_required, $field
->isRequired());
$this
->assertEquals($expected_entity_type . '.' . $expected_name, $field
->getFieldStorageDefinition()
->id());
$this
->assertEquals($expected_translatable, $field
->isTranslatable());
}
}