uc_product_kit.test 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @file
  4. * Ubercart product kit tests
  5. */
  6. class UbercartProductKitTestCase extends UbercartTestHelper {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Product kits',
  10. 'description' => 'Ensure that the product kit functions properly.',
  11. 'group' => 'Ubercart',
  12. );
  13. }
  14. /**
  15. * Overrides DrupalWebTestCase::setUp().
  16. */
  17. public function setUp() {
  18. parent::setUp(array('uc_product_kit'), array('create product_kit content', 'edit any product_kit content'));
  19. }
  20. public function testProductKitNodeForm() {
  21. $this->drupalLogin($this->adminUser);
  22. // Allow the default quantity to be set.
  23. variable_set('uc_product_add_to_cart_qty', TRUE);
  24. // Create some test products.
  25. $products = array();
  26. for ($i = 0; $i < 3; $i++) {
  27. $products[$i] = $this->createProduct();
  28. }
  29. // Test the product kit fields.
  30. $this->drupalGet('node/add/product-kit');
  31. foreach (array('mutable', 'products[]', 'ordering') as $field) {
  32. $this->assertFieldByName($field);
  33. }
  34. // Test creation of a basic kit.
  35. $body_key = 'body[und][0][value]';
  36. $edit = array(
  37. 'title' => $this->randomName(32),
  38. $body_key => $this->randomName(64),
  39. 'products[]' => array(
  40. $products[0]->nid,
  41. $products[1]->nid,
  42. $products[2]->nid,
  43. ),
  44. 'default_qty' => mt_rand(2, 100),
  45. );
  46. $this->drupalPost('node/add/product-kit', $edit, 'Save');
  47. $this->assertText(t('Product kit @title has been created.', array('@title' => $edit['title'])));
  48. $this->assertText($edit[$body_key], 'Product kit body found.');
  49. $this->assertText('1 × ' . $products[0]->title, 'Product 1 title found.');
  50. $this->assertText('1 × ' . $products[1]->title, 'Product 2 title found.');
  51. $this->assertText('1 × ' . $products[2]->title, 'Product 3 title found.');
  52. $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price;
  53. $this->assertText(uc_currency_format($total), 'Product kit total found.');
  54. $this->assertFieldByName('qty', $edit['default_qty']);
  55. }
  56. public function testProductKitDiscounts() {
  57. $this->drupalLogin($this->adminUser);
  58. // Create some test products and a kit.
  59. $products = array();
  60. for ($i = 0; $i < 3; $i++) {
  61. $products[$i] = $this->createProduct();
  62. }
  63. $kit = $this->drupalCreateNode(array(
  64. 'type' => 'product_kit',
  65. 'title' => $this->randomName(32),
  66. 'products' => array(
  67. $products[0]->nid,
  68. $products[1]->nid,
  69. $products[2]->nid,
  70. ),
  71. 'mutable' => UC_PRODUCT_KIT_UNMUTABLE_NO_LIST,
  72. 'default_qty' => 1,
  73. 'ordering' => 0,
  74. ));
  75. // Test the product kit extra fields available to configure discounts.
  76. $this->drupalGet('node/' . $kit->nid . '/edit');
  77. $this->assertFieldByName('kit_total');
  78. foreach ($products as $product) {
  79. $this->assertFieldByName('items[' . $product->nid . '][qty]');
  80. $this->assertFieldByName('items[' . $product->nid . '][ordering]');
  81. $this->assertFieldByName('items[' . $product->nid . '][discount]');
  82. }
  83. // Set some discounts.
  84. $discounts = array(
  85. mt_rand(-100, 100),
  86. mt_rand(-100, 100),
  87. mt_rand(-100, 100),
  88. );
  89. $edit = array(
  90. 'items[' . $products[0]->nid . '][discount]' => $discounts[0],
  91. 'items[' . $products[1]->nid . '][discount]' => $discounts[1],
  92. 'items[' . $products[2]->nid . '][discount]' => $discounts[2],
  93. );
  94. $this->drupalPost('node/' . $kit->nid . '/edit', $edit, 'Save');
  95. // Check the discounted total.
  96. $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price;
  97. $total += array_sum($discounts);
  98. $this->assertText(uc_currency_format($total), 'Discounted product kit total found.');
  99. // Check the discounts on the edit page.
  100. $this->drupalGet('node/' . $kit->nid . '/edit');
  101. $this->assertText('Currently, the total sell price is ' . uc_currency_format($total), 'Discounted product kit total found.');
  102. $this->assertFieldByName('items[' . $products[0]->nid . '][discount]', $discounts[0]);
  103. $this->assertFieldByName('items[' . $products[1]->nid . '][discount]', $discounts[1]);
  104. $this->assertFieldByName('items[' . $products[2]->nid . '][discount]', $discounts[2]);
  105. // Set the kit total.
  106. $total = 2 * ($products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price);
  107. $this->drupalPost('node/' . $kit->nid . '/edit', array('kit_total' => $total), 'Save');
  108. // Check the fixed total.
  109. $this->assertText(uc_currency_format($total), 'Fixed product kit total found.');
  110. // Check the discounts on the edit page.
  111. $this->drupalGet('node/' . $kit->nid . '/edit');
  112. $this->assertFieldByName('kit_total', $total);
  113. $this->assertFieldByName('items[' . $products[0]->nid . '][discount]', $products[0]->sell_price);
  114. $this->assertFieldByName('items[' . $products[1]->nid . '][discount]', $products[1]->sell_price);
  115. $this->assertFieldByName('items[' . $products[2]->nid . '][discount]', $products[2]->sell_price);
  116. }
  117. public function testProductKitMutability() {
  118. $this->drupalLogin($this->adminUser);
  119. // Create some test products and prepare a kit.
  120. $products = array();
  121. for ($i = 0; $i < 3; $i++) {
  122. $products[$i] = $this->createProduct();
  123. }
  124. $kit_data = array(
  125. 'type' => 'product_kit',
  126. 'title' => $this->randomName(32),
  127. 'products' => array(
  128. $products[0]->nid,
  129. $products[1]->nid,
  130. $products[2]->nid,
  131. ),
  132. 'default_qty' => 1,
  133. 'ordering' => 0,
  134. );
  135. // Test kits with no listing.
  136. $kit_data['mutable'] = UC_PRODUCT_KIT_UNMUTABLE_NO_LIST;
  137. $kit = $this->drupalCreateNode($kit_data);
  138. $this->drupalGet('node/' . $kit->nid);
  139. $this->assertText($kit->title, 'Product kit title found.');
  140. $this->assertNoText($products[0]->title, 'Product 1 title not shown.');
  141. $this->assertNoText($products[1]->title, 'Product 2 title not shown.');
  142. $this->assertNoText($products[2]->title, 'Product 3 title not shown.');
  143. $this->drupalPost('node/' . $kit->nid, array(), 'Add to cart');
  144. $this->drupalGet('cart');
  145. $this->assertText($kit->title, 'Product kit title found.');
  146. $this->assertNoText($products[0]->title, 'Product 1 title not shown.');
  147. $this->assertNoText($products[1]->title, 'Product 2 title not shown.');
  148. $this->assertNoText($products[2]->title, 'Product 3 title not shown.');
  149. $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price;
  150. $this->assertText('Subtotal: ' . uc_currency_format($total), 'Product kit total found.');
  151. $qty = mt_rand(2, 10);
  152. $this->drupalPost(NULL, array('items[2][qty]' => $qty), 'Update cart');
  153. $this->assertText('Subtotal: ' . uc_currency_format($total * $qty), 'Updated product kit total found.');
  154. $this->drupalPost(NULL, array(), 'Remove');
  155. $this->assertText('There are no products in your shopping cart.');
  156. // Test kits with listing.
  157. $kit_data['mutable'] = UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST;
  158. $kit = $this->drupalCreateNode($kit_data);
  159. $this->drupalGet('node/' . $kit->nid);
  160. $this->assertText($kit->title, 'Product kit title found.');
  161. $this->assertText($products[0]->title, 'Product 1 title shown.');
  162. $this->assertText($products[1]->title, 'Product 2 title shown.');
  163. $this->assertText($products[2]->title, 'Product 3 title shown.');
  164. $this->drupalPost('node/' . $kit->nid, array(), 'Add to cart');
  165. $this->drupalGet('cart');
  166. $this->assertText($kit->title, 'Product kit title found.');
  167. $this->assertText($products[0]->title, 'Product 1 title shown.');
  168. $this->assertText($products[1]->title, 'Product 2 title shown.');
  169. $this->assertText($products[2]->title, 'Product 3 title shown.');
  170. $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price;
  171. $this->assertText('Subtotal: ' . uc_currency_format($total), 'Product kit total found.');
  172. $qty = mt_rand(2, 10);
  173. $this->drupalPost(NULL, array('items[2][qty]' => $qty), 'Update cart');
  174. $this->assertText('Subtotal: ' . uc_currency_format($total * $qty), 'Updated product kit total found.');
  175. $this->drupalPost(NULL, array(), 'Remove');
  176. $this->assertText('There are no products in your shopping cart.');
  177. // Test mutable kits.
  178. $kit_data['mutable'] = UC_PRODUCT_KIT_MUTABLE;
  179. $kit = $this->drupalCreateNode($kit_data);
  180. $this->drupalGet('node/' . $kit->nid);
  181. $this->assertText($kit->title, 'Product kit title found.');
  182. $this->assertText($products[0]->title, 'Product 1 title shown.');
  183. $this->assertText($products[1]->title, 'Product 2 title shown.');
  184. $this->assertText($products[2]->title, 'Product 3 title shown.');
  185. $this->drupalPost('node/' . $kit->nid, array(), 'Add to cart');
  186. $this->drupalGet('cart');
  187. $this->assertNoText($kit->title, 'Product kit title not shown.');
  188. $this->assertText($products[0]->title, 'Product 1 title shown.');
  189. $this->assertText($products[1]->title, 'Product 2 title shown.');
  190. $this->assertText($products[2]->title, 'Product 3 title shown.');
  191. $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price;
  192. $this->assertText('Subtotal: ' . uc_currency_format($total), 'Product kit total found.');
  193. $qty = array(mt_rand(2, 10), mt_rand(2, 10), mt_rand(2, 10));
  194. $edit = array(
  195. 'items[0][qty]' => $qty[0],
  196. 'items[1][qty]' => $qty[1],
  197. 'items[2][qty]' => $qty[2],
  198. );
  199. $this->drupalPost(NULL, $edit, 'Update cart');
  200. $total = $products[0]->sell_price * $qty[0];
  201. $total += $products[1]->sell_price * $qty[1];
  202. $total += $products[2]->sell_price * $qty[2];
  203. $this->assertText('Subtotal: ' . uc_currency_format($total), 'Updated product kit total found.');
  204. $this->drupalPost(NULL, array(), 'Remove');
  205. $this->drupalPost(NULL, array(), 'Remove');
  206. $this->drupalPost(NULL, array(), 'Remove');
  207. $this->assertText('There are no products in your shopping cart.');
  208. }
  209. }