You are here

public function SubscriptionTest::testSubscriptionScheduledCancelling in Commerce Recurring Framework 8

Tests scheduled subscription cancellation.

File

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

Class

SubscriptionTest
Tests the subscription UI.

Namespace

Drupal\Tests\commerce_recurring\Functional

Code

public function testSubscriptionScheduledCancelling() {
  $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-scheduled');
  $name = $radio
    ->getAttribute('name');
  $this
    ->assertEqual($name, 'cancel_option');
  $option = $radio
    ->getAttribute('value');
  $this
    ->assertEqual($option, 'scheduled');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption($name, $option);
  $this
    ->getSession()
    ->getPage()
    ->findButton('Confirm')
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('The subscription has been scheduled for cancellation.');
  $this
    ->drupalGet('admin/commerce/subscriptions/' . $subscription
    ->id() . '/cancel');
  $this
    ->assertSession()
    ->pageTextContains('A cancellation has already been scheduled for ');
}