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';
  drupal_http_request($stats_path, array(
    'method' => 'POST',
    'data' => $post,
    'headers' => $headers,
    'timeout' => 10000,
  ));
  $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.');
  node_delete($this->test_node->nid);
  $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.');
}