You are here

public function UserEventTest::testUserFormatNameAlterEvent in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/user_event_dispatcher/tests/src/Unit/User/UserEventTest.php \Drupal\Tests\user_event_dispatcher\Unit\User\UserEventTest::testUserFormatNameAlterEvent()

User format name alter event test.

File

modules/user_event_dispatcher/tests/src/Unit/User/UserEventTest.php, line 117

Class

UserEventTest
Class UserEventTest.

Namespace

Drupal\Tests\user_event_dispatcher\Unit\User

Code

public function testUserFormatNameAlterEvent() : void {
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::USER_FORMAT_NAME_ALTER => static function (UserFormatNameAlterEvent $event) {
      $name =& $event
        ->getName();
      $name .= ' improved!';
    },
  ]);
  $name = 'Test name';

  /** @var \Drupal\Core\Session\AccountInterface $account */
  $account = $this
    ->createMock(AccountInterface::class);
  user_event_dispatcher_user_format_name_alter($name, $account);

  /** @var \Drupal\user_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());
}