You are here

public function EmbeddedContentTest::testEntityEmbed in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/FunctionalJavascript/EmbeddedContentTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\EmbeddedContentTest::testEntityEmbed()
  2. 8.2 tests/src/FunctionalJavascript/EmbeddedContentTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\EmbeddedContentTest::testEntityEmbed()

Tests the Entity Embed parsing.

File

tests/src/FunctionalJavascript/EmbeddedContentTest.php, line 31

Class

EmbeddedContentTest
Basic functional tests for the usage tracking of embedded content.

Namespace

Drupal\Tests\entity_usage\FunctionalJavascript

Code

public function testEntityEmbed() {
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();
  $assert_session = $this
    ->assertSession();

  /** @var \Drupal\entity_usage\EntityUsage $usage_service */
  $usage_service = \Drupal::service('entity_usage.usage');

  // Create node 1.
  $this
    ->drupalGet('/node/add/eu_test_ct');
  $page
    ->fillField('title[0][value]', 'Node 1');
  $page
    ->pressButton('Save');
  $session
    ->wait(500);
  $this
    ->saveHtmlOutput();
  $assert_session
    ->pageTextContains('eu_test_ct Node 1 has been created.');
  $node1 = $this
    ->getLastEntityOfType('node', TRUE);

  // Nobody is using this guy for now.
  $usage = $usage_service
    ->listSources($node1);
  $this
    ->assertEquals([], $usage);

  // Create node 2 referencing node 1 using an Entity Embed markup.
  $embedded_text = '<drupal-entity data-embed-button="node" data-entity-embed-display="entity_reference:entity_reference_label" data-entity-embed-display-settings="{&quot;link&quot;:1}" data-entity-type="node" data-entity-uuid="' . $node1
    ->uuid() . '"></drupal-entity>';
  $node2 = Node::create([
    'type' => 'eu_test_ct',
    'title' => 'Node 2',
    'field_eu_test_rich_text' => [
      'value' => $embedded_text,
      'format' => 'eu_test_text_format',
    ],
  ]);
  $node2
    ->save();
  $this
    ->triggerPostRequestTracking();

  // Check that we correctly registered the relation between N2 and N1.
  $usage = $usage_service
    ->listSources($node1);
  $expected = [
    'node' => [
      $node2
        ->id() => [
        [
          'source_langcode' => $node2
            ->language()
            ->getId(),
          'source_vid' => $node2
            ->getRevisionId(),
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected, $usage);

  // Create node 3 referencing N2 and N1 on the same field.
  $embedded_text .= '<p>Foo bar</p>' . '<drupal-entity data-embed-button="node" data-entity-embed-display="entity_reference:entity_reference_label" data-entity-embed-display-settings="{&quot;link&quot;:1}" data-entity-type="node" data-entity-uuid="' . $node2
    ->uuid() . '"></drupal-entity>';
  $node3 = Node::create([
    'type' => 'eu_test_ct',
    'title' => 'Node 3',
    'field_eu_test_rich_text' => [
      'value' => $embedded_text,
      'format' => 'eu_test_text_format',
    ],
  ]);
  $node3
    ->save();
  $this
    ->triggerPostRequestTracking();

  // Check that both targets are tracked.
  $usage = $usage_service
    ->listTargets($node3);
  $expected = [
    'node' => [
      $node2
        ->id(),
      $node1
        ->id(),
    ],
  ];
  $this
    ->assertEquals($expected, $usage);
}