You are here

public function SubscriptionTest::testSubscriptionCreation in Commerce Recurring Framework 8

Tests creating a subscription.

File

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

Class

SubscriptionTest
Tests the subscription UI.

Namespace

Drupal\Tests\commerce_recurring\Functional

Code

public function testSubscriptionCreation() {
  $this
    ->drupalGet('admin/commerce/subscriptions/add');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->clickLink('Product variation');
  $this
    ->assertSession()
    ->addressEquals('admin/commerce/subscriptions/product_variation/add');
  $start_date = DrupalDateTime::createFromTimestamp(time() + 3600);
  $values = [
    'title[0][value]' => 'Test subscription',
    'billing_schedule' => $this->billingSchedule
      ->id(),
    'payment_method[target_id]' => $this->paymentMethod
      ->label() . ' (' . $this->paymentMethod
      ->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);
  $this
    ->assertSession()
    ->pageTextContains($subscription
    ->getTitle());
  $this
    ->assertSession()
    ->pageTextContains($subscription
    ->getState()
    ->getId());
  $this
    ->assertSession()
    ->pageTextContains($subscription
    ->getBillingSchedule()
    ->label());
  $this
    ->assertEquals($values['title[0][value]'], $subscription
    ->getTitle());
  $this
    ->assertEquals($this->billingSchedule
    ->id(), $subscription
    ->getBillingSchedule()
    ->id());
  $this
    ->assertEquals($this->paymentMethod
    ->id(), $subscription
    ->getPaymentMethodId());
  $this
    ->assertNull($subscription
    ->getTrialStartTime());
  $this
    ->assertNull($subscription
    ->getTrialEndTime());
  $this
    ->assertNull($subscription
    ->getEndTime());
  $this
    ->assertEquals($start_date
    ->getTimestamp(), $subscription
    ->getStartTime());
  $this
    ->assertEquals($subscription
    ->getCustomerId(), $this->adminUser
    ->id());
  $this
    ->assertEquals('pending', $subscription
    ->getState()
    ->getId());
}