StatisticsAdminTest.php

Definition of Drupal\statistics\Tests\StatisticsAdminTest.

Namespace

Drupal\statistics\Tests

File

drupal/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php
View source
<?php

/**
 * @file
 * Definition of Drupal\statistics\Tests\StatisticsAdminTest.
 */
namespace Drupal\statistics\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests the statistics administration screen.
 */
class StatisticsAdminTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'node',
    'statistics',
  );

  /**
   * A user that has permission to administer statistics.
   *
   * @var object|FALSE
   *
   * A fully loaded user object, or FALSE if user creation failed.
   */
  protected $privileged_user;

  /**
   * A page node for which to check content statistics.
   *
   * @var object
   */
  protected $test_node;

  /**
   * The Guzzle HTTP client.
   *
   * @var \Guzzle\Http\ClientInterface;
   */
  protected $client;
  public static function getInfo() {
    return array(
      'name' => 'Test statistics admin.',
      'description' => 'Tests the statistics admin.',
      'group' => 'Statistics',
    );
  }
  function setUp() {
    parent::setUp();

    // Create Basic page node type.
    if ($this->profile != 'standard') {
      $this
        ->drupalCreateContentType(array(
        'type' => 'page',
        'name' => 'Basic page',
      ));
    }
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'administer statistics',
      'view post access counter',
      'create page content',
    ));
    $this
      ->drupalLogin($this->privileged_user);
    $this->test_node = $this
      ->drupalCreateNode(array(
      'type' => 'page',
      'uid' => $this->privileged_user->uid,
    ));
    $this->client = \Drupal::httpClient();
    $this->client
      ->setConfig(array(
      'curl.options' => array(
        CURLOPT_TIMEOUT => 10,
      ),
    ));
  }

  /**
   * Verifies that the statistics settings page works.
   */
  function testStatisticsSettings() {
    $config = config('statistics.settings');
    $this
      ->assertFalse($config
      ->get('count_content_views'), 'Count content view log is disabled by default.');

    // Enable counter on content view.
    $edit['statistics_count_content_views'] = 1;
    $this
      ->drupalPost('admin/config/system/statistics', $edit, t('Save configuration'));
    $config = config('statistics.settings');
    $this
      ->assertTrue($config
      ->get('count_content_views'), 'Count content view log is enabled.');

    // Hit the node.
    $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();

    // Hit the node again (the counter is incremented after the hit, so
    // "1 view" will actually be shown when the node is hit the second time).
    $this
      ->drupalGet('node/' . $this->test_node->nid);
    $this->client
      ->post($stats_path, $headers, $post)
      ->send();
    $this
      ->assertText('1 view', 'Node is viewed once.');
    $this
      ->drupalGet('node/' . $this->test_node->nid);
    $this->client
      ->post($stats_path, $headers, $post)
      ->send();
    $this
      ->assertText('2 views', 'Node is viewed 2 times.');
  }

  /**
   * Tests that when a node is deleted, the node counter is deleted too.
   */
  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.');
  }

  /**
   * Tests that cron clears day counts and expired access logs.
   */
  function testExpiredLogs() {
    config('statistics.settings')
      ->set('count_content_views', 1)
      ->save();
    \Drupal::state()
      ->set('statistics.day_timestamp', 8640000);
    $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();
    $this
      ->drupalGet('node/' . $this->test_node->nid);
    $this->client
      ->post($stats_path, $headers, $post)
      ->send();
    $this
      ->assertText('1 view', 'Node is viewed once.');

    // statistics_cron() will subtract
    // statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in
    // the delete query, so wait two secs here to make sure the access log will
    // be flushed for the node just hit.
    sleep(2);
    $this
      ->cronRun();
    $this
      ->drupalGet('admin/reports/pages');
    $this
      ->assertNoText('node/' . $this->test_node->nid, 'No hit URL found.');
    $result = db_select('node_counter', 'nc')
      ->fields('nc', array(
      'daycount',
    ))
      ->condition('nid', $this->test_node->nid, '=')
      ->execute()
      ->fetchField();
    $this
      ->assertFalse($result, 'Daycounter is zero.');
  }

}

Classes

Namesort descending Description
StatisticsAdminTest Tests the statistics administration screen.