| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* ad_ubercart auto-tests.
|
| 7 |
*/
|
| 8 |
|
| 9 |
class AdUbercartFunctionalTest extends DrupalWebTestCase {
|
| 10 |
function getInfo() {
|
| 11 |
return array(
|
| 12 |
'name' => t('Various functional tests'),
|
| 13 |
'description' => '',
|
| 14 |
'group' => t('Ad Ubercart'),
|
| 15 |
);
|
| 16 |
}
|
| 17 |
/**
|
| 18 |
* Implementation of setUp().
|
| 19 |
*/
|
| 20 |
function setUp() {
|
| 21 |
parent::setUp('uc_store', 'uc_cart', 'uc_product', 'uc_order', 'ca', 'ad', 'ad_text', 'ad_ubercart');
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of tearDown().
|
| 26 |
*/
|
| 27 |
function tearDown() {
|
| 28 |
parent::tearDown();
|
| 29 |
}
|
| 30 |
|
| 31 |
function drupalCreateProduct($price = 100, $type = 'product') {
|
| 32 |
// Create a product
|
| 33 |
$node = array(
|
| 34 |
'type' => $type,
|
| 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 |
else {
|
| 73 |
$order->products = uc_cart_get_contents($user->uid);
|
| 74 |
}
|
| 75 |
|
| 76 |
$order->delivery_first_name = $this->randomName();
|
| 77 |
$order->delivery_last_name = $this->randomName();
|
| 78 |
$order->delivery_phone = $this->randomName();
|
| 79 |
$order->delivery_company = $this->randomName();
|
| 80 |
$order->delivery_street1 = $this->randomName();
|
| 81 |
$order->delivery_street2 = $this->randomName();
|
| 82 |
$order->delivery_city = $this->randomName();
|
| 83 |
$order->delivery_zone = $this->randomName();
|
| 84 |
$order->delivery_postal_code = $this->randomName();
|
| 85 |
$order->delivery_country = $this->randomName();
|
| 86 |
$order->billing_first_name = $this->randomName();
|
| 87 |
$order->billing_last_name = $this->randomName();
|
| 88 |
$order->billing_phone = $this->randomName();
|
| 89 |
$order->billing_company = $this->randomName();
|
| 90 |
$order->billing_street1 = $this->randomName();
|
| 91 |
$order->billing_street2 = $this->randomName();
|
| 92 |
$order->billing_city = $this->randomName();
|
| 93 |
$order->billing_zone = $this->randomName();
|
| 94 |
$order->billing_postal_code = $this->randomName();
|
| 95 |
$order->billing_country = $this->randomName();
|
| 96 |
$order->payment_method = $this->randomName();
|
| 97 |
$order->manufacturer = $this->randomName();
|
| 98 |
$order->weight = rand();
|
| 99 |
$order->data = array();
|
| 100 |
$order->line_items = array();
|
| 101 |
|
| 102 |
uc_order_save($order);
|
| 103 |
return $order;
|
| 104 |
}
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
function testQuotaAdministration() {
|
| 109 |
$admin = $this->drupalCreateUser(array('administer store'));
|
| 110 |
$this->drupalLogin($admin);
|
| 111 |
|
| 112 |
// Check if no quota warnings present.
|
| 113 |
$this->drupalGet('admin/settings/ad_ubercart');
|
| 114 |
$this->assertRaw(t('There is no available quota.'), 'No quota warning is present.');
|
| 115 |
$this->assertRaw(t('Please, <a href="!url">add</a> some quota first.', array('!url' => url('admin/settings/ad_ubercart/add_quota'))), 'Suggestion to add qouta is present.');
|
| 116 |
|
| 117 |
// Test settings' validators
|
| 118 |
$quota = array(
|
| 119 |
'month' => date('Y-m'),
|
| 120 |
'daily' => -22,
|
| 121 |
'monthly' => 33,
|
| 122 |
);
|
| 123 |
$this->drupalPost('admin/settings/ad_ubercart/add_quota', $quota, t('Save'));
|
| 124 |
$this->assertText(t('Should be a positive number or zero.'), 'Quota validation [Daily count]');
|
| 125 |
|
| 126 |
$quota['daily'] = 22;
|
| 127 |
$quota['monthly'] = -33;
|
| 128 |
$this->drupalPost('admin/settings/ad_ubercart/add_quota', $quota, t('Save'));
|
| 129 |
$this->assertText(t('Should be a positive number or zero.'), 'Quota validation [Daily count]');
|
| 130 |
|
| 131 |
// Submit real quota.
|
| 132 |
$quota['monthly'] = 33;
|
| 133 |
$this->drupalPost('admin/settings/ad_ubercart/add_quota', $quota, t('Save'));
|
| 134 |
$this->assertRaw(t('Quota for %month added successfully!', array('%month' => ad_ubercart_format_month($quota['month']))), 'Quota added [message present].');
|
| 135 |
$this->assertRaw('<td>'. ad_ubercart_format_month($quota['month']) .'</td><td>'. $quota['daily'] .' (Used: <em>0</em>)</td><td>'. $quota['monthly'] .' (Used: <em>0</em>)</td>', 'Quota added [data correct].');
|
| 136 |
|
| 137 |
// Check if submitted quota can't be added one more time.
|
| 138 |
$this->drupalGet('admin/settings/ad_ubercart/add_quota');
|
| 139 |
$this->assertNoRaw('<option value="'. $quota['month'] .'">'. ad_ubercart_format_month($quota['month']) .'</option>', "Added month isn't appearing in new quota select.");
|
| 140 |
|
| 141 |
// Check qota edit form.
|
| 142 |
$this->drupalGet('admin/settings/ad_ubercart/quota/'. $quota['month']);
|
| 143 |
$this->assertRaw('<select name="month" disabled="disabled" class="form-select" id="edit-month" >', 'Quota correct on edit form [month diabled].');
|
| 144 |
$this->assertRaw('<option value="'. $quota['month'] .'" selected="selected">'. ad_ubercart_format_month($quota['month']) .'</option>', 'Quota correct on edit form [month diabled].');
|
| 145 |
$this->assertRaw('<input type="text" maxlength="128" name="daily" id="edit-daily" size="60" value="'. $quota['daily'] .'" class="form-text" />', 'Quota correct on edit form [daily].');
|
| 146 |
$this->assertRaw('<input type="text" maxlength="128" name="monthly" id="edit-monthly" size="60" value="'. $quota['monthly'] .'" class="form-text" />', 'Quota correct on edit form [monthly].');
|
| 147 |
|
| 148 |
// Check qota edit form submission.
|
| 149 |
$quota['daily'] = 2;
|
| 150 |
$quota['monthly'] = 3;
|
| 151 |
$this->drupalPost('admin/settings/ad_ubercart/quota/'. $quota['month'], $quota, t('Save'));
|
| 152 |
$this->assertRaw(t('Quota for %month updated successfully!', array('%month' => ad_ubercart_format_month($quota['month']))), 'Quota updated [message present].');
|
| 153 |
$this->assertRaw('<td>'. ad_ubercart_format_month($quota['month']) .'</td><td>'. $quota['daily'] .' (Used: <em>0</em>)</td><td>'. $quota['monthly'] .' (Used: <em>0</em>)</td>', 'Quota updated [data correct].');
|
| 154 |
}
|
| 155 |
|
| 156 |
function testMonthlyAds() {
|
| 157 |
// We will sell text ads.
|
| 158 |
variable_set('ad_ubercart_ad_content_type', 'ad');
|
| 159 |
|
| 160 |
$admin = $this->drupalCreateUser(array('administer store'));
|
| 161 |
$this->drupalLogin($admin);
|
| 162 |
|
| 163 |
|
| 164 |
// Add & check monthly product.
|
| 165 |
$ad_product_monthly = $this->drupalCreateProduct(100, 'ad_product_monthly');
|
| 166 |
// Check product with no quota.
|
| 167 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 168 |
$this->assertRaw('Sorry, there are no available quota.', 'No quota warning is present [no quota].');
|
| 169 |
|
| 170 |
// Add empty quota.
|
| 171 |
$quota = array(
|
| 172 |
'month' => date('Y-m'),
|
| 173 |
'daily' => 0,
|
| 174 |
'monthly' => 0,
|
| 175 |
);
|
| 176 |
$this->drupalPost('admin/settings/ad_ubercart/add_quota', $quota, t('Save'));
|
| 177 |
|
| 178 |
// Check product with empty quota.
|
| 179 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 180 |
$this->assertRaw('Sorry, there are no available quota.', 'No quota warning is present [empty quota].');
|
| 181 |
|
| 182 |
// Add initial quota.
|
| 183 |
$quota['monthly'] = 1;
|
| 184 |
$this->drupalPost('admin/settings/ad_ubercart/quota/'. $quota['month'], $quota, t('Save'));
|
| 185 |
|
| 186 |
// Check product with quota.
|
| 187 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 188 |
$this->assertRaw('<label for="edit-period">'. t('Period') .': </label>', 'Check if period control exists.');
|
| 189 |
$this->assertRaw('id="edit-period" ><option value="'. $quota['month'] .'">'. ad_ubercart_format_month($quota['month']) .'</option></select>', 'Check if period filled with only one month.');
|
| 190 |
|
| 191 |
///////////////////////////////
|
| 192 |
// Adding first ad to cart.
|
| 193 |
|
| 194 |
$user1 = $this->drupalCreateUser(array('create advertisements'));
|
| 195 |
$this->drupalLogin($user1);
|
| 196 |
|
| 197 |
// Check validation.
|
| 198 |
$this->drupalPost('node/'. $ad_product_monthly->nid, array(), t('Add to cart'));
|
| 199 |
$this->assertRaw(t('Please, fill the period.'), 'Check if period selected.');
|
| 200 |
|
| 201 |
// Proceed to second step.
|
| 202 |
$cart = array('period[]' => array($quota['month']));
|
| 203 |
$this->drupalPost('node/'. $ad_product_monthly->nid, $cart, t('Add to cart'));
|
| 204 |
$this->assertRaw('<div id="ad-ubercart-wrapper">', 'Check if module serves the second step.');
|
| 205 |
// Submit ad information:
|
| 206 |
// ... select ad type
|
| 207 |
$this->drupalPost(NULL, array('adtype_select' => 'text'), t('Select'));
|
| 208 |
// ... submit everything else
|
| 209 |
$ad = array(
|
| 210 |
'title' => 'ad1',
|
| 211 |
'url' => 'http://google.com/',
|
| 212 |
'adheader' => 'adheader1',
|
| 213 |
'adbody' => 'adbody1',
|
| 214 |
);
|
| 215 |
$this->drupalPost(NULL, $ad, t('Save'));
|
| 216 |
// Check the cart.
|
| 217 |
$this->assertRaw('<strong>'. t('Months included') .':</strong>', 'Cart item includes month info [title].');
|
| 218 |
$this->assertRaw('<ul><li>'. ad_ubercart_format_month($quota['month']) ."</li>\n</ul>", 'Cart item includes month info [list of months].');
|
| 219 |
$this->assertRaw('<strong>'. t('Linked Advertisement') .':</strong>', 'Cart item includes linked ad [title].');
|
| 220 |
$this->assertRaw('adbody1', 'Cart item includes linked ad [ad body].');
|
| 221 |
if (preg_match('|ad/redirect/([0-9]*)/|', $this->content, $matches)) {
|
| 222 |
$ad_nid = $matches[1];
|
| 223 |
}
|
| 224 |
else {
|
| 225 |
$this->fail('Ad nid not found.');
|
| 226 |
}
|
| 227 |
|
| 228 |
///////////////////////////////
|
| 229 |
// Add existing ad to cart.
|
| 230 |
|
| 231 |
// Check product with quota.
|
| 232 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 233 |
$this->assertRaw('id="edit-period" ><option value="'. $quota['month'] .'">'. ad_ubercart_format_month($quota['month']) .'*</option></select>', 'Check if month starred.');
|
| 234 |
$this->assertRaw(t('<b>Note:</b> * — this item is already in your cart. If you select them, they will be removed from your old selections.'), 'Check if star description is present.');
|
| 235 |
|
| 236 |
// Proceed to second step.
|
| 237 |
$cart = array('period[]' => array($quota['month']));
|
| 238 |
$this->drupalPost('node/'. $ad_product_monthly->nid, $cart, t('Add to cart'));
|
| 239 |
$this->assertRaw('<select name="adnode" class="form-select" id="edit-adnode" ><option value="0">'. t('Please, choose') .'</option><option value="1">'. t('Existing...') .'</option><option value="2">'. t('Create new...') .'</option></select>', 'Should be existing/create new form.');
|
| 240 |
// Check existing ads.
|
| 241 |
$cart = array('adnode' => 1);
|
| 242 |
$this->drupalPost(NULL, $cart, t('Select'));
|
| 243 |
$this->assertRaw('<label>'. t('Your Advertisements') .': </label>', 'List of existing ads [title].');
|
| 244 |
$this->assertRaw('adbody1', 'List of existing ads [ad body].');
|
| 245 |
// Select existing ad.
|
| 246 |
$cart = array('existing_node' => $ad_nid);
|
| 247 |
$this->drupalPost(NULL, $cart, t('Submit'));
|
| 248 |
// Check the cart.
|
| 249 |
$this->assertRaw('<strong>'. t('Months included') .':</strong>', 'Cart item includes month info [title].');
|
| 250 |
$this->assertRaw('<ul><li>'. ad_ubercart_format_month($quota['month']) ."</li>\n</ul>", 'Cart item includes month info [list of months].');
|
| 251 |
$this->assertRaw('<strong>'. t('Linked Advertisement') .':</strong>', 'Cart item includes linked ad [title].');
|
| 252 |
$this->assertRaw('adbody1', 'Cart item includes linked ad [ad body].');
|
| 253 |
if (preg_match('|ad/redirect/([0-9]*)/|', $this->content, $matches)) {
|
| 254 |
$new_ad_nid = $matches[1];
|
| 255 |
if ($new_ad_nid == $ad_nid) {
|
| 256 |
$this->fail('New ad was not crated.');
|
| 257 |
}
|
| 258 |
}
|
| 259 |
else {
|
| 260 |
$this->fail('Ad nid not found.');
|
| 261 |
}
|
| 262 |
|
| 263 |
///////////////////////////////
|
| 264 |
// Add new ad to cart.
|
| 265 |
|
| 266 |
// Proceed to second step.
|
| 267 |
$cart = array('period[]' => array($quota['month']));
|
| 268 |
$this->drupalPost('node/'. $ad_product_monthly->nid, $cart, t('Add to cart'));
|
| 269 |
$this->assertRaw('<select name="adnode" class="form-select" id="edit-adnode" ><option value="0">'. t('Please, choose') .'</option><option value="1">'. t('Existing...') .'</option><option value="2">'. t('Create new...') .'</option></select>', 'Should be existing/create new form.');
|
| 270 |
// Check existing ads.
|
| 271 |
$cart = array('adnode' => 2);
|
| 272 |
$this->drupalPost(NULL, $cart, t('Select'));
|
| 273 |
|
| 274 |
// Submit ad information:
|
| 275 |
// ... select ad type
|
| 276 |
$this->drupalPost(NULL, array('adtype_select' => 'text'), t('Select'));
|
| 277 |
// ... submit everything else
|
| 278 |
$ad = array(
|
| 279 |
'title' => 'ad2',
|
| 280 |
'url' => 'http://google.com/',
|
| 281 |
'adheader' => 'adheader2',
|
| 282 |
'adbody' => 'adbody2',
|
| 283 |
);
|
| 284 |
$this->drupalPost(NULL, $ad, t('Save'));
|
| 285 |
// Check the cart.
|
| 286 |
$this->assertRaw('<strong>'. t('Months included') .':</strong>', 'Cart item includes month info [title].');
|
| 287 |
$this->assertRaw('<ul><li>'. ad_ubercart_format_month($quota['month']) ."</li>\n</ul>", 'Cart item includes month info [list of months].');
|
| 288 |
$this->assertRaw('<strong>'. t('Linked Advertisement') .':</strong>', 'Cart item includes linked ad [title].');
|
| 289 |
$this->assertRaw('adbody2', 'Cart item includes linked ad [ad body].');
|
| 290 |
|
| 291 |
//////////////////////////////////////
|
| 292 |
// Place order on the cart contents.
|
| 293 |
|
| 294 |
$order = $this->drupalCreateOrder(array(), $user1);
|
| 295 |
uc_order_update_status($order->order_id, 'completed');
|
| 296 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 297 |
|
| 298 |
// By this time we should run out of quota.
|
| 299 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 300 |
$this->assertRaw('Sorry, there are no available quota.', 'No quota warning is present [no quota].');
|
| 301 |
|
| 302 |
// Log to admin,
|
| 303 |
$this->drupalLogin($admin);
|
| 304 |
// ...as used quota should appear in quota administration screen.
|
| 305 |
$this->drupalGet('admin/settings/ad_ubercart');
|
| 306 |
$this->assertRaw('<td>'. ad_ubercart_format_month($quota['month']) .'</td><td>'. $quota['daily'] .' (Used: <em>0</em>)</td><td>'. $quota['monthly'] .' (Used: <em>'. 1 .'</em>)</td>', 'Monsthly quota was used.');
|
| 307 |
|
| 308 |
|
| 309 |
//////////////////////////////////////
|
| 310 |
// Check usage of two months.
|
| 311 |
|
| 312 |
// Add quota for another month.
|
| 313 |
$next_month = date('Y-m', mktime(0, 0, 0, date('m') + 1, 1, date('Y')));
|
| 314 |
$quota2 = array(
|
| 315 |
'month' => $next_month,
|
| 316 |
'daily' => 0,
|
| 317 |
'monthly' => 2,
|
| 318 |
);
|
| 319 |
$this->drupalPost('admin/settings/ad_ubercart/add_quota', $quota2, t('Save'));
|
| 320 |
|
| 321 |
$user1 = $this->drupalCreateUser(array('create advertisements'));
|
| 322 |
$this->drupalLogin($user1);
|
| 323 |
|
| 324 |
// In list should appear only one month (as previous is exhaused).
|
| 325 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 326 |
$this->assertRaw('<label for="edit-period">'. t('Period') .': </label>', 'Check if period control exists.');
|
| 327 |
$this->assertRaw('id="edit-period" ><option value="'. $quota2['month'] .'">'. ad_ubercart_format_month($quota2['month']) .'</option></select>', 'Check if period filled with only one month.');
|
| 328 |
|
| 329 |
// Emulate some past quota usage data.
|
| 330 |
$prev_month = date('Y-m', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
|
| 331 |
$data = array('month' => $prev_month, 'daily' => 5, 'monthly' => 5);
|
| 332 |
drupal_write_record('ad_quota', $data);
|
| 333 |
// Check everything if we have old months in DB.
|
| 334 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 335 |
$this->assertRaw('id="edit-period" ><option value="'. $quota2['month'] .'">'. ad_ubercart_format_month($quota2['month']) .'</option></select>', 'Check if period filled with only one month.');
|
| 336 |
|
| 337 |
$data = array('date' => $prev_month, 'nid' => 123, 'order_id' => 123, 'type' => 'monthly');
|
| 338 |
drupal_write_record('ad_used_quota', $data);
|
| 339 |
// Check everything if we have old months in DB.
|
| 340 |
$this->drupalGet('node/'. $ad_product_monthly->nid);
|
| 341 |
$this->assertRaw('id="edit-period" ><option value="'. $quota2['month'] .'">'. ad_ubercart_format_month($quota2['month']) .'</option></select>', 'Check if period filled with only one month.');
|
| 342 |
}
|
| 343 |
}
|