You are here

public function TaxNumberTest::testWidget in Commerce Core 8.2

Tests the widget.

File

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

Class

TaxNumberTest
Tests the tax number widget and formatter.

Namespace

Drupal\Tests\commerce_tax\FunctionalJavascript

Code

public function testWidget() {
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Confirm that the field is present for the allowed country (RS).
  $this
    ->assertSession()
    ->fieldExists('Tax number');
  $this
    ->getSession()
    ->getPage()
    ->fillField('Tax number', '601');
  $this
    ->submitForm([], 'Save');
  $this->customerProfile = $this
    ->reloadEntity($this->customerProfile);
  $tax_number_value = $this->customerProfile
    ->get('tax_number')
    ->first()
    ->getValue();
  $this
    ->assertEquals('serbian_vat', $tax_number_value['type']);
  $this
    ->assertEquals('601', $tax_number_value['value']);
  $this
    ->assertEquals('success', $tax_number_value['verification_state']);
  $this
    ->assertArrayHasKey('nonce', $tax_number_value['verification_result']);
  $original_nonce = $tax_number_value['verification_result']['nonce'];

  // Confirm that not changing the tax number does not re-verify the number.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->fieldValueEquals('Tax number', '601');
  $this
    ->submitForm([], 'Save');
  $this->customerProfile = $this
    ->reloadEntity($this->customerProfile);
  $tax_number_value = $this->customerProfile
    ->get('tax_number')
    ->first()
    ->getValue();
  $this
    ->assertEquals($original_nonce, $tax_number_value['verification_result']['nonce']);

  // Confirm that changing the tax number re-verifies the number.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->fieldValueEquals('Tax number', '601');
  $this
    ->getSession()
    ->getPage()
    ->fillField('Tax number', '603');
  $this
    ->submitForm([], 'Save');
  $this->customerProfile = $this
    ->reloadEntity($this->customerProfile);
  $tax_number_value = $this->customerProfile
    ->get('tax_number')
    ->first()
    ->getValue();
  $this
    ->assertEquals('serbian_vat', $tax_number_value['type']);
  $this
    ->assertEquals('603', $tax_number_value['value']);
  $this
    ->assertEquals('success', $tax_number_value['verification_state']);
  $this
    ->assertArrayHasKey('nonce', $tax_number_value['verification_result']);
  $this
    ->assertNotEquals($original_nonce, $tax_number_value['verification_result']);

  // Confirm that changing the country changes the tax number type.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Country', 'ME');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->fillField('City', 'Podgorica');
  $this
    ->assertSession()
    ->fieldValueEquals('Tax number', '603');
  $this
    ->submitForm([], 'Save');
  $this->customerProfile = $this
    ->reloadEntity($this->customerProfile);
  $tax_number_value = $this->customerProfile
    ->get('tax_number')
    ->first()
    ->getValue();
  $this
    ->assertEquals('other', $tax_number_value['type']);
  $this
    ->assertEquals('603', $tax_number_value['value']);
  $this
    ->assertNull($tax_number_value['verification_state']);
  $this
    ->assertNull($tax_number_value['verification_timestamp']);
  $this
    ->assertEmpty($tax_number_value['verification_result']);

  // Confirm that selecting a non-allowed country removes the field.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('Country', 'MK');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->fillField('City', 'Skopje');
  $this
    ->assertSession()
    ->fieldNotExists('Tax number');
  $this
    ->submitForm([], 'Save');
  $this->customerProfile = $this
    ->reloadEntity($this->customerProfile);
  $this
    ->assertTrue($this->customerProfile
    ->get('tax_number')
    ->isEmpty());
}