You are here

public function IntegrationTest::testLinkTracking in Entity Usage 8

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

Tests the tracking of nodes in link fields.

File

tests/src/FunctionalJavascript/IntegrationTest.php, line 143

Class

IntegrationTest
Basic functional tests for the usage tracking plugins.

Namespace

Drupal\Tests\entity_usage\FunctionalJavascript

Code

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

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

  // Add a link field to our test content type.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_link1',
    'entity_type' => 'node',
    'type' => 'link',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings' => [],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'eu_test_ct',
    'settings' => [
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkItemInterface::LINK_GENERIC,
    ],
  ]);
  $field
    ->save();
  entity_get_form_display('node', 'eu_test_ct', 'default')
    ->setComponent('field_link1', [
    'type' => 'link_default',
  ])
    ->save();
  entity_get_display('node', 'eu_test_ct', 'default')
    ->setComponent('field_link1', [
    'type' => 'link',
  ])
    ->save();

  // Create Node 1.
  $this
    ->drupalGet('/node/add/eu_test_ct');
  $page
    ->fillField('title[0][value]', 'Node 1');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('eu_test_ct Node 1 has been created.');
  $this
    ->saveHtmlOutput();
  $node1_id = \Drupal::entityQuery('node')
    ->sort('changed', 'DESC')
    ->execute();
  $node1_id = reset($node1_id);
  $this
    ->assertNotNull($node1_id);
  $node1 = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadUnchanged($node1_id);

  // Create Node 2, referencing Node 1.
  $this
    ->drupalGet('/node/add/eu_test_ct');
  $page
    ->fillField('title[0][value]', 'Node 2');
  $page
    ->fillField('field_link1[0][uri]', "Node 1 ({$node1_id})");
  $page
    ->fillField('field_link1[0][title]', "Linked text");
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('eu_test_ct Node 2 has been created.');
  $this
    ->saveHtmlOutput();
  $node2_id = \Drupal::entityQuery('node')
    ->sort('changed', 'DESC')
    ->execute();
  $node2_id = reset($node2_id);
  $this
    ->assertNotNull($node2_id);
  $node2 = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadUnchanged($node2_id);

  // Check that the usage of Node 1 points to Node 2.
  $usage = $usage_service
    ->listUsage($node1, TRUE);
  $this
    ->assertEquals([
    $node2_id => '1',
  ], $usage['link']['node']);

  // Edit Node 2, remove reference.
  $this
    ->drupalGet("/node/{$node2_id}/edit");
  $page
    ->fillField('field_link1[0][uri]', '');
  $page
    ->fillField('field_link1[0][title]', '');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('eu_test_ct Node 2 has been updated.');
  $this
    ->saveHtmlOutput();

  // Verify the usage was released.
  $usage = $usage_service
    ->listUsage($node1);
  $this
    ->assertEquals([], $usage);

  // Reference Node 1 again, and then delete the host node.
  $this
    ->drupalGet("/node/{$node2_id}/edit");
  $page
    ->fillField('field_link1[0][uri]', "Node 1 ({$node1_id})");
  $page
    ->fillField('field_link1[0][title]', "Linked text");
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('eu_test_ct Node 2 has been updated.');
  $this
    ->saveHtmlOutput();

  // Usage now should be there.
  $usage = $usage_service
    ->listUsage($node1, TRUE);
  $this
    ->assertEquals([
    $node2_id => '1',
  ], $usage['link']['node']);

  // Delete the host and usage should be released.
  $node2
    ->delete();
  $usage = $usage_service
    ->listUsage($node1);
  $this
    ->assertEquals([], $usage);
}