You are here

public function CartLinksTest::testCartLinksAllowEmptying in Ubercart 8.4

Tests Cart Links cart empty action.

File

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

Class

CartLinksTest
Tests the Cart Links functionality.

Namespace

Drupal\uc_cart_links\Tests

Code

public function testCartLinksAllowEmptying() {

  // 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 empty cart action.
  //
  // Allow links to empty cart.
  $this
    ->setCartLinksUiAllowEmptying(TRUE);

  // Go to page with Cart Links.
  $this
    ->drupalGet('node/' . $page
    ->id());

  // Pick one of the links at random and add it to the cart.
  $test_link_0 = array_rand($cart_links);
  $this
    ->clickLink(t('Cart Link #@link', [
    '@link' => $test_link_0,
  ]));

  // Pick another link at random and prepend an 'e-' so it will empty cart.
  $in_cart = $cart_links[$test_link_0];

  // (Don't want to use the same link.)
  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.');

  // Allow.
  $this
    ->drupalPostForm(NULL, [], t('Confirm'));

  // Verify cart doesn't have the first item and does have the second item.
  $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.');

  // Still have something ($test_link) in the cart.
  // Forbid links to empty cart.
  $this
    ->setCartLinksUiAllowEmptying(FALSE);

  // Re-use $test_link_0 and prepend an 'e-' so it will (try to) empty cart.
  $this
    ->drupalGet(str_replace('add/p', 'add/e-p', $in_cart));

  // Verify the cart has both items - cart wasn't emptied.
  $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();
}