You are here

public function CronTest::testCanceled in Commerce Recurring Framework 8

Tests handling canceled subscriptions and their orders.

File

tests/src/Kernel/CronTest.php, line 197

Class

CronTest
@coversDefaultClass \Drupal\commerce_recurring\Cron @group commerce_recurring

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testCanceled() {
  $postpaid_subscription = Subscription::create([
    'type' => 'product_variation',
    'store_id' => $this->store
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'uid' => $this->user,
    'payment_method' => $this->paymentMethod,
    'purchased_entity' => $this->variation,
    'title' => $this->variation
      ->getOrderItemTitle(),
    'unit_price' => new Price('2', 'USD'),
    'state' => 'active',
    'starts' => strtotime('2019-02-15 00:00'),
  ]);
  $postpaid_subscription
    ->save();
  $postpaid_order = $this->recurringOrderManager
    ->startRecurring($postpaid_subscription);

  // Cancel the subscription half-way through.
  $this
    ->rewindTime(strtotime('2019-02-21 00:00'));
  $postpaid_subscription
    ->cancel(FALSE);
  $postpaid_subscription
    ->save();

  // Confirm that the order is scheduling for closing, but not renewal.
  $this
    ->rewindTime(strtotime('2019-03-01 00:00'));
  $this->container
    ->get('commerce_recurring.cron')
    ->run();

  /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
  $queue = Queue::load('commerce_recurring');
  $counts = array_filter($queue
    ->getBackend()
    ->countJobs());
  $this
    ->assertEquals([
    Job::STATE_QUEUED => 1,
  ], $counts);
  $job = $queue
    ->getBackend()
    ->claimJob();
  $this
    ->assertSame([
    'order_id' => $postpaid_order
      ->id(),
  ], $job
    ->getPayload());
  $this
    ->assertEquals('commerce_recurring_order_close', $job
    ->getType());
  $postpaid_subscription
    ->delete();
  $postpaid_order
    ->delete();
  $queue
    ->getBackend()
    ->deleteQueue();
  $this->billingSchedule
    ->setBillingType(BillingScheduleInterface::BILLING_TYPE_PREPAID);
  $this->billingSchedule
    ->save();
  $prepaid_subscription = Subscription::create([
    'type' => 'product_variation',
    'store_id' => $this->store
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'uid' => $this->user,
    'payment_method' => $this->paymentMethod,
    'purchased_entity' => $this->variation,
    'title' => $this->variation
      ->getOrderItemTitle(),
    'unit_price' => new Price('2', 'USD'),
    'state' => 'active',
    'starts' => strtotime('2019-02-15 00:00'),
  ]);
  $prepaid_subscription
    ->save();
  $prepaid_order = $this->recurringOrderManager
    ->startRecurring($prepaid_subscription);

  // Schedule the subscription for cancellation.
  $prepaid_subscription
    ->cancel();
  $prepaid_subscription
    ->save();

  // Confirm that both the subscription and its order have been canceled.
  $this->container
    ->get('commerce_recurring.cron')
    ->run();
  $prepaid_subscription = $this
    ->reloadEntity($prepaid_subscription);
  $this
    ->assertEquals('canceled', $prepaid_subscription
    ->getState()
    ->getId());
  $prepaid_order = $this
    ->reloadEntity($prepaid_order);
  $this
    ->assertEquals('canceled', $prepaid_order
    ->getState()
    ->getId());
  $counts = array_filter($queue
    ->getBackend()
    ->countJobs());
  $this
    ->assertEmpty($counts);
}