View source
<?php
namespace Drupal\uc_cart_links\Tests;
use Drupal\Core\Url;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\filter\Entity\FilterFormat;
use Drupal\uc_store\Tests\UbercartTestBase;
class CartLinksTest extends UbercartTestBase {
public static $modules = [
'uc_cart_links',
'uc_attribute',
'help',
'block',
];
public static $adminPermissions = [
'administer cart links',
'view cart links report',
'access administration pages',
];
protected function setUp() {
parent::setUp();
\Drupal::configFactory()
->getEditable('system.site')
->set('page.front', '/node')
->save();
$this
->drupalPlaceBlock('page_title_block');
$this
->drupalPlaceBlock('help_block', [
'region' => 'help',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
]);
$full_html_format
->save();
}
public function testCartLinksUiSettingsPage() {
$this
->drupalGet('admin/store/config/cart-links');
$this
->assertResponse(403);
$this
->assertText('Access denied');
$this
->assertText('You are not authorized to access this page.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/cart-links');
$this
->assertResponse(200);
$this
->assertText('View the help page to learn how to create Cart Links.', 'Settings page found.');
$this
->assertFieldByName('uc_cart_links_add_show', 0, 'Display Cart Links product action messages is off.');
$this
->assertFieldByName('uc_cart_links_track', 1, 'Track clicks is on.');
$this
->assertFieldByName('uc_cart_links_empty', 1, 'Allow Cart Links to empty carts is on.');
$this
->assertFieldByName('uc_cart_links_messages', '', 'Cart Links messages is empty.');
$this
->assertFieldByName('uc_cart_links_restrictions', '', 'Cart Links restrictions is empty.');
$this
->clickLink('View the help page');
$this
->assertText('http://www.example.com/cart/add/<cart_link_content>', 'Help text found.');
}
public function testCartLinksBasicFunctionality() {
$products[] = $this
->createCartLinksProduct(FALSE);
$products[] = $this
->createCartLinksProduct(FALSE);
$link_array = $this
->createValidCartLinks($products);
$cart_links = $link_array['links'];
$link_data = $link_array['data'];
$page = $this
->createCartLinksPage($cart_links);
foreach ($cart_links as $key => $test_link) {
$this
->drupalGet('node/' . $page
->id());
$this
->assertLink(t('Cart Link #@link', [
'@link' => $key,
]), 0, SafeMarkup::format('Cart Link #@link found on page.', [
'@link' => $key,
]));
$this
->assertLinkByHref(t('@link', [
'@link' => $test_link,
]), 0, SafeMarkup::format('Cart Link @link found on page.', [
'@link' => $test_link,
]));
$this
->clickLink(t('Cart Link #@link', [
'@link' => $key,
]));
$this
->assertText(t('@title added to your shopping cart.', [
'@title' => $link_data[$key]['title'],
]), SafeMarkup::format('Product @title added to cart.', [
'@title' => $link_data[$key]['title'],
]));
$this
->assertUrl('cart');
$this
->assertText($link_data[$key]['title'], 'Product title correct in cart.');
$this
->assertFieldByName('items[0][qty]', $link_data[$key]['qty'], 'Product quantity correct in cart.');
foreach ($link_data[$key]['attributes'] as $label => $attribute) {
$this
->assertText($label . ':', "Attribute {$label} correct in cart.");
foreach ($attribute as $option) {
$this
->assertText($option, "Option {$option} correct in cart.");
}
}
$this
->drupalGet($test_link . '_s');
$this
->assertNoText(t('Your item(s) have been updated.'), 'Default add-to-cart message suppressed.');
$this
->drupalPostForm('cart', [], t('Remove'));
$this
->assertText(t('There are no products in your shopping cart.'));
}
}
public function testCartLinksProductActionMessage() {
$products[] = $this
->createCartLinksProduct(FALSE);
$products[] = $this
->createCartLinksProduct(FALSE);
$link_array = $this
->createValidCartLinks($products);
$cart_links = $link_array['links'];
$link_data = $link_array['data'];
$page = $this
->createCartLinksPage($cart_links);
$this
->drupalLogin($this->adminUser);
$this
->setCartLinksUiProductActionMessage(TRUE);
$this
->drupalGet('node/' . $page
->id());
$test_link = array_rand($cart_links);
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link,
]));
$this
->assertText(t('Cart Link product action: @link', [
'@link' => substr($cart_links[$test_link], 10),
]), 'Cart Link product action message found.');
$this
->assertUrl('cart');
$this
->drupalPostForm('cart', [], 'Remove');
$this
->assertText('There are no products in your shopping cart.');
$this
->setCartLinksUiProductActionMessage(FALSE);
$this
->drupalGet('node/' . $page
->id());
$test_link = array_rand($cart_links);
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link,
]));
$this
->assertNoText(t('Cart Link product action: @link', [
'@link' => substr($cart_links[$test_link], 10),
]), 'Cart Link product action message not present.');
$this
->drupalLogout();
}
public function testCartLinksAllowEmptying() {
$products[] = $this
->createCartLinksProduct(FALSE);
$products[] = $this
->createCartLinksProduct(FALSE);
$link_array = $this
->createValidCartLinks($products);
$cart_links = $link_array['links'];
$link_data = $link_array['data'];
$page = $this
->createCartLinksPage($cart_links);
$this
->drupalLogin($this->adminUser);
$this
->setCartLinksUiAllowEmptying(TRUE);
$this
->drupalGet('node/' . $page
->id());
$test_link_0 = array_rand($cart_links);
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link_0,
]));
$in_cart = $cart_links[$test_link_0];
unset($cart_links[$test_link_0]);
$test_link = array_rand($cart_links);
$this
->drupalGet(str_replace('add/p', 'add/e-p', $cart_links[$test_link]));
$this
->assertText(t('The current contents of your shopping cart will be lost. Are you sure you want to continue?'), 'Empty cart confirmation page found.');
$this
->drupalPostForm(NULL, [], t('Confirm'));
$this
->drupalGet('cart');
$this
->assertText($link_data[$test_link]['title'], 'Product title correct in cart.');
$this
->assertNoText($link_data[$test_link_0]['title'], 'Cart was emptied by Cart Link.');
$this
->setCartLinksUiAllowEmptying(FALSE);
$this
->drupalGet(str_replace('add/p', 'add/e-p', $in_cart));
$this
->drupalGet('cart');
$this
->assertText($link_data[$test_link_0]['title'], 'Cart was not emptied by Cart Link.');
$this
->assertText($link_data[$test_link]['title'], 'Cart was not emptied by Cart Link.');
$this
->drupalLogout();
}
public function testCartLinksRestrictions() {
$products[] = $this
->createCartLinksProduct(FALSE);
$products[] = $this
->createCartLinksProduct(FALSE);
$link_array = $this
->createValidCartLinks($products);
$cart_links = $link_array['links'];
$link_data = $link_array['data'];
$page = $this
->createCartLinksPage($cart_links);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/' . $page
->id());
$test_link_0 = array_rand($cart_links);
$this
->setCartLinksUiRestrictions(substr($cart_links[$test_link_0], 10));
$this
->drupalGet('node/' . $page
->id());
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link_0,
]));
$this
->assertText(t('@title added to your shopping cart.', [
'@title' => $link_data[$test_link_0]['title'],
]), SafeMarkup::format('Product @title added to cart.', [
'@title' => $link_data[$test_link_0]['title'],
]));
$this
->assertUrl('cart');
$in_cart = $cart_links[$test_link_0];
unset($cart_links[$test_link_0]);
$test_link = array_rand($cart_links);
$this
->drupalGet('node/' . $page
->id());
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link,
]));
$this
->assertText('Welcome to Drupal');
$this
->assertText('No front page content has been created yet.', 'Redirected to front page for link not in restrictions.');
$this
->assertUrl('<front>');
$redirect_page = $this
->drupalCreateNode([
'body' => [
0 => [
'value' => 'ERROR: Invalid Cart Link!',
],
],
'promote' => 0,
]);
$this
->setCartLinksUiRedirect('node/' . $redirect_page
->id());
$this
->drupalGet('node/' . $page
->id());
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link,
]));
$this
->assertText('ERROR: Invalid Cart Link!', 'Redirected to error page for link not in restrictions.');
$this
->assertUrl('node/' . $redirect_page
->id());
$this
->setCartLinksUiRestrictions('');
$this
->drupalGet('node/' . $page
->id());
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link,
]));
$this
->assertText(t('@title added to your shopping cart.', [
'@title' => $link_data[$test_link]['title'],
]), SafeMarkup::format('Product @title added to cart.', [
'@title' => $link_data[$test_link]['title'],
]));
$this
->assertUrl('cart');
$this
->drupalLogout();
}
public function testCartLinksMessages() {
$products[] = $this
->createCartLinksProduct(FALSE);
$products[] = $this
->createCartLinksProduct(FALSE);
$link_array = $this
->createValidCartLinks($products);
$cart_links = $link_array['links'];
$link_data = $link_array['data'];
$page = $this
->createCartLinksPage($cart_links);
$this
->drupalLogin($this->adminUser);
$messages = [];
for ($i = 0; $i < 15; $i++) {
$key = mt_rand(1, 999);
$messages[$key] = $key . '|' . $this
->randomMachineName(32);
}
$this
->setCartLinksUiMessages($messages);
$this
->drupalGet('node/' . $page
->id());
$test_link = array_rand($cart_links);
$message_key = array_rand($messages);
$message_text = explode('|', $messages[$message_key]);
$this
->drupalGet($cart_links[$test_link] . '-m' . $message_key);
$this
->assertText(t('@message', [
'@message' => $message_text[1],
]), SafeMarkup::format('Message @key displayed.', [
'@key' => $message_key,
]));
$this
->drupalPostForm('cart', [], t('Remove'));
$this
->assertText(t('There are no products in your shopping cart.'));
$this
->drupalLogout();
}
public function testCartLinksTracking() {
$products[] = $this
->createCartLinksProduct(FALSE);
$products[] = $this
->createCartLinksProduct(FALSE);
$link_array = $this
->createValidCartLinks($products);
$cart_links = $link_array['links'];
$link_data = $link_array['data'];
$page = $this
->createCartLinksPage($cart_links);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/' . $page
->id());
$tracking = [];
for ($i = 0; $i < 3; $i++) {
$tracking[$this
->randomMachineName(16)] = 0;
}
for ($i = 0; $i < 50; $i++) {
$test_link = array_rand($cart_links);
$tracking_id = array_rand($tracking);
$this
->drupalGet($cart_links[$test_link] . '-i' . $tracking_id);
$tracking[$tracking_id] += 1;
}
arsort($tracking, SORT_NUMERIC);
$this
->drupalGet('admin/store/reports/cart-links');
$total = 0;
foreach ($tracking as $id => $clicks) {
$total += $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();
}
protected function setCartLinksUiProductActionMessage($state = FALSE) {
$this
->drupalPostForm('admin/store/config/cart-links', [
'uc_cart_links_add_show' => $state,
], 'Save configuration');
$this
->assertFieldByName('uc_cart_links_add_show', $state, SafeMarkup::format('Display Cart Links product action messages is @state.', [
'@state' => $state ? 'TRUE' : 'FALSE',
]));
}
protected function setCartLinksUiTrackClicks($state = TRUE) {
$this
->drupalPostForm('admin/store/config/cart-links', [
'uc_cart_links_track' => 0,
], 'Save configuration');
$this
->assertFieldByName('uc_cart_links_track', $state ? 1 : 0, SafeMarkup::format('Track clicks is @state.', [
'@state' => $state ? 'TRUE' : 'FALSE',
]));
}
protected function setCartLinksUiAllowEmptying($state = TRUE) {
$this
->drupalPostForm('admin/store/config/cart-links', [
'uc_cart_links_empty' => $state,
], 'Save configuration');
$this
->assertFieldByName('uc_cart_links_empty', $state, SafeMarkup::format('Allow Cart Links to empty carts is @state.', [
'@state' => $state ? 'TRUE' : 'FALSE',
]));
}
protected function setCartLinksUiMessages($messages = '') {
$message_string = implode("\n", $messages);
$this
->drupalPostForm('admin/store/config/cart-links', [
'uc_cart_links_messages' => $message_string,
], 'Save configuration');
$this
->assertFieldByName('uc_cart_links_messages', $message_string, SafeMarkup::format('Cart Links messages contains "@messages".', [
'@messages' => $message_string,
]));
}
protected function setCartLinksUiRestrictions($restrictions = '') {
$this
->drupalPostForm('admin/store/config/cart-links', [
'uc_cart_links_restrictions' => $restrictions,
], 'Save configuration');
$this
->assertFieldByName('uc_cart_links_restrictions', $restrictions, SafeMarkup::format('Cart Links restrictions contains "@restrictions".', [
'@restrictions' => $restrictions,
]));
}
protected function setCartLinksUiRedirect($url = '') {
$this
->drupalPostForm('admin/store/config/cart-links', [
'uc_cart_links_invalid_page' => $url,
], 'Save configuration');
$this
->assertFieldByName('uc_cart_links_invalid_page', $url, SafeMarkup::format('Cart Links invalid page URL contains ":url".', [
':url' => $url,
]));
}
protected function createCartLinksPage(array $links = []) {
$item_list = [
'#theme' => 'links',
'#links' => [],
];
if (!empty($links)) {
$i = 0;
foreach ($links as $link) {
$item_list['#links'][] = [
'title' => t('Cart Link #@num', [
'@num' => $i++,
]),
'url' => Url::fromUri('base:' . $link),
];
}
}
$page = [
'type' => 'page',
'body' => [
0 => [
'value' => !empty($links) ? \Drupal::service('renderer')
->renderPlain($item_list) : $this
->randomMachineName(128),
'format' => 'full_html',
],
],
'promote' => 0,
];
return $this
->drupalCreateNode($page);
}
protected function createCartLinksProduct($product_class = FALSE) {
if ($product_class) {
$product = $this
->createProductClass([
'promote' => 0,
]);
}
else {
$product = $this
->createProduct([
'promote' => 0,
]);
}
for ($i = 0; $i < 5; $i++) {
$attribute = $this
->createAttribute();
$attributes[$attribute->aid] = $attribute;
}
$attribute_aids = array_keys($attributes);
$all_options = [];
foreach ($attribute_aids as $aid) {
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption([
'aid' => $aid,
]);
$all_options[$option->aid][$option->oid] = $option;
}
}
$attribute = uc_attribute_load($attribute->aid);
$attributes_with_options = uc_attribute_load_multiple();
$aids = array_rand($attributes, 3);
$loaded_attributes = uc_attribute_load_multiple($aids);
foreach ($loaded_attributes as $loaded_attribute) {
uc_attribute_subject_save($loaded_attribute, 'product', $product
->id(), TRUE);
}
return $product;
}
protected function createValidCartLinks(array $products = []) {
foreach ($products as $key => $product) {
$nid = $product
->id();
$title = $product
->label();
$qty = mt_rand(1, 19);
$link_data[$key] = [
'nid' => $nid,
'title' => $title,
'qty' => $qty,
'attributes' => [],
];
$cart_links[$key] = '/cart/add/p' . $nid . '_q' . $qty;
$attributes = uc_product_get_attributes($nid);
foreach ($attributes as $attribute) {
switch ($attribute->display) {
case 0:
$value = $this
->randomMachineName(12);
$link_data[$key]['attributes'][$attribute->label][] = $value;
$cart_links[$key] .= '_a' . $attribute->aid . 'o' . $value;
break;
case 1:
case 2:
$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:
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,
];
}
}