diff --git a/message_subscribe_email/src/EventSubscriber/FlagEvents.php b/message_subscribe_email/src/EventSubscriber/FlagEvents.php index 46fe3d4..2904c0d 100644 --- a/message_subscribe_email/src/EventSubscriber/FlagEvents.php +++ b/message_subscribe_email/src/EventSubscriber/FlagEvents.php @@ -99,10 +99,10 @@ protected function triggerEmailFlag(FlaggingInterface $flagging, $action) { if (!$flag) { throw new MessageSubscribeException('There is no corresponding email flag (' . $email_flag_name . ') for the ' . $flagging->getFlagId() . ' flag.'); } - if ($action == 'flag') { + if ($action === 'flag') { $this->flagService->flag($flag, $flagging->getFlaggable(), $flagging->getOwner()); } - elseif ($flag->isFlagged($flagging->getFlaggable(), $flagging->getOwner())) { + elseif ($this->flagService->getFlagging($flag, $flagging->getFlaggable(), $flagging->getOwner())) { $this->flagService->unflag($flag, $flagging->getFlaggable(), $flagging->getOwner()); } } diff --git a/message_subscribe_email/tests/src/FunctionalJavascript/MessageSubscribeEmailTest.php b/message_subscribe_email/tests/src/FunctionalJavascript/MessageSubscribeEmailTest.php new file mode 100644 index 0000000..aa2049a --- /dev/null +++ b/message_subscribe_email/tests/src/FunctionalJavascript/MessageSubscribeEmailTest.php @@ -0,0 +1,91 @@ +createContentType(); + foreach (range(1, 3) as $i) { + $this->nodes[$i] = $this->drupalCreateNode(['type' => $type->id()]); + } + + // Add some users. + $permissions = [ + 'flag subscribe_node', + 'unflag subscribe_node', + 'flag email_node', + 'unflag email_node', + ]; + + $this->users[1] = $this->createUser($permissions); + $this->users[2] = $this->createUser($permissions); + $this->users[3] = $this->createUser($permissions); + + $this->flagService = $this->container->get('flag'); + } + + /** + * Tests the flag/unflag UI. + */ + public function testUi() { + $flag = $this->flagService->getFlagById('subscribe_node'); + $this->drupalLogin($this->users[2]); + $this->drupalGet($this->nodes[2]->toUrl()); + + // Subscribe to the node. + $this->clickLink(t('Subscribe')); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->assertTrue($this->flagService->getFlagging($flag, $this->nodes[2], $this->users[2])); + + // Unsubscribe from the node. + $this->clickLink(t('Unsubscribe')); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->assertFalse($this->flagService->getFlagging($flag, $this->nodes[2], $this->users[2])); + + // Subscribe again! + $this->clickLink(t('Subscribe')); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->assertTrue($this->flagService->getFlagging($flag, $this->nodes[2], $this->users[2])); + } + +}