You are here

public function TaxNumberItemTest::testPreSave in Commerce Core 8.2

Tests the preSave() logic.

File

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

Class

TaxNumberItemTest
Tests the 'commerce_tax_number' field type.

Namespace

Drupal\Tests\commerce_tax\Kernel

Code

public function testPreSave() {

  // Verification failed.
  $entity = EntityTest::create([
    'test_tax_number' => [
      'type' => 'serbian_vat',
      'value' => '402',
    ],
  ]);
  $entity
    ->save();

  /** @var \Drupal\commerce_tax\Plugin\Field\FieldType\TaxNumberItemInterface $tax_number_item */
  $tax_number_item = $entity
    ->get('test_tax_number')
    ->first();
  $this
    ->assertEquals(VerificationResult::STATE_FAILURE, $tax_number_item->verification_state);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $tax_number_item->verification_timestamp);
  $this
    ->assertEmpty($tax_number_item->verification_result);

  // Verification service unavailable.
  $tax_number_item
    ->setValue([
    'type' => 'serbian_vat',
    'value' => '190',
  ]);
  $entity
    ->save();
  $this
    ->assertEquals(VerificationResult::STATE_UNKNOWN, $tax_number_item->verification_state);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $tax_number_item->verification_timestamp);
  $this
    ->assertEquals([
    'error' => 'http_429',
  ], $tax_number_item->verification_result);

  // Verification succeeded.
  $tax_number_item
    ->setValue([
    'type' => 'serbian_vat',
    'value' => '403',
  ]);
  $entity
    ->save();
  $this
    ->assertEquals(VerificationResult::STATE_SUCCESS, $tax_number_item->verification_state);
  $this
    ->assertEquals(\Drupal::time()
    ->getRequestTime(), $tax_number_item->verification_timestamp);
  $verification_result = $tax_number_item->verification_result;
  $this
    ->assertArrayHasKey('name', $verification_result);
  $this
    ->assertEquals('John Smith', $verification_result['name']);
  $original_nonce = $verification_result['nonce'];

  // Confirm that verification only runs once.
  $this->container
    ->get('entity.memory_cache')
    ->deleteAll();
  $entity
    ->save();

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