View source
<?php
namespace Drupal\Tests\uc_cart\Functional;
use Drupal\uc_cart\CartInterface;
use Drupal\Tests\uc_store\Functional\UbercartBrowserTestBase;
class CartCheckoutTest extends UbercartBrowserTestBase {
protected static $modules = [
'uc_payment',
'uc_payment_pack',
];
protected $customer;
protected $cartManager;
protected $cart;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this->cartManager = \Drupal::service('uc_cart.manager');
$this->cart = $this->cartManager
->get();
$this->customer = $this
->drupalCreateUser();
$this
->createPaymentMethod('check');
\Drupal::configFactory()
->getEditable('system.mail')
->set('interface.uc_order', 'test_mail_collector')
->save();
}
public function testCartApi() {
$items = $this->cart
->getContents();
$this
->assertEquals([], $items, 'Cart is an empty array.');
$this->cart
->addItem($this->product
->id());
$items = $this->cart
->getContents();
$this
->assertCount(1, $items, 'Cart contains one item.');
$item = reset($items);
$this
->assertEquals($this->product
->id(), $item->nid->target_id, 'Cart item nid is correct.');
$this
->assertEquals(1, $item->qty->value, 'Cart item quantity is correct.');
$qty = mt_rand(1, 100);
$this->cart
->addItem($this->product
->id(), $qty);
$items = $this->cart
->getContents();
$this
->assertCount(1, $items, 'Updated cart contains one item.');
$item = reset($items);
$this
->assertEquals($qty + 1, $item->qty->value, 'Updated cart item quantity is correct.');
$qty = mt_rand(1, 100);
$item->qty->value = $qty;
$item->data->updated = TRUE;
$item
->save();
$items = $this->cart
->getContents();
$item = reset($items);
$this
->assertEquals($qty, $item->qty->value, 'Set cart item quantity is correct.');
$this
->assertTrue($item->data->updated, 'Set cart item data is correct.');
$this->cart
->addItem($this->product
->id(), 1, [
'test' => TRUE,
]);
$items = $this->cart
->getContents();
$this
->assertCount(2, $items, 'Updated cart contains two items.');
foreach ($items as $item) {
$item
->delete();
}
$items = $this->cart
->getContents();
$this
->assertCount(0, $items, 'Cart is empty after removal.');
$this->cart
->addItem($this->product
->id());
$this->cart
->emptyCart();
$items = $this->cart
->getContents();
$this
->assertEquals([], $items, 'Cart is emptied correctly.');
}
public function testCartPage() {
$assert = $this
->assertSession();
\Drupal::service('module_installer')
->install([
'uc_cart_entity_test',
], FALSE);
$this
->drupalGet('cart');
$assert
->pageTextContains('There are no products in your shopping cart.');
$this
->addToCart($this->product);
$assert
->pageTextContains($this->product
->label() . ' added to your shopping cart.');
$assert
->pageTextContains('hook_uc_cart_item_insert fired');
$this
->drupalGet('cart');
$assert
->pageTextContains($this->product
->label(), 'The product is in the cart.');
$assert
->fieldValueEquals('items[0][qty]', 1);
$this
->addToCart($this->product);
$assert
->pageTextContains('Your item(s) have been updated.');
$assert
->pageTextContains('hook_uc_cart_item_update fired');
$this
->drupalGet('cart');
$assert
->fieldValueEquals('items[0][qty]', 2);
$qty = mt_rand(3, 100);
$this
->drupalGet('cart');
$this
->submitForm([
'items[0][qty]' => $qty,
], 'Update cart');
$assert
->pageTextContains('Your cart has been updated.');
$assert
->fieldValueEquals('items[0][qty]', $qty);
$assert
->pageTextContains('hook_uc_cart_item_update fired');
$this
->drupalGet('cart');
$this
->submitForm([
'items[0][qty]' => 0,
], 'Update cart');
$assert
->pageTextContains('Your cart has been updated.');
$assert
->pageTextContains('There are no products in your shopping cart.');
$assert
->pageTextContains('hook_uc_cart_item_delete fired');
$this
->addToCart($this->product);
$this
->drupalGet('cart');
$this
->submitForm([], 'Remove');
$assert
->pageTextContains($this->product
->label() . ' removed from your shopping cart.');
$assert
->pageTextContains('There are no products in your shopping cart.');
$assert
->pageTextContains('hook_uc_cart_item_delete fired');
$this
->addToCart($this->product);
$this
->drupalGet('cart');
$assert
->pageTextNotContains('Empty cart');
\Drupal::configFactory()
->getEditable('uc_cart.settings')
->set('empty_button', TRUE)
->save();
$this
->drupalGet('cart');
$this
->submitForm([], 'Empty cart');
$assert
->pageTextContains('Are you sure you want to empty your shopping cart?');
$this
->submitForm([], 'Confirm');
$assert
->pageTextContains('There are no products in your shopping cart.');
$assert
->pageTextContains('hook_uc_cart_item_delete fired');
}
public function testCartMerge() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->customer);
$this
->addToCart($this->product);
$assert
->pageTextContains($this->product
->label() . ' added to your shopping cart.');
$this
->drupalLogout();
$this
->addToCart($this->product);
$assert
->pageTextContains($this->product
->label() . ' added to your shopping cart.');
$this
->drupalLogin($this->customer);
$this
->drupalGet('cart');
$assert
->pageTextContains($this->product
->label(), 'The product remains in the cart after logging in.');
$assert
->fieldValueEquals('items[0][qty]', 2);
}
public function testDeletedCartItem() {
$assert = $this
->assertSession();
$this
->addToCart($this->product);
$this->product
->delete();
$this
->drupalGet('cart');
$assert
->pageTextContains('There are no products in your shopping cart.');
$this
->assertSame([], $this->cart
->getContents(), 'There are no items in the cart.');
}
public function testCheckoutCartPane() {
$assert = $this
->assertSession();
$this
->addToCart($this->product);
$this
->drupalGet('cart');
$assert
->fieldValueEquals('items[0][qty]', 1);
$this
->submitForm([], 'Checkout');
$assert
->pageTextContains($this->product
->label(), 'The product title is displayed.');
$assert
->pageTextContains('1 ×', 'The product quantity is displayed.');
$assert
->pageTextContains(uc_currency_format($this->product->price->value), 'The product price is displayed.');
$qty = mt_rand(3, 100);
$this
->drupalGet('cart');
$this
->submitForm([
'items[0][qty]' => $qty,
], 'Checkout');
$assert
->pageTextContains($this->product
->label(), 'The product title is displayed.');
$assert
->pageTextContains($qty . ' ×', 'The updated product quantity is displayed.');
$assert
->pageTextContains(uc_currency_format($qty * $this->product->price->value), 'The updated product price is displayed.');
}
public function testAuthenticatedCheckout() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->customer);
$this
->addToCart($this->product);
$order = $this
->checkout();
$assert
->responseContains('Your order is complete!');
$assert
->responseContains('While logged in');
$this
->assertEquals($this->customer
->id(), $order
->getOwnerId(), 'Order has the correct user ID.');
$this
->assertEquals($this->customer
->getEmail(), $order
->getEmail(), 'Order has the correct email address.');
$this
->drupalGet('cart');
$assert
->pageTextContains('There are no products in your shopping cart.');
}
public function testAnonymousCheckoutAccountGenerated() {
$assert = $this
->assertSession();
$this
->addToCart($this->product);
$this
->checkout();
$assert
->responseContains('Your order is complete!');
$mails = $this
->getMails([
'id' => 'user_register_no_approval_required',
]);
$mail = array_pop($mails);
$account = $mail['params']['account'];
$this
->assertNotEmpty($account->name->value, 'New username is not empty.');
$this
->assertNotEmpty($account->password, 'New password is not empty.');
$this
->assertNotSame(FALSE, strpos($mail['body'], $account->name->value), 'Mail body contains username.');
$mails = $this
->getMails([
'subject' => 'Your Order at Ubercart',
]);
$mail = array_pop($mails);
$this
->assertNotSame(FALSE, strpos($mail['body'], $account->name->value), 'Invoice body contains username.');
$this
->assertNotSame(FALSE, strpos($mail['body'], $account->password), 'Invoice body contains password.');
$assert
->pageTextContains($account->name->value, 'Username is shown on screen.');
$assert
->pageTextContains($account->password, 'Password is shown on screen.');
$this
->drupalGet('cart');
$assert
->pageTextContains('There are no products in your shopping cart.');
$edit = [
'name' => $account->name->value,
'pass' => $account->password,
];
$this
->drupalGet('user');
$this
->submitForm($edit, 'Log in');
}
public function testAnonymousCheckoutAccountProvided() {
$assert = $this
->assertSession();
$settings = [
'uc_cart_new_account_name' => TRUE,
'uc_cart_new_account_password' => TRUE,
];
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/checkout');
$this
->submitForm($settings, 'Save configuration');
$this
->drupalLogout();
$username = $this
->randomMachineName(20);
$password = $this
->randomMachineName(20);
$this
->addToCart($this->product);
$this
->checkout([
'panes[customer][new_account][name]' => $username,
'panes[customer][new_account][pass]' => $password,
'panes[customer][new_account][pass_confirm]' => $password,
]);
$assert
->responseContains('Your order is complete!');
$assert
->pageTextContains($username, 'Username is shown on screen.');
$assert
->pageTextNotContains($password, 'Password is not shown on screen.');
$mails = $this
->getMails([
'id' => 'user_register_no_approval_required',
]);
$mail = array_pop($mails);
$this
->assertNotSame(FALSE, strpos($mail['body'], $username), 'Mail body contains username.');
$mails = $this
->getMails([
'subject' => 'Your Order at Ubercart',
]);
$mail = array_pop($mails);
$this
->assertNotSame(FALSE, strpos($mail['body'], $username), 'Invoice body contains username.');
$this
->assertSame(FALSE, strpos($mail['body'], $password), 'Invoice body does not contain password.');
$this
->drupalGet('cart');
$assert
->pageTextContains('There are no products in your shopping cart.');
$edit = [
'name' => $username,
'pass' => $password,
];
$this
->drupalGet('user');
$this
->submitForm($edit, 'Log in');
}
public function testAnonymousCheckoutAccountExists() {
$assert = $this
->assertSession();
$this
->addToCart($this->product);
$this
->checkout([
'panes[customer][primary_email]' => $this->customer
->getEmail(),
]);
$assert
->responseContains('Your order is complete!');
$assert
->responseContains('order has been attached to the account we found');
$this
->drupalGet('cart');
$assert
->pageTextContains('There are no products in your shopping cart.');
}
public function testCheckoutNewUsername() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$settings = [
'uc_cart_new_account_name' => TRUE,
'panes[delivery][status]' => FALSE,
'panes[billing][status]' => FALSE,
];
$this
->drupalGet('admin/store/config/checkout');
$this
->submitForm($settings, 'Save configuration');
$this
->drupalLogout();
$this
->addToCart($this->product);
$edit = [
'panes[customer][primary_email]' => $this
->randomMachineName(8) . '@example.com',
'panes[customer][new_account][name]' => $this->adminUser->name->value,
];
$this
->drupalGet('cart/checkout');
$this
->submitForm($edit, 'Review order');
$assert
->pageTextContains('The username ' . $this->adminUser->name->value . ' is already taken.');
$edit = [
'panes[customer][primary_email]' => $this
->randomMachineName(8) . '@example.com',
'panes[customer][new_account][name]' => '',
];
$this
->drupalGet('cart/checkout');
$this
->submitForm($edit, 'Review order');
$this
->submitForm([], 'Submit order');
$assert
->pageTextContains('Your order is complete!');
$assert
->pageTextContains('A new account has been created');
}
public function testCheckoutBlockedUser() {
$assert = $this
->assertSession();
$settings = [
'uc_new_customer_status_active' => FALSE,
];
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/checkout');
$this
->submitForm($settings, 'Save configuration');
$this
->drupalLogout();
$this
->addToCart($this->product);
$this
->checkout();
$assert
->responseContains('Your order is complete!');
$mails = $this
->getMails([
'id' => 'user_register_pending_approval',
]);
$this
->assertNotEmpty($mails, 'Blocked user email found.');
$mails = $this
->getMails([
'id' => 'user_register_no_approval_required',
]);
$this
->assertEmpty($mails, 'No unblocked user email found.');
}
public function testCheckoutLogin() {
$assert = $this
->assertSession();
$settings = [
'uc_new_customer_login' => TRUE,
];
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/checkout');
$this
->submitForm($settings, 'Save configuration');
$this
->drupalLogout();
$this
->addToCart($this->product);
$this
->checkout();
$assert
->responseContains('Your order is complete!');
$assert
->responseContains('you are already logged in');
$this
->drupalGet('<front>');
$assert
->pageTextContains('Member for ', 'User is logged in.');
$this
->drupalGet('cart');
$assert
->pageTextContains('There are no products in your shopping cart.');
}
public function testCheckoutComplete() {
$assert = $this
->assertSession();
$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);
$this
->assertNotSame(FALSE, strpos($output['#message']['#markup'], 'new account has been created'), 'Checkout message mentions new account.');
$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.');
\Drupal::state()
->set('system.test_mail_collector', []);
$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());
$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.');
\Drupal::state()
->set('system.test_mail_collector', []);
$order = $this
->createOrder($order_data);
$output = $this->cartManager
->completeSale($order);
uc_payment_enter($order
->id(), 'other', $order
->getTotal());
$this
->assertNotSame(FALSE, strpos($output['#message']['#markup'], 'order has been attached to the account'), 'Checkout message mentions existing account.');
$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');
}
public function testCartOrderTimeout() {
$assert = $this
->assertSession();
$this
->addToCart($this->product);
$this
->drupalGet('cart');
$this
->submitForm([], 'Checkout');
$assert
->pageTextContains('Enter your billing address and information here.', 'Viewed cart page: Billing pane has been displayed.');
$edit = $this
->populateCheckoutForm();
$oldname = $edit['panes[delivery][first_name]'];
$this
->drupalGet('cart/checkout');
$this
->submitForm($edit, 'Review order');
$order_ids = \Drupal::entityQuery('uc_order')
->condition('delivery_first_name', $oldname)
->execute();
$order_id = reset($order_ids);
if ($order_id) {
$this
->drupalGet('<front>');
$this
->addToCart($this->product);
$this
->drupalGet('cart');
$this
->submitForm([], 'Checkout');
$assert
->responseContains($oldname);
$this
->drupalGet('cart/checkout');
$this
->submitForm($edit, 'Review order');
$new_order_ids = \Drupal::entityQuery('uc_order')
->condition('delivery_first_name', $edit['panes[delivery][first_name]'])
->execute();
$new_order_id = reset($new_order_ids);
$this
->assertEquals($order_id, $new_order_id, 'Original order_id was reused.');
\Drupal::database()
->update('uc_orders')
->fields([
'changed' => \Drupal::time()
->getCurrentTime() - CartInterface::ORDER_TIMEOUT - 1,
])
->condition('order_id', $order_id)
->execute();
$this
->drupalGet('<front>');
$this
->drupalGet('cart');
$this
->submitForm([], 'Checkout');
$assert
->responseNotContains($oldname);
$newname = $this
->randomMachineName(10);
$edit['panes[delivery][first_name]'] = $newname;
$this
->drupalGet('cart/checkout');
$this
->submitForm($edit, 'Review order');
$new_order_ids = \Drupal::entityQuery('uc_order')
->condition('delivery_first_name', $newname)
->execute();
$new_order_id = reset($new_order_ids);
$this
->assertNotEquals($order_id, $new_order_id, 'New order was created after timeout.');
$old_order = \Drupal::entityTypeManager()
->getStorage('uc_order')
->loadUnchanged($order_id);
$this
->assertEquals('abandoned', $old_order
->getStatusId(), 'Original order was marked abandoned.');
}
else {
$this
->fail('No order was created.');
}
}
public function testCustomerInformationCheckoutPane() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->customer);
$this
->addToCart($this->product);
$this
->drupalGet('cart');
$this
->submitForm([], 'Checkout');
$mail = $this->customer
->getEmail();
$assert
->pageTextContains('Customer information');
$assert
->pageTextContains('Order information will be sent to your account e-mail listed below.');
$assert
->pageTextContains('E-mail address: ' . $mail);
$new_mail = $this
->randomMachineName() . '@example.com';
$this
->clickLink('edit');
$data = [
'current_pass' => $this->customer->pass_raw,
'mail' => $new_mail,
];
$this
->submitForm($data, 'Save');
$assert
->pageTextContains('Order information will be sent to your account e-mail listed below.');
$assert
->pageTextNotContains('E-mail address: ' . $mail);
$assert
->pageTextContains('E-mail address: ' . $new_mail);
}
}