CurrencyAmountWebTest.php in Currency 8.3
File
tests/src/Functional/Element/CurrencyAmountWebTest.php
View source
<?php
namespace Drupal\Tests\currency\Functional\Element;
use Drupal\Tests\BrowserTestBase;
class CurrencyAmountWebTest extends BrowserTestBase {
public static $modules = array(
'currency_test',
);
protected $defaultTheme = 'stark';
public function setUp() : void {
parent::setUp();
$config_importer = \Drupal::service('currency.config_importer');
$config_importer
->importCurrency('EUR');
}
function testValidation() {
$state = \Drupal::state();
$path = 'currency_test-form-element-currency-amount/50.00/100';
$values = array(
'container[amount][amount]' => '50,95',
'container[amount][currency_code]' => 'EUR',
);
$this
->drupalPostForm($path, $values, t('Submit'));
$amount = $state
->get('currency_test_currency_amount_element');
$this
->assertEqual(50.95, $amount['amount']);
$this
->assertEqual('EUR', $amount['currency_code']);
$this
->drupalGet($path . '/NLG');
$this
->assertNoFieldByXPath("//input[@name='container[amount][currency_code]']");
$values = array(
'container[amount][amount]' => '50,95',
);
$this
->drupalPostForm($path . '/NLG', $values, t('Submit'));
$amount = $state
->get('currency_test_currency_amount_element');
$this
->assertEqual(50.95, $amount['amount']);
$this
->assertEqual('NLG', $amount['currency_code']);
$invalid_amounts = array(
$this
->randomMachineName(2),
'49,.95',
'49.95',
'999',
);
foreach ($invalid_amounts as $amount) {
$values = array(
'container[amount][amount]' => $amount,
);
$this
->drupalPostForm($path, $values, t('Submit'));
$this
->assertFieldByXPath("//input[@name='container[amount][amount]' and contains(@class, 'error')]");
$this
->assertNoFieldByXPath("//input[not(@name='container[amount][amount]') and contains(@class, 'error')]");
}
}
}