You are here

public function BillingScheduleTest::testBillingScheduleEditing in Commerce Recurring Framework 8

Tests editing a billing schedule.

File

tests/src/FunctionalJavascript/BillingScheduleTest.php, line 93

Class

BillingScheduleTest
Tests the billing schedule UI.

Namespace

Drupal\Tests\commerce_recurring\FunctionalJavascript

Code

public function testBillingScheduleEditing() {
  $billing_schedule = BillingSchedule::create([
    'id' => 'test',
    'label' => 'Test',
    'displayLabel' => 'Awesome test',
    'billingType' => BillingScheduleInterface::BILLING_TYPE_POSTPAID,
    'plugin' => 'fixed',
    'configuration' => [
      'interval' => [
        'number' => '2',
        'unit' => 'month',
      ],
      'start_day' => '4',
    ],
    'prorater' => 'proportional',
    'proraterConfiguration' => [],
  ]);
  $billing_schedule
    ->save();
  $this
    ->drupalGet('admin/commerce/config/billing-schedules/manage/' . $billing_schedule
    ->id());
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('dunning[num_retries]', '2');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('prorater', 'full_price');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([
    'label' => 'Test (Modified)',
    'displayLabel' => 'Awesome test (Modified)',
    'billingType' => BillingScheduleInterface::BILLING_TYPE_PREPAID,
    'dunning[retry][0]' => '6',
    'dunning[retry][1]' => '7',
    'dunning[unpaid_subscription_state]' => 'active',
    'configuration[fixed][interval][number]' => '1',
    'configuration[fixed][interval][unit]' => 'year',
    'configuration[fixed][start_month]' => '2',
    'configuration[fixed][start_day]' => '5',
  ], 'Save');
  \Drupal::entityTypeManager()
    ->getStorage('commerce_billing_schedule')
    ->resetCache();
  $billing_schedule = BillingSchedule::load('test');
  $this
    ->assertEquals('test', $billing_schedule
    ->id());
  $this
    ->assertEquals('Test (Modified)', $billing_schedule
    ->label());
  $this
    ->assertEquals('Awesome test (Modified)', $billing_schedule
    ->getDisplayLabel());
  $this
    ->assertEquals(BillingScheduleInterface::BILLING_TYPE_PREPAID, $billing_schedule
    ->getBillingType());
  $this
    ->assertEquals([
    6,
    7,
  ], $billing_schedule
    ->getRetrySchedule());
  $this
    ->assertEquals('active', $billing_schedule
    ->getUnpaidSubscriptionState());
  $this
    ->assertEquals('fixed', $billing_schedule
    ->getPluginId());
  $this
    ->assertEquals([
    'interval' => [
      'number' => '1',
      'unit' => 'year',
    ],
    'start_month' => '2',
    'start_day' => '5',
    'trial_interval' => [],
  ], $billing_schedule
    ->getPluginConfiguration());
  $this
    ->assertEquals($billing_schedule
    ->getPluginConfiguration(), $billing_schedule
    ->getPlugin()
    ->getConfiguration());
  $this
    ->assertEquals('full_price', $billing_schedule
    ->getProraterId());

  // Check that some fields are disabled when editing a billing schedule that
  // is used by subscriptions.
  $variation = ProductVariation::create([
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => '39.99',
      'currency_code' => 'USD',
    ],
  ]);
  $variation
    ->save();
  $subscription = Subscription::create([
    'type' => 'product_variation',
    'title' => $this
      ->randomString(),
    'uid' => $this->adminUser
      ->id(),
    'billing_schedule' => $billing_schedule,
    'purchased_entity' => $variation,
    'store_id' => $this->store
      ->id(),
    'unit_price' => $variation
      ->getPrice(),
    'starts' => time(),
    'state' => 'active',
  ]);
  $subscription
    ->save();
  $this
    ->drupalGet('admin/commerce/config/billing-schedules/manage/' . $billing_schedule
    ->id());
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->assertSession()
    ->pageTextContains('Some fields are disabled since the Test (Modified) billing schedule is used by subscriptions.');
  $disabled_fields = [
    'edit-billingtype-prepaid',
    'edit-billingtype-postpaid',
    'edit-plugin-fixed',
    'edit-plugin-rolling',
    'edit-configuration-fixed-trial-interval-allow-trials',
    'edit-configuration-fixed-interval-number',
    'edit-configuration-fixed-interval-unit',
    'edit-prorater-full-price',
    'edit-prorater-proportional',
  ];
  foreach ($disabled_fields as $disabled_field) {
    $field = $page
      ->findField($disabled_field);
    $this
      ->assertEquals($field
      ->getAttribute('disabled'), 'disabled');
  }
}