uc_restrict_qty.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * @file
  4. * UC Restrict Qty auto-tests.
  5. */
  6. class UCRestrictQtyFunctionalTest extends DrupalWebTestCase {
  7. function getInfo() {
  8. return array(
  9. 'name' => t('Various functional tests'),
  10. 'description' => '',
  11. 'group' => t('UC Restrict Qty'),
  12. );
  13. }
  14. /**
  15. * Implementation of setUp().
  16. */
  17. function setUp() {
  18. parent::setUp('uc_store', 'uc_cart', 'uc_product', 'uc_order', 'ca', 'uc_restrict_qty');
  19. }
  20. /**
  21. * Implementation of tearDown().
  22. */
  23. function tearDown() {
  24. parent::tearDown();
  25. }
  26. function drupalCreateProduct($price = 100) {
  27. // Create a product
  28. $node = array(
  29. 'type' => 'product',
  30. 'sell_price' => $price,
  31. 'list_price' => '',
  32. 'cost' => '',
  33. 'model' => '',
  34. 'weight' => '',
  35. 'weight_units' => '',
  36. 'length' => '',
  37. 'width' => '',
  38. 'height' => '',
  39. 'length_units' => '',
  40. 'default_qty' => '',
  41. 'pkg_qty' => '',
  42. 'shippable' => 0,
  43. 'ordering' => '',
  44. );
  45. $product = $this->drupalCreateNode($node);
  46. return $product;
  47. }
  48. function drupalCreateOrder($products = array(), $user = NULL) {
  49. if (!isset($user)) {
  50. $user = $this->drupalCreateUser();
  51. }
  52. $order = uc_order_new($user->uid);
  53. if (!empty($products)) {
  54. foreach ($products as $product) {
  55. $product_data->order_product_id = 0;
  56. $product_data->nid = $product->nid;
  57. $product_data->qty = 1;
  58. $product_data->title = $product->title;
  59. $product_data->model = $product->model;
  60. $product_data->cost = $product->cost;
  61. $product_data->price = $product->sell_price;
  62. $order->products[] = $product_data;
  63. }
  64. }
  65. $order->delivery_first_name = $this->randomName();
  66. $order->delivery_last_name = $this->randomName();
  67. $order->delivery_phone = $this->randomName();
  68. $order->delivery_company = $this->randomName();
  69. $order->delivery_street1 = $this->randomName();
  70. $order->delivery_street2 = $this->randomName();
  71. $order->delivery_city = $this->randomName();
  72. $order->delivery_zone = $this->randomName();
  73. $order->delivery_postal_code = $this->randomName();
  74. $order->delivery_country = $this->randomName();
  75. $order->billing_first_name = $this->randomName();
  76. $order->billing_last_name = $this->randomName();
  77. $order->billing_phone = $this->randomName();
  78. $order->billing_company = $this->randomName();
  79. $order->billing_street1 = $this->randomName();
  80. $order->billing_street2 = $this->randomName();
  81. $order->billing_city = $this->randomName();
  82. $order->billing_zone = $this->randomName();
  83. $order->billing_postal_code = $this->randomName();
  84. $order->billing_country = $this->randomName();
  85. $order->payment_method = $this->randomName();
  86. $order->manufacturer = $this->randomName();
  87. $order->weight = rand();
  88. $order->data = array();
  89. uc_order_save($order);
  90. return $order;
  91. }
  92. function testAdminProductSettings() {
  93. $admin = $this->drupalCreateUser(array('administer product features'));
  94. $this->drupalLogin($admin);
  95. // Test settings' validators
  96. $settings = array(
  97. 'uc_restrict_qty_global' => 'test',
  98. 'uc_restrict_qty_default_qty' => 1,
  99. 'uc_restrict_qty_default_lifetime' => 1,
  100. );
  101. $this->drupalPost('admin/store/settings/products/edit/features', $settings, t('Save configuration'));
  102. $this->assertText(t('You must enter 0 or a positive number value.'), 'Settings validation [Global limit]');
  103. $settings = array(
  104. 'uc_restrict_qty_global' => 1,
  105. 'uc_restrict_qty_default_qty' => 'test',
  106. 'uc_restrict_qty_default_lifetime' => 1,
  107. );
  108. $this->drupalPost('admin/store/settings/products/edit/features', $settings, t('Save configuration'));
  109. $this->assertText(t('You must enter 0 or a positive number value.'), 'Settings validation [Default maximum limit for a product]');
  110. // Submit real data
  111. $settings = array(
  112. 'uc_restrict_qty_global' => 5,
  113. 'uc_restrict_qty_default_qty' => 2,
  114. 'uc_restrict_qty_default_lifetime' => 1,
  115. );
  116. $this->drupalPost('admin/store/settings/products/edit/features', $settings, t('Save configuration'));
  117. $this->assertText(t('The configuration options have been saved.'), 'Settings saved');
  118. $product = $this->drupalCreateProduct();
  119. // Check if feature available
  120. $this->drupalGet('node/'. $product->nid .'/edit/features');
  121. $this->assertRaw('<option value="restrict_qty">'. t('Restrict Qty') .'</option>', 'Check if feature ready to be added');
  122. // Test feature form default values
  123. $test = array('feature' => 'restrict_qty');
  124. $this->drupalPost('node/'. $product->nid .'/edit/features', $test, t('Add'));
  125. $this->assertRaw('<input type="text" maxlength="5" name="quantity" id="edit-quantity" size="5" value="'. $settings['uc_restrict_qty_default_qty'] .'" class="form-text" />', 'Check if defaults prefilled [qty]');
  126. $this->assertRaw('<input type="checkbox" name="lifetime" id="edit-lifetime" value="1" checked="checked" class="form-checkbox" />', 'Check if defaults prefilled [lifetime]');
  127. // Test feature form submision
  128. $product_settings = array(
  129. 'model' => $product->model,
  130. 'quantity' => 'test',
  131. 'lifetime' => 1,
  132. );
  133. $this->drupalPost('node/'. $product->nid .'/edit/features/restrict_qty/add', $product_settings, t('Save feature'));
  134. $this->assertText(t('You must enter 0 or a positive integer value.'), 'New product feature [validation]');
  135. // Save proper data
  136. $product_settings['quantity'] = 1;
  137. $this->drupalPost('node/'. $product->nid .'/edit/features/restrict_qty/add', $product_settings, t('Save feature'));
  138. $this->assertText(t('The product feature has been added.'), 'New product feature [save|success message]');
  139. $this->assertRaw('<td nowrap="nowrap">Restrict Qty</td>', 'New product feature [save|appeared in the table]');
  140. // Double SKU submit check
  141. $this->drupalPost('node/'. $product->nid .'/edit/features/restrict_qty/add', $product_settings, t('Save feature'));
  142. $this->assertText(t('A quantity restriction has already been set up for this SKU'), 'New product feature [validation SKU]');
  143. $this->drupalGet('node/'. $product->nid .'/edit/features');
  144. if (preg_match('|node/[0-9]*/edit/features/restrict_qty/[0-9]*|', $this->content, $matches)) {
  145. $edit_url = $matches[0];
  146. $this->drupalGet($edit_url);
  147. $this->assertRaw('<input type="text" maxlength="5" name="quantity" id="edit-quantity" size="5" value="'. $product_settings['quantity'] .'" class="form-text" />', 'Check if new data saved [qty]');
  148. $this->assertRaw('<input type="checkbox" name="lifetime" id="edit-lifetime" value="1" checked="checked" class="form-checkbox" />', 'Check if new data saved [lifetime]');
  149. // Test feature form updation
  150. $product_settings = array(
  151. 'model' => $product->model,
  152. 'quantity' => 55,
  153. 'lifetime' => FALSE,
  154. );
  155. $this->drupalPost($edit_url, $product_settings, t('Save feature'));
  156. $this->assertText(t('The product feature has been updated.'), 'Feature updated');
  157. $this->drupalGet($edit_url);
  158. $this->assertRaw('<input type="text" maxlength="5" name="quantity" id="edit-quantity" size="5" value="'. $product_settings['quantity'] .'" class="form-text" />', 'Check if updated data saved [qty]');
  159. $this->assertRaw('<input type="checkbox" name="lifetime" id="edit-lifetime" value="1" class="form-checkbox" />', 'Check if updated data saved [lifetime]');
  160. }
  161. else {
  162. $this->fail('Feature edit link not found');
  163. }
  164. }
  165. function testGlobalLimit() {
  166. $limit = 2;
  167. variable_set('uc_restrict_qty_global', $limit);
  168. $product = $this->drupalCreateProduct();
  169. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  170. $this->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $product->title, '!url' => url('cart'))), 'Global limit [adding to cart below limit]');
  171. $product = $this->drupalCreateProduct();
  172. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  173. $this->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $product->title, '!url' => url('cart'))), 'Global limit [close to limit]');
  174. $product = $this->drupalCreateProduct();
  175. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  176. $this->assertNoRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $product->title, '!url' => url('cart'))), 'Global limit [above the limit|not added]');
  177. $this->assertRaw(format_plural($limit, "Sorry, you may only have a total of 1 item in your cart. You must <a href='@checkout'>checkout</a> or remove the item in <a href='@cart'>your cart</a> before adding a different item.",
  178. "Sorry, you may only have a total of @count items in your cart. You must <a href='@checkout'>checkout</a> or remove items from <a href='@cart'>your cart</a> before adding others.", array('@cart' => url('cart'), '@checkout' => url('cart/checkout'))), 'Global limit [above the limit|message shown]');
  179. variable_set('uc_restrict_qty_global', 0);
  180. $product = $this->drupalCreateProduct();
  181. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  182. $this->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $product->title, '!url' => url('cart'))), 'Global limit [no limit]');
  183. }
  184. function testProductLimit() {
  185. $admin = $this->drupalCreateUser(array('administer product features'));
  186. $this->drupalLogin($admin);
  187. $product = $this->drupalCreateProduct();
  188. variable_set('uc_restrict_qty_global', 0);
  189. $product_settings = array(
  190. 'model' => $product->model,
  191. 'quantity' => 1,
  192. 'lifetime' => 0,
  193. );
  194. $this->drupalPost('node/'. $product->nid .'/edit/features/restrict_qty/add', $product_settings, t('Save feature'));
  195. $user1 = $this->drupalCreateUser();
  196. $this->drupalLogin($user1);
  197. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  198. $this->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $product->title, '!url' => url('cart'))), 'Product limit [#1 to add]');
  199. // Single item should be not included into input field
  200. $this->assertRaw('<td class="qty">1</td>', 'Global limit [#1 without input in cart]');
  201. // Second should cause error
  202. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  203. $this->assertRaw(format_plural($product_settings['quantity'], 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $product->title)), 'Product limit [above the limit]');
  204. $this->drupalLogin($admin);
  205. $this->drupalGet('node/'. $product->nid .'/edit/features');
  206. if (preg_match('|node/[0-9]*/edit/features/restrict_qty/[0-9]*|', $this->content, $matches)) {
  207. $edit_url = $matches[0];
  208. $product_settings = array(
  209. 'model' => $product->model,
  210. 'quantity' => 2,
  211. 'lifetime' => FALSE,
  212. );
  213. $this->drupalPost($edit_url, $product_settings, t('Save feature'));
  214. // Login another user (to get empty cart)
  215. $user2 = $this->drupalCreateUser();
  216. $this->drupalLogin($user2);
  217. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  218. $this->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $product->title, '!url' => url('cart'))), 'Product limit [#2 to add|first]');
  219. // Item should be included into input field
  220. $this->assertRaw('<input type="text" maxlength="6" name="items[0][qty]" id="edit-items-0-qty" size="5" value="1" class="form-text" />', 'Product limit [#2 with input in cart]');
  221. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  222. $this->assertRaw(t('Your item(s) have been updated.'), '#2 to add|second');
  223. $this->assertNoRaw(format_plural($product_settings['quantity'], 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $product->title)), 'Product limit [#2 to add|second|no warnings]');
  224. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  225. $this->assertRaw(format_plural($product_settings['quantity'], 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $product->title)), 'Product limit [#2 to add|third|above limit warning]');
  226. $this->assertRaw('<input type="text" maxlength="6" name="items[0][qty]" id="edit-items-0-qty" size="5" value="2" class="form-text" />', 'Product limit [#2 to add|third|#2 in cart]');
  227. // Test lifetime limit
  228. $this->drupalLogin($admin);
  229. $product_settings = array(
  230. 'model' => $product->model,
  231. 'quantity' => 2,
  232. 'lifetime' => 1,
  233. );
  234. $this->drupalPost($edit_url, $product_settings, t('Save feature'));
  235. $user3 = $this->drupalCreateUser(array('view own orders'));
  236. $this->drupalLogin($user3);
  237. $products = array($product);
  238. $order = $this->drupalCreateOrder($products, $user3);
  239. uc_order_update_status($order->order_id, 'completed');
  240. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  241. $this->assertRaw(t('<strong>@product-title</strong> added to <a href="!url">your shopping cart</a>.', array('@product-title' => $product->title, '!url' => url('cart'))), 'Product lifetime limit [first and last]');
  242. // It should be single item without input, as we have limit 2 with one alrready ordered
  243. $this->assertRaw('<td class="qty">1</td>', 'Product lifetime limit [#1 without input in cart]');
  244. // Second should cause error
  245. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  246. $this->assertRaw(format_plural($product_settings['quantity'] - count($products), 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $product->title)), 'Product lifetime limit [above the limit]');
  247. // Restrict adding product to the cart, if limit has been reached
  248. $this->drupalLogin($admin);
  249. $product_settings = array(
  250. 'model' => $product->model,
  251. 'quantity' => 1,
  252. 'lifetime' => 1,
  253. );
  254. $this->drupalPost($edit_url, $product_settings, t('Save feature'));
  255. $this->drupalLogin($user3);
  256. // Any adding to cart should cause warning, as everything available already ordered
  257. $this->drupalPost('node/'. $product->nid, array(), t('Add to cart'));
  258. $this->assertRaw(t('Sorry, you have reached the quantity limit for this product. You can not order more items of this product.'), 'Product lifetime limit [above the limit]');
  259. }
  260. else {
  261. $this->fail('Feature edit link not found');
  262. }
  263. }
  264. }