<?php

/**
 * @file
 * Ubercart Product Tests
 */

// Ensure UbercartTestHelper is available.
module_load_include('test', 'uc_store', 'uc_store');

class UbercartProductTestCase extends UbercartTestHelper {

  public static function getInfo() {
    return array(
      'name' => 'Products',
      'description' => 'Ensure that the product content types provided function properly.',
      'group' => 'Ubercart',
    );
  }

  public function setUp() {
    parent::setUp();
    $this->drupalLogin($this->adminUser);
  }

  public function testProductNodeForm() {
    $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', 'pkg_qty', 'default_qty', 'ordering') as $field) {
      $this->assertFieldByName($field, NULL);
    }

    $body_key = 'body';

    // 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),
      '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'),
      )),
    );

    $this->drupalPost('node/add/product', $edit, t('Save'));
    $this->assertText(t('Product @title has been created.', array('@title' => $edit['title'])));
    $this->assertText($edit[$body_key], 'Product body found.');
    $this->assertText(t('SKU: @model', array('@model' => $edit['model'])), 'Product model found.');
    $this->assertText(t('Price: @price', array('@price' => uc_currency_format($edit['sell_price']))), 'Product sell price found.');
    $this->assertNoUniqueText(uc_currency_format($edit['sell_price']), 'Price appears more than once.');
  }

  public function testProductClassForm() {
    // Try making a new product class.
    $class = $this->randomName(12);
    $type = strtolower($class);
    $edit = array(
      'pcid' => $class,
      'name' => $class,
      'description' => $this->randomName(32),
    );

    $this->drupalPost('admin/store/products/classes', $edit, t('Submit'));
    $this->assertText(t('Product class saved.'), 'Product class form submitted.');

    $base = db_result(db_query("SELECT module FROM {node_type} WHERE type = '%s'", $type));
    $this->assertEqual($base, 'uc_product', 'The new content type has been created in the database.');

    // Make an existing node type a product class.
    $type = $this->drupalCreateContentType();
    $edit = array(
      'pcid' => $type->type,
      'name' => $type->name,
      'description' => $type->description,
    );

    $this->drupalPost('admin/store/products/classes', $edit, t('Submit'));
    $this->assertText(t('Product class saved.'), 'Product class form submitted.');

    $base = db_result(db_query("SELECT module FROM {node_type} WHERE type = '%s'", $type->type));
    $this->assertEqual($base, 'uc_product', 'The new content type has been taken over by uc_product.');
  }
}
