class UserEventTest in Hook Event Dispatcher 8
Class UserEventTest.
@package Drupal\Tests\hook_event_dispatcher\Unit\User
@group hook_event_dispatcher
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\hook_event_dispatcher\Unit\User\UserEventTest
Expanded class hierarchy of UserEventTest
File
- tests/
src/ Unit/ User/ UserEventTest.php, line 20
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\UserView source
class UserEventTest extends UnitTestCase {
/**
* The manager.
*
* @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
*/
private $manager;
/**
* {@inheritdoc}
*/
public function setUp() {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
\Drupal::setContainer($builder);
}
/**
* User cancel event test.
*/
public function testUserCancelEvent() {
$edit = [
'Test',
'array',
];
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this
->createMock(AccountInterface::class);
$method = 'Test method';
hook_event_dispatcher_user_cancel($edit, $account, $method);
/** @var \Drupal\hook_event_dispatcher\Event\User\UserCancelEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_CANCEL);
self::assertSame($edit, $event
->getEdit());
self::assertSame($account, $event
->getAccount());
self::assertSame($method, $event
->getMethod());
}
/**
* User cancel methods alter event by reference test.
*/
public function testUserCancelMethodsAlterEventByReference() {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::USER_CANCEL_METHODS_ALTER => function (UserCancelMethodsAlterEvent $event) {
$name =& $event
->getMethods()[0];
$name .= ' improved!';
},
]);
$methods = [
'Test method',
];
hook_event_dispatcher_user_cancel_methods_alter($methods);
/** @var \Drupal\hook_event_dispatcher\Event\User\UserCancelMethodsAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_CANCEL_METHODS_ALTER);
self::assertSame([
'Test method improved!',
], $event
->getMethods());
}
/**
* User cancel methods alter event with set test.
*/
public function testUserCancelMethodsAlterEventWithSet() {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::USER_CANCEL_METHODS_ALTER => function (UserCancelMethodsAlterEvent $event) {
$event
->setMethods([
'New method',
]);
},
]);
$methods = [
'Test method',
];
hook_event_dispatcher_user_cancel_methods_alter($methods);
/** @var \Drupal\hook_event_dispatcher\Event\User\UserCancelMethodsAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_CANCEL_METHODS_ALTER);
self::assertSame([
'New method',
], $event
->getMethods());
}
/**
* User login event test.
*/
public function testUserLoginEvent() {
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this
->createMock(AccountInterface::class);
hook_event_dispatcher_user_login($account);
/** @var \Drupal\hook_event_dispatcher\Event\User\UserLoginEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_LOGIN);
self::assertEquals($account, $event
->getAccount());
}
/**
* User logout event test.
*/
public function testUserLogoutEvent() {
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this
->createMock(AccountInterface::class);
hook_event_dispatcher_user_logout($account);
/** @var \Drupal\hook_event_dispatcher\Event\User\UserLogoutEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_LOGOUT);
self::assertEquals($account, $event
->getAccount());
}
/**
* User format name alter event by reference test.
*/
public function testUserFormatNameAlterEventByReference() {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::USER_FORMAT_NAME_ALTER => function (UserFormatNameAlterEvent $event) {
$name =& $event
->getName();
$name .= ' improved!';
},
]);
$name = 'Test name';
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this
->createMock(AccountInterface::class);
hook_event_dispatcher_user_format_name_alter($name, $account);
/** @var \Drupal\hook_event_dispatcher\Event\User\UserFormatNameAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_FORMAT_NAME_ALTER);
self::assertSame('Test name improved!', $event
->getName());
self::assertSame($account, $event
->getAccount());
}
/**
* User format name alter event with set test.
*/
public function testUserFormatNameAlterEventWithSet() {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::USER_FORMAT_NAME_ALTER => function (UserFormatNameAlterEvent $event) {
$event
->setName('New name!');
},
]);
$name = 'Test name';
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this
->createMock(AccountInterface::class);
hook_event_dispatcher_user_format_name_alter($name, $account);
/** @var \Drupal\hook_event_dispatcher\Event\User\UserFormatNameAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_FORMAT_NAME_ALTER);
self::assertSame('New name!', $event
->getName());
self::assertSame($account, $event
->getAccount());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UserEventTest:: |
private | property | The manager. | |
UserEventTest:: |
public | function |
Overrides UnitTestCase:: |
|
UserEventTest:: |
public | function | User cancel event test. | |
UserEventTest:: |
public | function | User cancel methods alter event by reference test. | |
UserEventTest:: |
public | function | User cancel methods alter event with set test. | |
UserEventTest:: |
public | function | User format name alter event by reference test. | |
UserEventTest:: |
public | function | User format name alter event with set test. | |
UserEventTest:: |
public | function | User login event test. | |
UserEventTest:: |
public | function | User logout event test. |