PaymentLineItemsInputWebTest.php in Payment 8.2
File
tests/src/Functional/Element/PaymentLineItemsInputWebTest.php
View source
<?php
namespace Drupal\Tests\payment\Functional\Element;
use Drupal\payment\Payment;
use Drupal\payment\Tests\Generate;
use Drupal\Tests\BrowserTestBase;
class PaymentLineItemsInputWebTest extends BrowserTestBase {
public static $modules = array(
'payment',
'payment_test',
);
protected $defaultTheme = 'stark';
protected function lineItemData(array $names) {
$data = [];
foreach ($names as $name) {
$data += array(
'line_item[line_items][' . $name . '][plugin_form][amount][amount]' => '10.0',
'line_item[line_items][' . $name . '][plugin_form][description]' => 'foo',
'line_item[line_items][' . $name . '][plugin_form][quantity]' => '1',
);
}
return $data;
}
protected function assertLineItemElements(array $names) {
foreach (array_keys($this
->lineItemData($names)) as $input_name) {
$this
->assertFieldByName($input_name);
}
}
protected function assertAddMore($present) {
$elements = $this
->xpath('//select[@name="line_item[add_more][type]"]');
$this
->assertEqual($present, isset($elements[0]));
$elements = $this
->xpath('//input[@id="edit-line-item-add-more-add"]');
$this
->assertEqual($present, isset($elements[0]));
}
public function testElement() {
$state = \Drupal::state();
$names = [];
foreach (Generate::createPaymentLineItems() as $line_item) {
$names[] = $line_item
->getName();
}
$type = 'payment_basic';
$this
->drupalGet('payment_test-element-payment-line-item');
$this
->assertLineItemElements($names);
$this
->assertAddMore(TRUE);
$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);
$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);
$name = 'line_item[line_items][' . reset($names) . '][weight]';
$this
->assertFieldByXPath('//select[@name="' . $name . '"]/option[@value="1" and @selected="selected"]');
$this
->drupalPostForm(NULL, array(
$name => 5,
'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));
$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']);
}
$this
->assertEqual(end($line_items)
->getName(), reset($names));
}
}