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