You are here

public function CartCheckoutTest::testCheckoutComplete in Ubercart 8.4

Tests checkout complete functioning.

File

uc_cart/tests/src/Functional/CartCheckoutTest.php, line 531

Class

CartCheckoutTest
Tests the cart and checkout functionality.

Namespace

Drupal\Tests\uc_cart\Functional

Code

public function testCheckoutComplete() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

  // Payment notification is received first.
  $order_data = [
    'uid' => 0,
    'primary_email' => 'simpletest@example.com',
  ];
  $order = $this
    ->createOrder($order_data);
  uc_payment_enter($order
    ->id(), 'other', $order
    ->getTotal());
  $output = $this->cartManager
    ->completeSale($order);

  // Check that a new account was created.
  $this
    ->assertNotSame(FALSE, strpos($output['#message']['#markup'], 'new account has been created'), 'Checkout message mentions new account.');

  // 3 e-mails: new account, customer invoice, admin invoice.
  $this
    ->assertMailString('subject', 'Account details', 3, 'New account email was sent');
  $this
    ->assertMailString('subject', 'Your Order at Ubercart', 3, 'Customer invoice was sent');
  $this
    ->assertMailString('subject', 'New Order at Ubercart', 3, 'Admin notification was sent');
  $mails = $this
    ->getMails();
  $password = $mails[0]['params']['account']->password;
  $this
    ->assertNotEmpty($password, 'New password is not empty.');

  // Clear contents of mail collector.
  \Drupal::state()
    ->set('system.test_mail_collector', []);

  // Different user, sees the checkout page first.
  $order_data = [
    'uid' => 0,
    'primary_email' => 'simpletest2@example.com',
  ];
  $order = $this
    ->createOrder($order_data);
  $output = $this->cartManager
    ->completeSale($order);
  uc_payment_enter($order
    ->id(), 'other', $order
    ->getTotal());

  // 3 e-mails: new account, customer invoice, admin invoice.
  $this
    ->assertMailString('subject', 'Account details', 3, 'New account email was sent');
  $this
    ->assertMailString('subject', 'Your Order at Ubercart', 3, 'Customer invoice was sent');
  $this
    ->assertMailString('subject', 'New Order at Ubercart', 3, 'Admin notification was sent');
  $mails = $this
    ->getMails();
  $password = $mails[0]['params']['account']->password;
  $this
    ->assertNotEmpty($password, 'New password is not empty.');

  // Clear contents of mail collector.
  \Drupal::state()
    ->set('system.test_mail_collector', []);

  // Same user, new order.
  $order = $this
    ->createOrder($order_data);
  $output = $this->cartManager
    ->completeSale($order);
  uc_payment_enter($order
    ->id(), 'other', $order
    ->getTotal());

  // Check that no new account was created.
  $this
    ->assertNotSame(FALSE, strpos($output['#message']['#markup'], 'order has been attached to the account'), 'Checkout message mentions existing account.');

  // 2 e-mails: customer invoice, admin invoice.
  $this
    ->assertNoMailString('subject', 'Account details', 3, 'New account email was sent');
  $this
    ->assertMailString('subject', 'Your Order at Ubercart', 3, 'Customer invoice was sent');
  $this
    ->assertMailString('subject', 'New Order at Ubercart', 3, 'Admin notification was sent');
}