public function CartLinksTest::testCartLinksRestrictions in Ubercart 8.4
Tests Cart Links restrictions.
File
- uc_cart_links/
src/ Tests/ CartLinksTest.php, line 337
Class
- CartLinksTest
- Tests the Cart Links functionality.
Namespace
Drupal\uc_cart_links\TestsCode
public function testCartLinksRestrictions() {
// 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);
// Go to page with Cart Links.
$this
->drupalGet('node/' . $page
->id());
// Pick one of the links at random and restrict it.
$test_link_0 = array_rand($cart_links);
// Only this link is allowed - strip '/cart/add/' from beginning.
$this
->setCartLinksUiRestrictions(substr($cart_links[$test_link_0], 10));
// Attempt to click link - should pass.
$this
->drupalGet('node/' . $page
->id());
$this
->clickLink(t('Cart Link #@link', [
'@link' => $test_link_0,
]));
// Check for notice that item was added (this notice is set ON
// by default, see admin/store/config/cart).
$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'],
]));
// Check that the redirect to the cart worked.
$this
->assertUrl('cart');
// Pick another link at random, as long as it is different from first.
$in_cart = $cart_links[$test_link_0];
unset($cart_links[$test_link_0]);
$test_link = array_rand($cart_links);
// Attempt to click it.
// It should fail and redirect to the home page (default).
$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.');
// Check that the redirect to the home page (default) worked.
$this
->assertUrl('<front>');
// Now create a special redirect page for bad links.
$redirect_page = $this
->drupalCreateNode([
'body' => [
0 => [
'value' => 'ERROR: Invalid Cart Link!',
],
],
'promote' => 0,
]);
// Set redirect link.
$this
->setCartLinksUiRedirect('node/' . $redirect_page
->id());
// Attempt to click same restricted link as above.
// It should fail again but this time redirect to $redirect_page.
$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.');
// Check that the redirect to $redirect_page worked.
$this
->assertUrl('node/' . $redirect_page
->id());
// Remove restrictions, try to add again - it should pass.
$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'],
]));
// Check that the redirect to the cart worked.
$this
->assertUrl('cart');
$this
->drupalLogout();
}