uc_order.test 8.3 KB

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