You are here

public function StatisticsAdminTest::testDeleteNode in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testDeleteNode()
  2. 10 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 117

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 . '/' . drupal_get_path('module', '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
    ->assertEqual($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.');
}