commerce_donation_flow-1.0.x-dev/tests/src/FunctionalJavascript/CommerceDonationFlowInteractiveFeaturesTest.php
tests/src/FunctionalJavascript/CommerceDonationFlowInteractiveFeaturesTest.php
<?php
namespace Drupal\Tests\commerce_donation_flow\FunctionalJavascript;
use Behat\Mink\Element\NodeElement;
use Drupal\commerce_payment\Entity\PaymentGateway;
use Drupal\Tests\commerce\FunctionalJavascript\CommerceWebDriverTestBase;
use Drupal\user\Entity\Role;
/**
* Test donation.js and notify.js.
*/
class CommerceDonationFlowInteractiveFeaturesTest extends CommerceWebDriverTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'system',
'block',
'field',
'commerce',
'commerce_price',
'commerce_store',
'commerce_product',
'commerce_order',
'commerce_cart',
'commerce_checkout',
'commerce_payment',
'commerce_donation_flow',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function getAdministratorPermissions() {
return array_merge(
[
'make donation',
],
parent::getAdministratorPermissions()
);
}
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->store->set('billing_countries', ['US']);
$this->store->save();
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway = PaymentGateway::create([
'id' => 'manual',
'label' => 'Manual',
'plugin' => 'manual',
'configuration' => [
'display_label' => 'Cash on delivery',
'instructions' => [
'value' => 'Sample payment instructions.',
'format' => 'plain_text',
],
],
]);
$payment_gateway->save();
/** @var \Drupal\commerce_order\Entity\OrderType $defaultOrders */
$defaultOrders = $this->container->get('entity_type.manager')
->getStorage('commerce_order_type')
->load('default');
// Use our donation flow.
$defaultOrders->setThirdPartySetting('commerce_checkout', 'checkout_flow', 'donation_flow');
$defaultOrders->save();
}
/**
* Test JS features added to checkout panes.
*/
public function testJsFeatures() {
$this->drupalLogout();
$this->grantPermissions(Role::load(Role::ANONYMOUS_ID), ['make donation']);
// Test donation.js:
$this->drupalGet('/donate');
$designated = $this->waitForAppearance('input[data-drupal-selector="edit-commerce-donation-pane-field-designated-value"]');
if (!($designated instanceof NodeElement)) {
throw new \UnexpectedValueException('Designated checkbox not found');
}
$designated->click();
$button = $this->getSession()->getPage()->find('css', 'input[data-drupal-selector="edit-actions-next"]');
if (!($button instanceof NodeElement)) {
throw new \UnexpectedValueException('Pane submit button not found');
}
$this->getSession()->wait(2000);
$this->assertEquals('Continue to Dedication', $button->getValue());
$button->click();
$recipientFieldSet = $this->waitForAppearance('fieldset.commerce-memorial-pane--recipient');
if (!($recipientFieldSet instanceof NodeElement)) {
throw new \UnexpectedValueException('Designated checkbox not found');
}
$this->assertFalse($recipientFieldSet->isVisible());
$notify = $this->waitForAppearance('input[data-drupal-selector="edit-commerce-memorial-pane-field-notify-value"]');
if (!($notify instanceof NodeElement)) {
throw new \UnexpectedValueException('Notify checkbox not found');
}
$notify->click();
$this->getSession()->wait(2000);
$this->assertTrue($recipientFieldSet->isVisible());
}
/**
* Helper method that does not screen for visibility.
*
* @param string $selector
* CSS selector.
*
* @return ?NodeElement
* The element if found.
*/
protected function waitForAppearance(string $selector): ?NodeElement {
$page = $this->getSession()->getPage();
return $page->waitFor(10,
function () use ($selector, $page) {
return $page->find('css', $selector);
});
}
}
