diff --git a/modules/payment_form/src/Plugin/Payment/Type/PaymentForm.php b/modules/payment_form/src/Plugin/Payment/Type/PaymentForm.php index 0e8680d..9e3a47b 100644 --- a/modules/payment_form/src/Plugin/Payment/Type/PaymentForm.php +++ b/modules/payment_form/src/Plugin/Payment/Type/PaymentForm.php @@ -12,6 +12,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Url; use Drupal\payment\Plugin\Payment\Type\PaymentTypeBase; +use Drupal\payment\Response; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -93,7 +94,7 @@ class PaymentForm extends PaymentTypeBase implements ContainerFactoryPluginInter * {@inheritdoc} */ protected function doGetResumeContextResponse() { - return new RedirectResponse(Url::fromUri($this->getDestinationUrl())); + return new Response(Url::fromUri($this->getDestinationUrl())); } /** diff --git a/modules/payment_form/tests/src/Unit/Plugin/Payment/Type/PaymentFormUnitTest.php b/modules/payment_form/tests/src/Unit/Plugin/Payment/Type/PaymentFormUnitTest.php index 22d54c3..070fe24 100644 --- a/modules/payment_form/tests/src/Unit/Plugin/Payment/Type/PaymentFormUnitTest.php +++ b/modules/payment_form/tests/src/Unit/Plugin/Payment/Type/PaymentFormUnitTest.php @@ -8,12 +8,10 @@ namespace Drupal\Tests\payment_form\Unit\Plugin\Payment\Type; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\payment_form\Plugin\Payment\Type\PaymentForm; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\HttpKernel\KernelEvents; /** * @coversDefaultClass \Drupal\payment_form\Plugin\Payment\Type\PaymentForm @@ -210,7 +208,7 @@ class PaymentFormUnitTest extends UnitTestCase { $response = $this->paymentType->getResumeContextResponse(); $this->assertInstanceOf('\Drupal\payment\ResponseInterface', $response); - $this->assertSame($url, $response->getRedirectUrl()->toString()); + $this->assertSame($url, $response->getRedirectUrl()->getUri()); } /** diff --git a/modules/payment_reference/src/Element/PaymentReference.php b/modules/payment_reference/src/Element/PaymentReference.php index d5b168a..fabe586 100644 --- a/modules/payment_reference/src/Element/PaymentReference.php +++ b/modules/payment_reference/src/Element/PaymentReference.php @@ -33,13 +33,6 @@ class PaymentReference extends PaymentReferenceBase { protected $paymentQueue; /** - * The temporary payment storage. - * - * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface - */ - protected $temporaryPaymentStorage; - - /** * Creates a new instance. * * @param array $configuration @@ -55,13 +48,11 @@ class PaymentReference extends PaymentReferenceBase { * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator * @param \Drupal\payment\Plugin\Payment\MethodSelector\PaymentMethodSelectorManagerInterface $payment_method_selector_manager * @param \Drupal\Component\Utility\Random $random - * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $temporary_payment_storage * @param \Drupal\payment\QueueInterface $payment_queue */ - public function __construct($configuration, $plugin_id, $plugin_definition, RequestStack $request_stack, EntityStorageInterface $payment_storage, TranslationInterface $string_translation, DateFormatter $date_formatter, LinkGeneratorInterface $link_generator, PaymentMethodSelectorManagerInterface $payment_method_selector_manager, Random $random, KeyValueStoreExpirableInterface $temporary_payment_storage, QueueInterface $payment_queue) { + public function __construct($configuration, $plugin_id, $plugin_definition, RequestStack $request_stack, EntityStorageInterface $payment_storage, TranslationInterface $string_translation, DateFormatter $date_formatter, LinkGeneratorInterface $link_generator, PaymentMethodSelectorManagerInterface $payment_method_selector_manager, Random $random, QueueInterface $payment_queue) { parent::__construct($configuration, $plugin_id, $plugin_definition, $request_stack, $payment_storage, $string_translation, $date_formatter, $link_generator, $payment_method_selector_manager, $random); $this->paymentQueue = $payment_queue; - $this->temporaryPaymentStorage = $temporary_payment_storage; } /** @@ -71,10 +62,7 @@ class PaymentReference extends PaymentReferenceBase { /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */ $entity_manager = $container->get('entity.manager'); - /** @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $key_value_expirable */ - $key_value_expirable = $container->get('keyvalue.expirable'); - - return new static($configuration, $plugin_id, $plugin_definition, $container->get('request_stack'), $entity_manager->getStorage('payment'), $container->get('string_translation'), $container->get('date.formatter'), $container->get('link_generator'), $container->get('plugin.manager.payment.method_selector'), new Random(), $key_value_expirable->get('payment.payment_type.payment_reference'), $container->get('payment_reference.queue')); + return new static($configuration, $plugin_id, $plugin_definition, $container->get('request_stack'), $entity_manager->getStorage('payment'), $container->get('string_translation'), $container->get('date.formatter'), $container->get('link_generator'), $container->get('plugin.manager.payment.method_selector'), new Random(), $container->get('payment_reference.queue')); } /** @@ -84,11 +72,4 @@ class PaymentReference extends PaymentReferenceBase { return $this->paymentQueue; } - /** - * {@inheritdoc} - */ - protected function getTemporaryPaymentStorage() { - return $this->temporaryPaymentStorage; - } - } diff --git a/modules/payment_reference/src/Element/PaymentReferenceBase.php b/modules/payment_reference/src/Element/PaymentReferenceBase.php index f7d5caf..1c0d617 100644 --- a/modules/payment_reference/src/Element/PaymentReferenceBase.php +++ b/modules/payment_reference/src/Element/PaymentReferenceBase.php @@ -245,12 +245,12 @@ abstract class PaymentReferenceBase extends FormElement implements FormElementIn '#type' => 'payment_line_items_display', ); $build['payment_method'] = $payment_method_selector->buildConfigurationForm(array(), $form_state); - if ($this->hasTemporaryPayment($element, $form_state)) { + if (!$payment_method_selector->getPaymentMethod() || !$payment_method_selector->getPaymentMethod()->getPaymentExecutionResult()->hasCompleted()) { $this->disableChildren($build['payment_method']); } $this->getEntityFormDisplay($element, $form_state)->buildForm($payment_method_selector->getPayment(), $build, $form_state); $build['pay_link'] = $this->buildCompletePaymentLink($element, $form_state); - $build['pay_link']['#access'] = $this->hasTemporaryPayment($element, $form_state); + $build['pay_link']['#access'] = !$payment_method_selector->getPaymentMethod() || !$payment_method_selector->getPaymentMethod()->getPaymentExecutionResult()->hasCompleted(); $build['pay_link']['#process'] = array(array(get_class($this), 'processMaxWeight')); $plugin_id = $this->getPluginId(); $build['pay_button'] = array( @@ -305,8 +305,9 @@ abstract class PaymentReferenceBase extends FormElement implements FormElementIn * A render array. */ protected function buildRefreshButton(array $element, FormStateInterface $form_state) { + $payment_method_selector = $this->getPaymentMethodSelector($element, $form_state); $class = array('payment_reference-refresh-button'); - if (!$element['#default_value'] && $element['#available_payment_id'] && !$this->hasTemporaryPayment($element, $form_state)) { + if (!$element['#default_value'] && $element['#available_payment_id'] && (!$payment_method_selector->getPaymentMethod() || !$payment_method_selector->getPaymentMethod()->getPaymentExecutionResult()->hasCompleted())) { $class[] = 'payment-reference-hidden'; } $plugin_id = $this->getPluginId(); @@ -456,15 +457,13 @@ abstract class PaymentReferenceBase extends FormElement implements FormElementIn $payment->setPaymentMethod($payment_method); $result = $payment->execute(); - if (!$result->hasCompleted()) { - if (!$this->requestStack->getCurrentRequest()->isXmlHttpRequest()) { - $url = $payment->urlInfo('complete'); - $url->setOption('attributes', [ - 'target' => '_blank', - ]); - $link = $this->linkGenerator->generate($this->t('Complete payment (opens in a new window).'), $url); - drupal_set_message($link); - } + if (!$result->hasCompleted() && !$this->requestStack->getCurrentRequest()->isXmlHttpRequest()) { + $url = $payment->urlInfo('complete'); + $url->setOption('attributes', [ + 'target' => '_blank', + ]); + $link = $this->linkGenerator->generate($this->t('Complete payment (opens in a new window).'), $url); + drupal_set_message($link); } $form_state->setRebuild(); } diff --git a/modules/payment_reference/src/Plugin/Payment/Type/PaymentReference.php b/modules/payment_reference/src/Plugin/Payment/Type/PaymentReference.php index 064b1ce..8d0210b 100644 --- a/modules/payment_reference/src/Plugin/Payment/Type/PaymentReference.php +++ b/modules/payment_reference/src/Plugin/Payment/Type/PaymentReference.php @@ -98,11 +98,11 @@ class PaymentReference extends PaymentTypeBase implements ContainerFactoryPlugin * {@inheritdoc} */ protected function doGetResumeContextResponse() { - return new RedirectResponse(new Url('payment_reference.resume_context', array( + return new RedirectResponse((new Url('payment_reference.resume_context', array( 'payment' => $this->getPayment()->id(), ), array( 'absolute' => TRUE, - ))); + )))->toString()); } /** diff --git a/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceBaseUnitTest.php b/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceBaseUnitTest.php index 2f54129..9491e8a 100644 --- a/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceBaseUnitTest.php +++ b/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceBaseUnitTest.php @@ -9,7 +9,8 @@ namespace Drupal\Tests\payment_reference\Unit\Element { use Drupal\Component\Utility\Random; use Drupal\Core\Form\FormState; -use Drupal\payment_reference\Element\PaymentReferenceBase; + use Drupal\Core\Url; + use Drupal\payment_reference\Element\PaymentReferenceBase; use Drupal\Tests\UnitTestCase; /** @@ -150,111 +151,6 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { } /** - * @covers ::getTemporaryPaymentStorageKey - */ - public function testGetTemporaryPaymentStorageKey() { - $method = new \ReflectionMethod($this->element, 'getTemporaryPaymentStorageKey'); - $method->setAccessible(TRUE); - - $element = array( - '#name' => $this->randomMachineName(), - ); - $form_state = new FormState(); - - $key = $method->invoke($this->element, $element, $form_state); - $this->assertSame($key, $method->invoke($this->element, $element, $form_state)); - } - - /** - * @covers ::hasTemporaryPayment - * - * @depends testGetTemporaryPaymentStorageKey - */ - public function testHasTemporaryPayment() { - $has_method = new \ReflectionMethod($this->element, 'hasTemporaryPayment'); - $has_method->setAccessible(TRUE); - $get_key_method = new \ReflectionMethod($this->element, 'getTemporaryPaymentStorageKey'); - $get_key_method->setAccessible(TRUE); - - $element = array( - '#name' => $this->randomMachineName(), - ); - $form_state = new FormState(); - - $key = $get_key_method->invoke($this->element, $element, $form_state); - - // Use a random string instead of a boolean so we can assert the value. - $has = $this->randomMachineName(); - - $this->temporaryPaymentStorage->expects($this->once()) - ->method('has') - ->with($key) - ->willReturn($has); - - $this->assertSame($has, $has_method->invoke($this->element, $element, $form_state)); - } - - /** - * @covers ::getTemporaryPayment - * - * @depends testGetTemporaryPaymentStorageKey - */ - public function testGetTemporaryPayment() { - $has_method = new \ReflectionMethod($this->element, 'getTemporaryPayment'); - $has_method->setAccessible(TRUE); - $get_key_method = new \ReflectionMethod($this->element, 'getTemporaryPaymentStorageKey'); - $get_key_method->setAccessible(TRUE); - - $element = array( - '#name' => $this->randomMachineName(), - ); - $form_state = new FormState(); - - $key = $get_key_method->invoke($this->element, $element, $form_state); - - $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') - ->disableOriginalConstructor() - ->getMock(); - - $this->temporaryPaymentStorage->expects($this->once()) - ->method('get') - ->with($key) - ->willReturn($payment); - - $this->assertSame($payment, $has_method->invoke($this->element, $element, $form_state)); - } - - /** - * @covers ::SetTemporaryPayment - * - * @depends testGetTemporaryPaymentStorageKey - */ - public function testSetTemporaryPayment() { - $set_method = new \ReflectionMethod($this->element, 'setTemporaryPayment'); - $set_method->setAccessible(TRUE); - $get_method = new \ReflectionMethod($this->element, 'getTemporaryPaymentStorageKey'); - $get_method->setAccessible(TRUE); - - $element = array( - '#name' => $this->randomMachineName(), - ); - $form_state = new FormState(); - - $key = $get_method->invoke($this->element, $element, $form_state); - - $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') - ->disableOriginalConstructor() - ->getMock(); - - $this->temporaryPaymentStorage->expects($this->once()) - ->method('setWithExpire') - ->with($key, $payment, PaymentReferenceBase::KEY_VALUE_TTL) - ->willReturn($payment); - - $set_method->invoke($this->element, $element, $form_state, $payment); - } - - /** * @covers ::valueCallback */ public function testValueCallbackWithDefaultValue() { @@ -323,7 +219,7 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { * * @dataProvider providerTestPay */ - public function testPay($has_payment_execution_completed, $is_xml_http_request) { + public function testPay($has_completed, $is_xml_http_request) { $configuration = array(); $plugin_id = $this->randomMachineName(); $this->pluginDefinition = array(); @@ -361,29 +257,36 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { $request = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request') ->disableOriginalConstructor() ->getMock(); - $request->expects($has_payment_execution_completed ? $this->never() : $this->atLeastOnce()) + $request->expects($has_completed ? $this->never() : $this->atLeastOnce()) ->method('isXmlHttpRequest') ->willReturn($is_xml_http_request); - $this->requestStack->expects($has_payment_execution_completed ? $this->never() : $this->atLeastOnce()) + $this->requestStack->expects($has_completed ? $this->never() : $this->atLeastOnce()) ->method('getCurrentRequest') ->willReturn($request); + $result = $this->getMock('\Drupal\payment\PaymentExecutionResultInterface'); + $result->expects($this->atLeastOnce()) + ->method('hasCompleted') + ->willReturn($has_completed); + $payment_method = $this->getMock('\Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface'); - $payment_method->expects($this->atLeastOnce()) - ->method('isPaymentExecutionInterruptive') - ->willReturn($is_payment_execution_interruptive); + + $url = new Url($this->randomMachineName()); $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') ->disableOriginalConstructor() ->getMock(); - $payment->expects($is_payment_execution_interruptive ? $this->never() : $this->once()) - ->method('execute'); - $payment->expects($is_payment_execution_interruptive ? $this->never() : $this->once()) - ->method('save'); - $payment->expects($this->once()) + $payment->expects($this->atLeastOnce()) + ->method('execute') + ->willReturn($result); + $payment->expects($this->atLeastOnce()) ->method('setPaymentMethod') ->with($payment_method); + $payment->expects(!$has_completed && !$is_xml_http_request ? $this->atLeastOnce() : $this->never()) + ->method('urlInfo') + ->with('complete') + ->willReturn($url); $payment_method_selector = $this->getMock('\Drupal\payment\Plugin\Payment\MethodSelector\PaymentMethodSelectorInterface'); $payment_method_selector->expects($this->atLeastOnce()) @@ -407,9 +310,6 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { $this->element->expects($this->atLeastOnce()) ->method('getPaymentMethodSelector') ->willReturn($payment_method_selector); - $this->element->expects($is_payment_execution_interruptive ? $this->once() : $this->never()) - ->method('setTemporaryPayment') - ->with($form['foo']['bar'], $form_state, $payment); $this->element->pay($form, $form_state); } @@ -430,18 +330,29 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { * * @dataProvider providerTestAjaxPay */ - public function testAjaxPay($is_payment_execution_interruptive, $number_of_commands) { + public function testAjaxPay($has_completed, $number_of_commands) { $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') ->disableOriginalConstructor() ->getMock(); $payment->expects($this->atLeastOnce()) ->method('createDuplicate') ->willReturnSelf(); + $payment->expects($has_completed ? $this->never() : $this->atLeastOnce()) + ->method('urlInfo') + ->with('complete'); + + $result = $this->getMock('\Drupal\payment\PaymentExecutionResultInterface'); + $result->expects($this->atLeastOnce()) + ->method('hasCompleted') + ->willReturn($has_completed); $payment_method = $this->getMock('\Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface'); - $payment_method->expects($this->once()) - ->method('isPaymentExecutionInterruptive') - ->willReturn($is_payment_execution_interruptive); + $payment_method->expects($this->atLeastOnce()) + ->method('getPayment') + ->willReturn($payment); + $payment_method->expects($this->atLeastOnce()) + ->method('getPaymentExecutionResult') + ->willReturn($result); $payment_method_selector_plugin_id = $this->randomMachineName(); @@ -495,8 +406,8 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { */ public function providerTestAjaxPay() { return array( - array(TRUE, 2), - array(FALSE, 1), + array(TRUE, 1), + array(FALSE, 2), ); } @@ -670,7 +581,14 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { ); $form_state = $this->getMock('\Drupal\Core\Form\FormStateInterface'); + $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') + ->disableOriginalConstructor() + ->getMock(); + $payment_method = $this->getMock('\Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface'); + $payment_method->expects($this->atLeastOnce()) + ->method('getPayment') + ->willReturn($payment); $payment_method_selector = $this->getMock('\Drupal\payment\Plugin\Payment\MethodSelector\PaymentMethodSelectorInterface'); $payment_method_selector->expects($this->atLeastOnce()) @@ -798,11 +716,37 @@ class PaymentReferenceBaseUnitTest extends UnitTestCase { * @covers ::buildRefreshButton */ public function testBuildRefreshButton() { + $limit_allowed_payment_method_ids = [$this->randomMachineName()]; + + $payment_method_selector_id = $this->randomMachineName(); + + $payment_method_selector = $this->getMock('\Drupal\payment\Plugin\Payment\MethodSelector\PaymentMethodSelectorInterface'); + $payment_method_selector->expects($this->atLeastOnce()) + ->method('setAllowedPaymentMethodIds') + ->with($limit_allowed_payment_method_ids); + + $this->paymentMethodSelectorManager->expects($this->atLeastOnce()) + ->method('createInstance') + ->with($payment_method_selector_id) + ->willReturn($payment_method_selector); + + $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') + ->disableOriginalConstructor() + ->getMock(); + $payment->expects($this->atLeastOnce()) + ->method('createDuplicate') + ->willReturnSelf(); + $element = array( '#default_value' => mt_rand(), 'container' => array( '#id' => $this->randomMachineName(), ), + '#limit_allowed_payment_method_ids' => $limit_allowed_payment_method_ids, + '#name' => $this->randomMachineName(), + '#payment_method_selector_id' => $payment_method_selector_id, + '#prototype_payment' => $payment, + '#required' => (bool) mt_rand(0, 1), ); $form_state = $this->getMock('\Drupal\Core\Form\FormStateInterface'); diff --git a/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceUnitTest.php b/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceUnitTest.php index 381b549..6dedf9f 100644 --- a/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceUnitTest.php +++ b/modules/payment_reference/tests/src/Unit/Element/PaymentReferenceUnitTest.php @@ -76,13 +76,6 @@ class PaymentReferenceUnitTest extends UnitTestCase { protected $stringTranslation; /** - * The temporary payment storage. - * - * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $temporaryPaymentStorage; - - /** * The url generator. * * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject @@ -113,15 +106,13 @@ class PaymentReferenceUnitTest extends UnitTestCase { $this->stringTranslation = $this->getStringTranslationStub(); - $this->temporaryPaymentStorage = $this->getMock('\Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface'); - $this->urlGenerator = $this->getMock('\Drupal\Core\Routing\UrlGeneratorInterface'); $configuration = array(); $plugin_id = $this->randomMachineName(); $plugin_definition = array(); - $this->element = new PaymentReference($configuration, $plugin_id, $plugin_definition, $this->requestStack, $this->paymentStorage, $this->stringTranslation, $this->dateFormatter, $this->linkGenerator, $this->paymentMethodSelectorManager, new Random(), $this->temporaryPaymentStorage, $this->paymentQueue); + $this->element = new PaymentReference($configuration, $plugin_id, $plugin_definition, $this->requestStack, $this->paymentStorage, $this->stringTranslation, $this->dateFormatter, $this->linkGenerator, $this->paymentMethodSelectorManager, new Random(), $this->paymentQueue); } /** @@ -134,17 +125,10 @@ class PaymentReferenceUnitTest extends UnitTestCase { ->with('payment') ->willReturn($this->paymentStorage); - $key_value_expirable = $this->getMock('\Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface'); - $key_value_expirable->expects($this->once()) - ->method('get') - ->with('payment.payment_type.payment_reference') - ->willReturn($this->temporaryPaymentStorage); - $container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerInterface'); $map = array( array('date.formatter', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->dateFormatter), array('entity.manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $entity_manager), - array('keyvalue.expirable', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $key_value_expirable), array('link_generator', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->linkGenerator), array('payment_reference.queue', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->paymentQueue), array('plugin.manager.payment.method_selector', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->paymentMethodSelectorManager), @@ -172,13 +156,4 @@ class PaymentReferenceUnitTest extends UnitTestCase { $this->assertSame($this->paymentQueue, $method->invoke($this->element)); } - /** - * @covers ::getTemporaryPaymentStorage - */ - public function testGetTemporaryPaymentStorage() { - $method = new \ReflectionMethod($this->element, 'getTemporaryPaymentStorage'); - $method->setAccessible(TRUE); - $this->assertSame($this->temporaryPaymentStorage, $method->invoke($this->element)); - } - } diff --git a/modules/payment_reference/tests/src/Unit/Plugin/Payment/Type/PaymentReferenceUnitTest.php b/modules/payment_reference/tests/src/Unit/Plugin/Payment/Type/PaymentReferenceUnitTest.php index 4361bcf..b703778 100644 --- a/modules/payment_reference/tests/src/Unit/Plugin/Payment/Type/PaymentReferenceUnitTest.php +++ b/modules/payment_reference/tests/src/Unit/Plugin/Payment/Type/PaymentReferenceUnitTest.php @@ -8,12 +8,10 @@ namespace Drupal\Tests\payment_reference\Unit\Plugin\Payment\Type; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\payment_reference\Plugin\Payment\Type\PaymentReference; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\HttpKernel\KernelEvents; /** * @coversDefaultClass \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference @@ -235,6 +233,16 @@ class PaymentReferenceUnitTest extends UnitTestCase { * @covers ::doGetResumeContextResponse */ public function testDoGetResumeContextResponse() { + $url_generator = $this->getMock('\Drupal\Core\Routing\UrlGeneratorInterface'); + $url_generator->expects($this->atLeastOnce()) + ->method('generateFromRoute') + ->willReturn($this->randomMachineName()); + + $container = new ContainerBuilder(); + $container->set('url_generator', $url_generator); + + \Drupal::setContainer($container); + $response = $this->paymentType->getResumeContextResponse(); $this->assertInstanceOf('\Drupal\payment\ResponseInterface', $response); diff --git a/src/Plugin/Payment/Method/Unavailable.php b/src/Plugin/Payment/Method/Unavailable.php index 2b5fa09..3ae39ba 100644 --- a/src/Plugin/Payment/Method/Unavailable.php +++ b/src/Plugin/Payment/Method/Unavailable.php @@ -94,13 +94,6 @@ class Unavailable extends PluginBase implements PaymentMethodInterface { /** * {@inheritdoc} */ - public function getPaymentExecutionStatus() { - return FALSE; - } - - /** - * {@inheritdoc} - */ public function capturePaymentAccess(AccountInterface $account) { return FALSE; } diff --git a/tests/src/Unit/Controller/PaymentUnitTest.php b/tests/src/Unit/Controller/PaymentUnitTest.php index d9120ba..df1c35d 100644 --- a/tests/src/Unit/Controller/PaymentUnitTest.php +++ b/tests/src/Unit/Controller/PaymentUnitTest.php @@ -105,31 +105,36 @@ class PaymentUnitTest extends UnitTestCase { } /** - * @covers ::resume + * @covers ::complete */ - public function testResumeWithCompletedExecution() { - $status = TRUE; + public function testComplete() { + $symfony_response = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Response') + ->disableOriginalConstructor() + ->getMock(); + + $response = $this->getMock('\Drupal\payment\ResponseInterface'); + $response->expects($this->atLeastOnce()) + ->method('getResponse') + ->willReturn($symfony_response); + + $result = $this->getMock('\Drupal\payment\PaymentExecutionResultInterface'); + $result->expects($this->atLeastOnce()) + ->method('getCompletionResponse') + ->willReturn($response); $payment_method = $this->getMock('\Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface'); $payment_method->expects($this->atLeastOnce()) - ->method('getPaymentExecutionStatus') - ->willReturn($status); + ->method('getPaymentExecutionResult') + ->willReturn($result); $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') ->disableOriginalConstructor() ->getMock(); - $payment->expects($this->once()) - ->method('id') - ->will($this->returnValue($id)); + $payment->expects($this->atLeastOnce()) + ->method('getPaymentMethod') + ->willReturn($payment_method); - $this->stringTranslation->expects($this->any()) - ->method('translate') - ->with($string, array( - '!payment_id' => $id, - )) - ->will($this->returnArgument(0)); - - $this->assertSame($string, $this->controller->editTitle($payment)); + $this->assertSame($symfony_response, $this->controller->complete($payment)); } } diff --git a/tests/src/Unit/PaymentExecutionResultUnitTest.php b/tests/src/Unit/PaymentExecutionResultUnitTest.php index 1360986..61cbf60 100644 --- a/tests/src/Unit/PaymentExecutionResultUnitTest.php +++ b/tests/src/Unit/PaymentExecutionResultUnitTest.php @@ -45,10 +45,10 @@ class PaymentExecutionResultUnitTest extends UnitTestCase { */ function testHasCompleted() { $this->paymentExecutionResult = new PaymentExecutionResult(); - $this->assertFalse($this->paymentExecutionResult->hasCompleted()); + $this->assertTrue($this->paymentExecutionResult->hasCompleted()); $this->paymentExecutionResult = new PaymentExecutionResult($this->response); - $this->assertTrue($this->paymentExecutionResult->hasCompleted()); + $this->assertFalse($this->paymentExecutionResult->hasCompleted()); } /** diff --git a/tests/src/Unit/Plugin/Payment/Method/BasicUnitTest.php b/tests/src/Unit/Plugin/Payment/Method/BasicUnitTest.php index c5c1bcb..7533b2d 100644 --- a/tests/src/Unit/Plugin/Payment/Method/BasicUnitTest.php +++ b/tests/src/Unit/Plugin/Payment/Method/BasicUnitTest.php @@ -180,48 +180,6 @@ class BasicUnitTest extends PaymentMethodBaseUnitTestBase { } /** - * @covers ::getPaymentExecutionStatus - */ - public function testGetPaymentExecutionStatusWithCompletedExecution() { - $payment_status = $this->getMock('\Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface'); - $payment_status->expects($this->atLeastOnce()) - ->method('getPluginId') - ->willReturn($this->pluginDefinition['execute_status_id']); - - $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') - ->disableOriginalConstructor() - ->getMock(); - $payment->expects($this->once()) - ->method('getPaymentStatus') - ->willReturn($payment_status); - - $this->paymentMethod->setPayment($payment); - - $this->assertTrue($this->paymentMethod->getPaymentExecutionStatus()); - } - - /** - * @covers ::getPaymentExecutionStatus - */ - public function testGetPaymentExecutionStatusWithIncompleteExecution() { - $payment_status = $this->getMock('\Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface'); - $payment_status->expects($this->atLeastOnce()) - ->method('getPluginId') - ->willReturn($this->randomMachineName()); - - $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') - ->disableOriginalConstructor() - ->getMock(); - $payment->expects($this->once()) - ->method('getPaymentStatus') - ->willReturn($payment_status); - - $this->paymentMethod->setPayment($payment); - - $this->assertFalse($this->paymentMethod->getPaymentExecutionStatus()); - } - - /** * @covers ::doCapturePaymentAccess * * @dataProvider providerDoCapturePaymentAccess diff --git a/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTest.php b/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTest.php index e413504..fa45b8b 100644 --- a/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTest.php +++ b/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTest.php @@ -50,7 +50,7 @@ class PaymentMethodBaseUnitTest extends PaymentMethodBaseUnitTestBase { $this->moduleHandler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface'); $this->plugin = $this->getMockBuilder('\Drupal\payment\Plugin\Payment\Method\PaymentMethodBase') - ->setConstructorArgs(array(array(), '', $this->pluginDefinition, $this->moduleHandler, $this->eventDispatcher, $this->token)) + ->setConstructorArgs(array(array(), '', $this->pluginDefinition, $this->moduleHandler, $this->eventDispatcher, $this->token, $this->paymentStatusManager)) ->getMockForAbstractClass(); } @@ -62,6 +62,7 @@ class PaymentMethodBaseUnitTest extends PaymentMethodBaseUnitTestBase { $map = array( array('event_dispatcher', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->eventDispatcher), array('module_handler', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->moduleHandler), + array('plugin.manager.payment.status', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->paymentStatusManager), array('token', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->token), ); $container->expects($this->any()) @@ -205,6 +206,13 @@ class PaymentMethodBaseUnitTest extends PaymentMethodBaseUnitTestBase { * @covers ::executePayment */ public function testExecutePayment() { + $payment_status = $this->getMock('\Drupal\payment\Plugin\Payment\Status\PaymentStatusInterface'); + + $this->paymentStatusManager->expects($this->atLeastOnce()) + ->method('createInstance') + ->with('payment_pending') + ->willReturn($payment_status); + $payment = $this->getMockBuilder('\Drupal\payment\Entity\Payment') ->disableOriginalConstructor() ->getMock(); @@ -243,7 +251,7 @@ class PaymentMethodBaseUnitTest extends PaymentMethodBaseUnitTestBase { $this->pluginDefinition['active'] = $active; /** @var \Drupal\payment\Plugin\Payment\Method\PaymentMethodBase|\PHPUnit_Framework_MockObject_MockObject $payment_method */ $payment_method = $this->getMockBuilder('\Drupal\payment\Plugin\Payment\Method\PaymentMethodBase') - ->setConstructorArgs(array(array(), '', $this->pluginDefinition, $this->moduleHandler, $this->eventDispatcher, $this->token)) + ->setConstructorArgs(array(array(), '', $this->pluginDefinition, $this->moduleHandler, $this->eventDispatcher, $this->token, $this->paymentStatusManager)) ->setMethods(array('executePaymentAccessCurrency', 'executePaymentAccessEvent', 'doExecutePaymentAccess')) ->getMockForAbstractClass(); $payment_method->expects($this->any()) diff --git a/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTestBase.php b/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTestBase.php index a7e051c..e78ae23 100644 --- a/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTestBase.php +++ b/tests/src/Unit/Plugin/Payment/Method/PaymentMethodBaseUnitTestBase.php @@ -30,6 +30,13 @@ abstract class PaymentMethodBaseUnitTestBase extends UnitTestCase { protected $token; /** + * The payment status manager. + * + * @var \Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $paymentStatusManager; + + /** * The definition of the payment method plugin under test. * * @var array @@ -51,6 +58,8 @@ abstract class PaymentMethodBaseUnitTestBase extends UnitTestCase { $this->eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->paymentStatusManager = $this->getMock('\Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface'); + $this->token = $this->getMockBuilder('\Drupal\Core\Utility\Token') ->disableOriginalConstructor() ->getMock(); diff --git a/tests/src/Unit/Plugin/Payment/Method/UnavailableUnitTest.php b/tests/src/Unit/Plugin/Payment/Method/UnavailableUnitTest.php index 277ff73..0911671 100644 --- a/tests/src/Unit/Plugin/Payment/Method/UnavailableUnitTest.php +++ b/tests/src/Unit/Plugin/Payment/Method/UnavailableUnitTest.php @@ -181,11 +181,4 @@ class UnavailableUnitTest extends UnitTestCase { $this->plugin->capturePayment(); } - /** - * @covers ::getPaymentExecutionStatus - */ - public function testGetPaymentExecutionStatus() { - $this->assertInternalType('bool', $this->plugin->getPaymentExecutionStatus()); - } - } diff --git a/tests/src/Unit/ResponseUnitTest.php b/tests/src/Unit/ResponseUnitTest.php index 9496576..c6e4aae 100644 --- a/tests/src/Unit/ResponseUnitTest.php +++ b/tests/src/Unit/ResponseUnitTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\payment\Unit; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Url; use Drupal\payment\Response; use Drupal\Tests\UnitTestCase; @@ -73,6 +74,17 @@ class ResponseUnitTest extends UnitTestCase { * @covers ::__construct */ function testGetResponseWithAutomaticallyCreatedResponse() { + $url_generator = $this->getMock('\Drupal\Core\Routing\UrlGeneratorInterface'); + $url_generator->expects($this->atLeastOnce()) + ->method('generateFromRoute') + ->with($this->redirectUrl->getRouteName()) + ->willReturn($this->randomMachineName()); + + $container = new ContainerBuilder(); + $container->set('url_generator', $url_generator); + + \Drupal::setContainer($container); + $this->response = new Response($this->redirectUrl); $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $this->response->getResponse());