View source
<?php
namespace Drupal\Tests\payment_form\Functional\Plugin\Field\FieldFormatter;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\payment\Tests\Generate;
use Drupal\payment_form\Plugin\Payment\Type\PaymentForm;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
class PaymentFormWebTest extends BrowserTestBase {
protected $paymentMethod;
protected $paymentStorage;
protected $executeStatusPluginId = 'payment_pending';
protected $user;
public static $modules = [
'field',
'filter',
'payment',
'payment_form',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->paymentStorage = \Drupal::entityTypeManager()
->getStorage('payment');
$config_importer = \Drupal::service('currency.config_importer');
$config_importer
->importCurrency('EUR');
$field_name = strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'cardinality' => FieldStorageConfigInterface::CARDINALITY_UNLIMITED,
'entity_type' => 'user',
'field_name' => $field_name,
'type' => 'payment_form',
])
->save();
FieldConfig::create([
'bundle' => 'user',
'entity_type' => 'user',
'field_name' => $field_name,
'settings' => [
'currency_code' => 'EUR',
],
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('user', 'user', 'default')
->setComponent($field_name, [
'type' => 'payment_form',
])
->save();
$this->user = User::create([
'name' => $this
->randomString(),
'status' => TRUE,
]);
foreach (Generate::createPaymentLineItems() as $line_item) {
$this->user
->get($field_name)
->appendItem([
'plugin_id' => $line_item
->getPluginId(),
'plugin_configuration' => $line_item
->getConfiguration(),
]);
}
$this->user
->save();
$this->paymentMethod = Generate::createPaymentMethodConfiguration(2, 'payment_basic');
$this->paymentMethod
->setPluginConfiguration([
'execute_status_id' => $this->executeStatusPluginId,
]);
$this->paymentMethod
->save();
}
public function testFormatter() {
$this
->assertEqual(count($this->paymentStorage
->loadMultiple()), 0);
$user = $this
->drupalCreateUser([
'access user profiles',
]);
$this
->drupalLogin($user);
$path = 'user/' . $this->user
->id();
$this
->drupalPostForm($path, [], t('Pay'));
$this
->assertResponse('200');
$payment = $this->paymentStorage
->load(1);
if ($this
->assertTrue((bool) $payment)) {
$this
->assertTrue($payment
->getPaymentType() instanceof PaymentForm);
$this
->assertIdentical($payment
->getPaymentStatus()
->getPluginId(), $this->executeStatusPluginId);
}
}
}