Skip to content

Commit 75b64d5

Browse files
committed
Issue #2958865 by czigor: Call to undefined method Drupal\commerce_payment\PaymentOptionsBuilder::t()
1 parent 4250f83 commit 75b64d5

File tree

6 files changed

+75
-14
lines changed

6 files changed

+75
-14
lines changed

modules/payment/commerce_payment.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ services:
2323

2424
commerce_payment.options_builder:
2525
class: Drupal\commerce_payment\PaymentOptionsBuilder
26-
arguments: ['@entity_type.manager']
26+
arguments: ['@entity_type.manager', '@string_translation']

modules/payment/src/PaymentOptionsBuilder.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use Drupal\commerce_order\Entity\OrderInterface;
77
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface;
88
use Drupal\Core\Entity\EntityTypeManagerInterface;
9+
use Drupal\Core\StringTranslation\StringTranslationTrait;
10+
use Drupal\Core\StringTranslation\TranslationInterface;
911

1012
class PaymentOptionsBuilder implements PaymentOptionsBuilderInterface {
1113

14+
use StringTranslationTrait;
15+
1216
/**
1317
* The entity type manager.
1418
*
@@ -21,9 +25,12 @@ class PaymentOptionsBuilder implements PaymentOptionsBuilderInterface {
2125
*
2226
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
2327
* The entity type manager.
28+
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
29+
* The string translation.
2430
*/
25-
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
31+
public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
2632
$this->entityTypeManager = $entity_type_manager;
33+
$this->stringTranslation = $string_translation;
2734
}
2835

2936
/**

modules/payment/tests/modules/commerce_payment_test/commerce_payment_test.info.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ core: 8.x
44
type: module
55
dependencies:
66
- commerce:commerce_payment
7+
- commerce:commerce_payment_example
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
commerce_payment.commerce_payment_gateway.plugin.test_onsite:
2+
type: commerce_payment_gateway_configuration
3+
mapping:
4+
api_key:
5+
type: string
6+
label: 'API key'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Drupal\commerce_payment_test\Plugin\Commerce\PaymentGateway;
4+
5+
use Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway\Onsite;
6+
7+
/**
8+
* Provides the Test on-site payment gateway.
9+
*
10+
* This is a copy of example_onsite with a different display_label.
11+
*
12+
* @CommercePaymentGateway(
13+
* id = "test_onsite",
14+
* label = "Test (On-site)",
15+
* display_label = "Test",
16+
* forms = {
17+
* "add-payment-method" = "Drupal\commerce_payment_example\PluginForm\Onsite\PaymentMethodAddForm",
18+
* },
19+
* payment_method_types = {"credit_card"},
20+
* credit_card_types = {
21+
* "amex", "dinersclub", "discover", "jcb", "maestro", "mastercard", "visa",
22+
* },
23+
* )
24+
*/
25+
class TestOnsite extends Onsite {}

modules/payment/tests/src/Kernel/PaymentOptionsBuilderTest.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class PaymentOptionsBuilderTest extends CommerceKernelTestBase {
4848
'commerce_order',
4949
'commerce_payment',
5050
'commerce_payment_example',
51+
'commerce_payment_test',
5152
];
5253

5354
/**
@@ -257,21 +258,42 @@ public function testBuildOptions() {
257258
}
258259

259260
/**
260-
* Tests building options for a specific gateway.
261+
* Tests building options for two different on-site gateways.
262+
*
263+
* Confirms that the payment gateway list can be restricted, and that
264+
* multiple on-site gateways get unique "add" option labels.
261265
*
262266
* @covers ::buildOptions
263267
*/
264-
public function testBuildOptionsForSpecificGateway() {
265-
$payment_gateway = PaymentGateway::load('cash_on_delivery');
266-
$options = $this->paymentOptionsBuilder->buildOptions($this->order, [$payment_gateway]);
267-
$this->assertCount(1, $options);
268-
269-
$option = reset($options);
270-
$this->assertEquals('cash_on_delivery', $option->getId());
271-
$this->assertEquals('Cash on delivery', $option->getLabel());
272-
$this->assertEquals('cash_on_delivery', $option->getPaymentGatewayId());
273-
$this->assertNull($option->getPaymentMethodId());
274-
$this->assertNull($option->getPaymentMethodTypeId());
268+
public function testBuildOptionsWithTwoOnsiteGateways() {
269+
$first_payment_gateway = PaymentGateway::create([
270+
'id' => 'first_onsite',
271+
'label' => 'First (On-site)',
272+
'plugin' => 'example_onsite',
273+
]);
274+
$second_payment_gateway = PaymentGateway::create([
275+
'id' => 'second_onsite',
276+
'label' => 'Second (On-site)',
277+
'plugin' => 'test_onsite',
278+
]);
279+
$second_payment_gateway->save();
280+
$payment_gateways = [$first_payment_gateway, $second_payment_gateway];
281+
$options = $this->paymentOptionsBuilder->buildOptions($this->order, $payment_gateways);
282+
/** @var \Drupal\commerce_payment\PaymentOption[] $options */
283+
$options = array_values($options);
284+
$this->assertCount(2, $options);
285+
286+
$this->assertEquals('new--credit_card--first_onsite', $options[0]->getId());
287+
$this->assertEquals('New credit card (Example)', $options[0]->getLabel());
288+
$this->assertEquals('first_onsite', $options[0]->getPaymentGatewayId());
289+
$this->assertNull($options[0]->getPaymentMethodId());
290+
$this->assertEquals('credit_card', $options[0]->getPaymentMethodTypeId());
291+
292+
$this->assertEquals('new--credit_card--second_onsite', $options[1]->getId());
293+
$this->assertEquals('New credit card (Test)', $options[1]->getLabel());
294+
$this->assertEquals('second_onsite', $options[1]->getPaymentGatewayId());
295+
$this->assertNull($options[1]->getPaymentMethodId());
296+
$this->assertEquals('credit_card', $options[1]->getPaymentMethodTypeId());
275297
}
276298

277299
/**

0 commit comments

Comments
 (0)