function EventPropertyAccessTest::testEventContextDefinition

Tests the event metadata to ensure that all properties may be accessed.

Access to properties declared in the metadata is tested using code copied from \Drupal\rules\EventSubscriber\GenericEventSubscriber, so this test does not directly test GenericEventSubscriber - that is done correctly in the Kernel test \Drupal\Tests\rules\Kernel\EventPropertyAccessTest.

@dataProvider provideTestEvent

Parameters

string $event_name: The Plugin ID of the event being tested.

object $event: The event object being tested. In Drupal 9 this will be a \Symfony\Component\EventDispatcher\Event, In Drupal 10 this will be a \Symfony\Contracts\EventDispatcher\Event.

File

tests/src/Unit/Integration/Event/EventPropertyAccessTest.php, line 53

Class

EventPropertyAccessTest
Checks that the events defined in the rules_test_event module are correct.

Namespace

Drupal\Tests\rules\Unit\Integration\Event

Code

public function testEventContextDefinition($event_name, object $event) : void {
    $plugin = $this->eventManager
        ->createInstance($event_name);
    $context_definitions = $plugin->getContextDefinitions();
    foreach ($context_definitions as $name => $definition) {
        $this->assertSame('string', $definition->getDataType());
        // Properties for these test events should be named <visibility>Property.
        // We just want the <visibility> part.
        $visibility = substr($name, 0, -8);
        $this->assertSame('A ' . $visibility . ' string', $definition->getLabel());
        // If this is a GenericEvent, get the value of the context variable from
        // the event arguments.
        if ($event instanceof SymfonyGenericEvent) {
            $value = $event->getArgument($name);
        }
        elseif ($definition->hasGetter()) {
            $value = $event->{$definition->getGetter()}();
        }
        else {
            $getter = function ($property) {
                return $this->{$property};
            };
            $value = $getter->call($event, $name);
        }
        $this->assertEquals($visibility . ' property', $value);
    }
}