You are here

public function SubscriptionTest::testSubscriptionImmediateCancelling in Commerce Recurring Framework 8

Tests cancelling a subscription immediately.

File

tests/src/Functional/SubscriptionTest.php, line 302

Class

SubscriptionTest
Tests the subscription UI.

Namespace

Drupal\Tests\commerce_recurring\Functional

Code

public function testSubscriptionImmediateCancelling() {
  $this
    ->drupalGet('admin/commerce/subscriptions/add');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->clickLink('Product variation');
  $start_date = DrupalDateTime::createFromTimestamp(time() + 3600);
  $values = [
    'title[0][value]' => 'Test subscription',
    'billing_schedule' => $this->billingSchedule
      ->id(),
    'purchased_entity[0][target_id]' => $this->variation
      ->getTitle() . ' (' . $this->variation
      ->id() . ')',
    'uid[0][target_id]' => $this->adminUser
      ->label() . ' (' . $this->adminUser
      ->id() . ')',
    'unit_price[0][number]' => '9.99',
    'starts[0][value][date]' => $start_date
      ->format($this->dateFormat),
    'starts[0][value][time]' => $start_date
      ->format($this->timeFormat),
  ];
  $this
    ->submitForm($values, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('A subscription been successfully saved.');

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = Subscription::load(1);

  // Since we use no payment provider in this test, the subscription gets the
  // state Pending instead of Active. We nudge it to Active to get things
  // going again.
  $subscription
    ->getState()
    ->applyTransitionById('activate');
  $subscription
    ->save();
  $this->recurringOrderManager
    ->startRecurring($subscription);
  $this
    ->drupalGet('admin/commerce/subscriptions/' . $subscription
    ->id() . '/cancel');
  $radio = $this
    ->getSession()
    ->getPage()
    ->findField('edit-cancel-option-now');
  $name = $radio
    ->getAttribute('name');
  $this
    ->assertEqual($name, 'cancel_option');
  $option = $radio
    ->getAttribute('value');
  $this
    ->assertEqual($option, 'now');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption($name, $option);
  $this
    ->getSession()
    ->getPage()
    ->findButton('Confirm')
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('The subscription has been canceled.');

  // Check that already cancelled subscriptions aren't cancelable anymore.
  $this
    ->drupalGet('admin/commerce/subscriptions/' . $subscription
    ->id() . '/cancel');
  $this
    ->assertSession()
    ->pageTextContains('The subscription has already been canceled.');
}