You are here

protected function MediaMigrationAssertionsForNonMediaSourceTrait::assertNonMediaToMediaNode1FieldValues in Media Migration 8

Assertions of node 1.

2 calls to MediaMigrationAssertionsForNonMediaSourceTrait::assertNonMediaToMediaNode1FieldValues()
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/MediaMigrationAssertionsForNonMediaSourceTrait.php, line 349

Class

MediaMigrationAssertionsForNonMediaSourceTrait
Trait for non-media source to media migration tests.

Namespace

Drupal\Tests\media_migration\Traits

Code

protected function assertNonMediaToMediaNode1FieldValues() {
  $node = $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->load(1);
  assert($node instanceof NodeInterface);
  $props = $this
    ->getImportantEntityProperties($node);
  $node_body_text = $props['body'][0]['value'];
  $html5 = new HTML5([
    'disable_html_ns' => TRUE,
  ]);

  // Compatibility for older HTML5 versions (e.g. in Drupal core 8.9.x).
  $dom_text = '<html><body>' . $node_body_text . '</body></html>';
  try {
    $node_body_html = $html5
      ->parse($dom_text);
  } catch (\TypeError $e) {
    $text_stream = new StringInputStream($dom_text);
    $node_body_html = $html5
      ->parse($text_stream);
  }
  foreach ($node_body_html
    ->getElementsByTagName('a') as $anchor_node) {
    assert($anchor_node instanceof \DOMNode);
    if ($anchor_node
      ->hasAttribute('data-entity-uuid')) {
      $anchor_node
        ->setAttribute('data-entity-uuid', 'uuid');
    }
  }
  $props['body'][0]['value'] = $html5
    ->saveHTML($node_body_html->documentElement->firstChild->childNodes);
  $this
    ->assertEquals([
    'nid' => [
      [
        'value' => 1,
      ],
    ],
    'type' => [
      [
        'target_id' => 'article',
      ],
    ],
    'status' => [
      [
        'value' => 1,
      ],
    ],
    'uid' => [
      [
        'target_id' => 2,
      ],
    ],
    'title' => [
      [
        'value' => 'Article with images and files',
      ],
    ],
    'created' => [
      [
        'value' => 1594368799,
      ],
    ],
    'changed' => [
      [
        'value' => 1594368881,
      ],
    ],
    'promote' => [
      [
        'value' => 1,
      ],
    ],
    'sticky' => [
      [
        'value' => 0,
      ],
    ],
    'body' => [
      [
        'value' => '<p>Nulla tempor, nunc eu mollis finibus, risus nunc <a href="/file/7" data-entity-substitution="media" data-entity-type="media" data-entity-uuid="uuid">venenatis nulla</a>, in ullamcorper nisl nulla et nisi. Cras vel urna risus. Cras in sem a nulla aliquet pretium.</p><p>Quisque tortor libero, vulputate sit amet augue dictum, posuere bibendum lectus. Nunc fermentum justo odio, ut fermentum purus fermentum a. Aenean congue fringilla arcu sit amet pellentesque.</p>',
        'summary' => '',
        'format' => 'filtered_html',
      ],
    ],
    'field_file' => [
      [
        'target_id' => '6',
      ],
    ],
    'field_file_multi' => [
      [
        'target_id' => '12',
      ],
      [
        'target_id' => '10',
      ],
    ],
    'field_image' => [
      [
        'target_id' => '1',
      ],
    ],
    'field_image_multi' => [
      [
        'target_id' => '2',
      ],
      [
        'target_id' => '7',
      ],
      [
        'target_id' => '3',
      ],
    ],
  ], $props);

  // Test that the image and file fields are referencing media entities.
  $media_fields = [
    'field_file' => 1,
    'field_file_multi' => 2,
    'field_image' => 1,
    'field_image_multi' => 3,
  ];
  foreach ($media_fields as $field_name => $expected_count) {
    $referred_entities = $this
      ->getReferencedEntities($node, $field_name, $expected_count);
    assert($referred_entities[0] instanceof MediaInterface);
  }
}