function StatisticsAdminTest::testDeleteNode

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

File

drupal/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php, line 106
Definition of Drupal\statistics\Tests\StatisticsAdminTest.

Class

StatisticsAdminTest
Tests the statistics administration screen.

Namespace

Drupal\statistics\Tests

Code

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

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $this->test_node->nid;
  $post = http_build_query(array(
    'nid' => $nid,
  ));
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, $headers, $post)
    ->send();
  $result = db_select('node_counter', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->condition('n.nid', $this->test_node->nid)
    ->execute()
    ->fetchAssoc();
  $this
    ->assertEqual($result['nid'], $this->test_node->nid, 'Verifying that the node counter is incremented.');
  $this->test_node
    ->delete();
  $result = db_select('node_counter', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->condition('n.nid', $this->test_node->nid)
    ->execute()
    ->fetchAssoc();
  $this
    ->assertFalse($result, 'Verifying that the node counter is deleted.');
}