You are here

public function PaymentGatewayFormTest::testPaymentGatewayCreation in Commerce Core 8.2

Tests using the payment gateway add form.

File

modules/payment/tests/src/FunctionalJavascript/PaymentGatewayFormTest.php, line 38

Class

PaymentGatewayFormTest
Tests the payment gateway form.

Namespace

Drupal\Tests\commerce_payment\FunctionalJavascript

Code

public function testPaymentGatewayCreation() {
  $this
    ->drupalGet(Url::fromRoute('entity.commerce_payment_gateway.add_form')
    ->toString());
  $expected_options = [
    'Example (Off-site redirect with stored payment methods)',
    'Example (Off-site redirect)',
    'Example (On-site)',
    'Manual',
  ];
  $page = $this
    ->getSession()
    ->getPage();
  foreach ($expected_options as $expected_option) {
    $radio_button = $page
      ->findField($expected_option);
    $this
      ->assertNotNull($radio_button);
  }
  $default_radio_button = $page
    ->findField('Example (Off-site redirect with stored payment methods)');
  $this
    ->assertNotEmpty($default_radio_button
    ->getAttribute('checked'));
  $this
    ->assertSession()
    ->fieldExists('Name');
  $this
    ->assertSession()
    ->fieldExists('id');
  $this
    ->assertSession()
    ->fieldExists('Redirect via POST (automatic)');
  $radio_button = $this
    ->getSession()
    ->getPage()
    ->findField('On-site');
  $radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->fieldExists('Name');
  $this
    ->assertSession()
    ->fieldExists('API key');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField('label', 'My onsite name');
  $page
    ->fillField('configuration[example_onsite][api_key]', 'MyAPIKey');
  $this
    ->submitForm([], 'Save');

  // machine_id causes an issue I could not solve. Tried the same way as
  // NumberPatternTest does it, it did not work. This ugliness below however
  // does.
  $page
    ->fillField('id', 'my_onsite_name');
  $this
    ->submitForm([], 'Save');
  $this
    ->drupalGet('admin/commerce/config/payment-gateways');
  $entity_type_manager = $this->container
    ->get('entity_type.manager');

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $onsite_payment_gateway */
  $onsite_payment_gateway = $entity_type_manager
    ->getStorage('commerce_payment_gateway')
    ->load('my_onsite_name');
  $this
    ->assertEquals('MyAPIKey', $onsite_payment_gateway
    ->getPluginConfiguration()['api_key']);
  $this
    ->assertEquals('My onsite name', $onsite_payment_gateway
    ->label());
  $this
    ->assertEquals('test', $onsite_payment_gateway
    ->getPlugin()
    ->getMode());
}