You are here

public function SubscriptionTest::testSubscriptionEditing in Commerce Recurring Framework 8

Tests editing a subscription.

File

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

Class

SubscriptionTest
Tests the subscription UI.

Namespace

Drupal\Tests\commerce_recurring\Functional

Code

public function testSubscriptionEditing() {

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = $this
    ->createEntity('commerce_subscription', [
    'title' => $this
      ->randomString(),
    'uid' => $this->adminUser
      ->id(),
    'billing_schedule' => $this->billingSchedule,
    'type' => 'product_variation',
    'purchased_entity' => $this->variation,
    'store_id' => $this->store
      ->id(),
    'unit_price' => $this->variation
      ->getPrice(),
    'starts' => time() + 3600,
    'trial_starts' => time(),
    'state' => 'trial',
  ]);
  $trial_end = DrupalDateTime::createFromTimestamp($subscription
    ->getStartTime());
  $end = DrupalDateTime::createFromTimestamp($subscription
    ->getStartTime() + 7200);
  $this
    ->drupalGet('admin/commerce/subscriptions/' . $subscription
    ->id() . '/edit');
  $this
    ->assertSession()
    ->pageTextContains('Trial');
  $this
    ->assertSession()
    ->buttonExists('Cancel subscription');
  $values = [
    'title[0][value]' => 'Test (Modified)',
    'trial_ends[0][has_value]' => 1,
    'trial_ends[0][container][value][date]' => $trial_end
      ->format($this->dateFormat),
    'trial_ends[0][container][value][time]' => $trial_end
      ->format($this->timeFormat),
    'ends[0][has_value]' => 1,
    'ends[0][container][value][date]' => $end
      ->format($this->dateFormat),
    'ends[0][container][value][time]' => $end
      ->format($this->timeFormat),
  ];
  $this
    ->submitForm($values, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('A subscription been successfully saved.');
  $subscription = $this
    ->reloadEntity($subscription);
  $this
    ->assertEquals($values['title[0][value]'], $subscription
    ->getTitle());
  $this
    ->assertSession()
    ->pageTextContains($subscription
    ->getTitle());
  $this
    ->assertNotEmpty($subscription
    ->getTrialStartTime());
  $this
    ->assertEquals($subscription
    ->getStartTime(), $subscription
    ->getTrialEndTime());
}