You are here

protected function MediaMigrationAssertionsBaseTrait::assertMediaFieldsAllowedTypes in Media Migration 8

Tests the allowed media types of a media reference field.

Parameters

string $entity_type_id: The entity type ID of the entity that's field is checked.

string $bundle: The bundle of the entity where the field is present.

string $field_name: The name of the media reference field to check.

string[] $expected_media_types: The list of the expected allowed media types.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

4 calls to MediaMigrationAssertionsBaseTrait::assertMediaFieldsAllowedTypes()
MediaMigrationAssertionsForMediaSourceTrait::assertArticleImageFieldsAllowedTypes in tests/src/Traits/MediaMigrationAssertionsForMediaSourceTrait.php
Tests article's "field_image" media reference field's allowed media types.
MediaMigrationAssertionsForMediaSourceTrait::assertArticleMediaFieldsAllowedTypes in tests/src/Traits/MediaMigrationAssertionsForMediaSourceTrait.php
Tests article's "field_media" media reference field's allowed media types.
MediaMigrationPlainTest::testPlainFileToMediaMigration in tests/src/Kernel/Migrate/MediaMigrationPlainTest.php
Tests the migration of plain file and image fields to media reference.
MigrateMediaFromNonMediaSourceTest::testPlainFileToMediaMigration in tests/src/Functional/MigrateMediaFromNonMediaSourceTest.php
Tests the result of the media migration.

File

tests/src/Traits/MediaMigrationAssertionsBaseTrait.php, line 93

Class

MediaMigrationAssertionsBaseTrait
Trait for media migration tests.

Namespace

Drupal\Tests\media_migration\Traits

Code

protected function assertMediaFieldsAllowedTypes(string $entity_type_id, string $bundle, string $field_name, array $expected_media_types) {
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  assert($entity_type_manager instanceof EntityTypeManagerInterface);
  $field_config_id = implode('.', [
    $entity_type_id,
    $bundle,
    $field_name,
  ]);
  $field_media_config = $entity_type_manager
    ->getStorage('field_config')
    ->load($field_config_id);
  $this
    ->assertInstanceOf(FieldConfigInterface::class, $field_media_config, sprintf("The '%s' field of '%s' with type '%s' does not exist.", $field_name, $entity_type_id, $bundle));
  $this
    ->assertSame('entity_reference', $field_media_config
    ->getType(), sprintf("The '%s' field of '%s' with type '%s' is not an entity reference field.", $field_name, $entity_type_id, $bundle));
  $handler_settings = $field_media_config
    ->getSetting('handler_settings');
  $allowed_media_types = array_values($handler_settings['target_bundles']);
  sort($expected_media_types);
  sort($allowed_media_types);

  // @todo test the target entity type ID (=== media) as well.
  $this
    ->assertEquals($expected_media_types, $allowed_media_types, sprintf("The '%s' field of '%s' with type '%s' doesn't allows referring the expected media bundles.", $field_name, $entity_type_id, $bundle));
}