You are here

public function CartLinksTest::testCartLinksTracking in Ubercart 8.4

Tests Cart Links tracking.

File

uc_cart_links/src/Tests/CartLinksTest.php, line 488

Class

CartLinksTest
Tests the Cart Links functionality.

Namespace

Drupal\uc_cart_links\Tests

Code

public 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
    ->id());

  // Create three tracking IDs.
  $tracking = [];
  for ($i = 0; $i < 3; $i++) {
    $tracking[$this
      ->randomMachineName(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;
  }

  // Sort by # of clicks, as that is how Views displays them by default.
  arsort($tracking, SORT_NUMERIC);

  // 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;

    //    $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-cart-link-id ")]');
    //    $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-clicks ")]');
    $this
      ->assertTextPattern('/\\s+' . preg_quote($id, '/') . '\\s+' . preg_quote($clicks, '/') . '\\s+/', SafeMarkup::format('Tracking ID @id received @clicks clicks.', [
      '@id' => $id,
      '@clicks' => $clicks,
    ]));
  }
  $this
    ->assertEqual($total, 50, 'Fifty clicks recorded.');
  $this
    ->drupalLogout();
}