Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,91 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Menu\UserLocalTasksTest.
*/
namespace Drupal\Tests\user\Unit\Menu;
use Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase;
/**
* Tests user local tasks.
*
* @group user
*/
class UserLocalTasksTest extends LocalTaskIntegrationTestBase {
protected function setUp() {
$this->directoryList = array('user' => 'core/modules/user');
parent::setUp();
}
/**
* Tests local task existence.
*
* @dataProvider getUserAdminRoutes
*/
public function testUserAdminLocalTasks($route, $expected) {
$this->assertLocalTasks($route, $expected);
}
/**
* Provides a list of routes to test.
*/
public function getUserAdminRoutes() {
return array(
array('entity.user.collection', array(array('entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection'))),
array('user.admin_permissions', array(array('entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection'))),
array('entity.user_role.collection', array(array('entity.user.collection', 'user.admin_permissions', 'entity.user_role.collection'))),
array('entity.user.admin_form', array(array('user.account_settings_tab'))),
);
}
/**
* Checks user listing local tasks.
*
* @dataProvider getUserLoginRoutes
*/
public function testUserLoginLocalTasks($route) {
$tasks = array(
0 => array('user.register', 'user.pass', 'user.login',),
);
$this->assertLocalTasks($route, $tasks);
}
/**
* Provides a list of routes to test.
*/
public function getUserLoginRoutes() {
return array(
array('user.login'),
array('user.register'),
array('user.pass'),
);
}
/**
* Checks user listing local tasks.
*
* @dataProvider getUserPageRoutes
*/
public function testUserPageLocalTasks($route, $subtask = array()) {
$tasks = array(
0 => array('entity.user.canonical', 'entity.user.edit_form',),
);
if ($subtask) $tasks[] = $subtask;
$this->assertLocalTasks($route, $tasks);
}
/**
* Provides a list of routes to test.
*/
public function getUserPageRoutes() {
return array(
array('entity.user.canonical'),
array('entity.user.edit_form'),
);
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\PermissionAccessCheckTest.
*/
namespace Drupal\Tests\user\Unit;
use Drupal\Core\Access\AccessResult;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Access\PermissionAccessCheck;
use Symfony\Component\Routing\Route;
/**
* @coversDefaultClass \Drupal\user\Access\PermissionAccessCheck
* @group Routing
* @group AccessF
*/
class PermissionAccessCheckTest extends UnitTestCase {
/**
* The tested access checker.
*
* @var \Drupal\user\Access\PermissionAccessCheck
*/
public $accessCheck;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->accessCheck = new PermissionAccessCheck();
}
/**
* Provides data for the testAccess method.
*
* @return array
*/
public function providerTestAccess() {
$allowed = AccessResult::allowedIf(TRUE)->addCacheContexts(['user.permissions']);
$neutral = AccessResult::allowedIf(FALSE)->addCacheContexts(['user.permissions']);
return [
[[], AccessResult::allowedIf(FALSE)],
[['_permission' => 'allowed'], $allowed],
[['_permission' => 'denied'], $neutral],
[['_permission' => 'allowed+denied'], $allowed],
[['_permission' => 'allowed+denied+other'], $allowed],
[['_permission' => 'allowed,denied'], $neutral],
];
}
/**
* Tests the access check method.
*
* @dataProvider providerTestAccess
* @covers ::access
*/
public function testAccess($requirements, $access) {
$user = $this->getMock('Drupal\Core\Session\AccountInterface');
$user->expects($this->any())
->method('hasPermission')
->will($this->returnValueMap([
['allowed', TRUE],
['denied', FALSE],
['other', FALSE]
]
));
$route = new Route('', [], $requirements);
$this->assertEquals($access, $this->accessCheck->access($route, $user));
}
}

View file

@ -0,0 +1,403 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\PermissionHandlerTest.
*/
namespace Drupal\Tests\user\Unit;
use Drupal\Core\Extension\Extension;
use Drupal\Tests\UnitTestCase;
use Drupal\user\PermissionHandler;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper;
/**
* Tests the permission handler.
*
* @group user
*
* @coversDefaultClass \Drupal\user\PermissionHandler
*/
class PermissionHandlerTest extends UnitTestCase {
/**
* The tested permission handler.
*
* @var \Drupal\Tests\user\Unit\TestPermissionHandler|\Drupal\user\PermissionHandler
*/
protected $permissionHandler;
/**
* The mocked module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The mocked string translation.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stringTranslation;
/**
* The mocked controller resolver.
*
* @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $controllerResolver;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->stringTranslation = $this->getStringTranslationStub();
$this->controllerResolver = $this->getMock('Drupal\Core\Controller\ControllerResolverInterface');
}
/**
* Provides an extension object for a given module with a human name.
*
* @param string $module
* The module machine name.
* @param string $name
* The module human name.
*
* @return \Drupal\Core\Extension\Extension
* The extension object.
*/
protected function mockModuleExtension($module, $name) {
$extension = new Extension($this->root, $module, "modules/$module");
$extension->info['name'] = $name;
return $extension;
}
/**
* Tests permissions provided by YML files.
*
* @covers ::__construct
* @covers ::getPermissions
* @covers ::buildPermissionsYaml
* @covers ::moduleProvidesPermissions
*/
public function testBuildPermissionsYaml() {
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->moduleHandler->expects($this->once())
->method('getModuleDirectories')
->willReturn(array(
'module_a' => vfsStream::url('modules/module_a'),
'module_b' => vfsStream::url('modules/module_b'),
'module_c' => vfsStream::url('modules/module_c'),
));
$url = vfsStream::url('modules');
mkdir($url . '/module_a');
file_put_contents($url . '/module_a/module_a.permissions.yml',
"access_module_a: single_description"
);
mkdir($url . '/module_b');
file_put_contents($url . '/module_b/module_b.permissions.yml',
"'access module b':
title: 'Access B'
description: 'bla bla'
");
mkdir($url . '/module_c');
file_put_contents($url . '/module_c/module_c.permissions.yml',
"'access_module_c':
title: 'Access C'
description: 'bla bla'
'restrict access': TRUE
");
$modules = array('module_a', 'module_b', 'module_c');
$extensions = array(
'module_a' => $this->mockModuleExtension('module_a', 'Module a'),
'module_b' => $this->mockModuleExtension('module_b', 'Module b'),
'module_c' => $this->mockModuleExtension('module_c', 'Module c'),
);
$this->moduleHandler->expects($this->any())
->method('getImplementations')
->with('permission')
->willReturn(array());
$this->moduleHandler->expects($this->any())
->method('getModuleList')
->willReturn(array_flip($modules));
$this->controllerResolver->expects($this->never())
->method('getControllerFromDefinition');
$this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
// Setup system_rebuild_module_data().
$this->permissionHandler->setSystemRebuildModuleData($extensions);
$actual_permissions = $this->permissionHandler->getPermissions();
$this->assertPermissions($actual_permissions);
$this->assertTrue($this->permissionHandler->moduleProvidesPermissions('module_a'));
$this->assertTrue($this->permissionHandler->moduleProvidesPermissions('module_b'));
$this->assertTrue($this->permissionHandler->moduleProvidesPermissions('module_c'));
$this->assertFalse($this->permissionHandler->moduleProvidesPermissions('module_d'));
}
/**
* Tests permissions sort inside a module.
*
* @covers ::__construct
* @covers ::getPermissions
* @covers ::buildPermissionsYaml
* @covers ::sortPermissions
*/
public function testBuildPermissionsSortPerModule() {
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->moduleHandler->expects($this->once())
->method('getModuleDirectories')
->willReturn([
'module_a' => vfsStream::url('modules/module_a'),
]);
$url = vfsStream::url('modules');
mkdir($url . '/module_a');
file_put_contents($url . '/module_a/module_a.permissions.yml',
"access_module_a2: single_description
access_module_a1: single_description"
);
$modules = ['module_a'];
$extensions = [
'module_a' => $this->mockModuleExtension('module_a', 'Module a'),
];
$this->moduleHandler->expects($this->any())
->method('getImplementations')
->with('permission')
->willReturn([]);
$this->moduleHandler->expects($this->any())
->method('getModuleList')
->willReturn(array_flip($modules));
$this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
// Setup system_rebuild_module_data().
$this->permissionHandler->setSystemRebuildModuleData($extensions);
$actual_permissions = $this->permissionHandler->getPermissions();
$this->assertEquals(['access_module_a1', 'access_module_a2'], array_keys($actual_permissions));
}
/**
* Tests dynamic callback permissions provided by YML files.
*
* @covers ::__construct
* @covers ::getPermissions
* @covers ::buildPermissionsYaml
*/
public function testBuildPermissionsYamlCallback() {
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->moduleHandler->expects($this->once())
->method('getModuleDirectories')
->willReturn(array(
'module_a' => vfsStream::url('modules/module_a'),
'module_b' => vfsStream::url('modules/module_b'),
'module_c' => vfsStream::url('modules/module_c'),
));
$url = vfsStream::url('modules');
mkdir($url . '/module_a');
file_put_contents($url . '/module_a/module_a.permissions.yml',
"permission_callbacks:
- 'Drupal\\user\\Tests\\TestPermissionCallbacks::singleDescription'
");
mkdir($url . '/module_b');
file_put_contents($url . '/module_b/module_b.permissions.yml',
"permission_callbacks:
- 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'
");
mkdir($url . '/module_c');
file_put_contents($url . '/module_c/module_c.permissions.yml',
"permission_callbacks:
- 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess'
");
$modules = array('module_a', 'module_b', 'module_c');
$extensions = array(
'module_a' => $this->mockModuleExtension('module_a', 'Module a'),
'module_b' => $this->mockModuleExtension('module_b', 'Module b'),
'module_c' => $this->mockModuleExtension('module_c', 'Module c'),
);
$this->moduleHandler->expects($this->any())
->method('getImplementations')
->with('permission')
->willReturn(array());
$this->moduleHandler->expects($this->any())
->method('getModuleList')
->willReturn(array_flip($modules));
$this->controllerResolver->expects($this->at(0))
->method('getControllerFromDefinition')
->with('Drupal\\user\\Tests\\TestPermissionCallbacks::singleDescription')
->willReturn(array(new TestPermissionCallbacks(), 'singleDescription'));
$this->controllerResolver->expects($this->at(1))
->method('getControllerFromDefinition')
->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription')
->willReturn(array(new TestPermissionCallbacks(), 'titleDescription'));
$this->controllerResolver->expects($this->at(2))
->method('getControllerFromDefinition')
->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescriptionRestrictAccess')
->willReturn(array(new TestPermissionCallbacks(), 'titleDescriptionRestrictAccess'));
$this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
// Setup system_rebuild_module_data().
$this->permissionHandler->setSystemRebuildModuleData($extensions);
$actual_permissions = $this->permissionHandler->getPermissions();
$this->assertPermissions($actual_permissions);
}
/**
* Tests a YAML file containing both static permissions and a callback.
*/
public function testPermissionsYamlStaticAndCallback() {
vfsStreamWrapper::register();
$root = new vfsStreamDirectory('modules');
vfsStreamWrapper::setRoot($root);
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->moduleHandler->expects($this->once())
->method('getModuleDirectories')
->willReturn(array(
'module_a' => vfsStream::url('modules/module_a'),
));
$url = vfsStream::url('modules');
mkdir($url . '/module_a');
file_put_contents($url . '/module_a/module_a.permissions.yml',
"'access module a':
title: 'Access A'
description: 'bla bla'
permission_callbacks:
- 'Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription'
");
$modules = array('module_a');
$extensions = array(
'module_a' => $this->mockModuleExtension('module_a', 'Module a'),
);
$this->moduleHandler->expects($this->any())
->method('getImplementations')
->with('permission')
->willReturn(array());
$this->moduleHandler->expects($this->any())
->method('getModuleList')
->willReturn(array_flip($modules));
$this->controllerResolver->expects($this->once())
->method('getControllerFromDefinition')
->with('Drupal\\user\\Tests\\TestPermissionCallbacks::titleDescription')
->willReturn(array(new TestPermissionCallbacks(), 'titleDescription'));
$this->permissionHandler = new TestPermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
// Setup system_rebuild_module_data().
$this->permissionHandler->setSystemRebuildModuleData($extensions);
$actual_permissions = $this->permissionHandler->getPermissions();
$this->assertCount(2, $actual_permissions);
$this->assertEquals($actual_permissions['access module a']['title'], 'Access A');
$this->assertEquals($actual_permissions['access module a']['provider'], 'module_a');
$this->assertEquals($actual_permissions['access module a']['description'], 'bla bla');
$this->assertEquals($actual_permissions['access module b']['title'], 'Access B');
$this->assertEquals($actual_permissions['access module b']['provider'], 'module_a');
$this->assertEquals($actual_permissions['access module b']['description'], 'bla bla');
}
/**
* Checks that the permissions are like expected.
*
* @param array $actual_permissions
* The actual permissions
*/
protected function assertPermissions(array $actual_permissions) {
$this->assertCount(3, $actual_permissions);
$this->assertEquals($actual_permissions['access_module_a']['title'], 'single_description');
$this->assertEquals($actual_permissions['access_module_a']['provider'], 'module_a');
$this->assertEquals($actual_permissions['access module b']['title'], 'Access B');
$this->assertEquals($actual_permissions['access module b']['provider'], 'module_b');
$this->assertEquals($actual_permissions['access_module_c']['title'], 'Access C');
$this->assertEquals($actual_permissions['access_module_c']['provider'], 'module_c');
$this->assertEquals($actual_permissions['access_module_c']['restrict access'], TRUE);
}
}
class TestPermissionHandler extends PermissionHandler {
/**
* Test module data.
*
* @var array
*/
protected $systemModuleData;
protected function systemRebuildModuleData() {
return $this->systemModuleData;
}
public function setSystemRebuildModuleData(array $extensions) {
$this->systemModuleData = $extensions;
}
}
class TestPermissionCallbacks {
public function singleDescription() {
return array(
'access_module_a' => 'single_description'
);
}
public function titleDescription() {
return array(
'access module b' => array(
'title' => 'Access B',
'description' => 'bla bla',
),
);
}
public function titleDescriptionRestrictAccess() {
return array(
'access_module_c' => array(
'title' => 'Access C',
'description' => 'bla bla',
'restrict access' => TRUE,
),
);
}
}

View file

@ -0,0 +1,54 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Plugin\Action\AddRoleUserTest.
*/
namespace Drupal\Tests\user\Unit\Plugin\Action;
use Drupal\user\Plugin\Action\AddRoleUser;
/**
* @coversDefaultClass \Drupal\user\Plugin\Action\AddRoleUser
* @group user
*/
class AddRoleUserTest extends RoleUserTestBase {
/**
* Tests the execute method on a user with a role.
*/
public function testExecuteAddExistingRole() {
$this->account->expects($this->never())
->method('addRole');
$this->account->expects($this->any())
->method('hasRole')
->with($this->equalTo('test_role_1'))
->will($this->returnValue(TRUE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new AddRoleUser($config, 'user_add_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
/**
* Tests the execute method on a user without a specific role.
*/
public function testExecuteAddNonExistingRole() {
$this->account->expects($this->once())
->method('addRole');
$this->account->expects($this->any())
->method('hasRole')
->with($this->equalTo('test_role_1'))
->will($this->returnValue(FALSE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new AddRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
}

View file

@ -0,0 +1,54 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Plugin\Action\RemoveRoleUserTest.
*/
namespace Drupal\Tests\user\Unit\Plugin\Action;
use Drupal\user\Plugin\Action\RemoveRoleUser;
/**
* @coversDefaultClass \Drupal\user\Plugin\Action\RemoveRoleUser
* @group user
*/
class RemoveRoleUserTest extends RoleUserTestBase {
/**
* Tests the execute method on a user with a role.
*/
public function testExecuteRemoveExistingRole() {
$this->account->expects($this->once())
->method('removeRole');
$this->account->expects($this->any())
->method('hasRole')
->with($this->equalTo('test_role_1'))
->will($this->returnValue(TRUE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
/**
* Tests the execute method on a user without a specific role.
*/
public function testExecuteRemoveNonExistingRole() {
$this->account->expects($this->never())
->method('removeRole');
$this->account->expects($this->any())
->method('hasRole')
->with($this->equalTo('test_role_1'))
->will($this->returnValue(FALSE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Plugin\Action\RoleUserTestBase.
*/
namespace Drupal\Tests\user\Unit\Plugin\Action;
use Drupal\Tests\UnitTestCase;
/**
* Provides a base class for user role action tests.
*/
abstract class RoleUserTestBase extends UnitTestCase {
/**
* The mocked account.
*
* @var \Drupal\user\UserInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account;
/**
* The user role entity type.
*
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $userRoleEntityType;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->account = $this
->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->getMock();
$this->userRoleEntityType = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
}
}

View file

@ -0,0 +1,63 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Plugin\Core\Entity\UserTest.
*/
namespace Drupal\Tests\user\Unit\Plugin\Core\Entity;
use Drupal\Tests\Core\Session\UserSessionTest;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
/**
* @coversDefaultClass \Drupal\user\Entity\User
* @group user
*/
class UserTest extends UserSessionTest {
/**
* {@inheritdoc}
*/
protected function createUserSession(array $rids = array(), $authenticated = FALSE) {
$user = $this->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->setMethods(array('get', 'id'))
->getMock();
$user->expects($this->any())
->method('id')
// @todo Also test the uid = 1 handling.
->will($this->returnValue($authenticated ? 2 : 0));
$roles = array();
foreach ($rids as $rid) {
$roles[] = (object) array(
'target_id' => $rid,
);
}
$user->expects($this->any())
->method('get')
->with('roles')
->will($this->returnValue($roles));
return $user;
}
/**
* Tests the method getRoles exclude or include locked roles based in param.
*
* @see \Drupal\user\Entity\User::getRoles()
* @covers ::getRoles
*/
public function testUserGetRoles() {
// Anonymous user.
$user = $this->createUserSession(array());
$this->assertEquals(array(RoleInterface::ANONYMOUS_ID), $user->getRoles());
$this->assertEquals(array(), $user->getRoles(TRUE));
// Authenticated user.
$user = $this->createUserSession(array(), TRUE);
$this->assertEquals(array(RoleInterface::AUTHENTICATED_ID), $user->getRoles());
$this->assertEquals(array(), $user->getRoles(TRUE));
}
}

View file

@ -0,0 +1,314 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidatorTest.
*/
namespace Drupal\Tests\user\Unit\Plugin\Validation\Constraint;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraint;
use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator;
/**
* @coversDefaultClass \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator
* @group user
*/
class ProtectedUserFieldConstraintValidatorTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function createValidator() {
// Setup mocks that don't need to change.
$unchanged_field = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$unchanged_field->expects($this->any())
->method('getValue')
->willReturn('unchanged-value');
$unchanged_account = $this->getMock('Drupal\user\UserInterface');
$unchanged_account->expects($this->any())
->method('get')
->willReturn($unchanged_field);
$user_storage = $this->getMock('Drupal\user\UserStorageInterface');
$user_storage->expects($this->any())
->method('loadUnchanged')
->willReturn($unchanged_account);
$current_user = $this->getMock('Drupal\Core\Session\AccountProxyInterface');
$current_user->expects($this->any())
->method('id')
->willReturn('current-user');
return new ProtectedUserFieldConstraintValidator($user_storage, $current_user);
}
/**
* @covers ::validate
*
* @dataProvider providerTestValidate
*/
public function testValidate($items, $expected_violation, $name = FALSE) {
$constraint = new ProtectedUserFieldConstraint();
// If a violation is expected, then the context's addViolation method will
// be called, otherwise it should not be called.
$context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface');
if ($expected_violation) {
$context->expects($this->once())
->method('addViolation')
->with($constraint->message, array('%name' => $name));
}
else {
$context->expects($this->never())
->method('addViolation');
}
$validator = $this->createValidator();
$validator->initialize($context);
$validator->validate($items, $constraint);
}
/**
* Data provider for ::testValidate().
*/
public function providerTestValidate() {
$cases = [];
// Case 1: Validation context should not be touched if no items are passed.
$cases[] = [NULL, FALSE];
// Case 2: Empty user should be ignored.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn(NULL);
$cases[] = [$items, FALSE];
// Case 3: Account flagged to skip protected user should be ignored.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$account = $this->getMock('Drupal\user\UserInterface');
$account->_skipProtectedUserFieldConstraint = TRUE;
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$cases[] = [$items, FALSE];
// Case 4: New user should be ignored.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(TRUE);
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$cases[] = [$items, FALSE];
// Case 5: Mismatching user IDs should also be ignored.
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(FALSE);
$account->expects($this->once())
->method('id')
->willReturn('not-current-user');
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$cases[] = [$items, FALSE];
// Case 6: Non-password fields that have not changed should be ignored.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$field_definition->expects($this->exactly(2))
->method('getName')
->willReturn('field_not_password');
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(FALSE);
$account->expects($this->exactly(2))
->method('id')
->willReturn('current-user');
$account->expects($this->never())
->method('checkExistingPassword');
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$items->expects($this->once())
->method('getValue')
->willReturn('unchanged-value');
$cases[] = [$items, FALSE];
// Case 7: Password field with no value set should be ignored.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$field_definition->expects($this->once())
->method('getName')
->willReturn('pass');
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(FALSE);
$account->expects($this->exactly(2))
->method('id')
->willReturn('current-user');
$account->expects($this->never())
->method('checkExistingPassword');
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$cases[] = [$items, FALSE];
// Case 8: Non-password field changed, but user has passed provided current
// password.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$field_definition->expects($this->exactly(2))
->method('getName')
->willReturn('field_not_password');
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(FALSE);
$account->expects($this->exactly(2))
->method('id')
->willReturn('current-user');
$account->expects($this->once())
->method('checkExistingPassword')
->willReturn(TRUE);
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$items->expects($this->once())
->method('getValue')
->willReturn('changed-value');
$cases[] = [$items, FALSE];
// Case 9: Password field changed, current password confirmed.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$field_definition->expects($this->exactly(2))
->method('getName')
->willReturn('pass');
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(FALSE);
$account->expects($this->exactly(2))
->method('id')
->willReturn('current-user');
$account->expects($this->once())
->method('checkExistingPassword')
->willReturn(TRUE);
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$items->expects($this->any())
->method('getValue')
->willReturn('changed-value');
$items->expects($this->once())
->method('__get')
->with('value')
->willReturn('changed-value');
$cases[] = [$items, FALSE];
// The below calls should result in a violation.
// Case 10: Password field changed, current password not confirmed.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$field_definition->expects($this->exactly(2))
->method('getName')
->willReturn('pass');
$field_definition->expects($this->any())
->method('getLabel')
->willReturn('Password');
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(FALSE);
$account->expects($this->exactly(2))
->method('id')
->willReturn('current-user');
$account->expects($this->once())
->method('checkExistingPassword')
->willReturn(FALSE);
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$items->expects($this->once())
->method('getValue')
->willReturn('changed-value');
$items->expects($this->once())
->method('__get')
->with('value')
->willReturn('changed-value');
$cases[] = [$items, TRUE, 'Password'];
// Case 11: Non-password field changed, current password not confirmed.
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$field_definition->expects($this->exactly(2))
->method('getName')
->willReturn('field_not_password');
$field_definition->expects($this->any())
->method('getLabel')
->willReturn('Protected field');
$account = $this->getMock('Drupal\user\UserInterface');
$account->expects($this->once())
->method('isNew')
->willReturn(FALSE);
$account->expects($this->exactly(2))
->method('id')
->willReturn('current-user');
$account->expects($this->once())
->method('checkExistingPassword')
->willReturn(FALSE);
$items = $this->getMock('Drupal\Core\Field\FieldItemListInterface');
$items->expects($this->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items->expects($this->once())
->method('getEntity')
->willReturn($account);
$items->expects($this->once())
->method('getValue')
->willReturn('changed-value');
$cases[] = [$items, TRUE, 'Protected field'];
return $cases;
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Plugin\views\field\UserBulkFormTest.
*/
namespace Drupal\Tests\user\Unit\Plugin\views\field;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Plugin\views\field\UserBulkForm;
/**
* @coversDefaultClass \Drupal\user\Plugin\views\field\UserBulkForm
* @group user
*/
class UserBulkFormTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function tearDown() {
parent::tearDown();
$container = new ContainerBuilder();
\Drupal::setContainer($container);
}
/**
* Tests the constructor assignment of actions.
*/
public function testConstructor() {
$actions = array();
for ($i = 1; $i <= 2; $i++) {
$action = $this->getMock('\Drupal\system\ActionConfigEntityInterface');
$action->expects($this->any())
->method('getType')
->will($this->returnValue('user'));
$actions[$i] = $action;
}
$action = $this->getMock('\Drupal\system\ActionConfigEntityInterface');
$action->expects($this->any())
->method('getType')
->will($this->returnValue('node'));
$actions[] = $action;
$entity_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$entity_storage->expects($this->any())
->method('loadMultiple')
->will($this->returnValue($actions));
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$entity_manager->expects($this->once())
->method('getStorage')
->with('action')
->will($this->returnValue($entity_storage));
$language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$views_data = $this->getMockBuilder('Drupal\views\ViewsData')
->disableOriginalConstructor()
->getMock();
$views_data->expects($this->any())
->method('get')
->with('users')
->will($this->returnValue(array('table' => array('entity type' => 'user'))));
$container = new ContainerBuilder();
$container->set('views.views_data', $views_data);
$container->set('string_translation', $this->getStringTranslationStub());
\Drupal::setContainer($container);
$storage = $this->getMock('Drupal\views\ViewEntityInterface');
$storage->expects($this->any())
->method('get')
->with('base_table')
->will($this->returnValue('users'));
$executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$executable->storage = $storage;
$display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
->disableOriginalConstructor()
->getMock();
$definition['title'] = '';
$options = array();
$user_bulk_form = new UserBulkForm(array(), 'user_bulk_form', $definition, $entity_manager, $language_manager);
$user_bulk_form->init($executable, $display, $options);
$this->assertAttributeEquals(array_slice($actions, 0, -1, TRUE), 'actions', $user_bulk_form);
}
}

View file

@ -0,0 +1,286 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\PrivateTempStoreTest.
*/
namespace Drupal\Tests\user\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\user\PrivateTempStore;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @coversDefaultClass \Drupal\user\PrivateTempStore
* @group user
*/
class PrivateTempStoreTest extends UnitTestCase {
/**
* The mock key value expirable backend.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $keyValue;
/**
* The mock lock backend.
*
* @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $lock;
/**
* The user temp store.
*
* @var \Drupal\user\PrivateTempStore
*/
protected $tempStore;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $currentUser;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* A tempstore object belonging to the owner.
*
* @var \stdClass
*/
protected $ownObject;
/**
* A tempstore object not belonging to the owner.
*
* @var \stdClass
*/
protected $otherObject;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->keyValue = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
$this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
$this->currentUser = $this->getMock('Drupal\Core\Session\AccountProxyInterface');
$this->currentUser->expects($this->any())
->method('id')
->willReturn(1);
$this->requestStack = new RequestStack();
$request = Request::createFromGlobals();
$this->requestStack->push($request);
$this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800);
$this->ownObject = (object) array(
'data' => 'test_data',
'owner' => $this->currentUser->id(),
'updated' => (int) $request->server->get('REQUEST_TIME'),
);
// Clone the object but change the owner.
$this->otherObject = clone $this->ownObject;
$this->otherObject->owner = 2;
}
/**
* Tests the get() method.
*
* @covers ::get
*/
public function testGet() {
$this->keyValue->expects($this->at(0))
->method('get')
->with('1:test_2')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->at(1))
->method('get')
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(2))
->method('get')
->with('1:test')
->will($this->returnValue($this->otherObject));
$this->assertNull($this->tempStore->get('test_2'));
$this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
$this->assertNull($this->tempStore->get('test'));
}
/**
* Tests the set() method with no lock available.
*
* @covers ::set
* @expectedException \Drupal\user\TempStoreException
*/
public function testSetWithNoLockAvailable() {
$this->lock->expects($this->at(0))
->method('acquire')
->with('1:test')
->will($this->returnValue(FALSE));
$this->lock->expects($this->at(1))
->method('wait')
->with('1:test');
$this->lock->expects($this->at(2))
->method('acquire')
->with('1:test')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->once())
->method('getCollectionName');
$this->tempStore->set('test', 'value');
}
/**
* Tests a successful set() call.
*
* @covers ::set
*/
public function testSet() {
$this->lock->expects($this->once())
->method('acquire')
->with('1:test')
->will($this->returnValue(TRUE));
$this->lock->expects($this->never())
->method('wait');
$this->lock->expects($this->once())
->method('release')
->with('1:test');
$this->keyValue->expects($this->once())
->method('setWithExpire')
->with('1:test', $this->ownObject, 604800);
$this->tempStore->set('test', 'test_data');
}
/**
* Tests the getMetadata() method.
*
* @covers ::getMetadata
*/
public function testGetMetadata() {
$this->keyValue->expects($this->at(0))
->method('get')
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(1))
->method('get')
->with('1:test')
->will($this->returnValue(FALSE));
$metadata = $this->tempStore->getMetadata('test');
$this->assertObjectHasAttribute('owner', $metadata);
// Data should get removed.
$this->assertObjectNotHasAttribute('data', $metadata);
$this->assertNull($this->tempStore->getMetadata('test'));
}
/**
* Tests the locking in the delete() method.
*
* @covers ::delete
*/
public function testDeleteLocking() {
$this->keyValue->expects($this->once())
->method('get')
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->lock->expects($this->once())
->method('acquire')
->with('1:test')
->will($this->returnValue(TRUE));
$this->lock->expects($this->never())
->method('wait');
$this->lock->expects($this->once())
->method('release')
->with('1:test');
$this->keyValue->expects($this->once())
->method('delete')
->with('1:test');
$this->assertTrue($this->tempStore->delete('test'));
}
/**
* Tests the delete() method with no lock available.
*
* @covers ::delete
* @expectedException \Drupal\user\TempStoreException
*/
public function testDeleteWithNoLockAvailable() {
$this->keyValue->expects($this->once())
->method('get')
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->lock->expects($this->at(0))
->method('acquire')
->with('1:test')
->will($this->returnValue(FALSE));
$this->lock->expects($this->at(1))
->method('wait')
->with('1:test');
$this->lock->expects($this->at(2))
->method('acquire')
->with('1:test')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->once())
->method('getCollectionName');
$this->tempStore->delete('test');
}
/**
* Tests the delete() method.
*
* @covers ::delete
*/
public function testDelete() {
$this->lock->expects($this->once())
->method('acquire')
->with('1:test_2')
->will($this->returnValue(TRUE));
$this->keyValue->expects($this->at(0))
->method('get')
->with('1:test_1')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->at(1))
->method('get')
->with('1:test_2')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(2))
->method('delete')
->with('1:test_2');
$this->keyValue->expects($this->at(3))
->method('get')
->with('1:test_3')
->will($this->returnValue($this->otherObject));
$this->assertTrue($this->tempStore->delete('test_1'));
$this->assertTrue($this->tempStore->delete('test_2'));
$this->assertFalse($this->tempStore->delete('test_3'));
}
}

View file

@ -0,0 +1,358 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\SharedTempStoreTest.
*/
namespace Drupal\Tests\user\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\user\SharedTempStore;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @coversDefaultClass \Drupal\user\SharedTempStore
* @group user
*/
class SharedTempStoreTest extends UnitTestCase {
/**
* The mock key value expirable backend.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $keyValue;
/**
* The mock lock backend.
*
* @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $lock;
/**
* The user temp store.
*
* @var \Drupal\user\SharedTempStore
*/
protected $tempStore;
/**
* The owner used in this test.
*
* @var int
*/
protected $owner = 1;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* A tempstore object belonging to the owner.
*
* @var \stdClass
*/
protected $ownObject;
/**
* A tempstore object not belonging to the owner.
*
* @var \stdClass
*/
protected $otherObject;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->keyValue = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
$this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
$this->requestStack = new RequestStack();
$request = Request::createFromGlobals();
$this->requestStack->push($request);
$this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800);
$this->ownObject = (object) array(
'data' => 'test_data',
'owner' => $this->owner,
'updated' => (int) $request->server->get('REQUEST_TIME'),
);
// Clone the object but change the owner.
$this->otherObject = clone $this->ownObject;
$this->otherObject->owner = 2;
}
/**
* @covers ::get
*/
public function testGet() {
$this->keyValue->expects($this->at(0))
->method('get')
->with('test_2')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->at(1))
->method('get')
->with('test')
->will($this->returnValue($this->ownObject));
$this->assertNull($this->tempStore->get('test_2'));
$this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
}
/**
* Tests the getIfOwner() method.
*
* @covers ::getIfOwner
*/
public function testGetIfOwner() {
$this->keyValue->expects($this->at(0))
->method('get')
->with('test_2')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->at(1))
->method('get')
->with('test')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(2))
->method('get')
->with('test')
->will($this->returnValue($this->otherObject));
$this->assertNull($this->tempStore->getIfOwner('test_2'));
$this->assertSame($this->ownObject->data, $this->tempStore->getIfOwner('test'));
$this->assertNull($this->tempStore->getIfOwner('test'));
}
/**
* Tests the set() method with no lock available.
*
* @covers ::set
* @expectedException \Drupal\user\TempStoreException
*/
public function testSetWithNoLockAvailable() {
$this->lock->expects($this->at(0))
->method('acquire')
->with('test')
->will($this->returnValue(FALSE));
$this->lock->expects($this->at(1))
->method('wait')
->with('test');
$this->lock->expects($this->at(2))
->method('acquire')
->with('test')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->once())
->method('getCollectionName');
$this->tempStore->set('test', 'value');
}
/**
* Tests a successful set() call.
*
* @covers ::set
*/
public function testSet() {
$this->lock->expects($this->once())
->method('acquire')
->with('test')
->will($this->returnValue(TRUE));
$this->lock->expects($this->never())
->method('wait');
$this->lock->expects($this->once())
->method('release')
->with('test');
$this->keyValue->expects($this->once())
->method('setWithExpire')
->with('test', $this->ownObject, 604800);
$this->tempStore->set('test', 'test_data');
}
/**
* Tests the setIfNotExists() methods.
*
* @covers ::setIfNotExists
*/
public function testSetIfNotExists() {
$this->keyValue->expects($this->once())
->method('setWithExpireIfNotExists')
->with('test', $this->ownObject, 604800)
->will($this->returnValue(TRUE));
$this->assertTrue($this->tempStore->setIfNotExists('test', 'test_data'));
}
/**
* Tests the setIfOwner() method when no key exists.
*
* @covers ::setIfOwner
*/
public function testSetIfOwnerWhenNotExists() {
$this->keyValue->expects($this->once())
->method('setWithExpireIfNotExists')
->will($this->returnValue(TRUE));
$this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
}
/**
* Tests the setIfOwner() method when a key already exists but no object.
*
* @covers ::setIfOwner
*/
public function testSetIfOwnerNoObject() {
$this->keyValue->expects($this->once())
->method('setWithExpireIfNotExists')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->once())
->method('get')
->with('test')
->will($this->returnValue(FALSE));
$this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
}
/**
* Tests the setIfOwner() method with matching and non matching owners.
*
* @covers ::setIfOwner
*/
public function testSetIfOwner() {
$this->lock->expects($this->once())
->method('acquire')
->with('test')
->will($this->returnValue(TRUE));
$this->keyValue->expects($this->exactly(2))
->method('setWithExpireIfNotExists')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->exactly(2))
->method('get')
->with('test')
->will($this->onConsecutiveCalls($this->ownObject, $this->otherObject));
$this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
$this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
}
/**
* Tests the getMetadata() method.
*
* @covers ::getMetadata
*/
public function testGetMetadata() {
$this->keyValue->expects($this->at(0))
->method('get')
->with('test')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(1))
->method('get')
->with('test')
->will($this->returnValue(FALSE));
$metadata = $this->tempStore->getMetadata('test');
$this->assertObjectHasAttribute('owner', $metadata);
// Data should get removed.
$this->assertObjectNotHasAttribute('data', $metadata);
$this->assertNull($this->tempStore->getMetadata('test'));
}
/**
* Tests the delete() method.
*
* @covers ::delete
*/
public function testDelete() {
$this->lock->expects($this->once())
->method('acquire')
->with('test')
->will($this->returnValue(TRUE));
$this->lock->expects($this->never())
->method('wait');
$this->lock->expects($this->once())
->method('release')
->with('test');
$this->keyValue->expects($this->once())
->method('delete')
->with('test');
$this->tempStore->delete('test');
}
/**
* Tests the delete() method with no lock available.
*
* @covers ::delete
* @expectedException \Drupal\user\TempStoreException
*/
public function testDeleteWithNoLockAvailable() {
$this->lock->expects($this->at(0))
->method('acquire')
->with('test')
->will($this->returnValue(FALSE));
$this->lock->expects($this->at(1))
->method('wait')
->with('test');
$this->lock->expects($this->at(2))
->method('acquire')
->with('test')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->once())
->method('getCollectionName');
$this->tempStore->delete('test');
}
/**
* Tests the deleteIfOwner() method.
*
* @covers ::deleteIfOwner
*/
public function testDeleteIfOwner() {
$this->lock->expects($this->once())
->method('acquire')
->with('test_2')
->will($this->returnValue(TRUE));
$this->keyValue->expects($this->at(0))
->method('get')
->with('test_1')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->at(1))
->method('get')
->with('test_2')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(2))
->method('delete')
->with('test_2');
$this->keyValue->expects($this->at(3))
->method('get')
->with('test_3')
->will($this->returnValue($this->otherObject));
$this->assertTrue($this->tempStore->deleteIfOwner('test_1'));
$this->assertTrue($this->tempStore->deleteIfOwner('test_2'));
$this->assertFalse($this->tempStore->deleteIfOwner('test_3'));
}
}

View file

@ -0,0 +1,419 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\UserAccessControlHandlerTest.
*/
namespace Drupal\Tests\user\Unit;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Access\AccessResult;
use Drupal\Tests\UnitTestCase;
use Drupal\user\UserAccessControlHandler;
/**
* Tests the user access controller.
*
* @group Drupal
* @group User
*
* @coversDefaultClass \Drupal\user\UserAccessControlHandler
*/
class UserAccessControlHandlerTest extends UnitTestCase {
/**
* The user access controller to test.
*
* @var \Drupal\user\UserAccessControlHandler
*/
protected $accessControlHandler;
/**
* The mock user account with view access.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $viewer;
/**
* The mock user account that is able to change their own account name.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $owner;
/**
* The mock administrative test user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $admin;
/**
* The mocked test field items.
*
* @var \Drupal\Core\Field\FieldItemList
*/
protected $items;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->viewer = $this->getMock('\Drupal\Core\Session\AccountInterface');
$this->viewer
->expects($this->any())
->method('hasPermission')
->will($this->returnValue(FALSE));
$this->viewer
->expects($this->any())
->method('id')
->will($this->returnValue(1));
$this->owner = $this->getMock('\Drupal\Core\Session\AccountInterface');
$this->owner
->expects($this->any())
->method('hasPermission')
->will($this->returnValueMap(array(
array('administer users', FALSE),
array('change own username', TRUE),
)));
$this->owner
->expects($this->any())
->method('id')
->will($this->returnValue(2));
$this->admin = $this->getMock('\Drupal\Core\Session\AccountInterface');
$this->admin
->expects($this->any())
->method('hasPermission')
->will($this->returnValue(TRUE));
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$this->accessControlHandler = new UserAccessControlHandler($entity_type);
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$module_handler->expects($this->any())
->method('getImplementations')
->will($this->returnValue(array()));
$this->accessControlHandler->setModuleHandler($module_handler);
$this->items = $this->getMockBuilder('Drupal\Core\Field\FieldItemList')
->disableOriginalConstructor()
->getMock();
$this->items
->expects($this->any())
->method('defaultAccess')
->will($this->returnValue(AccessResult::allowed()));
}
/**
* Asserts correct field access grants for a field.
*/
public function assertFieldAccess($field, $viewer, $target, $view, $edit) {
$field_definition = $this->getMock('Drupal\Core\Field\FieldDefinitionInterface');
$field_definition->expects($this->any())
->method('getName')
->will($this->returnValue($field));
$this->items
->expects($this->any())
->method('getEntity')
->will($this->returnValue($this->{$target}));
foreach (array('view' => $view, 'edit' => $edit) as $operation => $result) {
$message = SafeMarkup::format("User @field field access returns @result with operation '@op' for @account accessing @target", array(
'@field' => $field,
'@result' => !isset($result) ? 'null' : ($result ? 'true' : 'false'),
'@op' => $operation,
'@account' => $viewer,
'@target' => $target,
));
$this->assertSame($result, $this->accessControlHandler->fieldAccess($operation, $field_definition, $this->{$viewer}, $this->items), $message);
}
}
/**
* Ensures user name access is working properly.
*
* @dataProvider userNameProvider
*/
public function testUserNameAccess($viewer, $target, $view, $edit) {
$this->assertFieldAccess('name', $viewer, $target, $view, $edit);
}
/**
* Provides test data for estUserNameAccess().
*/
public function userNameProvider() {
$name_access = array(
// The viewer user is allowed to see user names on all accounts.
array(
'viewer' => 'viewer',
'target' => 'viewer',
'view' => TRUE,
'edit' => FALSE,
),
array(
'viewer' => 'owner',
'target' => 'viewer',
'view' => TRUE,
'edit' => FALSE,
),
array(
'viewer' => 'viewer',
'target' => 'owner',
'view' => TRUE,
'edit' => FALSE,
),
// The owner user is allowed to change its own user name.
array(
'viewer' => 'owner',
'target' => 'owner',
'view' => TRUE,
'edit' => TRUE,
),
// The users-administrator user has full access.
array(
'viewer' => 'admin',
'target' => 'owner',
'view' => TRUE,
'edit' => TRUE,
),
);
return $name_access;
}
/**
* Tests that private user settings cannot be viewed by other users.
*
* @dataProvider hiddenUserSettingsProvider
*/
public function testHiddenUserSettings($field, $viewer, $target, $view, $edit) {
$this->assertFieldAccess($field, $viewer, $target, $view, $edit);
}
/**
* Provides test data for testHiddenUserSettings().
*/
public function hiddenUserSettingsProvider() {
$access_info = array();
$fields = array(
'preferred_langcode',
'preferred_admin_langcode',
'timezone',
'mail',
);
foreach ($fields as $field) {
$access_info[] = array(
'field' => $field,
'viewer' => 'viewer',
'target' => 'viewer',
'view' => TRUE,
'edit' => TRUE,
);
$access_info[] = array(
'field' => $field,
'viewer' => 'viewer',
'target' => 'owner',
'view' => FALSE,
// Anyone with edit access to the user can also edit these fields. In
// reality edit access will already be checked on entity level and the
// user without view access will typically not be able to edit.
'edit' => TRUE,
);
$access_info[] = array(
'field' => $field,
'viewer' => 'owner',
'target' => 'owner',
'view' => TRUE,
'edit' => TRUE,
);
$access_info[] = array(
'field' => $field,
'viewer' => 'admin',
'target' => 'owner',
'view' => TRUE,
'edit' => TRUE,
);
}
return $access_info;
}
/**
* Tests that private user settings cannot be viewed by other users.
*
* @dataProvider adminFieldAccessProvider
*/
public function testAdminFieldAccess($field, $viewer, $target, $view, $edit) {
$this->assertFieldAccess($field, $viewer, $target, $view, $edit);
}
/**
* Provides test data for testAdminFieldAccess().
*/
public function adminFieldAccessProvider() {
$access_info = array();
$fields = array(
'roles',
'status',
'access',
'login',
'init',
);
foreach ($fields as $field) {
$access_info[] = array(
'field' => $field,
'viewer' => 'viewer',
'target' => 'viewer',
'view' => FALSE,
'edit' => FALSE,
);
$access_info[] = array(
'field' => $field,
'viewer' => 'viewer',
'target' => 'owner',
'view' => FALSE,
'edit' => FALSE,
);
$access_info[] = array(
'field' => $field,
'viewer' => 'admin',
'target' => 'owner',
'view' => TRUE,
'edit' => TRUE,
);
}
return $access_info;
}
/**
* Tests that passwords cannot be viewed, just edited.
*
* @dataProvider passwordAccessProvider
*/
public function testPasswordAccess($viewer, $target, $view, $edit) {
$this->assertFieldAccess('pass', $viewer, $target, $view, $edit);
}
/**
* Provides test data for passwordAccessProvider().
*/
public function passwordAccessProvider() {
$pass_access = array(
array(
'viewer' => 'viewer',
'target' => 'viewer',
'view' => FALSE,
'edit' => TRUE,
),
array(
'viewer' => 'viewer',
'target' => 'owner',
'view' => FALSE,
// Anyone with edit access to the user can also edit these fields. In
// reality edit access will already be checked on entity level and the
// user without view access will typically not be able to edit.
'edit' => TRUE,
),
array(
'viewer' => 'owner',
'target' => 'viewer',
'view' => FALSE,
'edit' => TRUE,
),
array(
'viewer' => 'admin',
'target' => 'owner',
'view' => FALSE,
'edit' => TRUE,
),
);
return $pass_access;
}
/**
* Tests the user created field access.
*
* @dataProvider createdAccessProvider
*/
public function testCreatedAccess($viewer, $target, $view, $edit) {
$this->assertFieldAccess('created', $viewer, $target, $view, $edit);
}
/**
* Provides test data for testCreatedAccess().
*/
public function createdAccessProvider() {
$created_access = array(
array(
'viewer' => 'viewer',
'target' => 'viewer',
'view' => TRUE,
'edit' => FALSE,
),
array(
'viewer' => 'owner',
'target' => 'viewer',
'view' => TRUE,
'edit' => FALSE,
),
array(
'viewer' => 'admin',
'target' => 'owner',
'view' => TRUE,
'edit' => TRUE,
),
);
return $created_access;
}
/**
* Tests access to a non-existing base field.
*
* @dataProvider NonExistingFieldAccessProvider
*/
public function testNonExistingFieldAccess($viewer, $target, $view, $edit) {
// By default everyone has access to all fields that do not have explicit
// access control.
// @see EntityAccessControlHandler::checkFieldAccess()
$this->assertFieldAccess('some_non_existing_field', $viewer, $target, $view, $edit);
}
/**
* Provides test data for testNonExistingFieldAccess().
*/
public function NonExistingFieldAccessProvider() {
$created_access = array(
array(
'viewer' => 'viewer',
'target' => 'viewer',
'view' => TRUE,
'edit' => TRUE,
),
array(
'viewer' => 'owner',
'target' => 'viewer',
'view' => TRUE,
'edit' => TRUE,
),
array(
'viewer' => 'admin',
'target' => 'owner',
'view' => TRUE,
'edit' => TRUE,
),
);
return $created_access;
}
}

View file

@ -0,0 +1,199 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\UserAuthTest.
*/
namespace Drupal\Tests\user\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\user\UserAuth;
/**
* @coversDefaultClass \Drupal\user\UserAuth
* @group user
*/
class UserAuthTest extends UnitTestCase {
/**
* The mock user storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $userStorage;
/**
* The mocked password service.
*
* @var \Drupal\Core\Password\PasswordInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $passwordService;
/**
* The mock user.
*
* @var \Drupal\user\Entity\User|\PHPUnit_Framework_MockObject_MockObject
*/
protected $testUser;
/**
* The user auth object under test.
*
* @var \Drupal\user\UserAuth
*/
protected $userAuth;
/**
* The test username.
*
* @var string
*/
protected $username = 'test_user';
/**
* The test password.
*
* @var string
*/
protected $password = 'password';
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->userStorage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$entity_manager->expects($this->any())
->method('getStorage')
->with('user')
->will($this->returnValue($this->userStorage));
$this->passwordService = $this->getMock('Drupal\Core\Password\PasswordInterface');
$this->testUser = $this->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->setMethods(array('id', 'setPassword', 'save', 'getPassword'))
->getMock();
$this->userAuth = new UserAuth($entity_manager, $this->passwordService);
}
/**
* Tests failing authentication with missing credential parameters.
*
* @covers ::authenticate
*
* @dataProvider providerTestAuthenticateWithMissingCredentials
*/
public function testAuthenticateWithMissingCredentials($username, $password) {
$this->userStorage->expects($this->never())
->method('loadByProperties');
$this->assertFalse($this->userAuth->authenticate($username, $password));
}
/**
* Data provider for testAuthenticateWithMissingCredentials().
*
* @return array
*/
public function providerTestAuthenticateWithMissingCredentials() {
return array(
array(NULL, NULL),
array(NULL, ''),
array('', NULL),
array('', ''),
);
}
/**
* Tests the authenticate method with no account returned.
*
* @covers ::authenticate
*/
public function testAuthenticateWithNoAccountReturned() {
$this->userStorage->expects($this->once())
->method('loadByProperties')
->with(array('name' => $this->username))
->will($this->returnValue(array()));
$this->assertFalse($this->userAuth->authenticate($this->username, $this->password));
}
/**
* Tests the authenticate method with an incorrect password.
*
* @covers ::authenticate
*/
public function testAuthenticateWithIncorrectPassword() {
$this->userStorage->expects($this->once())
->method('loadByProperties')
->with(array('name' => $this->username))
->will($this->returnValue(array($this->testUser)));
$this->passwordService->expects($this->once())
->method('check')
->with($this->password, $this->testUser->getPassword())
->will($this->returnValue(FALSE));
$this->assertFalse($this->userAuth->authenticate($this->username, $this->password));
}
/**
* Tests the authenticate method with a correct password.
*
* @covers ::authenticate
*/
public function testAuthenticateWithCorrectPassword() {
$this->testUser->expects($this->once())
->method('id')
->will($this->returnValue(1));
$this->userStorage->expects($this->once())
->method('loadByProperties')
->with(array('name' => $this->username))
->will($this->returnValue(array($this->testUser)));
$this->passwordService->expects($this->once())
->method('check')
->with($this->password, $this->testUser->getPassword())
->will($this->returnValue(TRUE));
$this->assertsame(1, $this->userAuth->authenticate($this->username, $this->password));
}
/**
* Tests the authenticate method with a correct password and new password hash.
*
* @covers ::authenticate
*/
public function testAuthenticateWithCorrectPasswordAndNewPasswordHash() {
$this->testUser->expects($this->once())
->method('id')
->will($this->returnValue(1));
$this->testUser->expects($this->once())
->method('setPassword')
->with($this->password);
$this->testUser->expects($this->once())
->method('save');
$this->userStorage->expects($this->once())
->method('loadByProperties')
->with(array('name' => $this->username))
->will($this->returnValue(array($this->testUser)));
$this->passwordService->expects($this->once())
->method('check')
->with($this->password, $this->testUser->getPassword())
->will($this->returnValue(TRUE));
$this->passwordService->expects($this->once())
->method('needsRehash')
->with($this->testUser->getPassword())
->will($this->returnValue(TRUE));
$this->assertsame(1, $this->userAuth->authenticate($this->username, $this->password));
}
}

View file

@ -0,0 +1,87 @@
<?php
/**
* @file
* Contains \Drupal\Tests\user\Unit\Views\Argument\RolesRidTest.
*/
namespace Drupal\Tests\user\Unit\Views\Argument;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\Role;
use Drupal\user\Plugin\views\argument\RolesRid;
/**
* @coversDefaultClass \Drupal\user\Plugin\views\argument\RolesRid
* @group user
*/
class RolesRidTest extends UnitTestCase {
/**
* Tests the title_query method.
*
* @see \Drupal\user\Plugin\views\argument\RolesRid::title_query()
*/
public function testTitleQuery() {
$role1 = new Role(array(
'id' => 'test_rid_1',
'label' => 'test rid 1'
), 'user_role');
$role2 = new Role(array(
'id' => 'test_rid_2',
'label' => 'test <strong>rid 2</strong>',
), 'user_role');
// Creates a stub entity storage;
$role_storage = $this->getMockForAbstractClass('Drupal\Core\Entity\EntityStorageInterface');
$role_storage->expects($this->any())
->method('loadMultiple')
->will($this->returnValueMap(array(
array(array(), array()),
array(array('test_rid_1'), array('test_rid_1' => $role1)),
array(array('test_rid_1', 'test_rid_2'), array('test_rid_1' => $role1, 'test_rid_2' => $role2)),
)));
$entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity_type->expects($this->any())
->method('getKey')
->with('label')
->will($this->returnValue('label'));
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$entity_manager->expects($this->any())
->method('getDefinition')
->with($this->equalTo('user_role'))
->will($this->returnValue($entity_type));
$entity_manager
->expects($this->once())
->method('getStorage')
->with($this->equalTo('user_role'))
->will($this->returnValue($role_storage));
// @todo \Drupal\Core\Entity\Entity::entityType() uses a global call to
// entity_get_info(), which in turn wraps \Drupal::entityManager(). Set
// the entity manager until this is fixed.
$container = new ContainerBuilder();
$container->set('entity.manager', $entity_manager);
\Drupal::setContainer($container);
$roles_rid_argument = new RolesRid(array(), 'user__roles_rid', array(), $entity_manager);
$roles_rid_argument->value = array();
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array(), $titles);
$roles_rid_argument->value = array('test_rid_1');
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array('test rid 1'), $titles);
$roles_rid_argument->value = array('test_rid_1', 'test_rid_2');
$titles = $roles_rid_argument->title_query();
$this->assertEquals(array('test rid 1', SafeMarkup::checkPlain('test <strong>rid 2</strong>')), $titles);
}
}