You are here

public function TaxNumberItemTest::testCountries in Commerce Core 8.2

Tests the allowed countries setting.

File

modules/tax/tests/src/Kernel/TaxNumberItemTest.php, line 160

Class

TaxNumberItemTest
Tests the 'commerce_tax_number' field type.

Namespace

Drupal\Tests\commerce_tax\Kernel

Code

public function testCountries() {
  $entity = EntityTest::create([
    'test_tax_number' => [
      'type' => 'other',
      'value' => '123',
    ],
  ]);
  $entity
    ->save();
  $entity = $this
    ->reloadEntity($entity);

  /** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
  $tax_number_item = $entity
    ->get('test_tax_number')
    ->first();

  // Unrestricted country list.
  $country_repository = $this->container
    ->get('address.country_repository');
  $country_list = $country_repository
    ->getList();
  $this
    ->assertEquals(array_keys($country_list), $tax_number_item
    ->getAllowedCountries());
  $this
    ->assertEquals([
    'european_union_vat',
    'other',
    'serbian_vat',
  ], $tax_number_item
    ->getAllowedTypes());

  // Restricted to the EU.
  $this->field
    ->setSetting('countries', [
    'EU',
  ]);
  $this->field
    ->save();
  $entity = $this
    ->reloadEntity($entity);

  /** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
  $tax_number_item = $entity
    ->get('test_tax_number')
    ->first();

  // Confirm that "EU" expands to the full list of EU countries.
  $this
    ->assertNotContains('EU', $tax_number_item
    ->getAllowedCountries());
  $this
    ->assertCount(29, $tax_number_item
    ->getAllowedCountries());
  $this
    ->assertEquals([
    'european_union_vat',
  ], $tax_number_item
    ->getAllowedTypes());

  // Restricted to EU + a non-EU country.
  $this->field
    ->setSetting('countries', [
    'EU',
    'US',
  ]);
  $this->field
    ->save();
  $entity = $this
    ->reloadEntity($entity);

  /** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
  $tax_number_item = $entity
    ->get('test_tax_number')
    ->first();

  // Confirm that "EU" expands to the full list of EU countries.
  $this
    ->assertNotContains('EU', $tax_number_item
    ->getAllowedCountries());
  $this
    ->assertContains('US', $tax_number_item
    ->getAllowedCountries());
  $this
    ->assertCount(30, $tax_number_item
    ->getAllowedCountries());
  $this
    ->assertEquals([
    'european_union_vat',
    'other',
  ], $tax_number_item
    ->getAllowedTypes());

  // Restricted to a non-EU country.
  $this->field
    ->setSetting('countries', [
    'US',
  ]);
  $this->field
    ->save();
  $entity = $this
    ->reloadEntity($entity);

  /** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
  $tax_number_item = $entity
    ->get('test_tax_number')
    ->first();
  $this
    ->assertEquals([
    'US',
  ], $tax_number_item
    ->getAllowedCountries());
  $this
    ->assertEquals([
    'other',
  ], $tax_number_item
    ->getAllowedTypes());
}