Skip to content
Snippets Groups Projects
Verified Commit 5f77cf76 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2803621 by Mile23: Break BrowserTestBase & children dependency on...

Issue #2803621 by Mile23: Break BrowserTestBase & children dependency on Simpletest, deprecate stub BC traits
parent bd2227e4
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Showing
with 503 additions and 401 deletions
<?php
namespace Drupal\Tests\block\Traits;
use Drupal\block\Entity\Block;
/**
* Provides methods to create and place block with default settings.
*
* This trait is meant to be used only by test classes.
*/
trait BlockCreationTrait {
/**
* Creates a block instance based on default settings.
*
* @param string $plugin_id
* The plugin ID of the block type for this block instance.
* @param array $settings
* (optional) An associative array of settings for the block entity.
* Override the defaults by specifying the key and value in the array, for
* example:
* @code
* $this->drupalPlaceBlock('system_powered_by_block', array(
* 'label' => t('Hello, world!'),
* ));
* @endcode
* The following defaults are provided:
* - label: Random string.
* - ID: Random string.
* - region: 'sidebar_first'.
* - theme: The default theme.
* - visibility: Empty array.
*
* @return \Drupal\block\Entity\Block
* The block entity.
*
* @todo
* Add support for creating custom block instances.
*/
protected function placeBlock($plugin_id, array $settings = []) {
$config = \Drupal::configFactory();
$settings += [
'plugin' => $plugin_id,
'region' => 'sidebar_first',
'id' => strtolower($this->randomMachineName(8)),
'theme' => $config->get('system.theme')->get('default'),
'label' => $this->randomMachineName(8),
'visibility' => [],
'weight' => 0,
];
$values = [];
foreach (['region', 'id', 'theme', 'plugin', 'weight', 'visibility'] as $key) {
$values[$key] = $settings[$key];
// Remove extra values that do not belong in the settings array.
unset($settings[$key]);
}
foreach ($values['visibility'] as $id => $visibility) {
$values['visibility'][$id]['id'] = $id;
}
$values['settings'] = $settings;
$block = Block::create($values);
$block->save();
return $block;
}
}
<?php
namespace Drupal\Tests\node\Traits;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\node\Entity\NodeType;
use PHPUnit\Framework\TestCase;
/**
* Provides methods to create content type from given values.
*
* This trait is meant to be used only by test classes.
*/
trait ContentTypeCreationTrait {
/**
* Creates a custom content type based on default settings.
*
* @param array $values
* An array of settings to change from the defaults.
* Example: 'type' => 'foo'.
*
* @return \Drupal\node\Entity\NodeType
* Created content type.
*/
protected function createContentType(array $values = []) {
// Find a non-existent random type name.
if (!isset($values['type'])) {
do {
$id = strtolower($this->randomMachineName(8));
} while (NodeType::load($id));
}
else {
$id = $values['type'];
}
$values += [
'type' => $id,
'name' => $id,
];
$type = NodeType::create($values);
$status = $type->save();
node_add_body_field($type);
if ($this instanceof TestCase) {
$this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
}
else {
$this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
}
return $type;
}
}
<?php
namespace Drupal\Tests\node\Traits;
use Drupal\node\Entity\Node;
/**
* Provides methods to create node based on default settings.
*
* This trait is meant to be used only by test classes.
*/
trait NodeCreationTrait {
/**
* Get a node from the database based on its title.
*
* @param string|\Drupal\Component\Render\MarkupInterface $title
* A node title, usually generated by $this->randomMachineName().
* @param $reset
* (optional) Whether to reset the entity cache.
*
* @return \Drupal\node\NodeInterface
* A node entity matching $title.
*/
public function getNodeByTitle($title, $reset = FALSE) {
if ($reset) {
\Drupal::entityTypeManager()->getStorage('node')->resetCache();
}
// Cast MarkupInterface objects to string.
$title = (string) $title;
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties(['title' => $title]);
// Load the first node returned from the database.
$returned_node = reset($nodes);
return $returned_node;
}
/**
* Creates a node based on default settings.
*
* @param array $settings
* (optional) An associative array of settings for the node, as used in
* entity_create(). Override the defaults by specifying the key and value
* in the array, for example:
* @code
* $this->drupalCreateNode(array(
* 'title' => t('Hello, world!'),
* 'type' => 'article',
* ));
* @endcode
* The following defaults are provided:
* - body: Random string using the default filter format:
* @code
* $settings['body'][0] = array(
* 'value' => $this->randomMachineName(32),
* 'format' => filter_default_format(),
* );
* @endcode
* - title: Random string.
* - type: 'page'.
* - uid: The currently logged in user, or anonymous.
*
* @return \Drupal\node\NodeInterface
* The created node entity.
*/
protected function createNode(array $settings = []) {
// Populate defaults array.
$settings += [
'body' => [[
'value' => $this->randomMachineName(32),
'format' => filter_default_format(),
]],
'title' => $this->randomMachineName(8),
'type' => 'page',
'uid' => \Drupal::currentUser()->id(),
];
$node = Node::create($settings);
$node->save();
return $node;
}
}
...@@ -2,34 +2,18 @@ ...@@ -2,34 +2,18 @@
namespace Drupal\simpletest; namespace Drupal\simpletest;
use Drupal\Component\Render\MarkupInterface; use Drupal\Tests\AssertHelperTrait as BaseAssertHelperTrait;
/** /**
* Provides helper methods for assertions. * Provides helper methods for assertions.
*/
trait AssertHelperTrait {
/**
* Casts MarkupInterface objects into strings.
* *
* @param string|array $value * @deprecated in Drupal 8.4.x. Will be removed before Drupal 9.0.0. Use
* The value to act on. * Drupal\Tests\AssertHelperTrait instead.
* *
* @return mixed * @see https://www.drupal.org/node/2884454
* The input value, with MarkupInterface objects casted to string.
*/ */
protected static function castSafeStrings($value) { trait AssertHelperTrait {
if ($value instanceof MarkupInterface) {
$value = (string) $value; use BaseAssertHelperTrait;
}
if (is_array($value)) {
array_walk_recursive($value, function (&$item) {
if ($item instanceof MarkupInterface) {
$item = (string) $item;
}
});
}
return $value;
}
} }
...@@ -2,66 +2,20 @@ ...@@ -2,66 +2,20 @@
namespace Drupal\simpletest; namespace Drupal\simpletest;
use Drupal\block\Entity\Block; use Drupal\Tests\block\Traits\BlockCreationTrait as BaseBlockCreationTrait;
/** /**
* Provides methods to create and place block with default settings. * Provides methods to create and place block with default settings.
* *
* This trait is meant to be used only by test classes. * This trait is meant to be used only by test classes.
*/
trait BlockCreationTrait {
/**
* Creates a block instance based on default settings.
*
* @param string $plugin_id
* The plugin ID of the block type for this block instance.
* @param array $settings
* (optional) An associative array of settings for the block entity.
* Override the defaults by specifying the key and value in the array, for
* example:
* @code
* $this->drupalPlaceBlock('system_powered_by_block', array(
* 'label' => t('Hello, world!'),
* ));
* @endcode
* The following defaults are provided:
* - label: Random string.
* - ID: Random string.
* - region: 'sidebar_first'.
* - theme: The default theme.
* - visibility: Empty array.
* *
* @return \Drupal\block\Entity\Block * @deprecated in Drupal 8.4.x. Will be removed before Drupal 9.0.0. Use
* The block entity. * Drupal\Tests\AssertHelperTrait instead.
* *
* @todo * @see https://www.drupal.org/node/2884454
* Add support for creating custom block instances.
*/ */
protected function placeBlock($plugin_id, array $settings = []) { trait BlockCreationTrait {
$config = \Drupal::configFactory();
$settings += [ use BaseBlockCreationTrait;
'plugin' => $plugin_id,
'region' => 'sidebar_first',
'id' => strtolower($this->randomMachineName(8)),
'theme' => $config->get('system.theme')->get('default'),
'label' => $this->randomMachineName(8),
'visibility' => [],
'weight' => 0,
];
$values = [];
foreach (['region', 'id', 'theme', 'plugin', 'weight', 'visibility'] as $key) {
$values[$key] = $settings[$key];
// Remove extra values that do not belong in the settings array.
unset($settings[$key]);
}
foreach ($values['visibility'] as $id => $visibility) {
$values['visibility'][$id]['id'] = $id;
}
$values['settings'] = $settings;
$block = Block::create($values);
$block->save();
return $block;
}
} }
...@@ -2,53 +2,20 @@ ...@@ -2,53 +2,20 @@
namespace Drupal\simpletest; namespace Drupal\simpletest;
use Drupal\Component\Render\FormattableMarkup; use Drupal\Tests\node\Traits\ContentTypeCreationTrait as BaseContentTypeCreationTrait;
use Drupal\node\Entity\NodeType;
use PHPUnit\Framework\TestCase;
/** /**
* Provides methods to create content type from given values. * Provides methods to create content type from given values.
* *
* This trait is meant to be used only by test classes. * This trait is meant to be used only by test classes.
*/
trait ContentTypeCreationTrait {
/**
* Creates a custom content type based on default settings.
* *
* @param array $values * @deprecated in Drupal 8.4.x. Will be removed before Drupal 9.0.0. Use
* An array of settings to change from the defaults. * Drupal\Tests\ContentTypeCreationTrait instead.
* Example: 'type' => 'foo'.
* *
* @return \Drupal\node\Entity\NodeType * @see https://www.drupal.org/node/2884454
* Created content type.
*/ */
protected function createContentType(array $values = []) { trait ContentTypeCreationTrait {
// Find a non-existent random type name.
if (!isset($values['type'])) {
do {
$id = strtolower($this->randomMachineName(8));
} while (NodeType::load($id));
}
else {
$id = $values['type'];
}
$values += [
'type' => $id,
'name' => $id,
];
$type = NodeType::create($values);
$status = $type->save();
node_add_body_field($type);
if ($this instanceof TestCase) {
$this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
}
else {
$this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', ['%type' => $type->id()]))->__toString());
}
return $type; use BaseContentTypeCreationTrait;
}
} }
...@@ -2,83 +2,20 @@ ...@@ -2,83 +2,20 @@
namespace Drupal\simpletest; namespace Drupal\simpletest;
use Drupal\node\Entity\Node; use Drupal\Tests\node\Traits\NodeCreationTrait as BaseNodeCreationTrait;
/** /**
* Provides methods to create node based on default settings. * Provides methods to create node based on default settings.
* *
* This trait is meant to be used only by test classes. * This trait is meant to be used only by test classes.
*/
trait NodeCreationTrait {
/**
* Get a node from the database based on its title.
* *
* @param string|\Drupal\Component\Render\MarkupInterface $title * @deprecated in Drupal 8.4.x. Will be removed before Drupal 9.0.0. Use
* A node title, usually generated by $this->randomMachineName(). * Drupal\Tests\NodeCreationTrait instead.
* @param $reset
* (optional) Whether to reset the entity cache.
* *
* @return \Drupal\node\NodeInterface * @see https://www.drupal.org/node/2884454
* A node entity matching $title.
*/ */
public function getNodeByTitle($title, $reset = FALSE) { trait NodeCreationTrait {
if ($reset) {
\Drupal::entityTypeManager()->getStorage('node')->resetCache();
}
// Cast MarkupInterface objects to string.
$title = (string) $title;
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties(['title' => $title]);
// Load the first node returned from the database.
$returned_node = reset($nodes);
return $returned_node;
}
/**
* Creates a node based on default settings.
*
* @param array $settings
* (optional) An associative array of settings for the node, as used in
* entity_create(). Override the defaults by specifying the key and value
* in the array, for example:
* @code
* $this->drupalCreateNode(array(
* 'title' => t('Hello, world!'),
* 'type' => 'article',
* ));
* @endcode
* The following defaults are provided:
* - body: Random string using the default filter format:
* @code
* $settings['body'][0] = array(
* 'value' => $this->randomMachineName(32),
* 'format' => filter_default_format(),
* );
* @endcode
* - title: Random string.
* - type: 'page'.
* - uid: The currently logged in user, or anonymous.
*
* @return \Drupal\node\NodeInterface
* The created node entity.
*/
protected function createNode(array $settings = []) {
// Populate defaults array.
$settings += [
'body' => [[
'value' => $this->randomMachineName(32),
'format' => filter_default_format(),
]],
'title' => $this->randomMachineName(8),
'type' => 'page',
'uid' => \Drupal::currentUser()->id(),
];
$node = Node::create($settings);
$node->save();
return $node; use BaseNodeCreationTrait;
}
} }
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Drupal\Core\Test\TestDatabase; use Drupal\Core\Test\TestDatabase;
use Drupal\Core\Test\TestSetupTrait; use Drupal\Core\Test\TestSetupTrait;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Drupal\Tests\AssertHelperTrait as BaseAssertHelperTrait;
use Drupal\Tests\ConfigTestTrait; use Drupal\Tests\ConfigTestTrait;
use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\RandomGeneratorTrait;
use Drupal\Tests\SessionTestTrait; use Drupal\Tests\SessionTestTrait;
...@@ -24,10 +25,10 @@ ...@@ -24,10 +25,10 @@
*/ */
abstract class TestBase { abstract class TestBase {
use BaseAssertHelperTrait;
use TestSetupTrait; use TestSetupTrait;
use SessionTestTrait; use SessionTestTrait;
use RandomGeneratorTrait; use RandomGeneratorTrait;
use AssertHelperTrait;
use GeneratePermutationsTrait; use GeneratePermutationsTrait;
// For backwards compatibility switch the visbility of the methods to public. // For backwards compatibility switch the visbility of the methods to public.
use ConfigTestTrait { use ConfigTestTrait {
......
...@@ -2,11 +2,7 @@ ...@@ -2,11 +2,7 @@
namespace Drupal\simpletest; namespace Drupal\simpletest;
use Drupal\Component\Utility\SafeMarkup; use Drupal\Tests\user\Traits\UserCreationTrait as BaseUserCreationTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
/** /**
* Provides methods to create additional test users and switch the currently * Provides methods to create additional test users and switch the currently
...@@ -14,201 +10,14 @@ ...@@ -14,201 +10,14 @@
* *
* This trait is meant to be used only by test classes extending * This trait is meant to be used only by test classes extending
* \Drupal\simpletest\TestBase. * \Drupal\simpletest\TestBase.
*/
trait UserCreationTrait {
/**
* Switch the current logged in user.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user account object.
*/
protected function setCurrentUser(AccountInterface $account) {
\Drupal::currentUser()->setAccount($account);
}
/**
* Create a user with a given set of permissions.
*
* @param array $permissions
* Array of permission names to assign to user. Note that the user always
* has the default permissions derived from the "authenticated users" role.
* @param string $name
* The user name.
* @param bool $admin
* (optional) Whether the user should be an administrator
* with all the available permissions.
*
* @return \Drupal\user\Entity\User|false
* A fully loaded user object with pass_raw property, or FALSE if account
* creation fails.
*/
protected function createUser(array $permissions = [], $name = NULL, $admin = FALSE) {
// Create a role with the given permission set, if any.
$rid = FALSE;
if ($permissions) {
$rid = $this->createRole($permissions);
if (!$rid) {
return FALSE;
}
}
// Create a user assigned to that role.
$edit = [];
$edit['name'] = !empty($name) ? $name : $this->randomMachineName();
$edit['mail'] = $edit['name'] . '@example.com';
$edit['pass'] = user_password();
$edit['status'] = 1;
if ($rid) {
$edit['roles'] = [$rid];
}
if ($admin) {
$edit['roles'][] = $this->createAdminRole();
}
$account = User::create($edit);
$account->save();
$this->assertTrue($account->id(), SafeMarkup::format('User created with name %name and pass %pass', ['%name' => $edit['name'], '%pass' => $edit['pass']]), 'User login');
if (!$account->id()) {
return FALSE;
}
// Add the raw password so that we can log in as this user.
$account->pass_raw = $edit['pass'];
// Support BrowserTestBase as well.
$account->passRaw = $account->pass_raw;
return $account;
}
/**
* Creates an administrative role.
*
* @param string $rid
* (optional) The role ID (machine name). Defaults to a random name.
* @param string $name
* (optional) The label for the role. Defaults to a random string.
* @param int $weight
* (optional) The weight for the role. Defaults NULL so that entity_create()
* sets the weight to maximum + 1.
*
* @return string
* Role ID of newly created role, or FALSE if role creation failed.
*/
protected function createAdminRole($rid = NULL, $name = NULL, $weight = NULL) {
$rid = $this->createRole([], $rid, $name, $weight);
if ($rid) {
/** @var \Drupal\user\RoleInterface $role */
$role = Role::load($rid);
$role->setIsAdmin(TRUE);
$role->save();
}
return $rid;
}
/**
* Creates a role with specified permissions.
*
* @param array $permissions
* Array of permission names to assign to role.
* @param string $rid
* (optional) The role ID (machine name). Defaults to a random name.
* @param string $name
* (optional) The label for the role. Defaults to a random string.
* @param int $weight
* (optional) The weight for the role. Defaults NULL so that entity_create()
* sets the weight to maximum + 1.
*
* @return string
* Role ID of newly created role, or FALSE if role creation failed.
*/
protected function createRole(array $permissions, $rid = NULL, $name = NULL, $weight = NULL) {
// Generate a random, lowercase machine name if none was passed.
if (!isset($rid)) {
$rid = strtolower($this->randomMachineName(8));
}
// Generate a random label.
if (!isset($name)) {
// In the role UI role names are trimmed and random string can start or
// end with a space.
$name = trim($this->randomString(8));
}
// Check the all the permissions strings are valid.
if (!$this->checkPermissions($permissions)) {
return FALSE;
}
// Create new role.
$role = Role::create([
'id' => $rid,
'label' => $name,
]);
if (isset($weight)) {
$role->set('weight', $weight);
}
$result = $role->save();
$this->assertIdentical($result, SAVED_NEW, SafeMarkup::format('Created role ID @rid with name @name.', [
'@name' => var_export($role->label(), TRUE),
'@rid' => var_export($role->id(), TRUE),
]), 'Role');
if ($result === SAVED_NEW) {
// Grant the specified permissions to the role, if any.
if (!empty($permissions)) {
$this->grantPermissions($role, $permissions);
$assigned_permissions = Role::load($role->id())->getPermissions();
$missing_permissions = array_diff($permissions, $assigned_permissions);
if (!$missing_permissions) {
$this->pass(SafeMarkup::format('Created permissions: @perms', ['@perms' => implode(', ', $permissions)]), 'Role');
}
else {
$this->fail(SafeMarkup::format('Failed to create permissions: @perms', ['@perms' => implode(', ', $missing_permissions)]), 'Role');
}
}
return $role->id();
}
else {
return FALSE;
}
}
/**
* Checks whether a given list of permission names is valid.
* *
* @param array $permissions * @deprecated in Drupal 8.4.x. Will be removed before Drupal 9.0.0. Use
* The permission names to check. * Drupal\Tests\UserCreationTrait instead.
* *
* @return bool * @see https://www.drupal.org/node/2884454
* TRUE if the permissions are valid, FALSE otherwise.
*/ */
protected function checkPermissions(array $permissions) { trait UserCreationTrait {
$available = array_keys(\Drupal::service('user.permissions')->getPermissions());
$valid = TRUE;
foreach ($permissions as $permission) {
if (!in_array($permission, $available)) {
$this->fail(SafeMarkup::format('Invalid permission %permission.', ['%permission' => $permission]), 'Role');
$valid = FALSE;
}
}
return $valid;
}
/** use BaseUserCreationTrait;
* Grant permissions to a user role.
*
* @param \Drupal\user\RoleInterface $role
* The ID of a user role to alter.
* @param array $permissions
* (optional) A list of permission names to grant.
*/
protected function grantPermissions(RoleInterface $role, array $permissions) {
foreach ($permissions as $permission) {
$role->grantPermission($permission);
}
$role->trustData()->save();
}
} }
...@@ -17,8 +17,12 @@ ...@@ -17,8 +17,12 @@
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\Tests\EntityViewTrait; use Drupal\Tests\EntityViewTrait;
use Drupal\Tests\block\Traits\BlockCreationTrait as BaseBlockCreationTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\Traits\Core\CronRunTrait; use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\Tests\TestFileCreationTrait; use Drupal\Tests\TestFileCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\XdebugRequestTrait; use Drupal\Tests\XdebugRequestTrait;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
...@@ -36,7 +40,7 @@ abstract class WebTestBase extends TestBase { ...@@ -36,7 +40,7 @@ abstract class WebTestBase extends TestBase {
compareFiles as drupalCompareFiles; compareFiles as drupalCompareFiles;
} }
use AssertPageCacheContextsAndTagsTrait; use AssertPageCacheContextsAndTagsTrait;
use BlockCreationTrait { use BaseBlockCreationTrait {
placeBlock as drupalPlaceBlock; placeBlock as drupalPlaceBlock;
} }
use ContentTypeCreationTrait { use ContentTypeCreationTrait {
......
<?php
namespace Drupal\Tests\user\Traits;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
/**
* Provides methods to create additional test users and switch the currently
* logged in one.
*
* This trait is meant to be used only by test classes.
*/
trait UserCreationTrait {
/**
* Switch the current logged in user.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user account object.
*/
protected function setCurrentUser(AccountInterface $account) {
\Drupal::currentUser()->setAccount($account);
}
/**
* Create a user with a given set of permissions.
*
* @param array $permissions
* Array of permission names to assign to user. Note that the user always
* has the default permissions derived from the "authenticated users" role.
* @param string $name
* The user name.
* @param bool $admin
* (optional) Whether the user should be an administrator
* with all the available permissions.
*
* @return \Drupal\user\Entity\User|false
* A fully loaded user object with pass_raw property, or FALSE if account
* creation fails.
*/
protected function createUser(array $permissions = [], $name = NULL, $admin = FALSE) {
// Create a role with the given permission set, if any.
$rid = FALSE;
if ($permissions) {
$rid = $this->createRole($permissions);
if (!$rid) {
return FALSE;
}
}
// Create a user assigned to that role.
$edit = [];
$edit['name'] = !empty($name) ? $name : $this->randomMachineName();
$edit['mail'] = $edit['name'] . '@example.com';
$edit['pass'] = user_password();
$edit['status'] = 1;
if ($rid) {
$edit['roles'] = [$rid];
}
if ($admin) {
$edit['roles'][] = $this->createAdminRole();
}
$account = User::create($edit);
$account->save();
$this->assertTrue($account->id(), SafeMarkup::format('User created with name %name and pass %pass', ['%name' => $edit['name'], '%pass' => $edit['pass']]), 'User login');
if (!$account->id()) {
return FALSE;
}
// Add the raw password so that we can log in as this user.
$account->pass_raw = $edit['pass'];
// Support BrowserTestBase as well.
$account->passRaw = $account->pass_raw;
return $account;
}
/**
* Creates an administrative role.
*
* @param string $rid
* (optional) The role ID (machine name). Defaults to a random name.
* @param string $name
* (optional) The label for the role. Defaults to a random string.
* @param int $weight
* (optional) The weight for the role. Defaults NULL so that entity_create()
* sets the weight to maximum + 1.
*
* @return string
* Role ID of newly created role, or FALSE if role creation failed.
*/
protected function createAdminRole($rid = NULL, $name = NULL, $weight = NULL) {
$rid = $this->createRole([], $rid, $name, $weight);
if ($rid) {
/** @var \Drupal\user\RoleInterface $role */
$role = Role::load($rid);
$role->setIsAdmin(TRUE);
$role->save();
}
return $rid;
}
/**
* Creates a role with specified permissions.
*
* @param array $permissions
* Array of permission names to assign to role.
* @param string $rid
* (optional) The role ID (machine name). Defaults to a random name.
* @param string $name
* (optional) The label for the role. Defaults to a random string.
* @param int $weight
* (optional) The weight for the role. Defaults NULL so that entity_create()
* sets the weight to maximum + 1.
*
* @return string
* Role ID of newly created role, or FALSE if role creation failed.
*/
protected function createRole(array $permissions, $rid = NULL, $name = NULL, $weight = NULL) {
// Generate a random, lowercase machine name if none was passed.
if (!isset($rid)) {
$rid = strtolower($this->randomMachineName(8));
}
// Generate a random label.
if (!isset($name)) {
// In the role UI role names are trimmed and random string can start or
// end with a space.
$name = trim($this->randomString(8));
}
// Check the all the permissions strings are valid.
if (!$this->checkPermissions($permissions)) {
return FALSE;
}
// Create new role.
$role = Role::create([
'id' => $rid,
'label' => $name,
]);
if (isset($weight)) {
$role->set('weight', $weight);
}
$result = $role->save();
$this->assertIdentical($result, SAVED_NEW, SafeMarkup::format('Created role ID @rid with name @name.', [
'@name' => var_export($role->label(), TRUE),
'@rid' => var_export($role->id(), TRUE),
]), 'Role');
if ($result === SAVED_NEW) {
// Grant the specified permissions to the role, if any.
if (!empty($permissions)) {
$this->grantPermissions($role, $permissions);
$assigned_permissions = Role::load($role->id())->getPermissions();
$missing_permissions = array_diff($permissions, $assigned_permissions);
if (!$missing_permissions) {
$this->pass(SafeMarkup::format('Created permissions: @perms', ['@perms' => implode(', ', $permissions)]), 'Role');
}
else {
$this->fail(SafeMarkup::format('Failed to create permissions: @perms', ['@perms' => implode(', ', $missing_permissions)]), 'Role');
}
}
return $role->id();
}
else {
return FALSE;
}
}
/**
* Checks whether a given list of permission names is valid.
*
* @param array $permissions
* The permission names to check.
*
* @return bool
* TRUE if the permissions are valid, FALSE otherwise.
*/
protected function checkPermissions(array $permissions) {
$available = array_keys(\Drupal::service('user.permissions')->getPermissions());
$valid = TRUE;
foreach ($permissions as $permission) {
if (!in_array($permission, $available)) {
$this->fail(SafeMarkup::format('Invalid permission %permission.', ['%permission' => $permission]), 'Role');
$valid = FALSE;
}
}
return $valid;
}
/**
* Grant permissions to a user role.
*
* @param \Drupal\user\RoleInterface $role
* The ID of a user role to alter.
* @param array $permissions
* (optional) A list of permission names to grant.
*/
protected function grantPermissions(RoleInterface $role, array $permissions) {
foreach ($permissions as $permission) {
$role->grantPermission($permission);
}
$role->trustData()->save();
}
}
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Test\TestDatabase; use Drupal\Core\Test\TestDatabase;
use Drupal\simpletest\AssertContentTrait; use Drupal\simpletest\AssertContentTrait;
use Drupal\simpletest\AssertHelperTrait; use Drupal\Tests\AssertHelperTrait;
use Drupal\Tests\ConfigTestTrait; use Drupal\Tests\ConfigTestTrait;
use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\RandomGeneratorTrait;
use Drupal\simpletest\TestServiceProvider; use Drupal\simpletest\TestServiceProvider;
......
<?php
namespace Drupal\Tests;
use Drupal\Component\Render\MarkupInterface;
/**
* Provides helper methods for assertions.
*/
trait AssertHelperTrait {
/**
* Casts MarkupInterface objects into strings.
*
* @param string|array $value
* The value to act on.
*
* @return mixed
* The input value, with MarkupInterface objects casted to string.
*/
protected static function castSafeStrings($value) {
if ($value instanceof MarkupInterface) {
$value = (string) $value;
}
if (is_array($value)) {
array_walk_recursive($value, function (&$item) {
if ($item instanceof MarkupInterface) {
$item = (string) $item;
}
});
}
return $value;
}
}
<?php <?php
/** namespace Drupal\Tests;
* @file
* Contains \Drupal\Tests\simpletest\Unit\AssertHelperTraitTest.
*/
namespace Drupal\Tests\simpletest\Unit;
use Drupal\Core\Render\Markup; use Drupal\Core\Render\Markup;
use Drupal\simpletest\AssertHelperTrait;
use Drupal\Tests\UnitTestCase;
/** /**
* @coversDefaultClass \Drupal\simpletest\AssertHelperTrait * @coversDefaultClass \Drupal\Tests\AssertHelperTrait
* @group simpletest * @group simpletest
* @group Tests
*/ */
class AssertHelperTraitTest extends UnitTestCase { class AssertHelperTraitTest extends UnitTestCase {
......
...@@ -19,11 +19,10 @@ ...@@ -19,11 +19,10 @@
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Drupal\FunctionalTests\AssertLegacyTrait; use Drupal\FunctionalTests\AssertLegacyTrait;
use Drupal\simpletest\AssertHelperTrait; use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\simpletest\BlockCreationTrait; use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\simpletest\ContentTypeCreationTrait; use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\simpletest\NodeCreationTrait; use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\simpletest\UserCreationTrait;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment