You are here

public function CartLinksTest::testCartLinksBasicFunctionality in Ubercart 8.4

Tests Cart Links on a page under a variety of conditions.

File

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

Class

CartLinksTest
Tests the Cart Links functionality.

Namespace

Drupal\uc_cart_links\Tests

Code

public function testCartLinksBasicFunctionality() {

  // 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'];

  // Need to test incorrect links as well:
  // - links which add invalid attributes.
  // - links which omit required attributes.
  // Create a page containing these links.
  $page = $this
    ->createCartLinksPage($cart_links);

  //
  // Test clicking on links.
  //
  foreach ($cart_links as $key => $test_link) {
    $this
      ->drupalGet('node/' . $page
      ->id());

    // Look for link on page.
    $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,
    ]));

    // Click on link.
    $this
      ->clickLink(t('Cart Link #@link', [
      '@link' => $key,
    ]));

    // 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[$key]['title'],
    ]), SafeMarkup::format('Product @title added to cart.', [
      '@title' => $link_data[$key]['title'],
    ]));

    // Check that the redirect to the cart worked.
    $this
      ->assertUrl('cart');

    // Check contents of 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.');

    // Check for correct attribute name(s) in cart.
    foreach ($link_data[$key]['attributes'] as $label => $attribute) {
      $this
        ->assertText($label . ':', "Attribute {$label} correct in cart.");
      foreach ($attribute as $option) {

        // Check for correct option name(s) in cart.
        $this
          ->assertText($option, "Option {$option} correct in cart.");
      }
    }

    // Use the same link, but this time append an '_s' to turn
    // off message display for this product.
    $this
      ->drupalGet($test_link . '_s');

    // Default add-to-cart message is different when adding a duplicate item.
    $this
      ->assertNoText(t('Your item(s) have been updated.'), 'Default add-to-cart message suppressed.');

    // Empty cart (press remove button).
    $this
      ->drupalPostForm('cart', [], t('Remove'));
    $this
      ->assertText(t('There are no products in your shopping cart.'));
  }
}