/** * {@inheritdoc */ protected function setUp() { parent::setUp(); $this->bundle = 'payment_unavailable'; $this->lineItemManager = Payment::lineItemManager(); $this->statusManager = Payment::statusManager(); $this->payment = entity_create('payment', array('bundle' => $this->bundle)); }
/** * @covers ::lineItemManager */ public function testLineItemManager() { $container = new Container(); $line_item_manager = $this->getMock(PaymentLineItemManagerInterface::class); $container->set('plugin.manager.payment.line_item', $line_item_manager); \Drupal::setContainer($container); $this->assertSame($line_item_manager, Payment::lineItemManager()); }
/** * Creates payment line items. * * @return \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface[] */ static function createPaymentLineItems() { $line_item_manager = Payment::lineItemManager(); /** @var \Drupal\currency\ConfigImporterInterface $config_importer */ $config_importer = \Drupal::service('currency.config_importer'); $config_importer->importCurrency('NLG'); $config_importer->importCurrency('JPY'); $config_importer->importCurrency('MGA'); $line_items = array($line_item_manager->createInstance('payment_basic', [])->setName('foo')->setAmount(9.9)->setCurrencyCode('NLG')->setDescription(static::getRandom()->string()), $line_item_manager->createInstance('payment_basic', [])->setName('bar')->setAmount(5.5)->setCurrencyCode('JPY')->setQuantity(2)->setDescription(static::getRandom()->string()), $line_item_manager->createInstance('payment_basic', [])->setName('baz')->setAmount(1.1)->setCurrencyCode('MGA')->setQuantity(3)->setDescription(static::getRandom()->string())); return $line_items; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { if ($form_state->has('payment_line_item')) { $line_item = $form_state->get('payment_line_item'); } else { $line_item = Payment::lineItemManager()->createInstance('payment_basic'); $form_state->set('payment_line_item', $line_item); } $form['line_item'] = $line_item->buildConfigurationForm([], $form_state); $form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); return $form; }
/** * {@inheritdoc} */ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { /** @var \Drupal\currency\FormHelperInterface $form_helper */ $form_helper = \Drupal::service('currency.form_helper'); $element['#element_validate'] = [get_class() . '::fieldSettingsFormValidate']; $element['currency_code'] = ['#empty_value' => '', '#type' => 'select', '#title' => $this->t('Payment currency'), '#options' => $form_helper->getCurrencyOptions(), '#default_value' => $this->getSetting('currency_code'), '#required' => TRUE]; $line_items = []; foreach ($this->getSetting('line_items_data') as $line_item_data) { $line_items[] = Payment::lineItemManager()->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']); } $element['line_items'] = ['#type' => 'payment_line_items_input', '#title' => $this->t('Line items'), '#default_value' => $line_items, '#required' => TRUE, '#currency_code' => '']; return $element; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->lineItem = Payment::lineItemManager()->createInstance('payment_basic'); }
/** * Tests the element. */ protected 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')); $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="0" and @selected="selected"]'); $this->drupalPostForm(NULL, array($name => count($names)), t('Submit')); $value = $state->get('payment_test_line_item_form_element'); if ($this->assertTrue(is_array($value))) { /// We end up with one more line item than we originally had. $this->assertEqual(count($value), count($names)); $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)); } }