You are here

public function AuthcacheCommerceTest::testCommerceAddToCartWithFormCache in Authenticated User Page Caching (Authcache) 7.2

Test that products are added to the proper cart.

Bug: https://www.drupal.org/node/2475503

File

modules/authcache_commerce/authcache_commerce.test, line 140
Test cases for the Authcache Commerce module.

Class

AuthcacheCommerceTest
Tests for markup substitution.

Code

public function testCommerceAddToCartWithFormCache() {
  $this->stubmod
    ->hook('authcache_p13n_client', array(
    'authcache_p13n_test' => array(
      'title' => t('Test Client'),
      'enabled' => TRUE,
    ),
  ));

  // Create a clone of store_customer with the same set of roles.
  $cloned_customer = $this
    ->drupalCreateUser();
  $cloned_customer->roles = $this->store_customer->roles;
  user_save($cloned_customer);
  $this
    ->drupalLogin($this->store_customer);

  // Customer 1: Visit the product display in order to warm the form cache.
  $excluded_stub = $this->stubmod
    ->hook('authcache_excluded');
  $canceled_stub = $this->stubmod
    ->hook('authcache_canceled');
  $this
    ->drupalGet('node/' . $this->productNode->nid);
  $this
    ->assertStub($excluded_stub, HookStub::never());
  $this
    ->assertStub($canceled_stub, HookStub::never());
  $content = $this
    ->drupalGetContent();
  $url = $this
    ->getUrl();

  // Customer 2: Add one product to the cart.
  $this
    ->drupalLogin($cloned_customer);

  // Simulate a cache-hit by restoring the content and url from
  // store_customer.
  $this
    ->drupalSetContent($content, $url);

  // Compute CSRF token and supply it via the extra_post parameter.
  $token = $this
    ->drupalGetToken('commerce_cart_add_to_cart_form');
  $extra_post = '&form_token=' . urlencode($token);
  $this
    ->drupalPost(NULL, array(), 'Add to cart', array(), array(), NULL, $extra_post);

  // Ensure that we are not running into CSRF protection.
  $this
    ->assertNoText('The form has become outdated.');

  // Ensure the add to cart message is displayed.
  $message = t('%title added to <a href="!cart-url">your cart</a>.', array(
    '%title' => 'field_1_value_1_field_2_value_1',
    '!cart-url' => url('cart'),
  ));
  $this
    ->assertRaw($message, t('Product add to cart message displayed.'));

  // Go to cart url.
  $this
    ->drupalGet($this
    ->getCommerceUrl('cart'));

  // Test if the page resolves and there is something in the cart.
  $this
    ->assertResponse(200);
  $this
    ->assertNoText(t('Your shopping cart is empty.'), t('Cart is not empty'));
  $this
    ->assertText('field_1_value_1_field_2_value_1', t('Product was added to the cart'));
  $this
    ->drupalLogout();
}