You are here

public function BatchUpdateTest::testBatchUpdate in Entity Usage 8

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

Tests the batch update.

File

tests/src/FunctionalJavascript/BatchUpdateTest.php, line 32

Class

BatchUpdateTest
Tests for the batch update functionality.

Namespace

Drupal\Tests\entity_usage\FunctionalJavascript

Code

public function testBatchUpdate() {
  $page = $this
    ->getSession()
    ->getPage();

  /** @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');
  $this
    ->assertSession()
    ->pageTextContains('eu_test_ct Node 1 has been created.');
  $node1 = Node::load(1);
  $this
    ->saveHtmlOutput();

  // Create node 2 referencing node 1 using reference field.
  $this
    ->drupalGet('/node/add/eu_test_ct');
  $page
    ->fillField('title[0][value]', 'Node 2');
  $page
    ->fillField('field_eu_test_related_nodes[0][target_id]', 'Node 1 (1)');
  $page
    ->pressButton('Save');
  $this
    ->assertSession()
    ->pageTextContains('eu_test_ct Node 2 has been created.');
  $this
    ->saveHtmlOutput();

  // Create node 3 also referencing node 1 in a reference field.
  $this
    ->drupalGet('/node/add/eu_test_ct');
  $page
    ->fillField('title[0][value]', 'Node 3');
  $page
    ->fillField('field_eu_test_related_nodes[0][target_id]', 'Node 1 (1)');
  $page
    ->pressButton('Save');
  $this
    ->assertSession()
    ->pageTextContains('eu_test_ct Node 3 has been created.');
  $this
    ->saveHtmlOutput();

  // Remove one of the records from the database to simulate an usage
  // non-tracked by the module.
  $usage_service
    ->delete(1, 'node', 2, 'node');
  $usage = $usage_service
    ->listUsage($node1);
  $this
    ->assertEquals($usage['node'], [
    '3' => '1',
  ]);

  // Go to the batch update page and check the update.
  $this
    ->drupalGet('/admin/config/entity-usage/batch-update');
  $this
    ->assertSession()
    ->pageTextContains('Batch Update');
  $this
    ->assertSession()
    ->pageTextContains('This form allows you to reset and track again all entity usages in your system.');
  $page
    ->pressButton('Recreate entity usage statistics');
  $this
    ->getSession()
    ->wait(5000);
  $this
    ->saveHtmlOutput();
  $this
    ->assertSession()
    ->pageTextContains('Recreated entity usage for');

  // Check if the resulting usage is the expected.
  $usage = $usage_service
    ->listUsage($node1);
  $this
    ->assertEquals($usage['node'], [
    '2' => '1',
    '3' => '1',
  ]);
}