uc_order.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Ubercart orders.
  5. */
  6. /**
  7. * Tests for Ubercart orders.
  8. */
  9. class UbercartOrderTestCase extends UbercartTestHelper {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Orders',
  13. 'description' => 'Ensure that orders function properly.',
  14. 'group' => 'Ubercart',
  15. );
  16. }
  17. /**
  18. * Tests order entity API functions.
  19. */
  20. public function testOrderApi() {
  21. // Test defaults.
  22. $order = uc_order_new();
  23. $this->assertEqual($order->uid, 0, 'New order is anonymous.');
  24. $this->assertEqual($order->order_status, 'in_checkout', 'New order is in checkout.');
  25. $order = uc_order_new($this->customer->uid, 'completed');
  26. $this->assertEqual($order->uid, $this->customer->uid, 'New order has correct uid.');
  27. $this->assertEqual($order->order_status, 'completed', 'New order is marked completed.');
  28. // Test deletion.
  29. uc_order_delete($order->order_id);
  30. $deleted_order = uc_order_load($order->order_id, TRUE);
  31. $this->assertFalse($deleted_order, 'Order was successfully deleted');
  32. }
  33. /**
  34. * Tests order CRUD operations.
  35. */
  36. public function testOrderEntity() {
  37. $order = entity_create('uc_order', array());
  38. $this->assertEqual($order->uid, 0, 'New order is anonymous.');
  39. $this->assertEqual($order->order_status, 'in_checkout', 'New order is in checkout.');
  40. $name = $this->randomName();
  41. $order = entity_create('uc_order', array(
  42. 'uid' => $this->customer->uid,
  43. 'order_status' => 'completed',
  44. 'billing_first_name' => $name,
  45. 'billing_last_name' => $name,
  46. ));
  47. $this->assertEqual($order->uid, $this->customer->uid, 'New order has correct uid.');
  48. $this->assertEqual($order->order_status, 'completed', 'New order is marked completed.');
  49. $this->assertEqual($order->billing_first_name, $name, 'New order has correct name.');
  50. $this->assertEqual($order->billing_last_name, $name, 'New order has correct name.');
  51. // Test deletion.
  52. entity_delete('uc_order', $order->order_id);
  53. $deleted_order = entity_load('uc_order', array($order->order_id), array(), TRUE);
  54. $this->assertFalse($deleted_order, 'Order was successfully deleted');
  55. }
  56. /**
  57. * Tests order entity CRUD hooks.
  58. */
  59. public function testEntityHooks() {
  60. module_enable(array('entity_crud_hook_test'));
  61. $_SESSION['entity_crud_hook_test'] = array();
  62. $order = uc_order_new();
  63. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type uc_order');
  64. $this->assertHookMessage('entity_crud_hook_test_entity_insert called for type uc_order');
  65. $_SESSION['entity_crud_hook_test'] = array();
  66. $order = uc_order_load($order->order_id);
  67. $this->assertHookMessage('entity_crud_hook_test_entity_load called for type uc_order');
  68. $_SESSION['entity_crud_hook_test'] = array();
  69. uc_order_save($order);
  70. $this->assertHookMessage('entity_crud_hook_test_entity_presave called for type uc_order');
  71. $this->assertHookMessage('entity_crud_hook_test_entity_update called for type uc_order');
  72. $_SESSION['entity_crud_hook_test'] = array();
  73. uc_order_delete($order->order_id);
  74. $this->assertHookMessage('entity_crud_hook_test_entity_delete called for type uc_order');
  75. }
  76. /**
  77. * Tests admin order creation.
  78. */
  79. public function testOrderCreation() {
  80. $this->drupalLogin($this->adminUser);
  81. $edit = array(
  82. 'customer_type' => 'search',
  83. 'customer[email]' => $this->customer->mail,
  84. );
  85. $this->drupalPost('admin/store/orders/create', $edit, t('Search'));
  86. $edit['customer[uid]'] = $this->customer->uid;
  87. $this->drupalPost(NULL, $edit, t('Create order'));
  88. $this->assertText(t('Order created by the administration.'), 'Order created by the administration.');
  89. $this->assertFieldByName('uid_text', $this->customer->uid, 'The customer UID appears on the page.');
  90. $order_id = db_query("SELECT order_id FROM {uc_orders} WHERE uid = :uid", array(':uid' => $this->customer->uid))->fetchField();
  91. $this->assertTrue($order_id, t('Found order ID @order_id', array('@order_id' => $order_id)));
  92. $this->drupalGet('admin/store/orders');
  93. $this->assertLinkByHref('admin/store/orders/' . $order_id, 0, 'View link appears on order list.');
  94. $this->assertText('Pending', 'New order is "Pending".');
  95. }
  96. /**
  97. * Tests admin editing of orders.
  98. */
  99. public function testOrderEditing() {
  100. $order = $this->ucCreateOrder($this->customer);
  101. $this->drupalLogin($this->customer);
  102. $this->drupalGet('user/' . $this->customer->uid . '/orders');
  103. $this->assertText(t('My order history'));
  104. $this->drupalGet('user/' . $this->customer->uid . '/orders/' . $order->order_id);
  105. $this->assertResponse(200, 'Customer can view their own order.');
  106. $this->drupalGet('admin/store/orders/' . $order->order_id);
  107. $this->assertResponse(403, 'Customer may not edit orders.');
  108. $this->drupalLogin($this->adminUser);
  109. $this->drupalGet('user/' . $this->customer->uid . '/orders/' . $order->order_id);
  110. $this->assertText(drupal_strtoupper($order->billing_first_name . ' ' . $order->billing_last_name), 'Found customer name.');
  111. $edit = array(
  112. 'billing_first_name' => $this->randomName(8),
  113. 'billing_last_name' => $this->randomName(15),
  114. );
  115. $this->drupalPost('admin/store/orders/' . $order->order_id . '/edit', $edit, t('Submit changes'));
  116. $this->assertText(t('Order changes saved.'));
  117. $this->assertFieldByName('billing_first_name', $edit['billing_first_name'], 'Billing first name changed.');
  118. $this->assertFieldByName('billing_last_name', $edit['billing_last_name'], 'Billing last name changed.');
  119. }
  120. /**
  121. * Helper function for creating an order programmatically.
  122. */
  123. protected function ucCreateOrder($customer) {
  124. $order = uc_order_new($customer->uid);
  125. uc_order_comment_save($order->order_id, 0, t('Order created programmatically.'), 'admin');
  126. $order_exists = db_query("SELECT 1 FROM {uc_orders} WHERE order_id = :order_id", array(':order_id' => $order->order_id))->fetchField();
  127. $this->assertTrue($order_exists, t('Found order ID @order_id', array('@order_id' => $order->order_id)));
  128. $countries = uc_country_option_list();
  129. $country = array_rand($countries);
  130. $zones = uc_zone_option_list();
  131. $order->delivery_first_name = $this->randomName(12);
  132. $order->delivery_last_name = $this->randomName(12);
  133. $order->delivery_street1 = $this->randomName(12);
  134. $order->delivery_street2 = $this->randomName(12);
  135. $order->delivery_city = $this->randomName(12);
  136. $order->delivery_zone = array_rand($zones[$countries[$country]]);
  137. $order->delivery_postal_code = mt_rand(10000, 99999);
  138. $order->delivery_country = $country;
  139. $order->billing_first_name = $this->randomName(12);
  140. $order->billing_last_name = $this->randomName(12);
  141. $order->billing_street1 = $this->randomName(12);
  142. $order->billing_street2 = $this->randomName(12);
  143. $order->billing_city = $this->randomName(12);
  144. $order->billing_zone = array_rand($zones[$countries[$country]]);
  145. $order->billing_postal_code = mt_rand(10000, 99999);
  146. $order->billing_country = $country;
  147. uc_order_save($order);
  148. $db_order = db_query("SELECT * FROM {uc_orders} WHERE order_id = :order_id", array(':order_id' => $order->order_id))->fetchObject();
  149. $this->assertEqual($order->delivery_first_name, $db_order->delivery_first_name);
  150. $this->assertEqual($order->delivery_last_name, $db_order->delivery_last_name);
  151. $this->assertEqual($order->delivery_street1, $db_order->delivery_street1);
  152. $this->assertEqual($order->delivery_street2, $db_order->delivery_street2);
  153. $this->assertEqual($order->delivery_city, $db_order->delivery_city);
  154. $this->assertEqual($order->delivery_zone, $db_order->delivery_zone);
  155. $this->assertEqual($order->delivery_postal_code, $db_order->delivery_postal_code);
  156. $this->assertEqual($order->delivery_country, $db_order->delivery_country);
  157. $this->assertEqual($order->billing_first_name, $db_order->billing_first_name);
  158. $this->assertEqual($order->billing_last_name, $db_order->billing_last_name);
  159. $this->assertEqual($order->billing_street1, $db_order->billing_street1);
  160. $this->assertEqual($order->billing_street2, $db_order->billing_street2);
  161. $this->assertEqual($order->billing_city, $db_order->billing_city);
  162. $this->assertEqual($order->billing_zone, $db_order->billing_zone);
  163. $this->assertEqual($order->billing_postal_code, $db_order->billing_postal_code);
  164. $this->assertEqual($order->billing_country, $db_order->billing_country);
  165. return $order;
  166. }
  167. /**
  168. * Helper function for testing order entity CRUD hooks.
  169. */
  170. protected function assertHookMessage($text, $message = NULL, $group = 'Other') {
  171. if (!isset($message)) {
  172. $message = $text;
  173. }
  174. return $this->assertTrue(array_search($text, $_SESSION['entity_crud_hook_test']) !== FALSE, $message, $group);
  175. }
  176. }