Commit 2fa2a86a authored by jbekker's avatar jbekker Committed by joshtaylor

Initial tests for the order entity

parent 69825b03
<?php
/**
* @file
* Definition of \Drupal\commerce_order\Tests\CommerceOrderTestBase.
*/
namespace Drupal\commerce_order\Tests;
use Drupal\Component\Utility\String;
use Drupal\simpletest\WebTestBase;
/**
* Defines base class for shortcut test cases.
*/
abstract class CommerceOrderTestBase extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('commerce', 'commerce_order');
/**
* User with permission to administer products.
*/
protected $adminUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(array(
'administer orders',
'administer order types',
'access administration pages',
));
$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 = entity_create($entityType, $values);
$status = $entity->save();
$this->assertEqual(
$status,
SAVED_NEW,
String::format('Created %label entity %type.',
array(
'%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 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');
// Create a order through the add form.
$this->drupalGet('/admin/commerce/orders');
$this->clickLink('Create a new order');
$values = array(
'mail[0][value]' => $this->loggedInUser->getEmail(),
);
$this->drupalPostForm(NULL, $values, t('Save'));
}
/**
* 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\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.');
}
}
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