View source
<?php
namespace Drupal\Tests\transaction\Functional;
use Drupal\transaction\Entity\TransactionOperation;
use Drupal\transaction\Entity\TransactionType;
class TransactionAdminTest extends FunctionalTransactionTestBase {
protected $adminUser;
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer transaction types',
'administer entity_test fields',
'administer transaction fields',
'access administration pages',
'view the administration theme',
]);
$this
->drupalLogin($this->adminUser);
$block_options = [
'region' => 'content',
];
$this
->drupalPlaceBlock('page_title_block', $block_options);
$this
->drupalPlaceBlock('local_tasks_block', $block_options);
$this
->drupalPlaceBlock('local_actions_block', $block_options);
}
public function testAdmin() {
$this
->doTestAddBasicTransactionType();
$this
->doTestSetTransactionTypeFieldCreation();
$this
->doTestTransactorConfiguration();
$this
->doTestAddTargetEntityTypeLocalTask();
$this
->doTestRemoveTransactionType();
}
public function doTestAddBasicTransactionType() {
$this
->drupalGet('admin/config/workflow/transaction');
$this
->clickLink('Add transaction type');
$transactor = 'transaction_generic';
$target_entity_type = 'entity_test';
$post = [
'target_entity_type' => $target_entity_type,
'transactor' => $transactor,
];
$this
->drupalPostForm(NULL, $post, 'Continue');
$label = 'Generic workflow';
$id = 'generic_workflow';
$post = [
'label' => $label,
'id' => $id,
];
$this
->drupalPostForm(NULL, $post, 'Create transaction type');
$transaction_type = TransactionType::load($id);
$this
->assertNotNull($transaction_type);
$this
->assertEqual($transaction_type
->label(), $label);
$this
->assertEqual($transaction_type
->getTargetEntityTypeId(), $target_entity_type);
$this
->assertEqual($transaction_type
->getPluginId(), $transactor);
$transactor_plugin = $transaction_type
->getPlugin();
$this
->assertEmpty($transactor_plugin
->getConfiguration());
}
public function doTestSetTransactionTypeFieldCreation() {
$post = [
'log_message' => '_create',
'log_message_field_name' => 'log_message',
'last_transaction' => '_create',
'last_transaction_label' => 'Last transaction',
'last_transaction_field_name' => 'last_transaction',
];
$this
->drupalPostForm('admin/config/workflow/transaction/edit/generic_workflow', $post, 'Save transaction type');
$entity_field_manager = \Drupal::service('entity_field.manager');
$fields = $entity_field_manager
->getFieldDefinitions('transaction', 'generic_workflow');
$this
->assertTrue(isset($fields['field_log_message']));
$fields = $entity_field_manager
->getFieldDefinitions('entity_test', 'basic');
$this
->assertTrue(isset($fields['field_last_transaction']));
}
public function doTestTransactorConfiguration() {
$post = [
'execution' => '3',
];
$this
->drupalPostForm('admin/config/workflow/transaction/edit/generic_workflow', $post, 'Save transaction type');
$expected_plugin_configuration = [
'log_message' => 'field_log_message',
'last_transaction' => 'field_last_transaction',
'execution' => '3',
];
$transaction_type = TransactionType::load('generic_workflow');
$transactor_plugin = $transaction_type
->getPlugin();
$this
->assertEquals($transactor_plugin
->getConfiguration(), $expected_plugin_configuration);
}
public function doTestAddTargetEntityTypeLocalTask() {
$post = [
'local_task' => TRUE,
];
$this
->drupalPostForm('admin/config/workflow/transaction/edit/generic_workflow', $post, 'Save transaction type');
$transaction_type = TransactionType::load('generic_workflow');
$transaction_type_options = $transaction_type
->getOptions();
$this
->assertNotEmpty($transaction_type_options['local_task']);
$route_provider = \Drupal::service('router.route_provider');
$route = $route_provider
->getRouteByName('entity.entity_test.generic_workflow-transaction');
$this
->assertNotNull($route);
}
public function doTestAddTransactionOperation() {
$this
->drupalGet('admin/config/workflow/transaction/edit/generic_workflow/operation');
$this
->assertSession()
->pageTextContains('No transaction operations available.');
$this
->clickLink('Add transaction operation');
$post = [
'label' => 'Test operation',
'id' => 'test_operation',
'description' => 'Transaction operation description',
'details' => 'Details line line 1' . PHP_EOL . 'Details line line 2',
];
$this
->drupalPostForm(NULL, $post, 'Save transaction operation');
$this
->assertSession()
->pageTextContains('Transaction operation Test operation has been added.');
$transaction_operation = TransactionOperation::load('test_operation');
$this
->assertEquals($transaction_operation
->getTransactionTypeId(), 'generic_workflow');
$this
->assertEquals($transaction_operation
->label(), $post['label']);
$this
->assertEquals($transaction_operation
->getDescription(), $post['description']);
$details = explode(PHP_EOL, $post['details']);
$this
->assertEquals($transaction_operation
->getDetails(), $details);
}
public function doTestRemoveTransactionType() {
$this
->drupalGet('admin/config/workflow/transaction/delete/generic_workflow');
$this
->assertSession()
->pageTextContains('Are you sure you want to delete Generic workflow?');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertSession()
->pageTextContains('Transaction type Generic workflow deleted.');
$this
->assertEmpty(TransactionType::loadMultiple());
$this
->assertEmpty(TransactionOperation::loadMultiple());
}
}