<?php

/**
 * @file
 * Tests for Ubercart orders.
 */

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

class UbercartOrderTestCase extends UbercartTestHelper {

  public static function getInfo() {
    return array(
      'name' => 'Orders',
      'description' => 'Ensure that orders function properly.',
      'group' => 'Ubercart',
    );
  }

  public function testOrderCreation() {
    $this->drupalLogin($this->adminUser);

    $edit = array('uid' => $this->customer->uid);
    $this->drupalPost('admin/store/orders/create', $edit, t('Create order'));
    $this->assertText(t('Order created by the administration.'), 'Order created by the administration.');
    $this->assertFieldByName('uid_text', $this->customer->uid, 'The customer UID appears on the page.');

    $order_id = db_result(db_query("SELECT order_id FROM {uc_orders} WHERE uid = %d", $this->customer->uid));
    $this->assertTrue($order_id, t('Found order ID @order_id', array('@order_id' => $order_id)));

    $this->drupalGet('admin/store/orders');
    $links = $this->xpath('//a[@href="' . url('admin/store/orders/' . $order_id) . '"]');
    $this->assertTrue(isset($links[0]), 'View link appears on order list.');
    $this->assertText('Pending', 'New order is "Pending".');
  }

  public function testOrderEditing() {
    $order = $this->ucCreateOrder($this->customer);

    $this->drupalLogin($this->customer);
    $this->drupalGet('user/' . $this->customer->uid . '/orders');
    $this->assertText(t('My order history'));

    $this->drupalGet('user/' . $this->customer->uid . '/orders/' . $order->order_id);
    $this->assertResponse(200, 'Customer can view their own order.');

    $this->drupalGet('admin/store/orders/' . $order->order_id);
    $this->assertResponse(403, 'Customer may not edit orders.');

    $this->drupalLogin($this->adminUser);
    $this->drupalGet('user/' . $this->customer->uid . '/order/' . $order->order_id);
    $this->assertText(drupal_strtoupper($order->billing_first_name . ' ' . $order->billing_last_name), 'Found customer name.');

    $edit = array(
      'billing_first_name' => $this->randomName(8),
      'billing_last_name' => $this->randomName(15),
    );
    $this->drupalPost('admin/store/orders/' . $order->order_id . '/edit', $edit, t('Submit changes'));
    $this->assertText(t('Order changes saved.'));
    $this->assertFieldByName('billing_first_name', $edit['billing_first_name'], 'Billing first name changed.');
    $this->assertFieldByName('billing_last_name', $edit['billing_last_name'], 'Billing last name changed.');
  }

  protected function ucCreateOrder($customer) {
    $order = uc_order_new($customer->uid);
    uc_order_comment_save($order->order_id, 0, t('Order created programmatically.'), 'admin');

    $order_exists = db_result(db_query("SELECT 1 FROM {uc_orders} WHERE order_id = %d", $order->order_id));
    $this->assertTrue($order_exists, t('Found order ID @order_id', array('@order_id' => $order->order_id)));

    $zone_id = db_result(db_query('SELECT zone_id FROM {uc_zones} WHERE zone_country_id = %s ORDER BY rand() LIMIT 1', variable_get('uc_store_country', 840)));

    $order->delivery_first_name = $this->randomName(12);
    $order->delivery_last_name = $this->randomName(12);
    $order->delivery_street1 = $this->randomName(12);
    $order->delivery_street2 = $this->randomName(12);
    $order->delivery_city = $this->randomName(12);
    $order->delivery_zone = $zone_id;
    $order->delivery_postal_code = mt_rand(10000, 99999);
    $order->delivery_country = 840;

    $order->billing_first_name = $this->randomName(12);
    $order->billing_last_name = $this->randomName(12);
    $order->billing_street1 = $this->randomName(12);
    $order->billing_street2 = $this->randomName(12);
    $order->billing_city = $this->randomName(12);
    $order->billing_zone = $zone_id;
    $order->billing_postal_code = mt_rand(10000, 99999);
    $order->billing_country = 840;

    uc_order_save($order);

    $db_order = db_fetch_object(db_query("SELECT * FROM {uc_orders} WHERE order_id = %d", $order->order_id));
    $this->assertEqual($order->delivery_first_name, $db_order->delivery_first_name);
    $this->assertEqual($order->delivery_last_name, $db_order->delivery_last_name);
    $this->assertEqual($order->delivery_street1, $db_order->delivery_street1);
    $this->assertEqual($order->delivery_street2, $db_order->delivery_street2);
    $this->assertEqual($order->delivery_city, $db_order->delivery_city);
    $this->assertEqual($order->delivery_zone, $db_order->delivery_zone);
    $this->assertEqual($order->delivery_postal_code, $db_order->delivery_postal_code);
    $this->assertEqual($order->delivery_country, $db_order->delivery_country);

    $this->assertEqual($order->billing_first_name, $db_order->billing_first_name);
    $this->assertEqual($order->billing_last_name, $db_order->billing_last_name);
    $this->assertEqual($order->billing_street1, $db_order->billing_street1);
    $this->assertEqual($order->billing_street2, $db_order->billing_street2);
    $this->assertEqual($order->billing_city, $db_order->billing_city);
    $this->assertEqual($order->billing_zone, $db_order->billing_zone);
    $this->assertEqual($order->billing_postal_code, $db_order->billing_postal_code);
    $this->assertEqual($order->billing_country, $db_order->billing_country);

    return $order;
  }
}
