You are here

public function EmbeddedContentTest::testLinkIt in Entity Usage 8.2

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

Tests the LinkIt parsing.

File

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

Class

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

Namespace

Drupal\Tests\entity_usage\FunctionalJavascript

Code

public function testLinkIt() {
  $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 a linkit markup.
  $embedded_text = '<p>foo <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="' . $node1
    ->uuid() . '">linked text</a> bar</p>';
  $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();

  // 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(),
          'method' => 'linkit',
          'field_name' => 'field_eu_test_rich_text',
          'count' => 1,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected, $usage);

  // Create node 3 referencing N2 and N1 on the same field.
  $embedded_text .= '<p>Foo bar</p>' . '<p>foo2 <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="' . $node2
    ->uuid() . '">linked text 2</a> bar 2</p>';
  $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();

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

  // Create node 4 referencing a non existing UUID using a linkit markup to
  // test removed entities.
  $embedded_text = '<p>foo <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="c7cae398-3c36-47d4-8ef0-a17902e76ff4">I do not exists</a> bar</p>';
  $node4 = Node::create([
    'type' => 'eu_test_ct',
    'title' => 'Node 4',
    'field_eu_test_rich_text' => [
      'value' => $embedded_text,
      'format' => 'eu_test_text_format',
    ],
  ]);
  $node4
    ->save();

  // Check that the usage for this source is empty.
  $usage = $usage_service
    ->listTargets($node4);
  $this
    ->assertEquals([], $usage);
}