You are here

public function StatisticsAdminTest::testDeleteNode in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testDeleteNode()
  2. 9 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testDeleteNode()

Tests that when a node is deleted, the node counter is deleted too.

File

core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php, line 119

Class

StatisticsAdminTest
Tests the statistics admin.

Namespace

Drupal\Tests\statistics\Functional

Code

public function testDeleteNode() {
  $this
    ->config('statistics.settings')
    ->set('count_content_views', 1)
    ->save();
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $this->testNode
    ->id();
  $post = [
    'nid' => $nid,
  ];
  global $base_url;
  $stats_path = $base_url . '/' . $this
    ->getModulePath('statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, [
    'form_params' => $post,
  ]);
  $connection = Database::getConnection();
  $result = $connection
    ->select('node_counter', 'n')
    ->fields('n', [
    'nid',
  ])
    ->condition('n.nid', $this->testNode
    ->id())
    ->execute()
    ->fetchAssoc();
  $this
    ->assertEquals($result['nid'], $this->testNode
    ->id(), 'Verifying that the node counter is incremented.');
  $this->testNode
    ->delete();
  $result = $connection
    ->select('node_counter', 'n')
    ->fields('n', [
    'nid',
  ])
    ->condition('n.nid', $this->testNode
    ->id())
    ->execute()
    ->fetchAssoc();
  $this
    ->assertFalse($result, 'Verifying that the node counter is deleted.');
}