You are here

protected function MediaMigrationAssertionsBaseTrait::assertEmbedTokenHtmlTags in Media Migration 8

Assert that embed HTML tags exist in the given text.

Tests that the given set of embed HTML tags and their attributes exists in a text field.

Parameters

string $body_text: The content of the text field to check.

array $embed_code_html_properties: Array of HTML entity attributes to search for in the given text.

2 calls to MediaMigrationAssertionsBaseTrait::assertEmbedTokenHtmlTags()
MediaMigrationAssertionsForMediaSourceTrait::assertNode1FieldValues in tests/src/Traits/MediaMigrationAssertionsForMediaSourceTrait.php
Assertions of node 1.
MigrateEmbedMediaTokenTestBase::assertMediaTokenTransform in tests/src/Functional/MigrateEmbedMediaTokenTestBase.php
Asserts the result of Media Migration's embed media token transform.

File

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

Class

MediaMigrationAssertionsBaseTrait
Trait for media migration tests.

Namespace

Drupal\Tests\media_migration\Traits

Code

protected function assertEmbedTokenHtmlTags($body_text, array $embed_code_html_properties) {
  $body_text_dom = Html::load($body_text);
  $embed_tag = MediaMigration::getEmbedTokenDestinationFilterPlugin() === MediaMigration::MEDIA_TOKEN_DESTINATION_FILTER_MEDIA_EMBED ? 'drupal-media' : 'drupal-entity';
  $embed_entity_html_tag_list = $body_text_dom
    ->getElementsByTagName($embed_tag);
  $expected_parsed_embed_tags = [];
  foreach ($embed_entity_html_tag_list as $tag_index => $embed_tag) {
    assert($embed_tag instanceof \DomNode);
    $actual_embed_tags[$tag_index] = [];
    if ($embed_tag
      ->hasAttributes() && !is_null($embed_tag->attributes)) {
      foreach ($embed_tag->attributes as $attr) {
        $actual_embed_tags[$tag_index][$attr->name] = $attr->value;
      }
    }
  }

  // Asserting that the expected attributes exist or even that they have the
  // expected values.
  foreach ($embed_code_html_properties as $delta => $embed_tag_attributes) {
    $embed_tag_domnode = $embed_entity_html_tag_list
      ->item($delta);
    assert($embed_tag_domnode instanceof \DOMNode);
    foreach ($embed_tag_attributes as $attribute_name => $attribute_value) {

      // If the value is TRUE, we only check whether the attribute exists.
      if ($attribute_value === TRUE) {
        $actual_parsed_embed_tags[$delta][$attribute_name] = TRUE;
        $expected_parsed_embed_tags[$delta][$attribute_name] = !empty($embed_tag_domnode
          ->getAttribute($attribute_name));
      }
      else {
        $actual_parsed_embed_tags[$delta][$attribute_name] = $attribute_value;
        $expected_parsed_embed_tags[$delta][$attribute_name] = $embed_tag_domnode
          ->getAttribute($attribute_name);
      }
    }
  }
  $this
    ->assertEquals($expected_parsed_embed_tags, $actual_parsed_embed_tags, sprintf("Expected embed tags are different than the actual ones. These tags are migrated: '%s'", Variable::export($actual_embed_tags)));

  // Ensure that we have the expected number of embed tags.
  $this
    ->assertCount(count($embed_code_html_properties), $embed_entity_html_tag_list, sprintf("Different number of embed tags were migrated than expected. These tags are migrated: '%s'", Variable::export($actual_embed_tags)));
}