Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jhedstrom committed Mar 16, 2017
1 parent bab7b1d commit 9ec6c7b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
7 changes: 5 additions & 2 deletions message_subscribe.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\message\MessageInterface;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;

/**
* Implements hook_message_subscribe_get_subscribers().
Expand Down Expand Up @@ -57,9 +58,11 @@ function message_subscribe_message_subscribe_get_subscribers(MessageInterface $m

foreach ($result as $row) {

$uids[$row->uid] = !empty($uids[$row->uid]) ? $uids[$row->uid] : ['notifiers' => []];
$uids[$row->uid] = !empty($uids[$row->uid]) ? $uids[$row->uid] : new DeliveryCandidate([], [], $row->uid);
// Register the flag name.
$uids[$row->uid]['flags'][] = $row->flag_id;
$flags = $uids[$row->uid]->getFlags();
$flags[] = $row->flag_id;
$uids[$row->uid]->setFlags(array_unique($flags));

if ($range) {
--$range;
Expand Down
1 change: 1 addition & 0 deletions src/Subscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public function sendMessage(EntityInterface $entity, MessageInterface $message,
foreach ($uids as $uid => $delivery_candidate) {
// Array usage is deprecated, but supported until 2.0.
if (!$delivery_candidate instanceof DeliveryCandidateInterface) {
trigger_error('Usage of arrays in subscriber information is deprecated and will be removed in Message Subscribe 2.0. Use a DeliveryCandidate object instead.', E_USER_DEPRECATED);
$delivery_candidate += ['notifiers' => []];
$delivery_candidate = new DeliveryCandidate($delivery_candidate['flags'], $delivery_candidate['notifiers'], $uid);
}
Expand Down
18 changes: 12 additions & 6 deletions tests/modules/message_subscribe_test/message_subscribe_test.module
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ function message_subscribe_test_message_subscribe_get_subscribers_alter(array &$
\Drupal::state('message_subscribe_test')->set('alter_hook_called', TRUE);

if (!\Drupal::state('message_subscribe_test')->get('disable_subscribers_alter', FALSE)) {
// Add non-existent user 10001. Using an array here until array support
// is removed in 2.0.
$uids[10001] = [
'flags' => ['bar_flag'],
'notifiers' => ['email'],
];
// Add non-existent user 10001.
if (\Drupal::state('message_subscribe_test')->get('use_array', FALSE)) {
// Test to ensure array usage still works (it will throw a deprecated
// notice).
$uids[10001] = [
'flags' => ['bar_flag'],
'notifiers' => ['email'],
];
}
else {
$uids[10001] = new DeliveryCandidate(['bar_flag'], ['email'], 10001);
}

// Remove user 2.
unset($uids[2]);
Expand Down
11 changes: 7 additions & 4 deletions tests/src/Kernel/SubscribersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,7 @@ public function testHooks() {
$this->assertTrue(\Drupal::state('message_subscribe_test')->get('alter_hook_called'));
$this->assertEquals([
4 => new DeliveryCandidate(['foo_flag'], ['email'], 4),
10001 => [
'flags' => ['bar_flag'],
'notifiers' => ['email'],
],
10001 => new DeliveryCandidate(['bar_flag'], ['email'], 10001),
], $uids);

// Disable the test module from adding a fake user.
Expand All @@ -326,6 +323,12 @@ public function testHooks() {
// called once for each subscriber, so 2 times).
$this->messageSubscribers->sendMessage($node, $message, [], ['entity access' => FALSE]);
$this->assertEquals(2, \Drupal::state('message_subscribe_test')->get('message_alter_hook_called', FALSE));

// Verify legacy array support. Will trigger a notice.
$this->setExpectedException(\PHPUnit_Framework_Error_Deprecated::class);
\Drupal::state('message_subscribe_test')->set('disable_subscribers_alter', FALSE);
\Drupal::state('message_subscribe_test')->set('use_array', TRUE);
$this->messageSubscribers->sendMessage($node, $message, [], ['entity access' => FALSE]);
}

/**
Expand Down

0 comments on commit 9ec6c7b

Please sign in to comment.