You are here

function UbercartCartLinksTestCase::createValidCartLinks in Ubercart 6.2

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

Creates cart links pointing to the given product(s).

Links containing many combinations of attributes and options wil be returned. Return value is an associative array containing two keys: -links: An array of the actual links we're building. -data: An array of metadata about the cart links so we won't have to try to re-construct this information by parsing the link at a later time.

The 'links' and 'data' sub-arrays are both indexed by the keys used in the $products array that is passed in as an argument, so these keys may be used to lookup the link and metadata for a specific product.

Parameters

$products: An array of products.

Return value

Array containing cart links and link metadata.

7 calls to UbercartCartLinksTestCase::createValidCartLinks()
UbercartCartLinksTestCase::testCartLinksAllowEmptying in uc_cart_links/uc_cart_links.test
Tests cart links on a page under a variety of conditions.
UbercartCartLinksTestCase::testCartLinksBasicFunctionality in uc_cart_links/uc_cart_links.test
Tests cart links on a page under a variety of conditions.
UbercartCartLinksTestCase::testCartLinksMessages in uc_cart_links/uc_cart_links.test
Tests cart links messages.
UbercartCartLinksTestCase::testCartLinksProductActionMessage in uc_cart_links/uc_cart_links.test
Tests cart links on a page under a variety of conditions.
UbercartCartLinksTestCase::testCartLinksRedirect in uc_cart_links/uc_cart_links.test
Tests page redirection for invalid cart links.

... See full list

File

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

Class

UbercartCartLinksTestCase
SimpleTests for Ubercart Cart Links.

Code

function createValidCartLinks($products = array()) {
  foreach ($products as $key => $product) {
    $nid = $product->nid;
    $title = $product->title;
    $qty = mt_rand(1, 19);

    // $link_data will hold meta information about the cart links
    // so we won't have to try to re-construct this information by
    // parsing the link at a later time.
    $link_data[$key] = array(
      'nid' => $nid,
      'title' => $title,
      'qty' => $qty,
      'attributes' => array(),
    );

    // $cart_links will hold the actual links we're building.
    // $cart_links and $link_data share the same keys.
    $cart_links[$key] = '/cart/add/p' . $nid . '_q' . $qty;

    // Loop over attributes, append all attribute/option combos to links
    $attributes = uc_product_get_attributes($nid);
    foreach ($attributes as $attribute) {

      // If this is textfield, radio, or select option, then
      // only 1 option allowed.  If checkbox, multiple are allowed.
      switch ($attribute->display) {
        case 0:

          // textfield
          $value = $this
            ->randomName(12);

          // Textfield
          $link_data[$key]['attributes'][$attribute->label][] = $value;
          $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $value;
          break;
        case 1:

        // select
        case 2:

          // radios
          $option = $attribute->options[array_rand($attribute->options)];
          $link_data[$key]['attributes'][$attribute->label][] = $option->name;
          $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $option->oid;
          break;
        case 3:

          // checkboxes
          foreach ($attribute->options as $option) {
            $link_data[$key]['attributes'][$attribute->label][] = $option->name;
            $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $option->oid;
          }
          break;
      }
    }
  }
  return array(
    'links' => $cart_links,
    'data' => $link_data,
  );
}