You are here

public function TaxNumberItemTest::testField in Commerce Core 8.2

Tests the field.

File

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

Class

TaxNumberItemTest
Tests the 'commerce_tax_number' field type.

Namespace

Drupal\Tests\commerce_tax\Kernel

Code

public function testField() {
  $entity = EntityTest::create([
    'test_tax_number' => [
      'type' => 'serbian_vat',
      'value' => '123',
      'verification_state' => VerificationResult::STATE_SUCCESS,
      'verification_timestamp' => strtotime('2019/08/08'),
      'verification_result' => [
        'name' => 'Bryan Centarro',
      ],
    ],
  ]);
  $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();
  $this
    ->assertEquals('serbian_vat', $tax_number_item->type);
  $this
    ->assertEquals('123', $tax_number_item->value);
  $this
    ->assertEquals(VerificationResult::STATE_SUCCESS, $tax_number_item->verification_state);
  $this
    ->assertEquals(strtotime('2019/08/08'), $tax_number_item->verification_timestamp);
  $this
    ->assertEquals([
    'name' => 'Bryan Centarro',
  ], $tax_number_item->verification_result);
  $type_plugin = $tax_number_item
    ->getTypePlugin();
  $this
    ->assertNotEmpty($type_plugin);
  $this
    ->assertEquals('serbian_vat', $type_plugin
    ->getPluginId());

  // Confirm that changing the type resets the verification state.
  $tax_number_item->type = 'invalid';
  $this
    ->assertNull($tax_number_item->verification_state);
  $this
    ->assertNull($tax_number_item->verification_timestamp);
  $this
    ->assertNull($tax_number_item->verification_result);

  // Test type fallback.
  $type_plugin = $tax_number_item
    ->getTypePlugin();
  $this
    ->assertNotEmpty($type_plugin);
  $this
    ->assertEquals('other', $type_plugin
    ->getPluginId());
}