You are here

public function BetterStatisticsTest::testConfigurationChanges in Better Statistics 7

Test admin configuration functionality of the module.

File

./better_statistics.test, line 54
Tests for the Better Statistics Module.

Class

BetterStatisticsTest
Functional tests for Better Statistics.

Code

public function testConfigurationChanges() {

  // Create a user and log it in.
  $this->admin_user = $this
    ->drupalCreateUser(array(
    'access administration pages',
    'administer statistics',
    'administer site configuration',
    'access content',
    'access statistics',
  ));
  $this
    ->drupalLogin($this->admin_user);

  // Check to see that the "cache" field is available.
  $this
    ->drupalGet($this->stats_admin);
  $this
    ->assertNoFieldChecked('edit-better-statistics-better-statistics-fields-cache', t('Cache field not enabled.'));

  // Enable the cache field.
  $edit = array(
    'statistics_enable_access_log' => BETTER_STATISTICS_ACCESSLOG_DEFAULT,
    'better_statistics[better_statistics][fields][cache]' => TRUE,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));

  // Check to see that the cache field is checked and in the table.
  $this
    ->assertFieldChecked('edit-better-statistics-better-statistics-fields-cache', t('Cache field is checked.'));
  $this
    ->assertTrue(db_field_exists('accesslog', 'cache'), t('Cache field added to the database.'));

  // Disable the cache field.
  $edit = array(
    'statistics_enable_access_log' => BETTER_STATISTICS_ACCESSLOG_DEFAULT,
    'better_statistics[better_statistics][fields][cache]' => FALSE,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));

  // Check to see that the cache field is gone and not in the table.
  $this
    ->assertNoFieldChecked('edit-better-statistics-better-statistics-fields-cache', t('Cache field disabled.'));
  $this
    ->assertFalse(db_field_exists('accesslog', 'cache'), t('Cache field removed from the databases.'));

  // @todo Split these tests off maybe?
  // Enable the user_agent field.
  $edit = array(
    'statistics_enable_access_log' => BETTER_STATISTICS_ACCESSLOG_DEFAULT,
    'better_statistics[better_statistics][fields][user_agent]' => TRUE,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));

  // Visit the homepage.
  $this
    ->drupalGet('node');

  // Check for an entry in the accesslog with that user-agent.
  $query = db_query('SELECT aid FROM {accesslog} WHERE user_agent LIKE :ua', array(
    ':ua' => 'simpletest%',
  ));
  $rows = $query
    ->rowCount();
  $this
    ->assertTrue($rows, t('User-agent string successfully found in the database.'));

  // Check that the user agent string is added to the accesslog detail page.
  $aid = $query
    ->fetchObject();
  $this
    ->drupalGet('admin/reports/access/' . $aid->aid);
  $this
    ->assertText(t('User-agent'), t('User-agent field displayed on accesslog detail page.'));

  // Disable the user_agent field.
  $edit = array(
    'statistics_enable_access_log' => BETTER_STATISTICS_ACCESSLOG_DEFAULT,
    'better_statistics[better_statistics][fields][user_agent]' => FALSE,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));

  // Statistics are collected on cached pages by default. Check that this is
  // reflected in the cache fieldset.
  $this
    ->drupalGet($this->stats_admin);
  $this
    ->assertText('Tracking of page requests served from cache is currently enabled', t('Stats/performance settings properly reflect configuration.'));

  // Disable logging of cached pages.
  $edit = array(
    'statistics_access_log_restrictions_cache' => FALSE,
  );
  $this
    ->drupalPost($this->perf_admin, $edit, t('Save configuration'));

  // Ensure the configuration is reflected.
  $this
    ->drupalGet($this->stats_admin);
  $this
    ->assertText('Tracking of page requests served from cache is currently disabled', t('Stats/performance settings properly reflect configuration.'));

  // Check that client-side only mode does not log any data for both
  // HTML-based and callback-based URLs from the server side.
  $edit = array(
    'statistics_enable_access_log' => BETTER_STATISTICS_ACCESSLOG_CLIENT,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));
  db_truncate('accesslog')
    ->execute();
  $this
    ->drupalGet('rss.xml');
  $this
    ->drupalGet('node');
  $query = db_query('SELECT aid FROM {accesslog}');
  $rows = $query
    ->rowCount();
  $this
    ->assertTrue($rows == 0, t('Accesslog data not being collected server-side when in client-side only mode.'));

  // Ensure the BS JS API snippet and file are included on the page.
  $js_cache = variable_get('drupal_js_cache_files');
  $bs_js_file = file_create_url($js_cache[key($js_cache)]);
  $this
    ->assertRaw("w['BetterStatsObj']", t('Found reference to BS JS API async loader.'));
  $this
    ->assertRaw($bs_js_file, t('Found reference to BS JS API file.'));

  // Check that mixed-mode logs data on callback-based pages, but not
  // HTML-based pages from the server-side.
  $edit = array(
    'statistics_enable_access_log' => BETTER_STATISTICS_ACCESSLOG_MIXED,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));
  db_truncate('accesslog')
    ->execute();
  $this
    ->drupalGet('rss.xml');
  $query = db_query('SELECT aid FROM {accesslog}');
  $rows = $query
    ->rowCount();
  $this
    ->assertTrue($rows == 1, t('Accesslog data being collected server-side when in mixed mode on non-HTML pages.'));
  $this
    ->drupalGet('node');
  $query = db_query('SELECT aid FROM {accesslog}');
  $rows = $query
    ->rowCount();
  $this
    ->assertTrue($rows == 1, t('Accesslog data not being collected server-side when in mixed mode on HTML pages.'));

  // Ensure the BS JS API snippet and file are included on the page.
  $js_cache = variable_get('drupal_js_cache_files');
  $bs_js_file = file_create_url($js_cache[key($js_cache)]);
  $this
    ->assertRaw("w['BetterStatsObj']", t('Found reference to BS JS API async loader.'));
  $this
    ->assertRaw($bs_js_file, t('Found reference to BS JS API file.'));

  // Revert to the default configuration.
  $edit = array(
    'statistics_enable_access_log' => BETTER_STATISTICS_ACCESSLOG_DEFAULT,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));

  // Check that client-side only mode does not log any data for both
  // HTML-based and callback-based URLs from the server side.
  $edit = array(
    'statistics_count_content_views' => BETTER_STATISTICS_ENTITY_VIEW_CLIENT,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));
  db_truncate('node_counter')
    ->execute();
  $this
    ->drupalGet('node/' . $this->node->nid);
  $query = db_query('SELECT * FROM {node_counter} WHERE nid=:nid AND daycount=1', array(
    ':nid' => $this->node->nid,
  ));
  $rows = $query
    ->rowCount();
  $this
    ->assertTrue($rows == 0, t('Entity View data not collected server-side when in client-side only mode.'));

  // Ensure the BS JS API snippet and file are included on the page.
  $js_cache = variable_get('drupal_js_cache_files');
  $bs_js_file = file_create_url($js_cache[key($js_cache)]);
  $this
    ->assertRaw("w['BetterStatsObj']", t('Found reference to BS JS API async loader.'));
  $this
    ->assertRaw($bs_js_file, t('Found reference to BS JS API file.'));

  // Revert to the default configuration.
  $edit = array(
    'statistics_count_content_views' => BETTER_STATISTICS_ENTITY_VIEW_DEFAULT,
  );
  $this
    ->drupalPost($this->stats_admin, $edit, t('Save configuration'));
}