You are here

class ucRecurringTestCase in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 uc_recurring.test \ucRecurringTestCase

@file UC Recurring simpletest

Hierarchy

Expanded class hierarchy of ucRecurringTestCase

File

./uc_recurring.test, line 8
UC Recurring simpletest

View source
class ucRecurringTestCase extends UbercartTestCase {

  /**
   * Add a recurring feature to a product.
   */
  function createRecurringFeature($nid, $feature = array()) {
    $interval_units = array(
      'days',
      'months',
      'years',
    );
    $defaults = array(
      'model' => '',
      'fee_same_product' => FALSE,
      'fee_amount' => rand(1, 999),
      'initial_charge_value' => rand(1, 10),
      'initial_charge_unit' => $interval_units[rand(0, 2)],
      'regular_interval_value' => rand(1, 10),
      'regular_interval_unit' => $interval_units[rand(0, 2)],
      'unlimited_intervals' => FALSE,
      'number_intervals' => rand(3, 10),
    );
    $feature = array_merge($defaults, $feature);
    $this
      ->drupalPost('node/' . $nid . '/edit/features/recurring/add', $feature, t('Save feature'));
    $this
      ->assertRaw(t('The product feature has been added.'), t('"The product feature has been added." was displayed on the edit features page'));
    return $feature;
  }

  /**
   * Create users with recurring permissions.
   */
  function createRecurringUsers() {

    // Create a store administrator user account.
    $this->user_recurring_admin = $this
      ->drupalCreateUser(array(
      'administer conditional actions',
      'administer order workflow',
      'create orders',
      'delete orders',
      'edit orders',
      'view all orders',
      'administer product classes',
      'administer product features',
      'administer products',
      'create products',
      'delete all products',
      'edit all products',
      'administer store',
      'view customers',
      'view store reports',
      'administer recurring fees',
    ));
    $this->user_recurring_customer = $this
      ->drupalCreateUser(array(
      'view own orders',
      'view own recurring fees',
    ));
  }

  /**
   * Returns the last order_id added in the database.
   */
  function lastCreatedOrderId() {
    if ($result = db_query('SELECT max(order_id) FROM {uc_orders}')) {
      return $result
        ->fetchField();
    }
    return -1;
  }

  /**
   * place an order for a product with a recurring fee.
   */
  function placeOrderWithRecurringFee($product) {
    $last_order_id = $this
      ->lastCreatedOrderId();

    // create an order
    $this
      ->drupalPost('node/' . $product->nid, array(), t('Add to cart'));
    $this
      ->assertRaw($product->title, t('The product name has been displayed on the cart page.'));
    $this
      ->assertRaw('added to', t('The product name has been displayed on the cart page.'));
    $this
      ->checkout(array(
      'panes[delivery][delivery_postal_code]' => '12345',
      'panes[billing][billing_postal_code]' => '12345',
    ));
    $this
      ->assertRaw('Your order is complete!', t('"Your order is complete!" appears on the thank you page.'));
    $order_id = $this
      ->lastCreatedOrderId();
    if ($order_id > $last_order_id) {
      return $order_id;
    }
    return FALSE;
  }

  /**
   * Get a single recurring fee from the order ID.
   */
  function getSingleRecurringFeeFromOrder($order_id) {
    $order = uc_order_load($order_id);
    $fees = uc_recurring_get_fees($order);
    $this
      ->assertEqual(count($fees), 1, t('Found 1 recurring fee.'));
    return array_shift($fees);
  }

  /**
   * Process a recurring fee.
   */
  function processRecurringFee($rfid, $times_to_renew) {
    $old_fee = uc_recurring_fee_user_load($rfid);
    $current_order_id = empty($old_fee->data['recurring orders']) ? $old_fee->order_id : max($old_fee->data['recurring orders']);

    // Simulate the order being ready for renewal by changing the "next_charge"
    // field.
    // TODO Please review the conversion of this statement to the D7 database API syntax.

    /* db_query("UPDATE {uc_recurring_users} SET next_charge=%d WHERE rfid=%d", REQUEST_TIME - 1, $rfid) */
    db_update('uc_recurring_users')
      ->fields(array(
      'next_charge' => REQUEST_TIME - 1,
    ))
      ->condition('rfid', $rfid)
      ->execute();

    // Run cron function.
    uc_recurring_cron();
    $new_order_id = $this
      ->lastCreatedOrderId();
    if ($old_fee->remaining_intervals == 0) {

      // There should be no new order.
      $this
        ->pass(t('Recurring Fee %rfid has expired', array(
        '%rfid' => $rfid,
      )));
    }
    else {
      $this
        ->pass(t('Order %order_id has been created', array(
        '%order_id' => $new_order_id,
      )));
      $fee = uc_recurring_fee_user_load($rfid);
      $this
        ->assertEqual($fee->charged_intervals, $old_fee->charged_intervals + 1, t('Number of intervals been charged is now: %charged.', array(
        '%charged' => $fee->charged_intervals,
      )));

      // Negative intervals means unlimited renewals.
      if ($old_fee->remaining_intervals < 0) {
        $this
          ->assertEqual($fee->remaining_intervals, -1, t('Still has unlimited intervals remaining.'));
      }
      else {
        $this
          ->assertEqual($fee->remaining_intervals, $old_fee->remaining_intervals - 1, t('%remaining_intervals remaining interval.', array(
          '%remaining_intervals' => $fee->remaining_intervals,
        )));
      }
      if ($times_to_renew > 1) {
        return $this
          ->processRecurringFee($rfid, $times_to_renew - 1);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ucRecurringTestCase::createRecurringFeature function Add a recurring feature to a product.
ucRecurringTestCase::createRecurringUsers function Create users with recurring permissions.
ucRecurringTestCase::getSingleRecurringFeeFromOrder function Get a single recurring fee from the order ID.
ucRecurringTestCase::lastCreatedOrderId function Returns the last order_id added in the database.
ucRecurringTestCase::placeOrderWithRecurringFee function place an order for a product with a recurring fee.
ucRecurringTestCase::processRecurringFee function Process a recurring fee.