You are here

public function TaxNumberTest::testFormatter in Commerce Core 8.2

Tests the formatter.

File

modules/tax/tests/src/FunctionalJavascript/TaxNumberTest.php, line 200

Class

TaxNumberTest
Tests the tax number widget and formatter.

Namespace

Drupal\Tests\commerce_tax\FunctionalJavascript

Code

public function testFormatter() {
  $this->customerProfile
    ->set('tax_number', [
    'type' => 'other',
    'value' => '122',
  ]);
  $this->customerProfile
    ->save();
  $this
    ->drupalGet($this->order
    ->toUrl('canonical'));
  $rendered_field = $this
    ->getSession()
    ->getPage()
    ->find('css', '.field--name-tax-number');
  $this
    ->assertStringContainsString('Tax number', $rendered_field
    ->getHtml());
  $this
    ->assertStringContainsString('122', $rendered_field
    ->getHtml());
  $this
    ->assertFalse($rendered_field
    ->hasLink('122'));
  $state_field = $rendered_field
    ->find('css', '.commerce-tax-number__verification-icon');
  $this
    ->assertEmpty($state_field);
  $this->customerProfile
    ->set('tax_number', [
    'type' => 'serbian_vat',
    'value' => '123',
    'verification_state' => VerificationResult::STATE_SUCCESS,
    'verification_timestamp' => strtotime('2019/08/08'),
    'verification_result' => [
      'name' => 'Centarro LLC',
    ],
  ]);
  $this->customerProfile
    ->save();
  $this
    ->drupalGet($this->order
    ->toUrl('canonical'));
  $rendered_field = $this
    ->getSession()
    ->getPage()
    ->find('css', '.field--name-tax-number');
  $this
    ->assertStringContainsString('Tax number', $rendered_field
    ->getHtml());
  $this
    ->assertTrue($rendered_field
    ->hasLink('123'));
  $this
    ->assertFalse($rendered_field
    ->hasLink('Reverify'));
  $state_field = $rendered_field
    ->find('css', '.commerce-tax-number__verification-icon');
  $this
    ->assertNotEmpty($state_field);
  $this
    ->assertEquals('Verification state: Success', $state_field
    ->getAttribute('title'));
  $this
    ->assertTrue($state_field
    ->hasClass('commerce-tax-number__verification-icon--success'));

  // Confirm that the verification result can be viewed.
  $this
    ->clickLink('123');
  $this
    ->assertSession()
    ->pageTextContains('August 8, 2019 - 00:00');
  $this
    ->assertSession()
    ->pageTextContains('Centarro LLC');
  $this->customerProfile
    ->set('tax_number', [
    'type' => 'serbian_vat',
    'value' => '124',
    'verification_state' => VerificationResult::STATE_FAILURE,
    'verification_timestamp' => strtotime('2019/08/09'),
    'verification_result' => [
      'name' => 'Google LLC',
    ],
  ]);
  $this->customerProfile
    ->save();
  $this
    ->drupalGet($this->order
    ->toUrl('canonical'));
  $rendered_field = $this
    ->getSession()
    ->getPage()
    ->find('css', '.field--name-tax-number');
  $this
    ->assertStringContainsString('Tax number', $rendered_field
    ->getHtml());
  $this
    ->assertTrue($rendered_field
    ->hasLink('124'));
  $this
    ->assertFalse($rendered_field
    ->hasLink('Reverify'));
  $state_field = $rendered_field
    ->find('css', '.commerce-tax-number__verification-icon');
  $this
    ->assertNotEmpty($state_field);
  $this
    ->assertEquals('Verification state: Failure', $state_field
    ->getAttribute('title'));
  $this
    ->assertTrue($state_field
    ->hasClass('commerce-tax-number__verification-icon--failure'));

  // Confirm that the verification result can be viewed.
  $this
    ->clickLink('124');
  $this
    ->assertSession()
    ->pageTextContains('August 9, 2019 - 00:00');
  $this
    ->assertSession()
    ->pageTextContains('Google LLC');
  $this->customerProfile
    ->set('tax_number', [
    'type' => 'serbian_vat',
    'value' => '125',
    'verification_state' => VerificationResult::STATE_UNKNOWN,
    'verification_timestamp' => strtotime('2019/08/10'),
    'verification_result' => [
      'error' => 'http_429',
    ],
  ]);
  $this->customerProfile
    ->save();
  $this
    ->drupalGet($this->order
    ->toUrl('canonical'));
  $rendered_field = $this
    ->getSession()
    ->getPage()
    ->find('css', '.field--name-tax-number');
  $this
    ->assertStringContainsString('Tax number', $rendered_field
    ->getHtml());
  $this
    ->assertTrue($rendered_field
    ->hasLink('125'));
  $this
    ->assertTrue($rendered_field
    ->hasLink('Reverify'));
  $state_field = $rendered_field
    ->find('css', '.commerce-tax-number__verification-icon');
  $this
    ->assertNotEmpty($state_field);
  $this
    ->assertEquals('Verification state: Unknown', $state_field
    ->getAttribute('title'));
  $this
    ->assertTrue($state_field
    ->hasClass('commerce-tax-number__verification-icon--unknown'));

  // Confirm that the verification result can be viewed.
  $this
    ->clickLink('125');
  $this
    ->assertSession()
    ->pageTextContains('August 10, 2019 - 00:00');
  $this
    ->assertSession()
    ->pageTextContains('Too many requests.');

  // Confirm that the number can be reverified.
  $this
    ->drupalGet($this->order
    ->toUrl('canonical'));
  $this
    ->clickLink('Reverify');
  $this
    ->assertSession()
    ->pageTextContains('The tax number 125 has been reverified.');

  // Confirm that invalid verification states are ignored.
  $this->customerProfile
    ->set('tax_number', [
    'type' => 'serbian_vat',
    'value' => '126',
    'verification_state' => 'INVALID',
    'verification_timestamp' => strtotime('2019/08/10'),
    'verification_result' => [
      'verification_id' => '123458',
    ],
  ]);
  $this->customerProfile
    ->save();
  $this
    ->drupalGet($this->order
    ->toUrl('canonical'));
  $rendered_field = $this
    ->getSession()
    ->getPage()
    ->find('css', '.field--name-tax-number');
  $this
    ->assertStringContainsString('Tax number', $rendered_field
    ->getHtml());
  $this
    ->assertStringContainsString('126', $rendered_field
    ->getHtml());
  $state_field = $rendered_field
    ->find('css', '.commerce-tax-number__verification-icon');
  $this
    ->assertEmpty($state_field);
}