You are here

public function PaymentLineItemsInputWebTest::testElement in Payment 8.2

Tests the element.

File

tests/src/Functional/Element/PaymentLineItemsInputWebTest.php, line 69

Class

PaymentLineItemsInputWebTest
payment_line_items_input element web test.

Namespace

Drupal\Tests\payment\Functional\Element

Code

public function testElement() {
  $state = \Drupal::state();
  $names = [];
  foreach (Generate::createPaymentLineItems() as $line_item) {
    $names[] = $line_item
      ->getName();
  }
  $type = 'payment_basic';

  // Test the presence of default elements.
  $this
    ->drupalGet('payment_test-element-payment-line-item');
  $this
    ->assertLineItemElements($names);
  $this
    ->assertAddMore(TRUE);

  // Add a line item through a regular submission.
  $this
    ->drupalPostForm(NULL, array(
    'line_item[add_more][type]' => $type,
  ), t('Add and configure a new line item'));
  $this
    ->assertLineItemElements(array_merge($names, array(
    $type,
  )));
  $this
    ->assertAddMore(FALSE);

  // Delete a line item through a regular submission.
  $this
    ->drupalPostForm(NULL, [], t('Delete'));
  array_shift($names);
  $this
    ->assertLineItemElements($names);
  $elements = $this
    ->xpath('//input[@name="line_item[line_items][' . $type . '][weight]"]');
  $this
    ->assertFalse(isset($elements[0]));
  $this
    ->assertAddMore(TRUE);

  // Change a line item's weight and test the element's value through a
  // regular submission.
  $name = 'line_item[line_items][' . reset($names) . '][weight]';
  $this
    ->assertFieldByXPath('//select[@name="' . $name . '"]/option[@value="1" and @selected="selected"]');
  $this
    ->drupalPostForm(NULL, array(
    // Change the first line item's weight to be the highest.
    $name => 5,
    // Set a description for the new element.
    'line_item[line_items][payment_basic][plugin_form][description]' => 'new',
  ), t('Submit'));
  $value = $state
    ->get('payment_test_line_item_form_element');
  $this
    ->assertTrue(is_array($value));

  // One item was deleted, one added, again resulting in 3.
  $this
    ->assertCount(3, $value);
  $line_items = [];
  foreach ($value as $line_item_data) {
    $this
      ->assertTrue(isset($line_item_data['plugin_configuration']));
    $this
      ->assertTrue(is_array($line_item_data['plugin_configuration']));
    $this
      ->assertTrue(isset($line_item_data['plugin_id']));
    $line_items[] = Payment::lineItemManager()
      ->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']);
  }

  // Check that the first line item is now the last.
  $this
    ->assertEqual(end($line_items)
    ->getName(), reset($names));
}