You are here

public function PageEventTest::testPageAttachments in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/core_event_dispatcher/tests/src/Unit/Theme/PageEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Page\PageEventTest::testPageAttachments()

Test the PageAttachmentsEvent.

File

modules/core_event_dispatcher/tests/src/Unit/Theme/PageEventTest.php, line 45

Class

PageEventTest
Class PageEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Page

Code

public function testPageAttachments() : void {
  $currentAttachments = [];
  $currentAttachments['current']['#attached']['library'] = [
    'current/current',
  ];
  $testAttachment = [];
  $testAttachment['#attached']['library'] = [
    'test/test',
  ];
  $expectedAttachments = $currentAttachments;
  $expectedAttachments['new'] = $testAttachment;
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::PAGE_ATTACHMENTS => static function (PageAttachmentsEvent $event) use ($testAttachment) {
      $eventAttachments =& $event
        ->getAttachments();
      $eventAttachments['new'] = $testAttachment;
    },
  ]);
  core_event_dispatcher_page_attachments($currentAttachments);

  /** @var \Drupal\core_event_dispatcher\Event\Theme\PageAttachmentsEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::PAGE_ATTACHMENTS);
  self::assertSame($expectedAttachments, $event
    ->getAttachments());
  self::assertSame($expectedAttachments, $currentAttachments);
}