UserEventTest.php in Hook Event Dispatcher 8.2
File
modules/user_event_dispatcher/tests/src/Unit/User/UserEventTest.php
View source
<?php
namespace Drupal\Tests\user_event_dispatcher\Unit\User;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Session\AccountInterface;
use Drupal\user_event_dispatcher\Event\User\UserCancelMethodsAlterEvent;
use Drupal\user_event_dispatcher\Event\User\UserFormatNameAlterEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use PHPUnit\Framework\TestCase;
use function user_event_dispatcher_user_cancel;
use function user_event_dispatcher_user_cancel_methods_alter;
use function user_event_dispatcher_user_format_name_alter;
use function user_event_dispatcher_user_login;
use function user_event_dispatcher_user_logout;
class UserEventTest extends TestCase {
private $manager;
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
public function testUserCancelEvent() : void {
$edit = [
'Test',
'array',
];
$account = $this
->createMock(AccountInterface::class);
$method = 'Test method';
user_event_dispatcher_user_cancel($edit, $account, $method);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_CANCEL);
self::assertSame($edit, $event
->getEdit());
self::assertSame($account, $event
->getAccount());
self::assertSame($method, $event
->getMethod());
}
public function testUserCancelMethodsAlterEvent() : void {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::USER_CANCEL_METHODS_ALTER => static function (UserCancelMethodsAlterEvent $event) {
$name =& $event
->getMethods()[0];
$name .= ' improved!';
},
]);
$methods = [
'Test method',
];
$expectedMethods = [
'Test method improved!',
];
user_event_dispatcher_user_cancel_methods_alter($methods);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_CANCEL_METHODS_ALTER);
self::assertSame($expectedMethods, $methods);
self::assertSame($expectedMethods, $event
->getMethods());
}
public function testUserLoginEvent() : void {
$account = $this
->createMock(AccountInterface::class);
user_event_dispatcher_user_login($account);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_LOGIN);
self::assertEquals($account, $event
->getAccount());
}
public function testUserLogoutEvent() : void {
$account = $this
->createMock(AccountInterface::class);
user_event_dispatcher_user_logout($account);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_LOGOUT);
self::assertEquals($account, $event
->getAccount());
}
public function testUserFormatNameAlterEvent() : void {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::USER_FORMAT_NAME_ALTER => static function (UserFormatNameAlterEvent $event) {
$name =& $event
->getName();
$name .= ' improved!';
},
]);
$name = 'Test name';
$account = $this
->createMock(AccountInterface::class);
user_event_dispatcher_user_format_name_alter($name, $account);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::USER_FORMAT_NAME_ALTER);
self::assertSame('Test name improved!', $event
->getName());
self::assertSame($account, $event
->getAccount());
}
}