protected function CartLinksTest::createValidCartLinks in Ubercart 8.4
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
array $products: An array of products.
Return value
array Array containing Cart Links and link metadata.
6 calls to CartLinksTest::createValidCartLinks()
- CartLinksTest::testCartLinksAllowEmptying in uc_cart_links/
src/ Tests/ CartLinksTest.php - Tests Cart Links cart empty action.
- CartLinksTest::testCartLinksBasicFunctionality in uc_cart_links/
src/ Tests/ CartLinksTest.php - Tests Cart Links on a page under a variety of conditions.
- CartLinksTest::testCartLinksMessages in uc_cart_links/
src/ Tests/ CartLinksTest.php - Tests Cart Links custom messages.
- CartLinksTest::testCartLinksProductActionMessage in uc_cart_links/
src/ Tests/ CartLinksTest.php - Tests Cart Links product action messages.
- CartLinksTest::testCartLinksRestrictions in uc_cart_links/
src/ Tests/ CartLinksTest.php - Tests Cart Links restrictions.
File
- uc_cart_links/
src/ Tests/ CartLinksTest.php, line 802
Class
- CartLinksTest
- Tests the Cart Links functionality.
Namespace
Drupal\uc_cart_links\TestsCode
protected function createValidCartLinks(array $products = []) {
foreach ($products as $key => $product) {
$nid = $product
->id();
$title = $product
->label();
$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] = [
'nid' => $nid,
'title' => $title,
'qty' => $qty,
'attributes' => [],
];
// $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
->randomMachineName(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 [
'links' => $cart_links,
'data' => $link_data,
];
}