From 2fa2a86a2cc9db1eff7b38ac9bb4de6634e275aa Mon Sep 17 00:00:00 2001 From: Jelle Bekker <jelle.bekker@gmail.com> Date: Fri, 30 Jan 2015 08:03:33 +0100 Subject: [PATCH] Initial tests for the order entity --- .../order/src/Tests/CommerceOrderTestBase.php | 67 +++++++++++++ modules/order/src/Tests/OrderTest.php | 70 +++++++++++++ modules/order/src/Tests/OrderTypeTest.php | 97 +++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 modules/order/src/Tests/CommerceOrderTestBase.php create mode 100644 modules/order/src/Tests/OrderTest.php create mode 100644 modules/order/src/Tests/OrderTypeTest.php diff --git a/modules/order/src/Tests/CommerceOrderTestBase.php b/modules/order/src/Tests/CommerceOrderTestBase.php new file mode 100644 index 000000000..7b30361dc --- /dev/null +++ b/modules/order/src/Tests/CommerceOrderTestBase.php @@ -0,0 +1,67 @@ +<?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; + } +} diff --git a/modules/order/src/Tests/OrderTest.php b/modules/order/src/Tests/OrderTest.php new file mode 100644 index 000000000..5f836ce8f --- /dev/null +++ b/modules/order/src/Tests/OrderTest.php @@ -0,0 +1,70 @@ +<?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.'); + } +} diff --git a/modules/order/src/Tests/OrderTypeTest.php b/modules/order/src/Tests/OrderTypeTest.php new file mode 100644 index 000000000..a96cd1970 --- /dev/null +++ b/modules/order/src/Tests/OrderTypeTest.php @@ -0,0 +1,97 @@ +<?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.'); + } +} -- GitLab