Skip to content

Commit

Permalink
Merge pull request #59 from Gizra/58-ajax-unflag-error
Browse files Browse the repository at this point in the history
Fix double unsubscribe on ajax
  • Loading branch information
jhedstrom authored Nov 10, 2016
2 parents 629ba4d + 73ec5be commit 78e52c3
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
4 changes: 2 additions & 2 deletions message_subscribe_email/src/EventSubscriber/FlagEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Drupal\Tests\message_subscribe_email\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\JavascriptTestBase;

/**
* Javascript tests for message subscribe email.
*
* @group message_subscribe_email
*/
class MessageSubscribeEmailTest extends JavascriptTestBase {

/**
* {@inheritdoc}
*/
public static $modules = ['message_subscribe_email', 'node'];

/**
* Flag service.
*
* @var \Drupal\flag\FlagServiceInterface
*/
protected $flagService;

/**
* Nodes to test with.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $nodes;

/**
* Users to test with.
*
* @var \Drupal\user\UserInterface[]
*/
protected $users;

/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();

// Add some nodes.
$type = $this->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]));
}

}

0 comments on commit 78e52c3

Please sign in to comment.