/**
  * @covers ::submitForm
  */
 function testSubmitFormWithoutCompletionResponse()
 {
     $operation_result = $this->getMock(OperationResultInterface::class);
     $operation_result->expects($this->atLeastOnce())->method('isCompleted')->willReturn(TRUE);
     $payment_method = $this->getMock(PaymentMethodRefundPaymentInterface::class);
     $payment_method->expects($this->once())->method('refundPayment')->willReturn($operation_result);
     $url = new Url($this->randomMachineName());
     $this->payment->expects($this->atLeastOnce())->method('getPaymentMethod')->willReturn($payment_method);
     $this->payment->expects($this->atLeastOnce())->method('urlInfo')->with('canonical')->willReturn($url);
     $form = [];
     $form_state = $this->getMock(FormStateInterface::class);
     $form_state->expects($this->atLeastOnce())->method('setRedirectUrl')->with($url);
     $this->sut->submitForm($form, $form_state);
 }
 /**
  * @covers ::submitForm
  * @covers ::getPluginSelector
  */
 public function testSubmitForm()
 {
     $form = ['payment_status' => ['foo' => $this->randomMachineName()]];
     $form_state = new FormState();
     $payment_status = $this->getMock(PaymentStatusInterface::class);
     $this->pluginSelectorManager->expects($this->once())->method('createInstance')->willReturn($this->pluginSelector);
     $this->pluginSelector->expects($this->atLeastOnce())->method('getSelectedPlugin')->willReturn($payment_status);
     $this->pluginSelector->expects($this->atLeastOnce())->method('submitSelectorForm')->with($form['payment_status'], $form_state);
     $url = new Url($this->randomMachineName());
     $this->payment->expects($this->once())->method('setPaymentStatus')->with($payment_status);
     $this->payment->expects($this->once())->method('save');
     $this->payment->expects($this->once())->method('urlInfo')->with('canonical')->willReturn($url);
     $this->sut->submitForm($form, $form_state);
     $this->assertSame($url, $form_state->getRedirect());
 }
 /**
  * Tests getAmount().
  */
 protected function testGetAmount()
 {
     $amount = 3;
     $quantity = 2;
     for ($i = 0; $i < 2; $i++) {
         /** @var \Drupal\payment\Plugin\Payment\LineItem\Basic $line_item */
         $line_item = $this->lineItemManager->createInstance('payment_basic');
         $name = $this->randomMachineName();
         $line_item->setName($name);
         $line_item->setAmount($amount);
         $line_item->setQuantity($quantity);
         $this->payment->setLineItem($line_item);
     }
     $this->assertEqual($this->payment->getAmount(), 12);
 }
 /**
  * Tests the element.
  */
 protected function testElement()
 {
     $user = $this->drupalCreateUser();
     $this->drupalLogin($user);
     /** @var \Drupal\currency\ConfigImporterInterface $config_importer */
     $config_importer = \Drupal::service('currency.config_importer');
     $config_importer->importCurrency('EUR');
     // Create the payment reference field and field instance.
     $payment_reference_field_name = 'foobarbaz';
     entity_create('field_storage_config', array('cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'entity_type' => 'user', 'field_name' => $payment_reference_field_name, 'type' => 'payment_reference'))->save();
     entity_create('field_config', array('bundle' => 'user', 'entity_type' => 'user', 'field_name' => $payment_reference_field_name, 'settings' => array('currency_code' => 'EUR', 'line_items_data' => [])))->save();
     // Create a field on the payment entity type.
     $payment_field_name = 'quxfoobar';
     entity_create('field_storage_config', array('cardinality' => 1, 'entity_type' => 'payment', 'field_name' => $payment_field_name, 'type' => 'text'))->save();
     entity_create('field_config', array('bundle' => 'payment_reference', 'entity_type' => 'payment', 'field_name' => $payment_field_name, 'required' => TRUE))->save();
     entity_get_form_display('payment', 'payment_reference', 'default')->setComponent($payment_field_name, array('type' => 'text_textfield'))->save();
     $state = \Drupal::state();
     $path = 'payment_reference_test-element-payment_reference';
     // Test without selecting a payment method.
     $this->drupalGet($path);
     $this->drupalPostForm(NULL, [], t('Pay'));
     $this->assertText('quxfoobar field is required');
     $value = $state->get('payment_reference_test_payment_reference_element');
     $this->assertNull($value);
     // Test with a payment method that returns no execution completion response.
     $text_field_value = $this->randomMachineName();
     $this->drupalPostForm($path, array('payment_reference[container][payment_form][payment_method][container][select][container][plugin_id]' => 'payment_test_no_response', 'payment_reference[container][payment_form][quxfoobar][0][value]' => $text_field_value), t('Choose'));
     $this->drupalPostForm(NULL, [], t('Pay'));
     $this->drupalPostForm(NULL, [], t('Submit'));
     $payment_id = $state->get('payment_reference_test_payment_reference_element');
     $this->assertTrue(is_int($payment_id));
     /** @var \Drupal\payment\Entity\PaymentInterface $payment */
     $payment = Payment::load($payment_id);
     $this->assertTrue($payment instanceof PaymentInterface);
     $this->assertEqual($payment->get('quxfoobar')[0]->get('value')->getValue(), $text_field_value);
     // Test with a payment method that returns an execution completion response.
     $text_field_value = $this->randomMachineName();
     $this->drupalPostForm($path, array('payment_reference[container][payment_form][payment_method][container][select][container][plugin_id]' => 'payment_test_response', 'payment_reference[container][payment_form][quxfoobar][0][value]' => $text_field_value), t('Choose'));
     $this->drupalPostForm(NULL, [], t('Pay'));
     $this->clickLink(t('Complete payment (opens in a new window).'));
     /** @var \Drupal\payment\Entity\PaymentInterface $payment */
     $payment = Payment::loadMultiple()[2];
     $this->assertEqual($payment->getPaymentStatus()->getPluginId(), 'payment_success');
     $this->assertEqual($payment->get('quxfoobar')[0]->get('value')->getValue(), $text_field_value);
 }
 /**
  * @covers ::submitForm
  * @covers ::getPluginSelector
  * @covers ::getPaymentMethodManager
  */
 public function testSubmitForm()
 {
     $redirect_url = new Url($this->randomMachineName());
     $response = new Response($redirect_url);
     $result = $this->getMock(OperationResultInterface::class);
     $result->expects($this->atLeastOnce())->method('getCompletionResponse')->willReturn($response);
     $form = ['payment_method' => ['#type' => $this->randomMachineName()]];
     $form_state = new FormState();
     $payment_method = $this->getMock(PaymentMethodInterface::class);
     $this->pluginSelectorManager->expects($this->once())->method('createInstance')->with($this->configFactoryConfiguration['payment_form.payment_type']['plugin_selector_id'])->willReturn($this->pluginSelector);
     $this->pluginSelector->expects($this->atLeastOnce())->method('getSelectedPlugin')->willReturn($payment_method);
     $this->pluginSelector->expects($this->atLeastOnce())->method('submitSelectorForm')->with($form['payment_method'], $form_state);
     $this->payment->expects($this->atLeastOnce())->method('setPaymentMethod')->with($payment_method);
     $this->payment->expects($this->atLeastOnce())->method('save');
     $this->payment->expects($this->atLeastOnce())->method('execute')->willReturn($result);
     $this->sut->submitForm($form, $form_state);
     $this->assertSame($redirect_url, $form_state->getRedirect());
 }