You are here

class EntityExtraEventTest in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/core_event_dispatcher/tests/src/Unit/Entity/EntityExtraEventTest.php \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityExtraEventTest

Class EntityExtraEventTest.

@group core_event_dispatcher

Hierarchy

  • class \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityExtraEventTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of EntityExtraEventTest

File

modules/core_event_dispatcher/tests/src/Unit/Entity/EntityExtraEventTest.php, line 20

Namespace

Drupal\Tests\core_event_dispatcher\Unit\Entity
View source
class EntityExtraEventTest extends TestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    Drupal::setContainer($builder);
  }

  /**
   * Test EntityExtraFieldInfoEvent with helper functions.
   */
  public function testEntityExtraFieldInfoEventWithHelperFunctions() : void {
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO => static function (EntityExtraFieldInfoEvent $event) {
        $event
          ->addDisplayFieldInfo('node', 'test', 'field_test', [
          'test' => 'node',
        ]);
        $event
          ->addFormFieldInfo('entity', 'test_entity', 'field_node', [
          'test' => 'entity',
        ]);
      },
    ]);
    $expectedFieldInfo = [
      'node' => [
        'test' => [
          'display' => [
            'field_test' => [
              'test' => 'node',
            ],
          ],
        ],
      ],
      'entity' => [
        'test_entity' => [
          'form' => [
            'field_node' => [
              'test' => 'entity',
            ],
          ],
        ],
      ],
    ];
    $hookFieldInfoResult = core_event_dispatcher_entity_extra_field_info();
    self::assertSame($expectedFieldInfo, $hookFieldInfoResult);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityExtraFieldInfoEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO);
    self::assertSame($expectedFieldInfo, $event
      ->getFieldInfo());
  }

  /**
   * Test EntityExtraFieldInfoEvent with set function.
   */
  public function testEntityExtraFieldInfoEventWithSetFunction() : void {
    $fieldInfo = [
      'node' => [
        'test' => [
          'display' => [
            'field_test' => [
              'test' => 'node',
            ],
          ],
        ],
      ],
      'entity' => [
        'test_entity' => [
          'form' => [
            'field_node' => [
              'test' => 'entity',
            ],
          ],
        ],
      ],
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO => static function (EntityExtraFieldInfoEvent $event) use ($fieldInfo) {
        $event
          ->setFieldInfo($fieldInfo);
      },
    ]);
    $hookFieldInfoResult = core_event_dispatcher_entity_extra_field_info();
    self::assertSame($fieldInfo, $hookFieldInfoResult);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityExtraFieldInfoEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO);
    self::assertSame($fieldInfo, $event
      ->getFieldInfo());
  }

  /**
   * Test EntityExtraFieldInfoAlterEvent.
   */
  public function testEntityExtraFieldInfoAlterEvent() : void {
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO_ALTER => static function (EntityExtraFieldInfoAlterEvent $event) {
        $info =& $event
          ->getFieldInfo();
        $info['taxonomy_term']['sheep']['display']['field_herd']['sheep'] = 'herd';
      },
    ]);
    $fieldInfo = $expectedFieldInfo = [
      'node' => [
        'test' => [
          'display' => [
            'field_test' => [
              'test' => 'node',
            ],
          ],
        ],
      ],
      'entity' => [
        'test_entity' => [
          'form' => [
            'field_node' => [
              'test' => 'entity',
            ],
          ],
        ],
      ],
    ];
    $expectedFieldInfo['taxonomy_term']['sheep']['display']['field_herd']['sheep'] = 'herd';
    core_event_dispatcher_entity_extra_field_info_alter($fieldInfo);

    /** @var \Drupal\core_event_dispatcher\Event\Entity\EntityExtraFieldInfoAlterEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_EXTRA_FIELD_INFO_ALTER);
    self::assertSame($expectedFieldInfo, $event
      ->getFieldInfo());
    self::assertSame($expectedFieldInfo, $fieldInfo);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityExtraEventTest::$manager private property The manager.
EntityExtraEventTest::setUp public function
EntityExtraEventTest::testEntityExtraFieldInfoAlterEvent public function Test EntityExtraFieldInfoAlterEvent.
EntityExtraEventTest::testEntityExtraFieldInfoEventWithHelperFunctions public function Test EntityExtraFieldInfoEvent with helper functions.
EntityExtraEventTest::testEntityExtraFieldInfoEventWithSetFunction public function Test EntityExtraFieldInfoEvent with set function.