Update to Drupal 8.1.1. For more information, see https://www.drupal.org/node/2718713
This commit is contained in:
parent
c0a0d5a94c
commit
9eae24d844
669 changed files with 3873 additions and 1553 deletions
|
@ -0,0 +1,157 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel\Condition;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the user role condition.
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserRoleConditionTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* The condition plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Condition\ConditionManager
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* An anonymous user for testing purposes.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $anonymous;
|
||||
|
||||
/**
|
||||
* An authenticated user for testing purposes.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $authenticated;
|
||||
|
||||
/**
|
||||
* A custom role for testing purposes.
|
||||
*
|
||||
* @var \Drupal\user\Entity\RoleInterface
|
||||
*/
|
||||
protected $role;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'user', 'field');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installSchema('system', 'sequences');
|
||||
$this->installEntitySchema('user');
|
||||
|
||||
$this->manager = $this->container->get('plugin.manager.condition');
|
||||
|
||||
// Set up the authenticated and anonymous roles.
|
||||
Role::create(array(
|
||||
'id' => RoleInterface::ANONYMOUS_ID,
|
||||
'label' => 'Anonymous user',
|
||||
))->save();
|
||||
Role::create(array(
|
||||
'id' => RoleInterface::AUTHENTICATED_ID,
|
||||
'label' => 'Authenticated user',
|
||||
))->save();
|
||||
|
||||
// Create new role.
|
||||
$rid = strtolower($this->randomMachineName(8));
|
||||
$label = $this->randomString(8);
|
||||
$role = Role::create(array(
|
||||
'id' => $rid,
|
||||
'label' => $label,
|
||||
));
|
||||
$role->save();
|
||||
$this->role = $role;
|
||||
|
||||
// Setup an anonymous user for our tests.
|
||||
$this->anonymous = User::create(array(
|
||||
'name' => '',
|
||||
'uid' => 0,
|
||||
));
|
||||
$this->anonymous->save();
|
||||
// Loading the anonymous user adds the correct role.
|
||||
$this->anonymous = User::load($this->anonymous->id());
|
||||
|
||||
// Setup an authenticated user for our tests.
|
||||
$this->authenticated = User::create(array(
|
||||
'name' => $this->randomMachineName(),
|
||||
));
|
||||
$this->authenticated->save();
|
||||
// Add the custom role.
|
||||
$this->authenticated->addRole($this->role->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the user_role condition.
|
||||
*/
|
||||
public function testConditions() {
|
||||
// Grab the user role condition and configure it to check against
|
||||
// authenticated user roles.
|
||||
/** @var $condition \Drupal\Core\Condition\ConditionInterface */
|
||||
$condition = $this->manager->createInstance('user_role')
|
||||
->setConfig('roles', array(RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID))
|
||||
->setContextValue('user', $this->anonymous);
|
||||
$this->assertFalse($condition->execute(), 'Anonymous users fail role checks for authenticated.');
|
||||
// Check for the proper summary.
|
||||
// Summaries require an extra space due to negate handling in summary().
|
||||
$this->assertEqual($condition->summary(), 'The user is a member of Authenticated user');
|
||||
|
||||
// Set the user role to anonymous.
|
||||
$condition->setConfig('roles', array(RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID));
|
||||
$this->assertTrue($condition->execute(), 'Anonymous users pass role checks for anonymous.');
|
||||
// Check for the proper summary.
|
||||
$this->assertEqual($condition->summary(), 'The user is a member of Anonymous user');
|
||||
|
||||
// Set the user role to check anonymous or authenticated.
|
||||
$condition->setConfig('roles', array(RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID));
|
||||
$this->assertTrue($condition->execute(), 'Anonymous users pass role checks for anonymous or authenticated.');
|
||||
// Check for the proper summary.
|
||||
$this->assertEqual($condition->summary(), 'The user is a member of Anonymous user, Authenticated user');
|
||||
|
||||
// Set the context to the authenticated user and check that they also pass
|
||||
// against anonymous or authenticated roles.
|
||||
$condition->setContextValue('user', $this->authenticated);
|
||||
$this->assertTrue($condition->execute(), 'Authenticated users pass role checks for anonymous or authenticated.');
|
||||
|
||||
// Set the role to just authenticated and recheck.
|
||||
$condition->setConfig('roles', array(RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID));
|
||||
$this->assertTrue($condition->execute(), 'Authenticated users pass role checks for authenticated.');
|
||||
|
||||
// Test Constructor injection.
|
||||
$condition = $this->manager->createInstance('user_role', array('roles' => array(RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID), 'context' => array('user' => $this->authenticated)));
|
||||
$this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.');
|
||||
|
||||
// Check the negated summary.
|
||||
$condition->setConfig('negate', TRUE);
|
||||
$this->assertEqual($condition->summary(), 'The user is not a member of Authenticated user');
|
||||
|
||||
// Check the complex negated summary.
|
||||
$condition->setConfig('roles', array(RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID));
|
||||
$this->assertEqual($condition->summary(), 'The user is not a member of Anonymous user, Authenticated user');
|
||||
|
||||
// Check a custom role.
|
||||
$condition->setConfig('roles', array($this->role->id() => $this->role->id()));
|
||||
$condition->setConfig('negate', FALSE);
|
||||
$this->assertTrue($condition->execute(), 'Authenticated user is a member of the custom role.');
|
||||
$this->assertEqual($condition->summary(), SafeMarkup::format('The user is a member of @roles', array('@roles' => $this->role->label())));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel\Field;
|
||||
|
||||
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* Tests the user_name formatter.
|
||||
*
|
||||
* @group field
|
||||
*/
|
||||
class UserNameFormatterTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['field', 'user', 'system'];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $entityType;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $bundle;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldName;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installConfig(['field']);
|
||||
$this->installEntitySchema('user');
|
||||
$this->installSchema('system', ['sequences']);
|
||||
|
||||
$this->entityType = 'user';
|
||||
$this->bundle = $this->entityType;
|
||||
$this->fieldName = 'name';
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders fields of a given entity with a given display.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
|
||||
* The entity object with attached fields to render.
|
||||
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
|
||||
* The display to render the fields in.
|
||||
*
|
||||
* @return string
|
||||
* The rendered entity fields.
|
||||
*/
|
||||
protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
|
||||
$content = $display->build($entity);
|
||||
$content = $this->render($content);
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the formatter output.
|
||||
*/
|
||||
public function testFormatter() {
|
||||
$user = User::create([
|
||||
'name' => 'test name',
|
||||
]);
|
||||
$user->save();
|
||||
|
||||
$result = $user->{$this->fieldName}->view(['type' => 'user_name']);
|
||||
$this->assertEqual('username', $result[0]['#theme']);
|
||||
$this->assertEqual(spl_object_hash($user), spl_object_hash($result[0]['#account']));
|
||||
|
||||
$result = $user->{$this->fieldName}->view(['type' => 'user_name', 'settings' => ['link_to_entity' => FALSE]]);
|
||||
$this->assertEqual($user->getDisplayName(), $result[0]['#markup']);
|
||||
|
||||
$user = User::getAnonymousUser();
|
||||
|
||||
$result = $user->{$this->fieldName}->view(['type' => 'user_name']);
|
||||
$this->assertEqual('username', $result[0]['#theme']);
|
||||
$this->assertEqual(spl_object_hash($user), spl_object_hash($result[0]['#account']));
|
||||
|
||||
$result = $user->{$this->fieldName}->view(['type' => 'user_name', 'settings' => ['link_to_entity' => FALSE]]);
|
||||
$this->assertEqual($user->getDisplayName(), $result[0]['#markup']);
|
||||
$this->assertEqual($this->config('user.settings')->get('anonymous'), $result[0]['#markup']);
|
||||
}
|
||||
|
||||
}
|
|
@ -51,7 +51,7 @@ class MigrateUserProfileEntityFormDisplayTest extends MigrateDrupal6TestBase {
|
|||
// Test that a checkbox field has the proper display label setting.
|
||||
$component = $display->getComponent('profile_love_migrations');
|
||||
$this->assertIdentical('boolean_checkbox', $component['type']);
|
||||
$this->assertIdentical(true, $component['settings']['display_label']);
|
||||
$this->assertIdentical(TRUE, $component['settings']['display_label']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ class MigrateUserConfigsTest extends MigrateDrupal6TestBase {
|
|||
$this->assertIdentical('Account details for [user:name] at [site:name] (approved)', $config->get('status_activated.subject'));
|
||||
$this->assertIdentical("[user:name],\n\nYour account at [site:name] has been activated.\n\nYou may now log in by clicking on this link or copying and pasting it in your browser:\n\n[user:one-time-login-url]\n\nThis is a one-time login, so it can be used only once.\n\nAfter logging in, you will be redirected to [user:edit-url] so you can change your password.\n\nOnce you have set your own password, you will be able to log in to [site:login-url] in the future using:\n\nusername: [user:name]\n", $config->get('status_activated.body'));
|
||||
$this->assertIdentical('Replacement login information for [user:name] at [site:name]', $config->get('password_reset.subject'));
|
||||
$this->assertIdentical("[user:name],\n\nA request to reset the password for your account has been made at [site:name].\n\nYou may now log in to [site:url-brief] by clicking on this link or copying and pasting it in your browser:\n\n[user:one-time-login-url]\n\nThis is a one-time login, so it can be used only once. It expires after one day and nothing will happen if it's not used.\n\nAfter logging in, you will be redirected to [user:edit-url] so you can change your password." , $config->get('password_reset.body'));
|
||||
$this->assertIdentical("[user:name],\n\nA request to reset the password for your account has been made at [site:name].\n\nYou may now log in to [site:url-brief] by clicking on this link or copying and pasting it in your browser:\n\n[user:one-time-login-url]\n\nThis is a one-time login, so it can be used only once. It expires after one day and nothing will happen if it's not used.\n\nAfter logging in, you will be redirected to [user:edit-url] so you can change your password.", $config->get('password_reset.body'));
|
||||
$this->assertIdentical('Account details for [user:name] at [site:name] (deleted)', $config->get('cancel_confirm.subject'));
|
||||
$this->assertIdentical("[user:name],\n\nYour account on [site:name] has been deleted.", $config->get('cancel_confirm.body'));
|
||||
$this->assertIdentical('An administrator created an account for you at [site:name]', $config->get('register_admin_created.subject'));
|
||||
|
|
146
core/modules/user/tests/src/Kernel/TempStoreDatabaseTest.php
Normal file
146
core/modules/user/tests/src/Kernel/TempStoreDatabaseTest.php
Normal file
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\Core\KeyValueStore\KeyValueExpirableFactory;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\SharedTempStoreFactory;
|
||||
use Drupal\Core\Lock\DatabaseLockBackend;
|
||||
use Drupal\Core\Database\Database;
|
||||
|
||||
/**
|
||||
* Tests the temporary object storage system.
|
||||
*
|
||||
* @group user
|
||||
* @see \Drupal\Core\TempStore\TempStore.
|
||||
*/
|
||||
class TempStoreDatabaseTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'user');
|
||||
|
||||
/**
|
||||
* A key/value store factory.
|
||||
*
|
||||
* @var \Drupal\user\SharedTempStoreFactory
|
||||
*/
|
||||
protected $storeFactory;
|
||||
|
||||
/**
|
||||
* The name of the key/value collection to set and retrieve.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $collection;
|
||||
|
||||
/**
|
||||
* An array of (fake) user IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $users = array();
|
||||
|
||||
/**
|
||||
* An array of random stdClass objects.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $objects = array();
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Install system tables to test the key/value storage without installing a
|
||||
// full Drupal environment.
|
||||
$this->installSchema('system', array('key_value_expire'));
|
||||
|
||||
// Create several objects for testing.
|
||||
for ($i = 0; $i <= 3; $i++) {
|
||||
$this->objects[$i] = $this->randomObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the UserTempStore API.
|
||||
*/
|
||||
public function testUserTempStore() {
|
||||
// Create a key/value collection.
|
||||
$factory = new SharedTempStoreFactory(new KeyValueExpirableFactory(\Drupal::getContainer()), new DatabaseLockBackend(Database::getConnection()), $this->container->get('request_stack'));
|
||||
$collection = $this->randomMachineName();
|
||||
|
||||
// Create two mock users.
|
||||
for ($i = 0; $i <= 1; $i++) {
|
||||
$users[$i] = mt_rand(500, 5000000);
|
||||
|
||||
// Storing the SharedTempStore objects in a class member variable causes a
|
||||
// fatal exception, because in that situation garbage collection is not
|
||||
// triggered until the test class itself is destructed, after tearDown()
|
||||
// has deleted the database tables. Store the objects locally instead.
|
||||
$stores[$i] = $factory->get($collection, $users[$i]);
|
||||
}
|
||||
|
||||
$key = $this->randomMachineName();
|
||||
// Test that setIfNotExists() succeeds only the first time.
|
||||
for ($i = 0; $i <= 1; $i++) {
|
||||
// setIfNotExists() should be TRUE the first time (when $i is 0) and
|
||||
// FALSE the second time (when $i is 1).
|
||||
$this->assertEqual(!$i, $stores[0]->setIfNotExists($key, $this->objects[$i]));
|
||||
$metadata = $stores[0]->getMetadata($key);
|
||||
$this->assertEqual($users[0], $metadata->owner);
|
||||
$this->assertIdenticalObject($this->objects[0], $stores[0]->get($key));
|
||||
// Another user should get the same result.
|
||||
$metadata = $stores[1]->getMetadata($key);
|
||||
$this->assertEqual($users[0], $metadata->owner);
|
||||
$this->assertIdenticalObject($this->objects[0], $stores[1]->get($key));
|
||||
}
|
||||
|
||||
// Remove the item and try to set it again.
|
||||
$stores[0]->delete($key);
|
||||
$stores[0]->setIfNotExists($key, $this->objects[1]);
|
||||
// This time it should succeed.
|
||||
$this->assertIdenticalObject($this->objects[1], $stores[0]->get($key));
|
||||
|
||||
// This user can update the object.
|
||||
$stores[0]->set($key, $this->objects[2]);
|
||||
$this->assertIdenticalObject($this->objects[2], $stores[0]->get($key));
|
||||
// The object is the same when another user loads it.
|
||||
$this->assertIdenticalObject($this->objects[2], $stores[1]->get($key));
|
||||
|
||||
// This user should be allowed to get, update, delete.
|
||||
$this->assertTrue($stores[0]->getIfOwner($key) instanceof \stdClass);
|
||||
$this->assertTrue($stores[0]->setIfOwner($key, $this->objects[1]));
|
||||
$this->assertTrue($stores[0]->deleteIfOwner($key));
|
||||
|
||||
// Another user can update the object and become the owner.
|
||||
$stores[1]->set($key, $this->objects[3]);
|
||||
$this->assertIdenticalObject($this->objects[3], $stores[0]->get($key));
|
||||
$this->assertIdenticalObject($this->objects[3], $stores[1]->get($key));
|
||||
$metadata = $stores[1]->getMetadata($key);
|
||||
$this->assertEqual($users[1], $metadata->owner);
|
||||
|
||||
// The first user should be informed that the second now owns the data.
|
||||
$metadata = $stores[0]->getMetadata($key);
|
||||
$this->assertEqual($users[1], $metadata->owner);
|
||||
|
||||
// The first user should no longer be allowed to get, update, delete.
|
||||
$this->assertNull($stores[0]->getIfOwner($key));
|
||||
$this->assertFalse($stores[0]->setIfOwner($key, $this->objects[1]));
|
||||
$this->assertFalse($stores[0]->deleteIfOwner($key));
|
||||
|
||||
// Now manually expire the item (this is not exposed by the API) and then
|
||||
// assert it is no longer accessible.
|
||||
db_update('key_value_expire')
|
||||
->fields(array('expire' => REQUEST_TIME - 1))
|
||||
->condition('collection', "user.shared_tempstore.$collection")
|
||||
->condition('name', $key)
|
||||
->execute();
|
||||
$this->assertFalse($stores[0]->get($key));
|
||||
$this->assertFalse($stores[1]->get($key));
|
||||
}
|
||||
|
||||
}
|
146
core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php
Normal file
146
core/modules/user/tests/src/Kernel/UserAccountFormFieldsTest.php
Normal file
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Verifies that the field order in user account forms is compatible with
|
||||
* password managers of web browsers.
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserAccountFormFieldsTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'user', 'field');
|
||||
|
||||
/**
|
||||
* Tests the root user account form section in the "Configure site" form.
|
||||
*/
|
||||
function testInstallConfigureForm() {
|
||||
require_once \Drupal::root() . '/core/includes/install.core.inc';
|
||||
require_once \Drupal::root() . '/core/includes/install.inc';
|
||||
$install_state = install_state_defaults();
|
||||
$form_state = new FormState();
|
||||
$form_state->addBuildInfo('args', [&$install_state]);
|
||||
$form = $this->container->get('form_builder')
|
||||
->buildForm('Drupal\Core\Installer\Form\SiteConfigureForm', $form_state);
|
||||
|
||||
// Verify name and pass field order.
|
||||
$this->assertFieldOrder($form['admin_account']['account']);
|
||||
|
||||
// Verify that web browsers may autocomplete the email value and
|
||||
// autofill/prefill the name and pass values.
|
||||
foreach (array('mail', 'name', 'pass') as $key) {
|
||||
$this->assertFalse(isset($form['account'][$key]['#attributes']['autocomplete']), "'$key' field: 'autocomplete' attribute not found.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the user registration form.
|
||||
*/
|
||||
function testUserRegistrationForm() {
|
||||
// Install default configuration; required for AccountFormController.
|
||||
$this->installConfig(array('user'));
|
||||
|
||||
// Disable email confirmation to unlock the password field.
|
||||
$this->config('user.settings')
|
||||
->set('verify_mail', FALSE)
|
||||
->save();
|
||||
|
||||
$form = $this->buildAccountForm('register');
|
||||
|
||||
// Verify name and pass field order.
|
||||
$this->assertFieldOrder($form['account']);
|
||||
|
||||
// Verify that web browsers may autocomplete the email value and
|
||||
// autofill/prefill the name and pass values.
|
||||
foreach (array('mail', 'name', 'pass') as $key) {
|
||||
$this->assertFalse(isset($form['account'][$key]['#attributes']['autocomplete']), "'$key' field: 'autocomplete' attribute not found.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the user edit form.
|
||||
*/
|
||||
function testUserEditForm() {
|
||||
// Install default configuration; required for AccountFormController.
|
||||
$this->installConfig(array('user'));
|
||||
|
||||
// Install the router table and then rebuild.
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
|
||||
$form = $this->buildAccountForm('default');
|
||||
|
||||
// Verify name and pass field order.
|
||||
$this->assertFieldOrder($form['account']);
|
||||
|
||||
// Verify that autocomplete is off on all account fields.
|
||||
foreach (array('mail', 'name', 'pass') as $key) {
|
||||
$this->assertIdentical($form['account'][$key]['#attributes']['autocomplete'], 'off', "'$key' field: 'autocomplete' attribute is 'off'.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the 'name' form element is directly before the 'pass' element.
|
||||
*
|
||||
* @param array $elements
|
||||
* A form array section that contains the user account form elements.
|
||||
*/
|
||||
protected function assertFieldOrder(array $elements) {
|
||||
$name_index = 0;
|
||||
$name_weight = 0;
|
||||
$pass_index = 0;
|
||||
$pass_weight = 0;
|
||||
$index = 0;
|
||||
foreach ($elements as $key => $element) {
|
||||
if ($key === 'name') {
|
||||
$name_index = $index;
|
||||
$name_weight = $element['#weight'];
|
||||
$this->assertTrue($element['#sorted'], "'name' field is #sorted.");
|
||||
}
|
||||
elseif ($key === 'pass') {
|
||||
$pass_index = $index;
|
||||
$pass_weight = $element['#weight'];
|
||||
$this->assertTrue($element['#sorted'], "'pass' field is #sorted.");
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
$this->assertEqual($name_index, $pass_index - 1, "'name' field ($name_index) appears before 'pass' field ($pass_index).");
|
||||
$this->assertTrue($name_weight < $pass_weight, "'name' field weight ($name_weight) is smaller than 'pass' field weight ($pass_weight).");
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the user account form for a given operation.
|
||||
*
|
||||
* @param string $operation
|
||||
* The entity operation; one of 'register' or 'default'.
|
||||
*
|
||||
* @return array
|
||||
* The form array.
|
||||
*/
|
||||
protected function buildAccountForm($operation) {
|
||||
// @see HtmlEntityFormController::getFormObject()
|
||||
$entity_type = 'user';
|
||||
$fields = array();
|
||||
if ($operation != 'register') {
|
||||
$fields['uid'] = 2;
|
||||
}
|
||||
$entity = $this->container->get('entity.manager')
|
||||
->getStorage($entity_type)
|
||||
->create($fields);
|
||||
$form_object = $this->container->get('entity.manager')
|
||||
->getFormObject($entity_type, $operation)
|
||||
->setEntity($entity);
|
||||
|
||||
// @see EntityFormBuilder::getForm()
|
||||
return $this->container->get('entity.form_builder')->getForm($entity, $operation);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\config\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
|
||||
/**
|
||||
* Ensures the user action for adding and removing roles have valid config
|
||||
* schema.
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserActionConfigSchemaTest extends KernelTestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'user');
|
||||
|
||||
/**
|
||||
* Tests whether the user action config schema are valid.
|
||||
*/
|
||||
function testValidUserActionConfigSchema() {
|
||||
$rid = strtolower($this->randomMachineName(8));
|
||||
Role::create(array('id' => $rid))->save();
|
||||
|
||||
// Test user_add_role_action configuration.
|
||||
$config = $this->config('system.action.user_add_role_action.' . $rid);
|
||||
$this->assertEqual($config->get('id'), 'user_add_role_action.' . $rid);
|
||||
$this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get());
|
||||
|
||||
// Test user_remove_role_action configuration.
|
||||
$config = $this->config('system.action.user_remove_role_action.' . $rid);
|
||||
$this->assertEqual($config->get('id'), 'user_remove_role_action.' . $rid);
|
||||
$this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
|
||||
/**
|
||||
* Tests the user reference field functionality.
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserEntityReferenceTest extends EntityKernelTestBase {
|
||||
|
||||
use EntityReferenceTestTrait;
|
||||
|
||||
/**
|
||||
* A randomly-generated role for testing purposes.
|
||||
*
|
||||
* @var \Drupal\user\RoleInterface
|
||||
*/
|
||||
protected $role1;
|
||||
|
||||
/**
|
||||
* A randomly-generated role for testing purposes.
|
||||
*
|
||||
* @var \Drupal\user\RoleInterface
|
||||
*/
|
||||
protected $role2;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->role1 = Role::create(array(
|
||||
'id' => strtolower($this->randomMachineName(8)),
|
||||
'label' => $this->randomMachineName(8),
|
||||
));
|
||||
$this->role1->save();
|
||||
|
||||
$this->role2 = Role::create(array(
|
||||
'id' => strtolower($this->randomMachineName(8)),
|
||||
'label' => $this->randomMachineName(8),
|
||||
));
|
||||
$this->role2->save();
|
||||
|
||||
$this->createEntityReferenceField('user', 'user', 'user_reference', 'User reference', 'user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests user selection by roles.
|
||||
*/
|
||||
function testUserSelectionByRole() {
|
||||
$field_definition = FieldConfig::loadByName('user', 'user', 'user_reference');
|
||||
$handler_settings = $field_definition->getSetting('handler_settings');
|
||||
$handler_settings['filter']['role'] = array(
|
||||
$this->role1->id() => $this->role1->id(),
|
||||
$this->role2->id() => 0,
|
||||
);
|
||||
$handler_settings['filter']['type'] = 'role';
|
||||
$field_definition->setSetting('handler_settings', $handler_settings);
|
||||
$field_definition->save();
|
||||
|
||||
$user1 = $this->createUser(array('name' => 'aabb'));
|
||||
$user1->addRole($this->role1->id());
|
||||
$user1->save();
|
||||
|
||||
$user2 = $this->createUser(array('name' => 'aabbb'));
|
||||
$user2->addRole($this->role1->id());
|
||||
$user2->save();
|
||||
|
||||
$user3 = $this->createUser(array('name' => 'aabbbb'));
|
||||
$user3->addRole($this->role2->id());
|
||||
$user3->save();
|
||||
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityAutocompleteMatcher $autocomplete */
|
||||
$autocomplete = \Drupal::service('entity.autocomplete_matcher');
|
||||
|
||||
$matches = $autocomplete->getMatches('user', 'default', $field_definition->getSetting('handler_settings'), 'aabb');
|
||||
$this->assertEqual(count($matches), 2);
|
||||
$users = array();
|
||||
foreach ($matches as $match) {
|
||||
$users[] = $match['label'];
|
||||
}
|
||||
$this->assertTrue(in_array($user1->label(), $users));
|
||||
$this->assertTrue(in_array($user2->label(), $users));
|
||||
$this->assertFalse(in_array($user3->label(), $users));
|
||||
|
||||
$matches = $autocomplete->getMatches('user', 'default', $field_definition->getSetting('handler_settings'), 'aabbbb');
|
||||
$this->assertEqual(count($matches), 0, '');
|
||||
}
|
||||
}
|
68
core/modules/user/tests/src/Kernel/UserEntityTest.php
Normal file
68
core/modules/user/tests/src/Kernel/UserEntityTest.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the user entity class.
|
||||
*
|
||||
* @group user
|
||||
* @see \Drupal\user\Entity\User
|
||||
*/
|
||||
class UserEntityTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'user', 'field');
|
||||
|
||||
/**
|
||||
* Tests some of the methods.
|
||||
*
|
||||
* @see \Drupal\user\Entity\User::getRoles()
|
||||
* @see \Drupal\user\Entity\User::addRole()
|
||||
* @see \Drupal\user\Entity\User::removeRole()
|
||||
*/
|
||||
public function testUserMethods() {
|
||||
$role_storage = $this->container->get('entity.manager')->getStorage('user_role');
|
||||
$role_storage->create(array('id' => 'test_role_one'))->save();
|
||||
$role_storage->create(array('id' => 'test_role_two'))->save();
|
||||
$role_storage->create(array('id' => 'test_role_three'))->save();
|
||||
|
||||
$values = array(
|
||||
'uid' => 1,
|
||||
'roles' => array('test_role_one'),
|
||||
);
|
||||
$user = User::create($values);
|
||||
|
||||
$this->assertTrue($user->hasRole('test_role_one'));
|
||||
$this->assertFalse($user->hasRole('test_role_two'));
|
||||
$this->assertEqual(array(RoleInterface::AUTHENTICATED_ID, 'test_role_one'), $user->getRoles());
|
||||
|
||||
$user->addRole('test_role_one');
|
||||
$this->assertTrue($user->hasRole('test_role_one'));
|
||||
$this->assertFalse($user->hasRole('test_role_two'));
|
||||
$this->assertEqual(array(RoleInterface::AUTHENTICATED_ID, 'test_role_one'), $user->getRoles());
|
||||
|
||||
$user->addRole('test_role_two');
|
||||
$this->assertTrue($user->hasRole('test_role_one'));
|
||||
$this->assertTrue($user->hasRole('test_role_two'));
|
||||
$this->assertEqual(array(RoleInterface::AUTHENTICATED_ID, 'test_role_one', 'test_role_two'), $user->getRoles());
|
||||
|
||||
$user->removeRole('test_role_three');
|
||||
$this->assertTrue($user->hasRole('test_role_one'));
|
||||
$this->assertTrue($user->hasRole('test_role_two'));
|
||||
$this->assertEqual(array(RoleInterface::AUTHENTICATED_ID, 'test_role_one', 'test_role_two'), $user->getRoles());
|
||||
|
||||
$user->removeRole('test_role_one');
|
||||
$this->assertFalse($user->hasRole('test_role_one'));
|
||||
$this->assertTrue($user->hasRole('test_role_two'));
|
||||
$this->assertEqual(array(RoleInterface::AUTHENTICATED_ID, 'test_role_two'), $user->getRoles());
|
||||
}
|
||||
|
||||
}
|
52
core/modules/user/tests/src/Kernel/UserFieldsTest.php
Normal file
52
core/modules/user/tests/src/Kernel/UserFieldsTest.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests available user fields in twig.
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserFieldsTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['user', 'system'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
|
||||
// Set up a test theme that prints the user's mail field.
|
||||
\Drupal::service('theme_handler')->install(array('user_test_theme'));
|
||||
\Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('user_test_theme'));
|
||||
// Clear the theme registry.
|
||||
$this->container->set('theme.registry', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests account's available fields.
|
||||
*/
|
||||
function testUserFields() {
|
||||
// Create the user to test the user fields.
|
||||
$user = User::create([
|
||||
'name' => 'foobar',
|
||||
'mail' => 'foobar@example.com',
|
||||
]);
|
||||
$build = user_view($user);
|
||||
$output = \Drupal::service('renderer')->renderRoot($build);
|
||||
$this->setRawContent($output);
|
||||
$userEmail = $user->getEmail();
|
||||
$this->assertText($userEmail, "User's mail field is found in the twig template");
|
||||
}
|
||||
|
||||
}
|
54
core/modules/user/tests/src/Kernel/UserInstallTest.php
Normal file
54
core/modules/user/tests/src/Kernel/UserInstallTest.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests user_install().
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserInstallTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('user');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->container->get('module_handler')->loadInclude('user', 'install');
|
||||
$this->installEntitySchema('user');
|
||||
user_install();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test that the initial users have correct values.
|
||||
*/
|
||||
public function testUserInstall() {
|
||||
$result = db_query('SELECT u.uid, u.uuid, u.langcode, uf.status FROM {users} u INNER JOIN {users_field_data} uf ON u.uid=uf.uid ORDER BY u.uid')
|
||||
->fetchAllAssoc('uid');
|
||||
$anon = $result[0];
|
||||
$admin = $result[1];
|
||||
$this->assertFalse(empty($anon->uuid), 'Anon user has a UUID');
|
||||
$this->assertFalse(empty($admin->uuid), 'Admin user has a UUID');
|
||||
|
||||
// Test that the anonymous and administrators languages are equal to the
|
||||
// site's default language.
|
||||
$this->assertEqual($anon->langcode, \Drupal::languageManager()->getDefaultLanguage()->getId());
|
||||
$this->assertEqual($admin->langcode, \Drupal::languageManager()->getDefaultLanguage()->getId());
|
||||
|
||||
// Test that the administrator is active.
|
||||
$this->assertEqual($admin->status, 1);
|
||||
// Test that the anonymous user is blocked.
|
||||
$this->assertEqual($anon->status, 0);
|
||||
}
|
||||
|
||||
}
|
74
core/modules/user/tests/src/Kernel/UserRoleDeleteTest.php
Normal file
74
core/modules/user/tests/src/Kernel/UserRoleDeleteTest.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* Tests the handling of user_role entity from the user module
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserRoleDeleteTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'user', 'field');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests removal of role references on role entity delete.
|
||||
*
|
||||
* @see user_user_role_delete()
|
||||
*/
|
||||
public function testRoleDeleteUserRoleReferenceDelete() {
|
||||
// Create two test roles.
|
||||
$role_storage = $this->container->get('entity.manager')->getStorage('user_role');
|
||||
$role_storage->create(array('id' => 'test_role_one'))->save();
|
||||
$role_storage->create(array('id' => 'test_role_two'))->save();
|
||||
|
||||
// Create user and assign both test roles.
|
||||
$values = array(
|
||||
'uid' => 1,
|
||||
'name' => $this->randomString(),
|
||||
'roles' => array('test_role_one', 'test_role_two'),
|
||||
);
|
||||
$user = User::create($values);
|
||||
$user->save();
|
||||
|
||||
// Check that user has both roles.
|
||||
$this->assertTrue($user->hasRole('test_role_one'));
|
||||
$this->assertTrue($user->hasRole('test_role_two'));
|
||||
|
||||
// Delete test role one.
|
||||
$test_role_one = $role_storage->load('test_role_one');
|
||||
$test_role_one->delete();
|
||||
|
||||
// Load user again from the database.
|
||||
$user = User::load($user->id());
|
||||
|
||||
// Check that user does not have role one anymore, still has role two.
|
||||
$this->assertFalse($user->hasRole('test_role_one'));
|
||||
$this->assertTrue($user->hasRole('test_role_two'));
|
||||
|
||||
// Create new role with same name.
|
||||
$role_storage->create(array('id' => 'test_role_one'))->save();
|
||||
|
||||
// Load user again from the database.
|
||||
$user = User::load($user->id());
|
||||
|
||||
// Check that user does not have role one.
|
||||
$this->assertFalse($user->hasRole('test_role_one'));
|
||||
$this->assertTrue($user->hasRole('test_role_two'));
|
||||
|
||||
}
|
||||
|
||||
}
|
48
core/modules/user/tests/src/Kernel/UserSaveStatusTest.php
Normal file
48
core/modules/user/tests/src/Kernel/UserSaveStatusTest.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* Tests user saving status.
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserSaveStatusTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('system', 'user', 'field');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SAVED_NEW and SAVED_UPDATED statuses for user entity type.
|
||||
*/
|
||||
function testUserSaveStatus() {
|
||||
// Create a new user.
|
||||
$values = array(
|
||||
'uid' => 1,
|
||||
'name' => $this->randomMachineName(),
|
||||
);
|
||||
$user = User::create($values);
|
||||
|
||||
// Test SAVED_NEW.
|
||||
$return = $user->save();
|
||||
$this->assertEqual($return, SAVED_NEW, "User was saved with SAVED_NEW status.");
|
||||
|
||||
// Test SAVED_UPDATED.
|
||||
$user->name = $this->randomMachineName();
|
||||
$return = $user->save();
|
||||
$this->assertEqual($return, SAVED_UPDATED, "User was saved with SAVED_UPDATED status.");
|
||||
}
|
||||
|
||||
}
|
215
core/modules/user/tests/src/Kernel/UserValidationTest.php
Normal file
215
core/modules/user/tests/src/Kernel/UserValidationTest.php
Normal file
|
@ -0,0 +1,215 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\user\Kernel;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Language\Language;
|
||||
use Drupal\Core\Render\Element\Email;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* Verify that user validity checks behave as designed.
|
||||
*
|
||||
* @group user
|
||||
*/
|
||||
class UserValidationTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('field', 'user', 'system');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
$this->installSchema('system', array('sequences'));
|
||||
|
||||
// Make sure that the default roles exist.
|
||||
$this->installConfig(array('user'));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests user name validation.
|
||||
*/
|
||||
function testUsernames() {
|
||||
$test_cases = array( // '<username>' => array('<description>', 'assert<testName>'),
|
||||
'foo' => array('Valid username', 'assertNull'),
|
||||
'FOO' => array('Valid username', 'assertNull'),
|
||||
'Foo O\'Bar' => array('Valid username', 'assertNull'),
|
||||
'foo@bar' => array('Valid username', 'assertNull'),
|
||||
'foo@example.com' => array('Valid username', 'assertNull'),
|
||||
'foo@-example.com' => array('Valid username', 'assertNull'), // invalid domains are allowed in usernames
|
||||
'þòøÇߪř€' => array('Valid username', 'assertNull'),
|
||||
'ᚠᛇᚻ᛫ᛒᛦᚦ' => array('Valid UTF8 username', 'assertNull'), // runes
|
||||
' foo' => array('Invalid username that starts with a space', 'assertNotNull'),
|
||||
'foo ' => array('Invalid username that ends with a space', 'assertNotNull'),
|
||||
'foo bar' => array('Invalid username that contains 2 spaces \' \'', 'assertNotNull'),
|
||||
'' => array('Invalid empty username', 'assertNotNull'),
|
||||
'foo/' => array('Invalid username containing invalid chars', 'assertNotNull'),
|
||||
'foo' . chr(0) . 'bar' => array('Invalid username containing chr(0)', 'assertNotNull'), // NULL
|
||||
'foo' . chr(13) . 'bar' => array('Invalid username containing chr(13)', 'assertNotNull'), // CR
|
||||
str_repeat('x', USERNAME_MAX_LENGTH + 1) => array('Invalid excessively long username', 'assertNotNull'),
|
||||
);
|
||||
foreach ($test_cases as $name => $test_case) {
|
||||
list($description, $test) = $test_case;
|
||||
$result = user_validate_name($name);
|
||||
$this->$test($result, $description . ' (' . $name . ')');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs entity validation checks.
|
||||
*/
|
||||
function testValidation() {
|
||||
$user = User::create(array(
|
||||
'name' => 'test',
|
||||
'mail' => 'test@example.com',
|
||||
));
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 0, 'No violations when validating a default user.');
|
||||
|
||||
// Only test one example invalid name here, the rest is already covered in
|
||||
// the testUsernames() method in this class.
|
||||
$name = $this->randomMachineName(61);
|
||||
$user->set('name', $name);
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when name is too long.');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The username %name is too long: it must be %max characters or less.', array('%name' => $name, '%max' => 60)));
|
||||
|
||||
// Create a second test user to provoke a name collision.
|
||||
$user2 = User::create([
|
||||
'name' => 'existing',
|
||||
'mail' => 'existing@example.com',
|
||||
]);
|
||||
$user2->save();
|
||||
$user->set('name', 'existing');
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found on name collision.');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The username %name is already taken.', array('%name' => 'existing')));
|
||||
|
||||
// Make the name valid.
|
||||
$user->set('name', $this->randomMachineName());
|
||||
|
||||
$user->set('mail', 'invalid');
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when email is invalid');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.'));
|
||||
|
||||
$mail = $this->randomMachineName(Email::EMAIL_MAX_LENGTH - 11) . '@example.com';
|
||||
$user->set('mail', $mail);
|
||||
$violations = $user->validate();
|
||||
// @todo There are two violations because EmailItem::getConstraints()
|
||||
// overlaps with the implicit constraint of the 'email' property type used
|
||||
// in EmailItem::propertyDefinitions(). Resolve this in
|
||||
// https://www.drupal.org/node/2023465.
|
||||
$this->assertEqual(count($violations), 2, 'Violations found when email is too long');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('%name: the email address can not be longer than @max characters.', array('%name' => $user->get('mail')->getFieldDefinition()->getLabel(), '@max' => Email::EMAIL_MAX_LENGTH)));
|
||||
$this->assertEqual($violations[1]->getPropertyPath(), 'mail.0.value');
|
||||
$this->assertEqual($violations[1]->getMessage(), t('This value is not a valid email address.'));
|
||||
|
||||
// Provoke an email collision with an existing user.
|
||||
$user->set('mail', 'existing@example.com');
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when email already exists.');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'mail');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The email address %mail is already taken.', array('%mail' => 'existing@example.com')));
|
||||
$user->set('mail', NULL);
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Email addresses may not be removed');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'mail');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('@name field is required.', array('@name' => $user->getFieldDefinition('mail')->getLabel())));
|
||||
$user->set('mail', 'someone@example.com');
|
||||
|
||||
$user->set('timezone', $this->randomString(33));
|
||||
$this->assertLengthViolation($user, 'timezone', 32, 2, 1);
|
||||
$user->set('timezone', 'invalid zone');
|
||||
$this->assertAllowedValuesViolation($user, 'timezone');
|
||||
$user->set('timezone', NULL);
|
||||
|
||||
$user->set('init', 'invalid');
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when init email is invalid');
|
||||
$user->set('init', NULL);
|
||||
|
||||
$user->set('langcode', 'invalid');
|
||||
$this->assertAllowedValuesViolation($user, 'langcode');
|
||||
$user->set('langcode', NULL);
|
||||
|
||||
// Only configurable langcodes are allowed for preferred languages.
|
||||
$user->set('preferred_langcode', Language::LANGCODE_NOT_SPECIFIED);
|
||||
$this->assertAllowedValuesViolation($user, 'preferred_langcode');
|
||||
$user->set('preferred_langcode', NULL);
|
||||
|
||||
$user->set('preferred_admin_langcode', Language::LANGCODE_NOT_SPECIFIED);
|
||||
$this->assertAllowedValuesViolation($user, 'preferred_admin_langcode');
|
||||
$user->set('preferred_admin_langcode', NULL);
|
||||
|
||||
Role::create(array('id' => 'role1'))->save();
|
||||
Role::create(array('id' => 'role2'))->save();
|
||||
|
||||
// Test cardinality of user roles.
|
||||
$user = User::create([
|
||||
'name' => 'role_test',
|
||||
'mail' => 'test@example.com',
|
||||
'roles' => array('role1', 'role2'),
|
||||
]);
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 0);
|
||||
|
||||
$user->roles[1]->target_id = 'unknown_role';
|
||||
$violations = $user->validate();
|
||||
$this->assertEqual(count($violations), 1);
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'roles.1.target_id');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%entity_type: %name) does not exist.', array('%entity_type' => 'user_role', '%name' => 'unknown_role')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that a length violation exists for the given field.
|
||||
*
|
||||
* @param \Drupal\core\Entity\EntityInterface $entity
|
||||
* The entity object to validate.
|
||||
* @param string $field_name
|
||||
* The field that violates the maximum length.
|
||||
* @param int $length
|
||||
* Number of characters that was exceeded.
|
||||
* @param int $count
|
||||
* (optional) The number of expected violations. Defaults to 1.
|
||||
* @param int $expected_index
|
||||
* (optional) The index at which to expect the violation. Defaults to 0.
|
||||
*/
|
||||
protected function assertLengthViolation(EntityInterface $entity, $field_name, $length, $count = 1, $expected_index = 0) {
|
||||
$violations = $entity->validate();
|
||||
$this->assertEqual(count($violations), $count, "Violation found when $field_name is too long.");
|
||||
$this->assertEqual($violations[$expected_index]->getPropertyPath(), "$field_name.0.value");
|
||||
$field_label = $entity->get($field_name)->getFieldDefinition()->getLabel();
|
||||
$this->assertEqual($violations[$expected_index]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => $length)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that a AllowedValues violation exists for the given field.
|
||||
*
|
||||
* @param \Drupal\core\Entity\EntityInterface $entity
|
||||
* The entity object to validate.
|
||||
* @param string $field_name
|
||||
* The name of the field to verify.
|
||||
*/
|
||||
protected function assertAllowedValuesViolation(EntityInterface $entity, $field_name) {
|
||||
$violations = $entity->validate();
|
||||
$this->assertEqual(count($violations), 1, "Allowed values violation for $field_name found.");
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The value you selected is not a valid choice.'));
|
||||
}
|
||||
|
||||
}
|
|
@ -12,7 +12,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|||
/**
|
||||
* @coversDefaultClass \Drupal\user\Access\PermissionAccessCheck
|
||||
* @group Routing
|
||||
* @group AccessF
|
||||
* @group Access
|
||||
*/
|
||||
class PermissionAccessCheckTest extends UnitTestCase {
|
||||
|
||||
|
|
|
@ -278,4 +278,3 @@ class PrivateTempStoreTest extends UnitTestCase {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -350,4 +350,3 @@ class SharedTempStoreTest extends UnitTestCase {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue