NodeActionsConfigurationTest.php in Drupal 9
File
core/modules/node/tests/src/Functional/NodeActionsConfigurationTest.php
View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
use Drupal\system\Entity\Action;
use Drupal\user\Entity\User;
class NodeActionsConfigurationTest extends BrowserTestBase {
protected static $modules = [
'action',
'node',
];
protected $defaultTheme = 'stark';
public function testAssignOwnerNodeActionConfiguration() {
$user = $this
->drupalCreateUser([
'administer actions',
]);
$this
->drupalLogin($user);
$edit = [];
$edit['action'] = 'node_assign_owner_action';
$this
->drupalGet('admin/config/system/actions');
$this
->submitForm($edit, 'Create');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [];
$action_label = $this
->randomMachineName();
$edit['label'] = $action_label;
$edit['id'] = strtolower($action_label);
$edit['owner_uid'] = $user
->id();
$this
->drupalGet('admin/config/system/actions/add/node_assign_owner_action');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$action_id = $edit['id'];
$this
->assertSession()
->pageTextContains('The action has been successfully saved.');
$this
->assertSession()
->pageTextContains($action_label);
$this
->clickLink('Configure');
$edit = [];
$new_action_label = $this
->randomMachineName();
$edit['label'] = $new_action_label;
$edit['owner_uid'] = $user
->id();
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The action has been successfully saved.');
$this
->assertSession()
->pageTextNotContains($action_label);
$this
->assertSession()
->pageTextContains($new_action_label);
$this
->drupalGet('admin/config/system/actions');
$this
->clickLink('Delete');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [];
$this
->submitForm($edit, 'Delete');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains("The action {$new_action_label} has been deleted.");
$this
->drupalGet('admin/config/system/actions');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains($new_action_label);
$action = Action::load($action_id);
$this
->assertNull($action, 'The node_assign_owner_action action is not available after being deleted.');
}
public function testAssignOwnerNodeActionAutocomplete() {
for ($i = 0; $i < 200; $i++) {
$this
->drupalCreateUser();
}
$this
->drupalLogin($this
->drupalCreateUser([
'administer actions',
'administer users',
]));
$this
->drupalGet('admin/config/system/actions');
$this
->submitForm([
'action' => 'node_assign_owner_action',
], 'Create');
$autocomplete_field = $this
->getSession()
->getPage()
->findField('owner_uid');
$autocomplete_url = $this
->getAbsoluteUrl($autocomplete_field
->getAttribute('data-autocomplete-path'));
$user = $this
->drupalCreateUser();
$data = Json::decode($this
->drupalGet($autocomplete_url, [
'query' => [
'q' => $user
->getDisplayName(),
'_format' => 'json',
],
]));
$this
->assertNotEmpty($data);
$anonymous = User::getAnonymousUser();
$this
->assertNotNull($anonymous);
$data = Json::decode($this
->drupalGet($autocomplete_url, [
'query' => [
'q' => $anonymous
->getDisplayName(),
'_format' => 'json',
],
]));
$this
->assertEmpty($data);
}
}