You are here

public function CronTest::testTrialCanceled in Commerce Recurring Framework 8

Tests handling canceled trial subscriptions and their orders.

File

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

Class

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

Namespace

Drupal\Tests\commerce_recurring\Kernel

Code

public function testTrialCanceled() {
  $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' => 'trial',
    'trial_starts' => strtotime('2019-02-05 17:00'),
    'trial_ends' => strtotime('2019-02-15 17:00'),
    'starts' => strtotime('2019-02-15 17:00'),
  ]);
  $postpaid_subscription
    ->save();
  $postpaid_order = $this->recurringOrderManager
    ->startTrial($postpaid_subscription);

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

  // Confirm that the trial order is scheduling for closing.
  $this
    ->rewindTime(strtotime('2019-02-15 17: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' => 'trial',
    'trial_starts' => strtotime('2019-02-05 17:00'),
    'trial_ends' => strtotime('2019-02-15 17:00'),
    'starts' => strtotime('2019-02-15 17:00'),
  ]);
  $prepaid_subscription
    ->save();
  $prepaid_order = $this->recurringOrderManager
    ->startTrial($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);
}