You are here

public function TaxNumberItemTest::testValidation in Commerce Core 8.2

Tests the validation.

File

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

Class

TaxNumberItemTest
Tests the 'commerce_tax_number' field type.

Namespace

Drupal\Tests\commerce_tax\Kernel

Code

public function testValidation() {
  $entity = EntityTest::create([
    'test_tax_number' => [
      'type' => 'other',
      'value' => 'MK1234567',
    ],
  ]);

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

  // Fallback plugin, value always accepted (no validation/verification).
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(0, $violations);

  // Missing type.
  $tax_number_item
    ->setValue([
    'value' => 'MK1234567',
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('type', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals('This value should not be null.', $violations[0]
    ->getMessage());

  // Unrecognized type.
  $tax_number_item
    ->setValue([
    'type' => 'INVALID',
    'value' => 'MK1234567',
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('type', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals('Invalid type specified.', $violations[0]
    ->getMessage());

  // Unrecognized verification_state.
  $tax_number_item
    ->setValue([
    'type' => 'other',
    'value' => 'MK1234567',
    'verification_state' => 'INVALID',
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('verification_state', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals('Invalid verification_state specified.', $violations[0]
    ->getMessage());

  // Value too long.
  $entity
    ->set('test_tax_number', [
    'type' => 'serbian_vat',
    'value' => hash('sha512', 'TEST'),
  ]);

  /** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
  $tax_number_item = $entity
    ->get('test_tax_number')
    ->first();
  $expected_message = new FormattableMarkup('%name: may not be longer than 64 characters.', [
    '%name' => $this->field
      ->label(),
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('value', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals($expected_message
    ->__toString(), $violations[0]
    ->getMessage());

  // Invalid format.
  $tax_number_item
    ->setValue([
    'type' => 'serbian_vat',
    'value' => '1234',
  ]);
  $expected_message = new FormattableMarkup('%name is not in the right format. Example: 901.', [
    '%name' => $this->field
      ->label(),
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('value', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals($expected_message
    ->__toString(), $violations[0]
    ->getMessage());

  // Invalid format (verification failed).
  $tax_number_item
    ->setValue([
    'type' => 'serbian_vat',
    'value' => '402',
  ]);
  $expected_message = new FormattableMarkup('%name could not be verified.', [
    '%name' => $this->field
      ->label(),
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('value', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals($expected_message
    ->__toString(), $violations[0]
    ->getMessage());

  // Valid format (verification succeeded).
  $tax_number_item
    ->setValue([
    'type' => 'serbian_vat',
    'value' => '403',
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(0, $violations);

  // Valid format (verification service unavailable).
  $tax_number_item
    ->setValue([
    'type' => 'serbian_vat',
    'value' => '190',
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(0, $violations);

  // Verification required, verification service unavailable.
  $this->field
    ->setSetting('allow_unverified', FALSE);
  $this->field
    ->save();
  $entity = EntityTest::create([
    'test_tax_number' => [
      'type' => 'serbian_vat',
      'value' => '190',
    ],
  ]);

  /** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
  $tax_number_item = $entity
    ->get('test_tax_number')
    ->first();
  $expected_message = new FormattableMarkup('%name could not be verified.', [
    '%name' => $this->field
      ->label(),
  ]);
  $violations = $tax_number_item
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals('value', $violations[0]
    ->getPropertyPath());
  $this
    ->assertEquals($expected_message
    ->__toString(), $violations[0]
    ->getMessage());
}