You are here

function UCRestrictQtyFunctionalTest::testProductLimit in Ubercart Restrict Qty 7

Same name and namespace in other branches
  1. 6.2 tests/uc_restrict_qty.test \UCRestrictQtyFunctionalTest::testProductLimit()

File

tests/uc_restrict_qty.test, line 209
UC Restrict Qty auto-tests.

Class

UCRestrictQtyFunctionalTest
@file UC Restrict Qty auto-tests.

Code

function testProductLimit() {
  $admin = $this
    ->drupalCreateUser(array(
    'administer product features',
  ));
  $this
    ->drupalLogin($admin);
  $product = $this
    ->drupalCreateProduct();
  variable_set('uc_restrict_qty_global', 0);
  $product_settings = array(
    'model' => $product->model,
    'quantity' => 1,
    'lifetime' => 0,
  );
  $this
    ->drupalPost('node/' . $product->nid . '/edit/features/restrict_qty/add', $product_settings, t('Save feature'));
  $user1 = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user1);
  $this
    ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
  $this
    ->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array(
    '@product-title' => $product->title,
    '!url' => url('cart'),
  )), 'Product limit [#1 to add]');

  // Single item should be not included into input field
  $this
    ->assertRaw('<td class="qty">1</td>', 'Global limit [#1 without input in cart]');

  // Second should cause error
  $this
    ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
  $this
    ->assertRaw(format_plural($product_settings['quantity'], 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array(
    '!item' => $product->title,
  )), 'Product limit [above the limit]');
  $this
    ->drupalLogin($admin);
  $this
    ->drupalGet('node/' . $product->nid . '/edit/features');
  if (preg_match('|node/[0-9]*/edit/features/restrict_qty/[0-9]*|', $this->content, $matches)) {
    $edit_url = $matches[0];
    $product_settings = array(
      'model' => $product->model,
      'quantity' => 2,
      'lifetime' => FALSE,
    );
    $this
      ->drupalPost($edit_url, $product_settings, t('Save feature'));

    // Login another user (to get empty cart)
    $user2 = $this
      ->drupalCreateUser();
    $this
      ->drupalLogin($user2);
    $this
      ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
    $this
      ->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array(
      '@product-title' => $product->title,
      '!url' => url('cart'),
    )), 'Product limit [#2 to add|first]');

    // Item should be included into input field
    $this
      ->assertRaw('<input type="text" maxlength="6" name="items[0][qty]" id="edit-items-0-qty" size="5" value="1" class="form-text" />', 'Product limit [#2 with input in cart]');
    $this
      ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
    $this
      ->assertRaw(t('Your item(s) have been updated.'), '#2 to add|second');
    $this
      ->assertNoRaw(format_plural($product_settings['quantity'], 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array(
      '!item' => $product->title,
    )), 'Product limit [#2 to add|second|no warnings]');
    $this
      ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
    $this
      ->assertRaw(format_plural($product_settings['quantity'], 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array(
      '!item' => $product->title,
    )), 'Product limit [#2 to add|third|above limit warning]');
    $this
      ->assertRaw('<input type="text" maxlength="6" name="items[0][qty]" id="edit-items-0-qty" size="5" value="2" class="form-text" />', 'Product limit [#2 to add|third|#2 in cart]');

    // Test lifetime limit
    $this
      ->drupalLogin($admin);
    $product_settings = array(
      'model' => $product->model,
      'quantity' => 2,
      'lifetime' => 1,
    );
    $this
      ->drupalPost($edit_url, $product_settings, t('Save feature'));
    $user3 = $this
      ->drupalCreateUser(array(
      'view own orders',
    ));
    $this
      ->drupalLogin($user3);
    $products = array(
      $product,
    );
    $order = $this
      ->drupalCreateOrder($products, $user3);
    uc_order_update_status($order->order_id, 'completed');
    $this
      ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
    $this
      ->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array(
      '@product-title' => $product->title,
      '!url' => url('cart'),
    )), 'Product lifetime limit [first and last]');

    // It should be single item without input, as we have limit 2 with one alrready ordered
    $this
      ->assertRaw('<td class="qty">1</td>', 'Product lifetime limit [#1 without input in cart]');

    // Second should cause error
    $this
      ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
    $this
      ->assertRaw(format_plural($product_settings['quantity'] - count($products), 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array(
      '!item' => $product->title,
    )), 'Product lifetime limit [above the limit]');

    // Restrict adding product to the cart, if limit has been reached
    $this
      ->drupalLogin($admin);
    $product_settings = array(
      'model' => $product->model,
      'quantity' => 1,
      'lifetime' => 1,
    );
    $this
      ->drupalPost($edit_url, $product_settings, t('Save feature'));
    $this
      ->drupalLogin($user3);

    // Any adding to cart should cause warning, as everything available already ordered
    $this
      ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
    $this
      ->assertRaw(t('Sorry, you have reached the quantity limit for this product. You can not order more items of this product.'), 'Product lifetime limit [above the limit]');
  }
  else {
    $this
      ->fail('Feature edit link not found');
  }
}