uc_product_kit.test 10 KB

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