You are here

public function UbercartCartCheckoutTestCase::testCheckout in Ubercart 7.3

Same name and namespace in other branches
  1. 6.2 uc_cart/uc_cart.test \UbercartCartCheckoutTestCase::testCheckout()

Tests the checkout process.

File

uc_cart/tests/uc_cart.test, line 225
Shopping cart and checkout tests.

Class

UbercartCartCheckoutTestCase
Tests the cart and checkout functionality.

Code

public function testCheckout() {

  // Allow customer to specify username and password,
  // but don't log in after checkout.
  $settings = array(
    'uc_cart_new_account_name' => TRUE,
    'uc_cart_new_account_password' => TRUE,
    'uc_new_customer_login' => FALSE,
  );
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalPost('admin/store/settings/checkout', $settings, t('Save configuration'));
  $this
    ->drupalLogout();
  $new_user = new stdClass();
  $new_user->name = $this
    ->randomName(20);
  $new_user->pass_raw = $this
    ->randomName(20);

  // Test as anonymous user.
  $this
    ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
  $this
    ->checkout(array(
    'panes[customer][new_account][name]' => $new_user->name,
    'panes[customer][new_account][pass]' => $new_user->pass_raw,
    'panes[customer][new_account][pass_confirm]' => $new_user->pass_raw,
  ));
  $this
    ->assertRaw('Your order is complete!');
  $this
    ->assertText($new_user->name, 'Username is shown on screen.');
  $this
    ->assertNoText($new_user->pass_raw, 'Password is not shown on screen.');

  // Check that cart is now empty.
  $this
    ->drupalGet('cart');
  $this
    ->assertText('There are no products in your shopping cart.');

  // Test new account email.
  $mail = $this
    ->drupalGetMails(array(
    'id' => 'user_register_no_approval_required',
  ));
  $mail = array_pop($mail);
  $this
    ->assertTrue(strpos($mail['body'], $new_user->name) !== FALSE, 'Mail body contains username.');

  // Test invoice email.
  $mail = $this
    ->drupalGetMails(array(
    'subject' => 'Your Order at Ubercart',
  ));
  $mail = array_pop($mail);
  $this
    ->assertTrue(strpos($mail['body'], $new_user->name) !== FALSE, 'Invoice body contains username.');
  $this
    ->assertFalse(strpos($mail['body'], $new_user->pass_raw) !== FALSE, 'Mail body does not contain password.');

  // Check that the password works.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($new_user);

  // Test again as authenticated user.
  $this
    ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
  $this
    ->checkout();
  $this
    ->assertRaw('Your order is complete!');
  $this
    ->assertRaw('While logged in');

  // Test again with generated username and password.
  $this
    ->drupalLogout();
  $this
    ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
  $this
    ->checkout();
  $this
    ->assertRaw('Your order is complete!');

  // Test new account email.
  $mail = $this
    ->drupalGetMails(array(
    'id' => 'user_register_no_approval_required',
  ));
  $mail = array_pop($mail);
  $new_user = new stdClass();
  $new_user->name = $mail['params']['account']->name;
  $new_user->pass_raw = $mail['params']['account']->password;
  $this
    ->assertTrue(!empty($new_user->name), 'New username is not empty.');
  $this
    ->assertTrue(!empty($new_user->pass_raw), 'New password is not empty.');
  $this
    ->assertTrue(strpos($mail['body'], $new_user->name) !== FALSE, 'Mail body contains username.');

  // Test invoice email.
  $mail = $this
    ->drupalGetMails(array(
    'subject' => 'Your Order at Ubercart',
  ));
  $mail = array_pop($mail);
  $this
    ->assertTrue(strpos($mail['body'], $new_user->name) !== FALSE, 'Invoice body contains username.');
  $this
    ->assertTrue(strpos($mail['body'], $new_user->pass_raw) !== FALSE, 'Invoice body contains password.');

  // We can check the password now we know it.
  $this
    ->assertText($new_user->name, 'Username is shown on screen.');
  $this
    ->assertText($new_user->pass_raw, 'Password is shown on screen.');

  // Check that the password works.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($new_user);

  // Test again with an existing email address.
  $this
    ->drupalLogout();
  $this
    ->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
  $this
    ->checkout(array(
    'panes[customer][primary_email]' => $this->customer->mail,
  ));
  $this
    ->assertRaw('Your order is complete!');
  $this
    ->assertRaw('order has been attached to the account we found');
}