<?php

/**
 * @file
 * Ubercart attribute checkout tests.
 */

class UbercartAttributeCheckoutTestCase extends UbercartTestHelper {

  public static function getInfo() {
    return array(
      'name' => 'Attribute Checkout',
      'description' => 'Test ordering products with attributes.',
      'group' => 'Ubercart',
    );
  }

  /**
   * Overrides DrupalWebTestCase::setUp().
   */
  function setUp() {
    parent::setUp(array('uc_attribute', 'uc_cart'), array('administer attributes', 'administer product attributes', 'administer product options'));
    $this->drupalLogin($this->adminUser);
  }

  /**
   * Tests that product in cart has the selected attribute option.
   */
  function testAttributeAddToCart() {
    for ($display = 0; $display <= 3; ++$display) {
      // Set up an attribute.
      $data = array(
        'display' => $display,
      );
      $attribute = UbercartAttributeTestCase::createAttribute($data);

      if ($display) {
        // Give the attribute an option.
        $option = UbercartAttributeTestCase::createAttributeOption(array('aid' => $attribute->aid));
      }

      $attribute = uc_attribute_load($attribute->aid);

      // Put the attribute on a product.
      $product = $this->createProduct();
      uc_attribute_subject_save($attribute, 'product', $product->nid, TRUE);

      // Add the product to the cart.
      if ($display == 3) {
        $edit = array("attributes[$attribute->aid][$option->oid]" => $option->oid);
      }
      elseif (isset($option)) {
        $edit = array("attributes[$attribute->aid]" => $option->oid);
      }
      else {
        $option = new stdClass();
        $option->name = self::randomName();
        $option->price = 0;
        $edit = array("attributes[$attribute->aid]" => $option->name);
      }

      $this->drupalPost('node/' . $product->nid, $edit, t('Add to cart'));
      $this->assertText("$attribute->label: $option->name", t('Option selected on cart item.'));
      $this->assertText(uc_currency_format($product->sell_price + $option->price), t('Product has adjusted price.'));
    }
  }

}
