You are here

class UserEventTest in Hook Event Dispatcher 8

Class UserEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\User

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of UserEventTest

File

tests/src/Unit/User/UserEventTest.php, line 20

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\User
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UserEventTest::$manager private property The manager.
UserEventTest::setUp public function Overrides UnitTestCase::setUp
UserEventTest::testUserCancelEvent public function User cancel event test.
UserEventTest::testUserCancelMethodsAlterEventByReference public function User cancel methods alter event by reference test.
UserEventTest::testUserCancelMethodsAlterEventWithSet public function User cancel methods alter event with set test.
UserEventTest::testUserFormatNameAlterEventByReference public function User format name alter event by reference test.
UserEventTest::testUserFormatNameAlterEventWithSet public function User format name alter event with set test.
UserEventTest::testUserLoginEvent public function User login event test.
UserEventTest::testUserLogoutEvent public function User logout event test.