View source
<?php
namespace Drupal\Tests\uc_cart\Functional;
use Drupal\Core\Url;
use Drupal\Tests\uc_store\Functional\UbercartBrowserTestBase;
class CartSettingsTest extends UbercartBrowserTestBase {
protected static $modules = [
'uc_cart',
'block',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('page_title_block');
}
public function testAddToCartMessage() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->addToCart($this->product);
$assert
->pageTextContains($this->product
->getTitle() . ' added to your shopping cart.');
$this
->drupalGet('cart');
$this
->submitForm([], 'Remove');
$this
->drupalGet('admin/store/config/cart');
$this
->submitForm([
'uc_cart_add_item_msg' => FALSE,
], 'Save configuration');
$this
->addToCart($this->product);
$assert
->pageTextNotContains($this->product
->getTitle() . ' added to your shopping cart.');
}
public function testAddToCartRedirect() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/cart');
$assert
->fieldExists('uc_add_item_redirect');
$redirect = 'admin/store';
$this
->drupalGet('admin/store/config/cart');
$this
->submitForm([
'uc_add_item_redirect' => $redirect,
], 'Save configuration');
$this
->drupalGet('node/' . $this->product
->id());
$this
->submitForm([], 'Add to cart');
$this
->assertEquals(Url::fromUri('base:' . $redirect, [
'absolute' => TRUE,
])
->toString(), $this
->getUrl(), 'Add to cart redirect takes user to the correct URL.');
}
public function testAddToCartQueryRedirect() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/cart');
$this
->submitForm([
'uc_add_item_redirect' => '<none>',
], 'Save configuration');
$this
->drupalGet('node/' . $this->product
->id(), [
'query' => [
'test' => 'querystring',
],
]);
$this
->submitForm([], 'Add to cart');
$this
->assertEquals($this->product
->toUrl('canonical', [
'absolute' => TRUE,
'query' => [
'test' => 'querystring',
],
])
->toString(), $this
->getUrl(), 'Add to cart no-redirect preserves the query string.');
}
public function testEmptyCart() {
$assert = $this
->assertSession();
$this
->drupalGet('node/' . $this->product
->id());
$this
->submitForm([], 'Add to cart');
$assert
->responseNotContains('Empty cart');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/cart');
$assert
->fieldExists('uc_cart_empty_button');
$this
->submitForm([
'uc_cart_empty_button' => TRUE,
], 'Save configuration');
$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.');
}
public function testMinimumSubtotal() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/cart');
$assert
->fieldExists('uc_minimum_subtotal');
$minimum_subtotal = mt_rand(2, 9999);
$this
->submitForm([
'uc_minimum_subtotal' => $minimum_subtotal,
], 'Save configuration');
$product_below_limit = $this
->createProduct([
'price' => $minimum_subtotal - 1,
]);
$product_above_limit = $this
->createProduct([
'price' => $minimum_subtotal + 1,
]);
$this
->drupalLogout();
$this
->drupalGet('node/' . $product_below_limit
->id());
$this
->submitForm([], 'Add to cart');
$this
->drupalGet('cart');
$this
->submitForm([], 'Checkout');
$assert
->responseContains('The minimum order subtotal for checkout is');
$this
->drupalGet('node/' . $product_above_limit
->id());
$this
->submitForm([], 'Add to cart');
$this
->drupalGet('cart');
$this
->submitForm([], 'Checkout');
$assert
->pageTextContains('Enter your billing address and information here.');
}
public function testContinueShopping() {
$assert = $this
->assertSession();
$this
->drupalGet('node/' . $this->product
->id());
$this
->submitForm([], 'Add to cart');
$assert
->linkExists('Continue shopping');
$links = $this
->xpath('//a[@href="' . $this->product
->toUrl()
->toString() . '"]');
$this
->assertTrue(isset($links[0]), 'Continue shopping link returns to the product page.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/cart');
$assert
->fieldExists('uc_continue_shopping_type');
$assert
->fieldExists('uc_continue_shopping_url');
$settings = [
'uc_continue_shopping_type' => 'button',
'uc_continue_shopping_use_last_url' => FALSE,
'uc_continue_shopping_url' => 'admin/store',
];
$this
->submitForm($settings, 'Save configuration');
$this
->drupalGet('cart');
$this
->submitForm([], 'Continue shopping');
$this
->assertEquals(Url::fromUri('base:' . $settings['uc_continue_shopping_url'], [
'absolute' => TRUE,
])
->toString(), $this
->getUrl(), 'Continue shopping button takes the user to the correct URL.');
}
public function testCartBreadcrumb() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/cart');
$assert
->fieldExists('uc_cart_breadcrumb_text');
$assert
->fieldExists('uc_cart_breadcrumb_url');
$settings = [
'uc_cart_breadcrumb_text' => $this
->randomMachineName(8),
'uc_cart_breadcrumb_url' => $this
->randomMachineName(8),
];
$this
->submitForm($settings, 'Save configuration');
$this
->drupalGet('node/' . $this->product
->id());
$this
->submitForm([], 'Add to cart');
$assert
->linkExists($settings['uc_cart_breadcrumb_text'], 0);
$links = $this
->xpath('//a[@href="' . Url::fromUri('internal:/' . $settings['uc_cart_breadcrumb_url'], [
'absolute' => TRUE,
])
->toString() . '"]');
$this
->assertTrue(isset($links[0]), 'The breadcrumb link is set correctly.');
}
}