View source
<?php
namespace Drupal\Tests\commerce_purchase_order\Functional;
use Behat\Mink\Element\NodeElement;
use Drupal\commerce_payment\Entity\PaymentGateway;
use Drupal\Tests\commerce\Functional\CommerceBrowserTestBase;
class PurchaseOrderTest extends CommerceBrowserTestBase {
protected $account;
protected $product;
public static $modules = [
'commerce_product',
'commerce_order',
'commerce_cart',
'commerce_checkout',
'commerce_payment',
'commerce_purchase_order',
];
protected $defaultTheme = 'stark';
protected function getAdministratorPermissions() {
return array_merge([
'administer commerce_checkout_flow',
], parent::getAdministratorPermissions());
}
protected function setUp() {
parent::setUp();
$this->store
->set('billing_countries', [
'US',
]);
$this->store
->save();
$payment_gateway = PaymentGateway::create([
'id' => 'po',
'label' => 'PO',
'plugin' => 'purchase_order_gateway',
'configuration' => [
'limit_open' => 3,
'user_approval' => 0,
'instructions' => [
'value' => 'Sample payment instructions.',
'format' => 'plain_text',
],
],
]);
$payment_gateway
->save();
$variation = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'price' => [
'number' => 9.99,
'currency_code' => 'USD',
],
]);
$this->product = $this
->createEntity('commerce_product', [
'type' => 'default',
'title' => 'My product',
'variations' => [
$variation,
],
'stores' => [
$this->store,
],
]);
$this
->placeBlock('commerce_cart');
$this
->placeBlock('commerce_checkout_progress');
}
public function testPurchaseOrderCheckout() {
$this
->drupalLogout();
$this
->drupalGet($this->product
->toUrl());
$this
->submitForm([], 'Add to cart');
$this
->assertSession()
->pageTextContains('1 item');
$cart_link = $this
->getSession()
->getPage()
->findLink('your cart');
$cart_link
->click();
$this
->submitForm([], 'Checkout');
$this
->assertSession()
->pageTextNotContains('Order Summary');
$this
->assertCheckoutProgressStep('Login');
$this
->submitForm([], 'Continue as Guest');
$this
->assertCheckoutProgressStep('Order information');
$this
->submitForm([
'payment_information[add_payment_method][payment_details][number]' => 'a99',
'contact_information[email]' => 'guest@example.com',
'contact_information[email_confirm]' => 'guest@example.com',
'payment_information[add_payment_method][billing_information][address][0][address][given_name]' => 'John',
'payment_information[add_payment_method][billing_information][address][0][address][family_name]' => 'Smith',
'payment_information[add_payment_method][billing_information][address][0][address][organization]' => 'Centarro',
'payment_information[add_payment_method][billing_information][address][0][address][address_line1]' => '9 Drupal Ave',
'payment_information[add_payment_method][billing_information][address][0][address][postal_code]' => '94043',
'payment_information[add_payment_method][billing_information][address][0][address][locality]' => 'Mountain View',
'payment_information[add_payment_method][billing_information][address][0][address][administrative_area]' => 'CA',
], 'Continue to review');
$this
->assertCheckoutProgressStep('Review');
$this
->assertSession()
->pageTextContains('Contact information');
$this
->assertSession()
->pageTextContains('Payment information');
$this
->assertSession()
->pageTextContains('Order Summary');
$this
->submitForm([], 'Pay and complete purchase');
$this
->assertSession()
->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
$this
->assertSession()
->pageTextContains('0 items');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this->product
->toUrl());
$this
->submitForm([], 'Add to cart');
$this
->assertSession()
->pageTextContains('1 item');
$cart_link = $this
->getSession()
->getPage()
->findLink('your cart');
$cart_link
->click();
$this
->submitForm([], 'Checkout');
$this
->assertSession()
->pageTextContains('Order Summary');
$this
->assertCheckoutProgressStep('Order information');
$this
->submitForm([
'payment_information[add_payment_method][payment_details][number]' => 'b99',
'payment_information[add_payment_method][billing_information][address][0][address][given_name]' => 'John',
'payment_information[add_payment_method][billing_information][address][0][address][family_name]' => 'Smith',
'payment_information[add_payment_method][billing_information][address][0][address][organization]' => 'Centarro',
'payment_information[add_payment_method][billing_information][address][0][address][address_line1]' => '9 Drupal Ave',
'payment_information[add_payment_method][billing_information][address][0][address][postal_code]' => '94043',
'payment_information[add_payment_method][billing_information][address][0][address][locality]' => 'Mountain View',
'payment_information[add_payment_method][billing_information][address][0][address][administrative_area]' => 'CA',
], 'Continue to review');
$this
->assertCheckoutProgressStep('Review');
$this
->assertSession()
->pageTextContains('Contact information');
$this
->assertSession()
->pageTextContains('Payment information');
$this
->assertSession()
->pageTextContains('Order Summary');
$this
->submitForm([], 'Pay and complete purchase');
$this
->assertSession()
->pageTextContains('Your order number is 2');
$this
->assertSession()
->pageTextContainsOnce('Sample payment instructions.');
$this
->assertSession()
->pageTextContains('0 items');
}
protected function assertCheckoutProgressStep($expected) {
$current_step_node = $this
->getSession()
->getPage()
->find('css', '.checkout-progress--step__current');
if (!$current_step_node instanceof NodeElement) {
$this
->fail('Checkout progress element is not present.');
}
$current_step = $current_step_node
->getText();
$this
->assertEquals($expected, $current_step);
}
}