You are here

function GoogleAnalyticsSearchTest::testGoogleAnalyticsSearchTracking in Google Analytics 7.2

Same name and namespace in other branches
  1. 6.4 googleanalytics.test \GoogleAnalyticsSearchTest::testGoogleAnalyticsSearchTracking()

File

./googleanalytics.test, line 686
Test file for Google Analytics module.

Class

GoogleAnalyticsSearchTest

Code

function testGoogleAnalyticsSearchTracking() {
  $ua_code = 'UA-123456-1';
  variable_set('googleanalytics_account', $ua_code);

  // Check tracking code visibility.
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code, '[testGoogleAnalyticsSearch]: Tracking code is displayed for authenticated users.');
  $this
    ->drupalGet('search/node');
  $this
    ->assertNoRaw('ga("set", "page",', '[testGoogleAnalyticsSearch]: Custom url not set.');

  // Enable site search support.
  variable_set('googleanalytics_site_search', 1);

  // Search for random string.
  $search = array();
  $search['keys'] = $this
    ->randomName(8);

  // Create a node to search for.
  $langcode = LANGUAGE_NONE;
  $edit = array();
  $edit['title'] = 'This is a test title';
  $edit["body[{$langcode}][0][value]"] = 'This test content contains ' . $search['keys'] . ' string.';

  // Fire a search, it's expected to get 0 results.
  $this
    ->drupalPost('search/node', $search, t('Search'));
  $this
    ->assertRaw('ga("set", "page", (window.googleanalytics_search_results) ?', '[testGoogleAnalyticsSearch]: Search results tracker is displayed.');
  $this
    ->assertRaw('window.googleanalytics_search_results = 0;', '[testGoogleAnalyticsSearch]: Search yielded no results.');

  // Save the node.
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->assertText(t('@type @title has been created.', array(
    '@type' => 'Basic page',
    '@title' => $edit['title'],
  )), 'Node was created.');

  // Index the node or it cannot found.
  $this
    ->cronRun();
  $this
    ->drupalPost('search/node', $search, t('Search'));
  $this
    ->assertRaw('ga("set", "page", (window.googleanalytics_search_results) ?', '[testGoogleAnalyticsSearch]: Search results tracker is displayed.');
  $this
    ->assertRaw('window.googleanalytics_search_results = 1;', '[testGoogleAnalyticsSearch]: One search result found.');
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $this
    ->assertText(t('@type @title has been created.', array(
    '@type' => 'Basic page',
    '@title' => $edit['title'],
  )), 'Node was created.');

  // Index the node or it cannot found.
  $this
    ->cronRun();
  $this
    ->drupalPost('search/node', $search, t('Search'));
  $this
    ->assertRaw('ga("set", "page", (window.googleanalytics_search_results) ?', '[testGoogleAnalyticsSearch]: Search results tracker is displayed.');
  $this
    ->assertRaw('window.googleanalytics_search_results = 2;', '[testGoogleAnalyticsSearch]: Two search results found.');
}