View source
<?php
namespace Drupal\Tests\commerce_recurring\Kernel;
use Drupal\advancedqueue\Entity\Queue;
use Drupal\advancedqueue\Job;
use Drupal\commerce_price\Price;
use Drupal\commerce_recurring\Entity\BillingScheduleInterface;
use Drupal\commerce_recurring\Entity\Subscription;
class CronTest extends RecurringKernelTestBase {
protected $recurringOrderManager;
protected function setUp() : void {
parent::setUp();
$this->recurringOrderManager = $this->container
->get('commerce_recurring.order_manager');
}
public function testTrial() {
$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'),
]);
$subscription
->save();
$order = $this->recurringOrderManager
->startTrial($subscription);
$this
->rewindTime(strtotime('2019-02-10 17:00'));
$this->container
->get('commerce_recurring.cron')
->run();
$queue = Queue::load('commerce_recurring');
$counts = array_filter($queue
->getBackend()
->countJobs());
$this
->assertEmpty($counts);
$this
->rewindTime(strtotime('2019-02-15 17:00'));
$this->container
->get('commerce_recurring.cron')
->run();
$this->container
->get('entity_type.manager')
->getStorage('advancedqueue_queue')
->resetCache();
$queue = Queue::load('commerce_recurring');
$counts = array_filter($queue
->getBackend()
->countJobs());
$this
->assertEquals([
Job::STATE_QUEUED => 2,
], $counts);
$first_job = $queue
->getBackend()
->claimJob();
$this
->assertSame([
'order_id' => $order
->id(),
], $first_job
->getPayload());
$this
->assertEquals('commerce_recurring_order_close', $first_job
->getType());
$second_job = $queue
->getBackend()
->claimJob();
$this
->assertSame([
'subscription_id' => $subscription
->id(),
], $second_job
->getPayload());
$this
->assertEquals('commerce_subscription_activate', $second_job
->getType());
}
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);
$this
->rewindTime(strtotime('2019-02-10 17:00'));
$postpaid_subscription
->cancel(FALSE);
$postpaid_subscription
->save();
$this
->rewindTime(strtotime('2019-02-15 17:00'));
$this->container
->get('commerce_recurring.cron')
->run();
$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);
$prepaid_subscription
->cancel();
$prepaid_subscription
->save();
$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);
}
public function testActive() {
$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'),
]);
$subscription
->save();
$order = $this->recurringOrderManager
->startRecurring($subscription);
$this
->rewindTime(strtotime('2019-02-24 17:00'));
$this->container
->get('commerce_recurring.cron')
->run();
$queue = Queue::load('commerce_recurring');
$counts = array_filter($queue
->getBackend()
->countJobs());
$this
->assertEmpty($counts);
$this
->rewindTime(strtotime('2019-03-01 00:00'));
$this->container
->get('commerce_recurring.cron')
->run();
$this->container
->get('entity_type.manager')
->getStorage('advancedqueue_queue')
->resetCache();
$queue = Queue::load('commerce_recurring');
$counts = array_filter($queue
->getBackend()
->countJobs());
$this
->assertEquals([
Job::STATE_QUEUED => 2,
], $counts);
$first_job = $queue
->getBackend()
->claimJob();
$this
->assertSame([
'order_id' => $order
->id(),
], $first_job
->getPayload());
$this
->assertEquals('commerce_recurring_order_close', $first_job
->getType());
$second_job = $queue
->getBackend()
->claimJob();
$this
->assertSame([
'order_id' => $order
->id(),
], $second_job
->getPayload());
$this
->assertEquals('commerce_recurring_order_renew', $second_job
->getType());
}
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);
$this
->rewindTime(strtotime('2019-02-21 00:00'));
$postpaid_subscription
->cancel(FALSE);
$postpaid_subscription
->save();
$this
->rewindTime(strtotime('2019-03-01 00:00'));
$this->container
->get('commerce_recurring.cron')
->run();
$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);
$prepaid_subscription
->cancel();
$prepaid_subscription
->save();
$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);
}
public function testPending() {
$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' => 'pending',
'starts' => strtotime('2019-02-15 17:00'),
]);
$subscription
->save();
$this
->rewindTime(strtotime('2019-02-15 19:00'));
$this->container
->get('commerce_recurring.cron')
->run();
$queue = Queue::load('commerce_recurring');
$counts = array_filter($queue
->getBackend()
->countJobs());
$this
->assertEquals([
Job::STATE_QUEUED => 1,
], $counts);
$job = $queue
->getBackend()
->claimJob();
$this
->assertSame([
'subscription_id' => $subscription
->id(),
], $job
->getPayload());
}
protected function rewindTime($new_time) {
parent::rewindTime($new_time);
$this->container
->set('commerce_recurring.cron', NULL);
$queue_storage = $this->container
->get('entity_type.manager')
->getStorage('advancedqueue_queue');
$queue_storage
->resetCache([
'commerce_recurring',
]);
$this->queue = $queue_storage
->load('commerce_recurring');
}
}