uc_coupon.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Ubercart Discount Coupon Tests.
  5. */
  6. /**
  7. * SimpleTests for Ubercart Discount Coupons
  8. */
  9. class UcCouponTestCase extends UbercartTestHelper {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Discount Coupons',
  13. 'description' => 'Test discount coupons.',
  14. 'group' => 'Ubercart',
  15. );
  16. }
  17. /**
  18. * Overrides DrupalWebTestCase::setUp().
  19. */
  20. function setUp() {
  21. $modules = array('uc_coupon', 'uc_payment', 'uc_payment_pack');
  22. $permissions = array('manage store coupons');
  23. parent::setUp($modules, $permissions);
  24. $this->drupalLogin($this->adminUser);
  25. }
  26. function createCoupon($edit = array()) {
  27. $edit += array(
  28. 'code' => $this->randomName(8),
  29. 'name' => $this->randomName(20),
  30. 'discount' => mt_rand(1, 50) . '%',
  31. 'status' => 1,
  32. );
  33. $form_state = array('values' => $edit);
  34. drupal_form_submit('uc_coupon_add_form', $form_state);
  35. $cid = db_query('SELECT cid FROM {uc_coupons} WHERE code = :code', array(':code' => trim(strtoupper($edit['code']))))->fetchField();
  36. $this->assertTrue($cid, 'Coupon was created successfully');
  37. if (!$cid) debug(form_get_errors(), 'Form errors:');
  38. $coupon = uc_coupon_load($cid);
  39. return $coupon;
  40. }
  41. public function applyCoupons($coupons, $products = array(), $total = NULL) {
  42. foreach ($products as $product) {
  43. $this->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
  44. $this->assertRaw($product->title, 'Product appears on cart page');
  45. }
  46. foreach ($coupons as $coupon) {
  47. $this->drupalPost('cart', array('code' => $coupon->code), t('Apply to order'));
  48. if ($total !== FALSE) {
  49. $this->assertRaw('Coupon ' . $coupon->code, 'Applied coupon code appears on cart page.');
  50. }
  51. else {
  52. $this->assertNoRaw('Coupon ' . $coupon->code, 'Invalid coupon code does not appear on cart page.');
  53. }
  54. }
  55. if ($total === FALSE) {
  56. $this->assertNoRaw(uc_currency_format($total), 'Invalid discount was not applied to total.');
  57. }
  58. elseif (!is_null($total)) {
  59. $this->assertRaw(uc_currency_format($total), 'Discounted total is correct on cart page.');
  60. }
  61. }
  62. // Our own version of the checkout sequence, optionally checking for coupons.
  63. function checkout($coupons = array(), $total = FALSE) {
  64. $total = uc_currency_format($total);
  65. $this->drupalPost('cart', array('items[0][qty]' => 1), t('Checkout'));
  66. foreach ($coupons as $coupon) {
  67. $this->assertRaw('Coupon ' . $coupon->code, "Coupon $coupon->code appears on checkout page.");
  68. }
  69. if ($total !== FALSE) {
  70. $this->assertRaw($total, "The order total of $total is correct on the checkout page");
  71. }
  72. $edit = $this->populateCheckoutForm();
  73. $this->drupalPost(NULL, $edit, t('Review order'));
  74. foreach ($coupons as $coupon) {
  75. $this->assertRaw('Coupon ' . $coupon->code, "Coupon $coupon->code appears on review page.");
  76. }
  77. if ($total !== FALSE) {
  78. $this->assertRaw($total, "The order total of $total is correct on the review page");
  79. }
  80. // Complete the review page.
  81. $this->drupalPost(NULL, array(), t('Submit order'));
  82. $order_id = db_query("SELECT order_id FROM {uc_orders} WHERE delivery_first_name = :name", array(':name' => $edit['panes[delivery][delivery_first_name]']))->fetchField();
  83. if ($order_id) {
  84. $order = uc_order_load($order_id);
  85. if ($total !== FALSE) {
  86. $this->assertEqual(uc_currency_format(uc_order_get_total($order)), $total, 'Saved order total is correct');
  87. }
  88. }
  89. else {
  90. $this->fail(t('No order was created.'));
  91. $order = FALSE;
  92. }
  93. return $order;
  94. }
  95. public function testGiftCertificate() {
  96. $coupon = $this->createCoupon(array(
  97. 'discount' => '100',
  98. 'store_credit' => 1,
  99. ));
  100. $product = $this->createProduct(array(
  101. 'sell_price' => '75',
  102. ));
  103. $this->applyCoupons(array($coupon), array($product), 0);
  104. $this->assertText('Subtotal: $0.00');
  105. $order = $this->checkout();
  106. uc_order_update_status($order->order_id, 'payment_received');
  107. $this->applyCoupons(array($coupon), array($product), 50);
  108. }
  109. public function testCoupon() {
  110. $coupon = $this->createCoupon( array(
  111. 'discount' => '100'
  112. ));
  113. $product = $this->createProduct(array(
  114. 'sell_price' => '150'
  115. ));
  116. $total = $product->sell_price - $coupon->value;
  117. $this->applyCoupons(array($coupon), array($product), $total);
  118. $order = $this->checkout(array($coupon), $total);
  119. // Try again to make sure the default usage limit works.
  120. uc_order_update_status($order->order_id, 'payment_received');
  121. $this->applyCoupons(array($coupon), array($product), FALSE);
  122. // Clear the usage limit and try again.
  123. $coupon->max_uses = 0;
  124. uc_coupon_save($coupon);
  125. $this->applyCoupons(array($coupon), array(), $total);
  126. }
  127. }