public function PaymentMethodTest::testPaymentMethodCreationAndUpdate in Commerce Core 8.2
Tests creating and updating a payment method.
File
- modules/
payment/ tests/ src/ Functional/ PaymentMethodTest.php, line 99
Class
- PaymentMethodTest
- Tests the payment method UI.
Namespace
Drupal\Tests\commerce_payment\FunctionalCode
public function testPaymentMethodCreationAndUpdate() {
$default_address = [
'country_code' => 'US',
'administrative_area' => 'SC',
'locality' => 'Greenville',
'postal_code' => '29616',
'address_line1' => '9 Drupal Ave',
'given_name' => 'Bryan',
'family_name' => 'Centarro',
];
$default_profile = $this
->createEntity('profile', [
'type' => 'customer',
'uid' => $this->user
->id(),
'address' => $default_address,
]);
/** @var \Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway\OnsiteInterface $plugin */
$this
->drupalGet($this->collectionUrl);
$this
->getSession()
->getPage()
->clickLink('Add payment method');
$this
->assertSession()
->addressEquals($this->collectionUrl . '/add');
$rendered_address = $default_address;
// Note that the full country name is rendered (not just the country code).
$rendered_address['country_code'] = 'United States';
// Confirm that the default profile's address is rendered.
foreach ($rendered_address as $property => $value) {
$prefix = 'add_payment_method[billing_information][address][0][address]';
$this
->assertSession()
->pageTextContains($value);
$this
->assertSession()
->fieldNotExists($prefix . '[' . $property . ']');
}
$form_values = [
'add_payment_method[payment_details][number]' => '4111111111111111',
'add_payment_method[payment_details][expiration][month]' => '01',
'add_payment_method[payment_details][expiration][year]' => date('Y') + 1,
'add_payment_method[payment_details][security_code]' => '111',
];
$this
->submitForm($form_values, 'Save');
$this
->assertSession()
->addressEquals($this->collectionUrl);
$this
->assertSession()
->pageTextContains('Visa ending in 1111 saved to your payment methods.');
$payment_method = PaymentMethod::load(1);
$billing_profile = $payment_method
->getBillingProfile();
$this
->assertEquals($this->user
->id(), $payment_method
->getOwnerId());
$this
->assertEquals($default_address, array_filter($billing_profile
->get('address')
->first()
->getValue()));
$this
->assertEquals(2, $payment_method
->getBillingProfile()
->id());
$this
->drupalGet($this->collectionUrl . '/' . $payment_method
->id() . '/edit');
// Confirm that the default profile's address is rendered.
foreach ($rendered_address as $property => $value) {
$prefix = 'add_payment_method[billing_information][address][0][address]';
$this
->assertSession()
->pageTextContains($value);
$this
->assertSession()
->fieldNotExists($prefix . '[' . $property . ']');
}
$this
->getSession()
->getPage()
->pressButton('billing_edit');
$form_values = [
'payment_method[payment_details][expiration][month]' => '02',
'payment_method[payment_details][expiration][year]' => '2026',
'payment_method[billing_information][address][0][address][given_name]' => 'Johnny',
'payment_method[billing_information][address][0][address][family_name]' => 'Appleseed',
'payment_method[billing_information][address][0][address][address_line1]' => '123 New York Drive',
'payment_method[billing_information][address][0][address][locality]' => 'New York City',
'payment_method[billing_information][address][0][address][administrative_area]' => 'NY',
'payment_method[billing_information][address][0][address][postal_code]' => '10001',
];
$this
->submitForm($form_values, 'Save');
$this
->assertSession()
->addressEquals($this->collectionUrl);
$this
->assertSession()
->pageTextContains('2/2026');
\Drupal::entityTypeManager()
->getStorage('commerce_payment_method')
->resetCache([
1,
]);
\Drupal::entityTypeManager()
->getStorage('profile')
->resetCache([
2,
]);
$payment_method = PaymentMethod::load(1);
$this
->assertEquals('2026', $payment_method
->get('card_exp_year')->value);
/** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
$billing_profile = $payment_method
->getBillingProfile();
$this
->assertEquals($this->user
->id(), $payment_method
->getOwnerId());
$this
->assertEquals('NY', $billing_profile
->get('address')
->first()
->getAdministrativeArea());
$this
->assertEquals(2, $payment_method
->getBillingProfile()
->id());
// Confirm that the address book profile was updated.
$default_profile = $this
->reloadEntity($default_profile);
$this
->assertTrue($billing_profile
->get('address')
->equals($default_profile
->get('address')));
}