<?php

/**
 * @file
 * Commerce Discounts usage UI tests.
 */

/**
 * Testing commerce discount usage module UI.
 */
class CommerceDiscountUsageUITest extends CommerceDiscountTestBase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Discounts usage',
      'description' => 'Test discounts usage UI',
      'group' => 'Commerce Discount UI',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
  }

  /**
   * Test usage specific elements in the add discount UI.
   */
  public function testCommerceDiscountUsageUIAddDiscount() {
    // Login with store admin.
    $this->drupalLogin($this->store_admin);

    // Access to the admin discount creation page.
    $this->drupalGet('admin/commerce/discounts/add');

    // Check the integrity of the add form.
    $this->assertFieldByName('commerce_discount_fields[discount_usage_per_person][und][0][value]', NULL, 'Maximum usage per customer field is present');
    $this->assertFieldByName('commerce_discount_fields[discount_usage_limit][und][0][value]', NULL, 'Maximum overall usage field is present');

    // Create a discount.
    $values = array(
      'label' => 'Order discount - fixed',
      'name' => 'order_discount_fixed',
      'component_title' => 'Order discount',
      'commerce_discount_fields[commerce_discount_offer][und][form][commerce_fixed_amount][und][0][amount]' => 12.77,
      'commerce_discount_fields[discount_usage_limit][und][0][value]' => 5,
    );
    $this->drupalPost(NULL, $values, t('Save discount'));

    // Load the discount and wrap it.
    $discount = entity_load_single('commerce_discount', 1);
    $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount);

    // Check the usage fields of the stored discount.
    $this->assertTrue($discount_wrapper->discount_usage_per_person->value() == 0, 'Discount uses field is empty.');
    $this->assertTrue($discount_wrapper->discount_usage_limit->value() == 5, 'Discount max uses stored correctly.');

    // Check the discounts listing.
    $this->assertText(t('@amount available', array('@amount' => 5)), 'Analytics - Max usage is shown.');
    $this->assertText(t('Used @amount times', array('@amount' => 0)), 'Analytics - Usage is shown.');
  }

  /**
   * Test usage specific elements in the edit discount UI.
   */
  public function testCommerceDiscountUsageUIEditDiscount() {
    // Testing fixed discount.
    // Create a fixed order discount of $3 limited to one use.
    $discount = $this->createUsageDiscount('order_discount', 'fixed_amount', 300, 1);

    // Login with store admin.
    $this->drupalLogin($this->store_admin);

    // Access to the admin discount edit page.
    $this->drupalGet('admin/commerce/discounts/manage/' . $discount->name);

    // Check the integrity of the add form.
    $this->assertFieldByName('commerce_discount_fields[discount_usage_per_person][und][0][value]', NULL, 'Maximum usage per customer field is present');
    $this->assertFieldByName('commerce_discount_fields[discount_usage_limit][und][0][value]', NULL, 'Maximum overall usage field is present');

    // Change the discount values.
    $values = array(
      'label' => 'Order discount - fixed',
      'name' => 'order_discount_fixed',
      'component_title' => 'Order discount',
      'commerce_discount_fields[commerce_discount_offer][und][form][commerce_fixed_amount][und][0][amount]' => 12.77,
      'commerce_discount_fields[discount_usage_limit][und][0][value]' => 5,
    );
    $this->drupalPost(NULL, $values, t('Save discount'));

    // Load the discount from the database and wrap it.
    $discounts = entity_load('commerce_discount', array($discount->discount_id), $conditions = array(), $reset = TRUE);
    $discount_wrapper = entity_metadata_wrapper('commerce_discount', reset($discounts));

    // Check the usage fields of the stored discount.
    $this->assertEqual($discount_wrapper->discount_usage_per_person->value(), 0, 'Discount uses field is empty.');
    $this->assertEqual($discount_wrapper->discount_usage_limit->value(), 5, 'Discount max uses stored correctly.');

    // Check the discounts listing.
    $this->assertText(t('@amount available', array('@amount' => 5)), 'Analytics - Max usage is shown.');
    $this->assertText(t('Used @amount times', array('@amount' => 0)), 'Analytics - Usage is shown.');
  }

}
