View source
<?php
namespace Drupal\Tests\uc_store\FunctionalJavascript;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\uc_country\Entity\Country;
use Drupal\uc_order\Entity\Order;
use Drupal\Tests\uc_attribute\Traits\AttributeTestTrait;
use Drupal\Tests\uc_order\Traits\OrderTestTrait;
abstract class UbercartJavascriptTestBase extends JavascriptTestBase {
use AssertMailTrait;
use AttributeTestTrait;
use OrderTestTrait;
protected $profile = 'testing';
protected static $modules = [
'block',
'uc_cart',
];
protected $strictConfigSchema = FALSE;
protected $adminUser;
protected static $adminPermissions = [
'access administration pages',
'access content',
'administer store',
'administer countries',
'administer order workflow',
'administer product classes',
'administer product features',
'administer products',
'administer content types',
'create product content',
'delete any product content',
'edit any product content',
'create orders',
'view all orders',
'edit orders',
'delete orders',
'unconditionally delete orders',
];
protected $product;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
$class = get_class($this);
$adminPermissions = [];
while ($class) {
if (property_exists($class, 'adminPermissions')) {
$adminPermissions = array_merge($adminPermissions, $class::$adminPermissions);
}
$class = get_parent_class($class);
}
$countries = \Drupal::service('country_manager')
->getAvailableList();
$country_ids = array_rand($countries, 8);
foreach ($country_ids as $country_id) {
Country::load($country_id)
->enable()
->save();
}
\Drupal::configFactory()
->getEditable('uc_store.settings')
->set('address.country', $country_id)
->save();
$this->adminUser = $this
->drupalCreateUser($adminPermissions);
$this->product = $this
->createProduct([
'uid' => $this->adminUser
->id(),
'promote' => 0,
]);
}
protected function addToCart($product, array $options = []) {
$this
->drupalGet('node/' . $product
->id());
$this
->submitForm($options, 'Add to cart');
}
protected function populateCheckoutForm(array $edit = []) {
foreach ([
'billing',
'delivery',
] as $pane) {
$prefix = 'panes[' . $pane . ']';
$key = $prefix . '[country]';
$country_id = isset($edit[$key]) ? $edit[$key] : \Drupal::config('uc_store.settings')
->get('address.country');
$country = \Drupal::service('country_manager')
->getCountry($country_id);
$edit += [
$prefix . '[first_name]' => $this
->randomMachineName(10),
$prefix . '[last_name]' => $this
->randomMachineName(10),
$prefix . '[street1]' => $this
->randomMachineName(10),
$prefix . '[city]' => $this
->randomMachineName(10),
$prefix . '[postal_code]' => (string) mt_rand(10000, 99999),
$prefix . '[country]' => $country_id,
];
if (!empty($country
->getZones())) {
$edit += [
$prefix . '[zone]' => array_rand($country
->getZones()),
];
}
}
if (!isset($edit['panes[customer][primary_email]']) && !$this->loggedInUser) {
$edit['panes[customer][primary_email]'] = $this
->randomMachineName(8) . '@example.com';
}
return $edit;
}
protected function checkout(array $edit = []) {
$assert = $this
->assertSession();
$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($edit);
$this
->drupalGet('cart/checkout');
$this
->submitForm($edit, 'Review order');
$assert
->pageTextContains('Your order is almost complete.');
$this
->submitForm([], 'Submit order');
$order_ids = \Drupal::entityQuery('uc_order')
->condition('billing_first_name', $edit['panes[billing][first_name]'])
->execute();
$order_id = reset($order_ids);
if ($order_id) {
$this
->assertTrue(TRUE, format_string('Order %order_id has been created', [
'%order_id' => $order_id,
]));
$order = Order::load($order_id);
}
else {
$this
->fail('No order was created.');
$order = FALSE;
}
return $order;
}
protected function createPaymentMethod($plugin_id, array $values = []) {
$has_user = $this->loggedInUser;
if (!$has_user) {
$this
->drupalLogin($this->adminUser);
}
$values += [
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
];
$this
->drupalGet('admin/store/config/payment/add/' . $plugin_id);
$this
->submitForm($values, 'Save');
if (!$has_user) {
$this
->drupalLogout();
}
return $values;
}
protected function assertNoMailString($field_name, $string, $email_depth, $message = '', $group = 'Other') {
$mails = $this
->getMails();
$string_found = FALSE;
for ($i = count($mails) - 1; $i >= count($mails) - $email_depth && $i >= 0; $i--) {
$mail = $mails[$i];
$normalized_mail = preg_replace('/\\s+/', ' ', $mail[$field_name]);
$normalized_string = preg_replace('/\\s+/', ' ', $string);
$string_found = FALSE !== strpos($normalized_mail, $normalized_string);
if ($string_found) {
break;
}
}
if (!$message) {
$message = format_string('Expected text not found in @field of email message: "@expected".', [
'@field' => $field_name,
'@expected' => $string,
]);
}
return $this
->assertFalse($string_found, $message, $group);
}
}