You are here

PaymentCommerceDeleteOrderWebTestCase.test in Payment for Drupal Commerce 7

Same filename and directory in other branches
  1. 7.2 tests/PaymentCommerceDeleteOrderWebTestCase.test

File

tests/PaymentCommerceDeleteOrderWebTestCase.test
View source
<?php

class PaymentCommerceDeleteOrderWebTestCase extends PaymentWebTestCase {

  /**
   * Implements DrupalTestCase::getInfo().
   */
  static function getInfo() {
    return array(
      'description' => '',
      'name' => 'Payment deletion upon order deletion',
      'group' => 'Payment for Drupal Commerce',
      'dependencies' => array(
        'payment_commerce',
      ),
    );
  }

  /**
   * Overrides parent::setUp().
   */
  function setUp(array $modules = array()) {
    $this->profile = 'testing';
    parent::setUp($modules + array(
      'paymentmethodbasic',
      'payment_commerce',
    ));
  }
  function testDeleteOrder() {

    // Delete an order, but keep its payments.
    $pid = $this
      ->assertCreateAndDeleteOrderAndPayment();
    $this
      ->assertTrue((bool) entity_load('payment', array(
      $pid,
    ), array(), TRUE));

    // Delete an order and its payments.
    variable_set('payment_commerce_order_delete', TRUE);
    $pid = $this
      ->assertCreateAndDeleteOrderAndPayment();
    $this
      ->assertFalse((bool) entity_load('payment', array(
      $pid,
    ), array(), TRUE));

    // Delete an order without payments.
    $order = commerce_order_new();
    commerce_order_save($order);
    commerce_order_delete($order->order_id);
    $this
      ->assertFalse(commerce_order_load($order->order_id));
  }

  /**
   * Creates an order, transaction and Payment, and then deletes them.
   *
   * @return integer
   *   The PID of the Payment that was created and deleted.
   */
  function assertCreateAndDeleteOrderAndPayment() {
    $order = commerce_order_new();
    commerce_order_save($order);
    $this
      ->assertTrue($order->order_id > 0);
    $payment_method = $this
      ->paymentMethodCreate(0, payment_method_controller_load('PaymentMethodBasicController'));
    $payment = payment_commerce_payment_create($order, $payment_method->pmid);
    entity_save('payment', $payment);
    $this
      ->assertTrue($payment->pid > 0);
    $transaction = commerce_payment_transaction_new('payment_commerce_' . $payment->method->pmid, $payment->context_data['order_id']);
    payment_commerce_transaction_fill($transaction, $payment);
    commerce_payment_transaction_save($transaction);
    payment_commerce_transaction_save($payment->pid, $transaction->transaction_id);
    commerce_order_delete($order->order_id);
    $this
      ->assertFalse(commerce_order_load($order->order_id));
    return $payment->pid;
  }

}