You are here

function CommerceRecurringTestCase::testCommerceRecurringAnonymousOrder in Commerce Recurring Framework 7.2

Test anonymous behaviour. Non-existing user

File

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

Class

CommerceRecurringTestCase
@file Unit tests for the commerce recurring module.

Code

function testCommerceRecurringAnonymousOrder() {
  $recurring_product = $this
    ->createRecurringProduct();
  $this
    ->createDummyProductDisplayContentType('product_display', TRUE, 'field_product', FIELD_CARDINALITY_UNLIMITED);
  $node = $this
    ->createDummyProductNode(array(
    $recurring_product->product_id,
  ));
  user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
    'access checkout' => TRUE,
  ));

  // Ensure we're not logged in.
  $this
    ->drupalLogout();

  // Override user variable to get the enviroment fully set.
  global $user;
  $user = user_load(0);

  // Submit the add to cart form.
  $this
    ->drupalPost('node/' . $node->nid, array(), t('Add to cart'));

  // Get the order for the anonymous user.
  $orders = commerce_order_load_multiple(array(), array(
    'uid' => $user->uid,
    'status' => 'cart',
  ), TRUE);
  $order = reset($orders);

  // Reset the cache as we don't want to keep the lock.
  entity_get_controller('commerce_order')
    ->resetCache();
  $this
    ->drupalPost($this
    ->getCommerceUrl('cart'), array(), t('Checkout'));

  // Generate random information, as city, postal code, etc.
  $address_info = $this
    ->generateAddressInformation();

  // Fill in the billing address information.
  $billing_pane = $this
    ->xpath("//select[starts-with(@name, 'customer_profile_billing[commerce_customer_address]')]");
  $this
    ->drupalPostAJAX(NULL, array(
    (string) $billing_pane[0]['name'] => 'US',
  ), (string) $billing_pane[0]['name']);

  // Fill in the required information for billing pane, with a random State.
  $info = array(
    'account[login][mail]' => $this
      ->randomName() . '@example.com',
    'customer_profile_billing[commerce_customer_address][und][0][name_line]' => $address_info['name_line'],
    'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => $address_info['thoroughfare'],
    'customer_profile_billing[commerce_customer_address][und][0][locality]' => $address_info['locality'],
    'customer_profile_billing[commerce_customer_address][und][0][administrative_area]' => 'KY',
    'customer_profile_billing[commerce_customer_address][und][0][postal_code]' => $address_info['postal_code'],
  );
  $this
    ->drupalPost(NULL, $info, t('Continue to next step'));

  // Finish checkout process
  $this
    ->drupalPost(NULL, array(
    'commerce_payment[payment_method]' => 'commerce_payment_example|commerce_payment_commerce_payment_example',
  ), t('Continue to next step'));

  // Load required entities.
  $order = commerce_order_load($order->order_id);
  entity_get_controller('commerce_order')
    ->resetCache();
  $recurring_entities = commerce_recurring_load_by_order($order);
  $recurring_entity = reset($recurring_entities);
  $recurring_wrapper = entity_metadata_wrapper('commerce_recurring', $recurring_entity);
  $this
    ->assertTrue(!empty($recurring_entities), 'A recurring entity was created for the order');
  $this
    ->assertEqual(count($recurring_entities), 1, 'Only one recurring entity was created for the order');
  $this
    ->assertEqual($recurring_wrapper->commerce_recurring_ref_product
    ->value(), $recurring_product, 'Recurring product is associated with the recurring entity');
  debug($recurring_entity);
  $this
    ->assertEqual($recurring_entity->uid, $order->uid, 'Recurring entity is assigned to the registered user');
}