You are here

public function TaxRateDefaultWidgetTest::testWidget in Commerce Product Tax 8

Tests the widget.

File

tests/src/Functional/TaxRateDefaultWidgetTest.php, line 105

Class

TaxRateDefaultWidgetTest
Tests the tax rate default widget.

Namespace

Drupal\Tests\commerce_product_tax\Functional

Code

public function testWidget() {
  $this
    ->drupalGet($this->variation
    ->toUrl('edit-form'));
  $select = $this
    ->getSession()
    ->getPage()
    ->findField('field_tax_rate[]');
  $this
    ->assertNotEmpty($select);
  $this
    ->assertSession()
    ->optionExists('field_tax_rate[]', 'fr|reduced');
  $this
    ->assertSession()
    ->optionExists('field_tax_rate[]', 'fr|' . TaxRateResolverInterface::NO_APPLICABLE_TAX_RATE);
  $this
    ->assertSession()
    ->optionExists('field_tax_rate[]', 'fr|standard');
  $this
    ->assertSession()
    ->optionExists('field_tax_rate[]', 'de|' . TaxRateResolverInterface::NO_APPLICABLE_TAX_RATE);
  $this
    ->assertSession()
    ->optionExists('field_tax_rate[]', 'de|reduced');
  $this
    ->assertSession()
    ->optionNotExists('field_tax_rate[]', 'it|reduced');
  $select
    ->selectOption('fr|reduced');
  $select
    ->selectOption('de|reduced', TRUE);
  $this
    ->submitForm([], t('Save'));
  $this->variation = $this
    ->reloadEntity($this->variation);
  $tax_rates = [];
  foreach ($this->variation
    ->get('field_tax_rate') as $tax_rate_item) {
    $tax_rates[] = $tax_rate_item->value;
  }
  $this
    ->assertFieldValues($tax_rates, [
    'fr|reduced',
    'de|reduced',
  ]);
  $this->field
    ->set('settings', [
    'tax_type' => 'eu_vat',
    'allowed_zones' => [],
  ])
    ->save();
  $this
    ->drupalGet($this->variation
    ->toUrl('edit-form'));
  $option_field = $this
    ->assertSession()
    ->optionExists('field_tax_rate[]', 'fr|reduced');
  $this
    ->assertTrue($option_field
    ->hasAttribute('selected'));
  $option_field = $this
    ->assertSession()
    ->optionExists('field_tax_rate[]', 'de|reduced');
  $this
    ->assertTrue($option_field
    ->hasAttribute('selected'));

  /** @var \Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeInterface $tax_type_plugin */
  $tax_type_plugin = $this->taxType
    ->getPlugin();
  foreach ($tax_type_plugin
    ->getZones() as $zone) {
    if ($zone
      ->getId() == 'ic') {
      continue;
    }
    foreach ($zone
      ->getRates() as $rate) {
      $this
        ->assertSession()
        ->optionExists('field_tax_rate[]', $zone
        ->getId() . '|' . $rate
        ->getId());
    }
  }
}