Commit 96d69152 authored by bojanz's avatar bojanz

Merge pull request #220 from joshuataylor/commerce-order-tests-with-admin

Order tests
parents 1579cf01 151ee6e7
......@@ -16,8 +16,8 @@ content:
type: price_default
weight: 3
settings:
strip_trailing_zeroes: 0
display_currency_code: 0
strip_trailing_zeroes: false
display_currency_code: false
third_party_settings: { }
label: above
product:
......@@ -42,8 +42,8 @@ content:
type: price_default
weight: 2
settings:
strip_trailing_zeroes: 0
display_currency_code: 0
strip_trailing_zeroes: false
display_currency_code: false
third_party_settings: { }
label: above
hidden: { }
......@@ -10,6 +10,7 @@ dependencies:
- commerce_store
- entity_reference
- inline_entity_form
- options
config_devel:
- views.view.commerce_orders
- commerce_order.commerce_order_type.order
......
......@@ -24,8 +24,8 @@ content:
type: price_default
weight: 1
settings:
strip_trailing_zeroes: 0
display_currency_code: 0
strip_trailing_zeroes: false
display_currency_code: false
third_party_settings: { }
label: above
hidden:
......
<?php
/**
* @file
* Definition of \Drupal\commerce_order\Tests\CommerceOrderTestBase.
*/
namespace Drupal\commerce_order\Tests;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_store\Entity\Store;
use Drupal\commerce_store\Tests\StoreTestBase;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\simpletest\WebTestBase;
/**
* Defines base class for commerce_order test cases.
*/
abstract class CommerceOrderTestBase extends WebTestBase {
/**
* The product to test against
*
* @var \Drupal\commerce_product\Entity\Product
*/
protected $product;
/**
* The store to test against
*
* @var \Drupal\commerce_store\Entity\Store
*/
protected $store;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'commerce',
'commerce_order',
'commerce_price',
'commerce_line_item',
'inline_entity_form'
];
/**
* A user with permission to administer orders.
*
* @var \Drupal\user\Entity\User
*/
protected $adminUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(array(
'administer orders',
'administer order types',
'access administration pages',
));
// Create a store
$values = [
'name' => t('Default store'),
'uid' => 1,
'mail' => \Drupal::config('system.site')->get('mail'),
'type' => 'default',
'default_currency' => "USD"
];
$this->store = Store::create($values);
$address = [
'country_code' => 'GB',
'address_line1' => '221B Baker Street',
'locality' => 'London',
'postal_code' => 'NW1 6XE'
];
$this->store->setAddress($address);
$this->store->save();
// Set as default store.
\Drupal::configFactory()->getEditable('commerce_store.settings')
->set('default_store', $this->store->uuid())->save();
// Create a product
$values = [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => 'product',
'store_id' => $this->store->id()
];
$this->product = Product::create($values);
$this->product->save();
$this->drupalLogin($this->adminUser);
}
/**
* Creates a new entity
*
* @param string $entityType
* @param array $values
* An array of settings.
* Example: 'id' => 'foo'.
*
* @return \Drupal\Core\Entity\EntityInterface
*/
protected function createEntity($entityType, $values) {
$entity = \Drupal::entityManager()
->getStorage($entityType)
->create($values);
$status = $entity->save();
$this->assertEqual(
$status,
SAVED_NEW,
SafeMarkup::format('Created %label entity %type.', [
'%label' => $entity->getEntityType()->getLabel(),
'%type' => $entity->id()
]
)
);
return $entity;
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_order\Tests\OrderTest.
*/
namespace Drupal\commerce_order\Tests;
use Drupal\commerce_order\Entity\Order;
/**
* Tests the commerce_order entity forms.
*
* @group commerce
*/
class OrderAdminTest extends CommerceOrderTestBase {
/**
* Tests creating a Order programaticaly and through the add form.
*/
public function testCreateOrder() {
// Create a order through the add form.
$this->drupalGet('/admin/store/products');
$this->drupalGet('/admin/commerce/orders');
$this->clickLink('Create a new order');
$values = array(
'line_items[form][inline_entity_form][product][0][target_id]' => $this->product->getTitle() . " (" . $this->product->id() . ")",
'line_items[form][inline_entity_form][quantity][0][value]' => 1,
'line_items[form][inline_entity_form][unit_price][0][amount]' => '9.99'
);
$this->drupalPostForm(NULL, $values, t('Create entity'));
$values = array(
'store_id' => $this->store->id(),
'mail[0][value]' => $this->loggedInUser->getEmail()
);
$this->drupalPostForm(NULL, $values, t('Save'));
$order_number = $this->cssSelect('tr td.views-field-order-number');
$this->assertEqual(count($order_number), 1, 'Order exists in the table.');
}
/**
* Tests deleting a order.
*/
public function testDeleteOrder() {
// Create a new order.
$order = $this->createEntity('commerce_order', array(
'type' => 'order',
'mail' => $this->loggedInUser->getEmail(),
)
);
$orderExists = (bool) Order::load($order->id());
$this->assertTrue($orderExists, 'The order has been created in the database.');
$this->drupalGet('admin/commerce/orders/' . $order->id() . '/delete');
$this->assertRaw(
t('Are you sure you want to delete the order %label?', array(
'%label' => $order->label(),
))
);
$this->assertText(t('This action cannot be undone.'), 'The order deletion confirmation form is available');
$this->drupalPostForm(NULL, NULL, t('Delete'));
// Remove the entity from cache and check if the order is deleted.
\Drupal::entityManager()->getStorage('commerce_order')->resetCache(array($order->id()));
$orderExists = (bool) Order::load('commerce_order', $order->id());
$this->assertFalse($orderExists, 'The order has been deleted from the database.');
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_order\Tests\OrderTest.
*/
namespace Drupal\commerce_order\Tests;
use Drupal\commerce_order\Entity\Order;
/**
* Tests the commerce_order entity forms.
*
* @group commerce
*/
class OrderTest extends CommerceOrderTestBase {
/**
* Tests creating a Order programaticaly and through the add form.
*/
public function testCreateOrder() {
// Create a order programmaticaly.
$order = $this->createEntity('commerce_order', array(
'type' => 'order',
'mail' => $this->loggedInUser->getEmail(),
)
);
$orderExists = (bool) Order::load($order->id());
$this->assertTrue($orderExists, 'The new order has been created in the database.');
$this->assertEqual($order->id(), $order->getOrderNumber(), 'The order number matches the order ID');
}
}
<?php
/**
* @file
* Contains \Drupal\commerce_order\Tests\OrderTypeTest.
*/
namespace Drupal\commerce_order\Tests;
use Drupal\commerce_order\Entity\OrderType;
/**
* Tests the commerce_order_type entity forms.
*
* @group commerce
*/
class OrderTypeTest extends CommerceOrderTestBase {
/**
* Tests if the default Order Type was created.
*/
public function testDefaultOrderType() {
$orderTypes = OrderType::loadMultiple();
$this->assertTrue(isset($orderTypes['order']), 'Order Type Order is available');
$orderType = OrderType::load('order');
$this->assertEqual($orderTypes['order'], $orderType, 'The correct Order Type is loaded');
}
/**
* Tests creating a Order Type programaticaly and through the add form.
*/
public function testCreateOrderType() {
// Create a order type programmaticaly.
$type = $this->createEntity('commerce_order_type', array(
'id' => 'kitten',
'label' => 'Label of kitten',
)
);
$typeExists = (bool) OrderType::load($type->id());
$this->assertTrue($typeExists, 'The new order type has been created in the database.');
// Create a order type through the add form.
$this->drupalGet('/admin/commerce/config/order-types');
$this->clickLink('Add a new order type');
$values = array(
'id' => 'foo',
'label' => 'Label of foo',
);
$this->drupalPostForm(NULL, $values, t('Save'));
$typeExists = (bool) OrderType::load($values['id']);
$this->assertTrue($typeExists, 'The new order type has been created in the database.');
}
/**
* Tests deleting a Order Type programmaticaly and through the form.
*/
public function testDeleteOrderType() {
// Create a order type programmaticaly.
$type = $this->createEntity('commerce_order_type', array(
'id' => 'foo',
'label' => 'Label for foo',
)
);
// Create a order.
$order = $this->createEntity('commerce_order', array(
'type' => $type->id(),
'mail' => $this->loggedInUser->getEmail(),
)
);
// Try to delete the order type.
$this->drupalGet('admin/commerce/config/order-types/' . $type->id() . '/delete');
$this->assertRaw(
t('%type is used by 1 order on your site. You can not remove this order type until you have removed all of the %type orders.', array('%type' => $type->label())),
'The order type will not be deleted until all orders of that type are deleted'
);
$this->assertNoText(t('This action cannot be undone.'), 'The order type deletion confirmation form is not available');
// Deleting the order type when its not being referenced by a order.
$order->delete();
$this->drupalGet('admin/commerce/config/order-types/' . $type->id() . '/delete');
$this->assertRaw(
t('Are you sure you want to delete the order type %label?', array(
'%label' => $type->label(),
))
);
$this->assertText(t('This action cannot be undone.'), 'The order type deletion confirmation form is available');
$this->drupalPostForm(NULL, NULL, t('Delete'));
$typeExists = (bool) OrderType::load($type->id());
$this->assertFalse($typeExists, 'The order type has been deleted from the database.');
}
}
......@@ -2,5 +2,5 @@ langcode: en
status: true
dependencies: { }
id: default
label: 'Default'
label: Default
description: null
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment