You are here

function UbercartCartLinksTestCase::testCartLinksTracking in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_cart_links/tests/uc_cart_links.test \UbercartCartLinksTestCase::testCartLinksTracking()

Tests cart links tracking.

File

uc_cart_links/uc_cart_links.test, line 496
Ubercart Cart Links Tests.

Class

UbercartCartLinksTestCase
SimpleTests for Ubercart Cart Links.

Code

function testCartLinksTracking() {

  // Create product
  $products[] = $this
    ->createCartLinksProduct(FALSE);

  // Create a product class
  $products[] = $this
    ->createCartLinksProduct(FALSE);

  // later ...
  // Create some valid cart links for these products
  $link_array = $this
    ->createValidCartLinks($products);
  $cart_links = $link_array['links'];
  $link_data = $link_array['data'];

  // Create a page containing these links
  $page = $this
    ->createCartLinksPage($cart_links);
  $this
    ->drupalLogin($this->adminUser);

  //
  // Test cart links tracking
  //
  // Go to page with cart links
  $this
    ->drupalGet('node/' . $page->nid);

  // Create three tracking IDs
  $tracking = array();
  for ($i = 0; $i < 3; $i++) {
    $tracking[$this
      ->randomName(16)] = 0;
  }

  // Click a number of links to create some statistics
  for ($i = 0; $i < 50; $i++) {

    // Pick one link at random and append an '-i<tracking ID>'
    $test_link = array_rand($cart_links);

    // Assign one of the tracking IDs
    $tracking_id = array_rand($tracking);
    $this
      ->drupalGet($cart_links[$test_link] . '-i' . $tracking_id);

    // Keep a record of how many links were assigned this key
    $tracking[$tracking_id] += 1;
  }

  // Check report to see these clicks have been recorded correctly
  $this
    ->drupalGet('admin/store/reports/cart_links');
  $total = 0;
  foreach ($tracking as $id => $clicks) {
    $total += $clicks;
    $this
      ->assertRaw(t('<td>@id</td><td>@clicks</td>', array(
      '@id' => $id,
      '@clicks' => $clicks,
    )), t('Tracking ID @id received @clicks clicks.', array(
      '@id' => $id,
      '@clicks' => $clicks,
    )));
  }
  $this
    ->assertEqual($total, 50, t('Fifty clicks recorded.'));
  $this
    ->drupalLogout();
}