View source
<?php
namespace Drupal\Tests\transaction\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\transaction\Entity\Transaction;
use Drupal\transaction\Entity\TransactionOperation;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
abstract class KernelTransactionTestBase extends KernelTestBase {
public static $modules = [
'system',
'user',
'filter',
'text',
'field',
'dynamic_entity_reference',
'token',
'transaction',
'entity_test',
];
protected $transactionType;
protected $transactionOperation;
protected $transaction;
protected $targetEntity;
protected $logMessage = 'Log message';
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('transaction');
$this
->installEntitySchema('user');
$this
->installConfig([
'system',
'user',
'transaction',
]);
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('administer transaction types')
->grantPermission('administer transactions')
->save();
$this
->prepareTransactionLogMessageField();
$this
->prepareTransactionType();
$this
->prepareTransactionOperation();
$this
->prepareTargetEntityLastTransactionField();
$this
->prepareTargetEntity();
$this
->prepareTransaction();
}
protected abstract function prepareTransactionType();
protected function prepareTransactionOperation() {
$this->transactionOperation = TransactionOperation::create([
'id' => 'test_operation',
'transaction_type' => $this->transactionType
->id(),
'description' => '[transaction:type] #[transaction:id]',
'details' => [
'Executed by UID: [transaction:executor:target_id]',
'Transaction UUID: [transaction:uuid]',
],
]);
$this->transactionOperation
->save();
}
protected function prepareTransaction() {
$this->transaction = Transaction::create([
'type' => $this->transactionType
->id(),
'target_entity' => $this->targetEntity,
'field_log_message' => $this->logMessage,
]);
}
protected function prepareTargetEntityLastTransactionField() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_last_transaction',
'type' => 'entity_reference',
'entity_type' => 'entity_test',
'settings' => [
'target_type' => 'transaction',
],
]);
$field_storage
->save();
FieldConfig::create([
'label' => 'Last transaction',
'field_storage' => $field_storage,
'bundle' => 'entity_test',
])
->save();
}
protected function prepareTargetEntity() {
$this->targetEntity = EntityTest::create([
'name' => 'Target entity test',
]);
$this->targetEntity
->save();
}
protected function prepareTransactionLogMessageField() {
FieldStorageConfig::create([
'field_name' => 'field_log_message',
'type' => 'string',
'entity_type' => 'transaction',
])
->save();
}
protected function addTransactionLogMessageField() {
FieldConfig::create([
'field_name' => 'field_log_message',
'entity_type' => 'transaction',
'bundle' => $this->transactionType
->id(),
])
->save();
}
}