You are here

function CommerceRecurringTestCase::testCommerceRecurringUpdateRecurringEntity in Commerce Recurring Framework 7.2

Test recurring entity update when orders created in cron are processed.

File

tests/commerce_recurring.test, line 366
Unit tests for the commerce recurring module.

Class

CommerceRecurringTestCase
@file Unit tests for the commerce recurring module.

Code

function testCommerceRecurringUpdateRecurringEntity() {

  // Create a recurring entity.
  $product = $this
    ->createRecurringProduct();
  $line_item = commerce_cart_product_add_by_id($product->product_id, 1, TRUE, $this->customer->uid);
  $start_date = new DateObject('2010-01-01');
  $due_date = new DateObject();
  $due_date
    ->sub(new DateInterval('P1D'));
  $recurring_entity = $this
    ->createRecurringEntity($product, 1, $start_date, $due_date);
  $initial_order = commerce_order_load($line_item->order_id);
  $initial_order->commerce_customer_billing = (array) $this
    ->createDummyCustomerProfile('billing', $this->customer->uid);
  $initial_order->status = 'completed';
  commerce_order_save($initial_order);

  // Run cron.
  drupal_cron_run();

  // Check if the order has been correctly created and it's linked to the
  // recurring entity.
  $recurring_entities = entity_load('commerce_recurring', array(
    $recurring_entity->id,
  ), array(), TRUE);
  $recurring_entity = reset($recurring_entities);
  $recurring_wrapper = entity_metadata_wrapper('commerce_recurring', $recurring_entity);

  // The order is the second one now.
  $order = $recurring_wrapper->commerce_recurring_order
    ->get(1)
    ->value();

  // Mock a payment that triggers Paid in full event.
  $payment_method = array(
    'instance_id' => 'commerce_payment_example',
  );
  $charge = array(
    'amount' => $order->commerce_order_total[LANGUAGE_NONE][0]['amount'],
    'currency_code' => $order->commerce_order_total[LANGUAGE_NONE][0]['currency_code'],
  );

  // Accomodate the default payment example order data values.
  $number = '4111111111111111';
  $order->data['commerce_payment_example'] = array(
    'credit_card' => array(
      'number' => substr($number, 0, 4) . str_repeat('-', strlen($number) - 8) . substr($number, -4),
      'exp_month' => date('m'),
      'exp_year' => date('Y'),
    ),
  );
  commerce_payment_example_transaction($payment_method, $order, $charge, 'Test payment');
  $recurring_entities = entity_load('commerce_recurring', array(
    $order->data['recurring_entity'],
  ), array(), TRUE);
  $reloaded_recurring_entity = reset($recurring_entities);
  $reloaded_recurring_entity_wrapper = entity_metadata_wrapper('commerce_recurring', $reloaded_recurring_entity);

  // Check if the recurring entity has been updated correctly.
  $this
    ->assertTrue(in_array($order->order_id, $reloaded_recurring_entity_wrapper->commerce_recurring_order
    ->raw()), t('Order is associated with the recurring entity.'));
  $count_values = array_count_values($reloaded_recurring_entity_wrapper->commerce_recurring_order
    ->raw());
  $this
    ->assertEqual($count_values[$order->order_id], 1, t('Order is associated once with the recurring entity.'));

  // Due date needs to be updated to the next recurring period.
  $product_wrapper = $reloaded_recurring_entity_wrapper->commerce_recurring_ref_product;
  $recurring_interval = $product_wrapper->commerce_recurring_rec_period
    ->value();
  interval_apply_interval($due_date, $recurring_interval, TRUE);
  $this
    ->assertEqual($due_date
    ->getTimestamp(), $reloaded_recurring_entity_wrapper->due_date
    ->value(), t('Recurring due date is correct.'));
}