Skip to content

Commit 3c0a51d

Browse files
Mile23paul-m
authored andcommitted
Issue #2976135 by Mile23: events_example does not have a handler for the 'cat in a tree' incident
1 parent 565731d commit 3c0a51d

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

events_example/src/EventSubscriber/EventsExampleSubscriber.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public static function getSubscribedEvents() {
6161
// of priority. If no priority is set the default is 0.
6262
$events[IncidentEvents::NEW_REPORT][] = ['notifyBatman', -100];
6363

64+
// We'll set an event listener with a very low priority to catch incident
65+
// types not yet defined. In practice, this will be the 'cat' incident.
66+
$events[IncidentEvents::NEW_REPORT][] = ['notifyDefault', -255];
67+
6468
return $events;
6569
}
6670

@@ -88,7 +92,11 @@ public function notifyMario(IncidentReportEvent $event) {
8892
// You can use the event object to access information about the event passed
8993
// along by the event dispatcher.
9094
if ($event->getType() == 'stolen_princess') {
91-
drupal_set_message($this->t('Mario has been alerted. Thank you. This message was set by an event subscriber. See \Drupal\events_example\EventSubscriber\EventsExampleSubscriber::notifyMario()'), 'status');
95+
drupal_set_message($this->t('Mario has been alerted. Thank you. This message was set by an event subscriber. See @method()', ['@method' => __METHOD__]), 'status');
96+
// Optionally use the event object to stop propagation.
97+
// If there are other subscribers that have not been called yet this will
98+
// cause them to be skipped.
99+
$event->stopPropagation();
92100
}
93101
}
94102

@@ -100,12 +108,20 @@ public function notifyMario(IncidentReportEvent $event) {
100108
*/
101109
public function notifyBatman(IncidentReportEvent $event) {
102110
if ($event->getType() == 'joker') {
103-
drupal_set_message($this->t('Batman has been alerted. Thank you. This message was set by an event subscriber. See \Drupal\events_example\EventSubscriber\EventsExampleSubscriber::notifyBatman()'), 'status');
104-
// Optionally use the event object to stop propagation.
105-
// If there are other subscribers that have not been called yet this will
106-
// cause them to be skipped.
111+
drupal_set_message($this->t('Batman has been alerted. Thank you. This message was set by an event subscriber. See @method()', ['@method' => __METHOD__]), 'status');
107112
$event->stopPropagation();
108113
}
109114
}
110115

116+
/**
117+
* Handle incidents not handled by the other handlers.
118+
*
119+
* @param \Drupal\events_example\Event\IncidentReportEvent $event
120+
* The event object containing the incident report.
121+
*/
122+
public function notifyDefault(IncidentReportEvent $event) {
123+
drupal_set_message($this->t('Thank you for reporting this incident. This message was set by an event subscriber. See @method()', ['@method' => __METHOD__]), 'status');
124+
$event->stopPropagation();
125+
}
126+
111127
}

events_example/tests/src/Functional/EventsExampleTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\Tests\events_example\Functional;
44

5+
use Drupal\Core\Url;
56
use Drupal\Tests\BrowserTestBase;
67

78
/**
@@ -10,8 +11,10 @@
1011
* For another example of testing whether or not events are dispatched see
1112
* \Drupal\Tests\migrate\Kernel\MigrateEventsTest.
1213
*
13-
* @ingroup events_example
14+
* @group events_example
1415
* @group examples
16+
*
17+
* @ingroup events_example
1518
*/
1619
class EventsExampleTest extends BrowserTestBase {
1720

@@ -30,7 +33,8 @@ class EventsExampleTest extends BrowserTestBase {
3033
*/
3134
public function testEventsExample() {
3235
// Test that the main page for the example is accessible.
33-
$this->drupalGet('examples/events-example');
36+
$events_example_form = Url::fromRoute('events_example.description');
37+
$this->drupalGet($events_example_form);
3438
$this->assertSession()->statusCodeEquals(200);
3539

3640
// Verify the page contains the required form fields.
@@ -48,7 +52,7 @@ public function testEventsExample() {
4852
'incident_type' => 'stolen_princess',
4953
'incident' => $this->randomString(),
5054
];
51-
$this->drupalPostForm('examples/events-example', $values, 'Submit');
55+
$this->drupalPostForm($events_example_form, $values, 'Submit');
5256
$this->assertSession()->pageTextContains('Mario has been alerted. Thank you.');
5357

5458
// Fill out the form again, this time testing that the
@@ -57,8 +61,17 @@ public function testEventsExample() {
5761
'incident_type' => 'joker',
5862
'incident' => $this->randomString(),
5963
];
60-
$this->drupalPostForm('examples/events-example', $values, 'Submit');
64+
$this->drupalPostForm($events_example_form, $values, 'Submit');
6165
$this->assertSession()->pageTextContains('Batman has been alerted. Thank you.');
66+
67+
// Fill out the form again, this time testing that our default handler
68+
// catches all the remaining values.
69+
$values = [
70+
'incident_type' => 'cat',
71+
'incident' => $this->randomString(),
72+
];
73+
$this->drupalPostForm($events_example_form, $values, 'Submit');
74+
$this->assertSession()->pageTextContains('notifyDefault()');
6275
}
6376

6477
}

events_example/tests/src/Kernel/EventsExampleServiceTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
/**
99
* Test to ensure 'events_example_subscriber' service is reachable.
1010
*
11-
* @ingroup events_example
11+
* @group events_example
1212
* @group examples
13+
*
14+
* @ingroup events_example
1315
*/
1416
class EventsExampleServiceTest extends KernelTestBase {
1517

0 commit comments

Comments
 (0)