You are here

protected function UbercartProductPowerToolsTestCase::validateSettings in Ubercart Product Power Tools 7

Helper function to set up given settings, submit a new product node and check that node is valid.

Parameters

array $edit:

array $overrides:

array $expected Values that are expected from automatic submission.:

1 call to UbercartProductPowerToolsTestCase::validateSettings()
UbercartProductPowerToolsTestCase::testProductPowerToolsNodeForm in tests/uc_product_power_tools.test

File

tests/uc_product_power_tools.test, line 424
Ubercart Product Power Tools Tests

Class

UbercartProductPowerToolsTestCase
@file Ubercart Product Power Tools Tests

Code

protected function validateSettings($edit = array(), $overrides = array(), $expected = array()) {
  $this
    ->drupalPost('admin/store/products/power-tools/product', $edit, t('Submit'));
  $this
    ->drupalGet('node/add/product');
  foreach (array(
    'model',
    'list_price',
    'cost',
    'sell_price',
    'shippable',
    'weight',
    'weight_units',
    'dim_length',
    'dim_width',
    'dim_height',
    'length_units',
    'default_qty',
    'pkg_qty',
    'ordering',
  ) as $field) {
    if (!isset($expected[$field])) {
      $this
        ->assertFieldByName($field, NULL);
    }
  }
  $body_key = 'body[und][0][value]';

  // Make a node with those fields.
  $edit = array(
    'title' => $this
      ->randomName(32),
    $body_key => $this
      ->randomName(64),
    'model' => $this
      ->randomName(8),
    'list_price' => mt_rand(1, 200),
    'cost' => mt_rand(0, 100),
    'sell_price' => mt_rand(1, 150),
    'default_qty' => mt_rand(1, 100),
    'pkg_qty' => mt_rand(1, 100),
    'shippable' => mt_rand(0, 1),
    'weight' => mt_rand(1, 50),
    'weight_units' => array_rand(array(
      'lb' => t('Pounds'),
      'kg' => t('Kilograms'),
      'oz' => t('Ounces'),
      'g' => t('Grams'),
    )),
    'dim_length' => mt_rand(1, 50),
    'dim_width' => mt_rand(1, 50),
    'dim_height' => mt_rand(1, 50),
    'length_units' => array_rand(array(
      'in' => t('Inches'),
      'ft' => t('Feet'),
      'cm' => t('Centimeters'),
      'mm' => t('Millimeters'),
    )),
    'ordering' => mt_rand(-25, 25),
  );
  foreach ($overrides as $override_name => $override_value) {
    $edit[$override_name] = $override_value;
  }

  // If we are expecting a certain value, unset it from the form submissions.
  foreach (array_keys($expected) as $unset_name) {
    unset($edit[$unset_name]);
  }
  $this
    ->drupalPost('node/add/product', $edit, t('Save'));
  $this
    ->assertText(t('Product @title has been created.', array(
    '@title' => $edit['title'],
  )), t('Product created.'));
  $model = isset($expected['model']) ? $expected['model'] : $edit['model'];
  $this
    ->assertText(t('SKU: @model', array(
    '@model' => $model,
  )), t('Product model found.'));
  $sell_price = isset($expected['sell_price']) ? $expected['sell_price'] : $edit['sell_price'];
  $this
    ->assertText(t('Price: @price', array(
    '@price' => uc_currency_format($sell_price),
  )), t('Product sell price found.'));
  $list_price = isset($expected['list_price']) ? $expected['list_price'] : $edit['list_price'];
  $this
    ->assertText(t('List price: @price', array(
    '@price' => uc_currency_format($list_price),
  )), t('Product list price found.'));
  $cost = isset($expected['cost']) ? $expected['cost'] : $edit['cost'];
  $this
    ->assertText(t('Cost: @price', array(
    '@price' => uc_currency_format($cost),
  )), t('Product cost found.'));
  $weight = isset($expected['weight']) ? $expected['weight'] : $edit['weight'];
  $weight_units = isset($expected['weight_units']) ? $expected['weight_units'] : $edit['weight_units'];
  $this
    ->assertText(t('Weight: @weight', array(
    '@weight' => uc_weight_format($weight, $weight_units),
  )), t('Product weight found.'));
  $length = isset($expected['dim_length']) ? $expected['dim_length'] : $edit['dim_length'];
  $width = isset($expected['dim_width']) ? $expected['dim_width'] : $edit['dim_width'];
  $height = isset($expected['dim_height']) ? $expected['dim_height'] : $edit['dim_height'];
  $length_units = isset($expected['length_units']) ? $expected['length_units'] : $edit['length_units'];
  $dimensions = uc_length_format($length, $length_units) . ' × ' . uc_length_format($width, $length_units) . ' × ' . uc_length_format($height, $length_units);
  $this
    ->assertText(t('Dimensions: @dimensions', array(
    '@dimensions' => $dimensions,
  )), t('Product dimensions found.'));
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $default_qty = isset($expected['default_qty']) ? $expected['default_qty'] : $edit['default_qty'];
  $this
    ->assertEqual($node->default_qty, $default_qty, t('Default quantity found.'));
  $pkg_qty = isset($expected['pkg_qty']) ? $expected['pkg_qty'] : $edit['pkg_qty'];
  $this
    ->assertEqual($node->pkg_qty, $pkg_qty, t('Package quantity found.'));
}