View source
<?php
namespace Drupal\Tests\uc_order\Functional;
use Drupal\uc_order\Entity\Order;
use Drupal\uc_store\Address;
use Drupal\Tests\uc_store\Functional\UbercartBrowserTestBase;
class OrderTest extends UbercartBrowserTestBase {
protected $customer;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this->customer = $this
->drupalCreateUser([
'view own orders',
]);
}
public function testOrderApi() {
$order = Order::create();
$order
->save();
$this
->assertEquals(0, $order
->getOwnerId(), 'New order is anonymous.');
$this
->assertEquals('in_checkout', $order
->getStatusId(), 'New order is in checkout.');
$order = Order::create([
'uid' => $this->customer
->id(),
'order_status' => uc_order_state_default('completed'),
]);
$order
->save();
$this
->assertEquals($this->customer
->id(), $order
->getOwnerId(), 'New order has correct uid.');
$this
->assertEquals('completed', $order
->getStatusId(), 'New order is marked completed.');
$order
->delete();
$deleted_order = Order::load($order
->id());
$this
->assertFalse($deleted_order, 'Order was successfully deleted');
}
public function testOrderEntity() {
$order = Order::create();
$this
->assertEquals(0, $order
->getOwnerId(), 'New order is anonymous.');
$this
->assertEquals('in_checkout', $order
->getStatusId(), 'New order is in checkout.');
$name = $this
->randomMachineName();
$order = Order::create([
'uid' => $this->customer
->id(),
'order_status' => 'completed',
'billing_first_name' => $name,
'billing_last_name' => $name,
]);
$this
->assertEquals($this->customer
->id(), $order
->getOwnerId(), 'New order has correct uid.');
$this
->assertEquals('completed', $order
->getStatusId(), 'New order is marked completed.');
$this
->assertEquals($name, $order
->getAddress('billing')
->getFirstName(), 'New order has correct name.');
$this
->assertEquals($name, $order
->getAddress('billing')
->getLastName(), 'New order has correct name.');
$order
->save();
$storage = \Drupal::entityTypeManager()
->getStorage('uc_order');
$entities = $storage
->loadMultiple([
$order
->id(),
]);
$storage
->delete($entities);
$storage
->resetCache([
$order
->id(),
]);
$deleted_order = Order::load($order
->id());
$this
->assertFalse($deleted_order, 'Order was successfully deleted');
}
public function testEntityHooks() {
\Drupal::service('module_installer')
->install([
'entity_crud_hook_test',
]);
$GLOBALS['entity_crud_hook_test'] = [];
$order = Order::create();
$order
->save();
$this
->assertHookMessage('entity_crud_hook_test_entity_presave called for type uc_order');
$this
->assertHookMessage('entity_crud_hook_test_entity_insert called for type uc_order');
$GLOBALS['entity_crud_hook_test'] = [];
$order = Order::load($order
->id());
$this
->assertHookMessage('entity_crud_hook_test_entity_load called for type uc_order');
$GLOBALS['entity_crud_hook_test'] = [];
$order
->save();
$this
->assertHookMessage('entity_crud_hook_test_entity_presave called for type uc_order');
$this
->assertHookMessage('entity_crud_hook_test_entity_update called for type uc_order');
$GLOBALS['entity_crud_hook_test'] = [];
$order
->delete();
$this
->assertHookMessage('entity_crud_hook_test_entity_delete called for type uc_order');
}
public function testOrderCreation() {
$this
->drupalLogin($this->adminUser);
$assert = $this
->assertSession();
$edit = [
'customer_type' => 'search',
'customer[email]' => $this->customer->mail->value,
];
$this
->drupalGet('admin/store/orders/create');
$this
->submitForm($edit, 'Search');
$edit['customer[uid]'] = $this->customer
->id();
$this
->submitForm($edit, 'Create order');
$assert
->pageTextContains('Order created by the administration.', 'Order created by the administration.');
$assert
->fieldValueEquals('uid_text', $this->customer
->id());
$order_ids = \Drupal::entityQuery('uc_order')
->condition('uid', $this->customer
->id())
->execute();
$order_id = reset($order_ids);
$this
->assertTrue($order_id, format_string('Found order ID @order_id', [
'@order_id' => $order_id,
]));
$this
->drupalGet('admin/store/orders/view');
$assert
->linkByHrefExists('admin/store/orders/' . $order_id, 0, 'View link appears on order list.');
$assert
->pageTextContains('Pending', 'New order is "Pending".');
$this
->drupalGet('admin/store/customers/orders/' . $this->customer
->id());
$assert
->linkByHrefExists('admin/store/orders/' . $order_id, 0, 'View link appears on customer order list.');
$this
->clickLink('Create order for this customer');
$assert
->pageTextContains('Order created by the administration.');
$assert
->fieldValueEquals('uid_text', $this->customer
->id());
}
public function testOrderView() {
$assert = $this
->assertSession();
$order = $this
->ucCreateOrder($this->customer);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/orders/' . $order
->id());
$billing_address = $order
->getAddress('billing');
$assert
->pageTextContains(mb_strtoupper($billing_address
->getFirstName()));
$assert
->pageTextContains(mb_strtoupper($billing_address
->getLastName()));
$assert
->pageTextContains(mb_strtoupper($billing_address
->getStreet1()));
$assert
->pageTextContains(mb_strtoupper($billing_address
->getStreet2()));
$country = \Drupal::service('country_manager')
->getCountry($billing_address
->getCountry());
if (strpos(implode('', $country
->getAddressFormat()), 'city') === FALSE) {
$assert
->pageTextContains(mb_strtoupper($billing_address
->getCity()));
}
$delivery_address = $order
->getAddress('delivery');
$assert
->pageTextContains(mb_strtoupper($delivery_address
->getFirstName()));
$assert
->pageTextContains(mb_strtoupper($delivery_address
->getLastName()));
$assert
->pageTextContains(mb_strtoupper($delivery_address
->getStreet1()));
$assert
->pageTextContains(mb_strtoupper($delivery_address
->getStreet2()));
$country = \Drupal::service('country_manager')
->getCountry($delivery_address
->getCountry());
if (strpos(implode('', $country
->getAddressFormat()), 'city') === FALSE) {
$assert
->pageTextContains(mb_strtoupper($delivery_address
->getCity()));
}
$assert
->linkExists($order
->getOwnerId(), 0, 'Link to customer account page found.');
$assert
->linkExists($order
->getEmail(), 0, 'Link to customer email address found.');
}
public function testOrderCustomerView() {
$assert = $this
->assertSession();
$order = $this
->ucCreateOrder($this->customer);
$order
->setStatusId('pending');
$order
->save();
$this
->drupalLogin($this->customer);
$this
->drupalGet('user/' . $this->customer
->id() . '/orders');
$assert
->pageTextContains('My order history');
$assert
->pageTextContains('Pending', 'Order status is visible to the customer.');
$this
->drupalGet('user/' . $this->customer
->id() . '/orders/' . $order
->id());
$assert
->statusCodeEquals(200, 'Customer can view their own order.');
$address = $order
->getAddress('billing');
$assert
->pageTextContains(mb_strtoupper($address
->getFirstName()));
$assert
->pageTextContains(mb_strtoupper($address
->getLastName()));
$this
->drupalGet('admin/store/orders/' . $order
->id());
$assert
->statusCodeEquals(403, 'Customer may not see the admin view of their order.');
$this
->drupalGet('admin/store/orders/' . $order
->id() . '/edit');
$assert
->statusCodeEquals(403, 'Customer may not edit orders.');
}
public function testOrderEditing() {
$assert = $this
->assertSession();
$order = $this
->ucCreateOrder($this->customer);
$this
->drupalLogin($this->adminUser);
$edit = [
'billing[first_name]' => $this
->randomMachineName(8),
'billing[last_name]' => $this
->randomMachineName(15),
];
$this
->drupalGet('admin/store/orders/' . $order
->id() . '/edit');
$this
->submitForm($edit, 'Save changes');
$assert
->pageTextContains('Order changes saved.');
$assert
->fieldValueEquals('billing[first_name]', $edit['billing[first_name]']);
$assert
->fieldValueEquals('billing[last_name]', $edit['billing[last_name]']);
}
public function testOrderState() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/orders');
$assert
->fieldValueEquals('order_states[in_checkout][default]', 'in_checkout');
$this
->assertEquals('in_checkout', uc_order_state_default('in_checkout'), 'uc_order_state_default() returns correct default status.');
$order = $this
->ucCreateOrder($this->customer);
$this
->assertEquals('in_checkout', $order
->getStateId(), 'Order has correct default state.');
$this
->assertEquals('in_checkout', $order
->getStatusId(), 'Order has correct default status.');
$this
->drupalGet('admin/store/config/orders');
$this
->clickLink('Create custom order status');
$edit = [
'id' => strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
'state' => 'in_checkout',
'weight' => -15,
];
$this
->submitForm($edit, 'Create');
$this
->assertEquals($edit['id'], uc_order_state_default('in_checkout'), 'uc_order_state_default() returns lowest weight status.');
$this
->submitForm([
'order_states[in_checkout][default]' => $edit['id'],
], 'Save configuration');
$assert
->fieldValueEquals('order_states[in_checkout][default]', $edit['id']);
$order = $this
->ucCreateOrder($this->customer);
$this
->assertEquals($edit['id'], $order
->getStatusId(), 'Order has correct custom status.');
}
public function testCustomOrderStatus() {
$assert = $this
->assertSession();
$order = $this
->ucCreateOrder($this->customer);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/store/config/orders');
$title = $this
->randomMachineName();
$edit = [
'order_statuses[in_checkout][name]' => $title,
];
$this
->submitForm($edit, 'Save configuration');
$assert
->fieldValueEquals('order_statuses[in_checkout][name]', $title);
$this
->drupalGet('admin/store/orders/view');
$assert
->pageTextContains($title);
$this
->drupalGet('admin/store/config/orders');
$this
->clickLink('Create custom order status');
$edit = [
'id' => strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
'state' => array_rand(uc_order_state_options_list()),
'weight' => mt_rand(-10, 10),
];
$this
->submitForm($edit, 'Create');
$assert
->pageTextContains($edit['id'], 'Custom status ID found.');
$assert
->fieldValueEquals('order_statuses[' . $edit['id'] . '][name]', $edit['name']);
$assert
->fieldValueEquals('order_statuses[' . $edit['id'] . '][weight]', $edit['weight']);
$this
->drupalGet('admin/store/orders/' . $order
->id());
$this
->submitForm([
'status' => $edit['id'],
], 'Update');
$this
->drupalGet('admin/store/orders/view');
$assert
->pageTextContains($edit['name'], 'Order displays custom status title.');
$this
->drupalGet('admin/store/config/orders');
$this
->submitForm([
'order_statuses[' . $edit['id'] . '][remove]' => 1,
], 'Save configuration');
$assert
->pageTextNotContains($edit['id'], 'Deleted status ID not found.');
}
protected function ucCreateOrder($customer) {
$order = Order::create([
'uid' => $customer
->id(),
]);
$order
->save();
uc_order_comment_save($order
->id(), 0, 'Order created programmatically.', 'admin');
$order_ids = \Drupal::entityQuery('uc_order')
->condition('order_id', $order
->id())
->execute();
$this
->assertTrue(in_array($order
->id(), $order_ids), format_string('Found order ID @order_id', [
'@order_id' => $order
->id(),
]));
$country_manager = \Drupal::service('country_manager');
$country = array_rand($country_manager
->getEnabledList());
$zones = $country_manager
->getZoneList($country);
$delivery_address = Address::create();
$delivery_address
->setFirstName($this
->randomMachineName(12))
->setLastName($this
->randomMachineName(12))
->setStreet1($this
->randomMachineName(12))
->setStreet2($this
->randomMachineName(12))
->setCity($this
->randomMachineName(12))
->setPostalCode(mt_rand(10000, 99999))
->setCountry($country);
if (!empty($zones)) {
$delivery_address
->setZone(array_rand($zones));
}
$billing_address = Address::create();
$billing_address
->setFirstName($this
->randomMachineName(12))
->setLastName($this
->randomMachineName(12))
->setStreet1($this
->randomMachineName(12))
->setStreet2($this
->randomMachineName(12))
->setCity($this
->randomMachineName(12))
->setPostalCode(mt_rand(10000, 99999))
->setCountry($country);
if (!empty($zones)) {
$billing_address
->setZone(array_rand($zones));
}
$order
->setAddress('delivery', $delivery_address)
->setAddress('billing', $billing_address)
->save();
$db_order = \Drupal::entityTypeManager()
->getStorage('uc_order')
->loadUnchanged($order
->id());
$db_delivery_address = $db_order
->getAddress('delivery');
$db_billing_address = $db_order
->getAddress('billing');
$this
->assertEquals($db_delivery_address, $delivery_address, 'Delivery address is equal to delivery address in database.');
$this
->assertEquals($db_billing_address, $billing_address, 'Billing address is equal to billing address in database.');
return $order;
}
protected function assertHookMessage($text, $message = NULL, $group = 'Other') {
if (!isset($message)) {
$message = $text;
}
return $this
->assertNotSame(FALSE, array_search($text, $GLOBALS['entity_crud_hook_test']), $message, $group);
}
}