From 1fc03f82dc54375c30e8951e459d4107eb525bef Mon Sep 17 00:00:00 2001 From: dpi Date: Sat, 3 Dec 2016 14:10:38 +0800 Subject: [PATCH 01/10] WIP - Enforce result and reports && Plugin incoming method --- src/EventSubscriber/SmsMessageProcessor.php | 67 +++++++++++++++++++ src/Exception/SmsPluginReportException.php | 8 +++ ...SmsGatewayPluginIncomingEventInterface.php | 20 ++++++ .../SmsGatewayPluginIncomingInterface.php | 23 ------- src/Provider/DefaultSmsProvider.php | 29 ++++---- src/Provider/SmsProviderInterface.php | 8 +-- .../src/Plugin/SmsGateway/Memory.php | 11 ++- 7 files changed, 119 insertions(+), 47 deletions(-) create mode 100644 src/Exception/SmsPluginReportException.php create mode 100644 src/Plugin/SmsGatewayPluginIncomingEventInterface.php delete mode 100644 src/Plugin/SmsGatewayPluginIncomingInterface.php diff --git a/src/EventSubscriber/SmsMessageProcessor.php b/src/EventSubscriber/SmsMessageProcessor.php index 7fe51a4..90ee8f7 100644 --- a/src/EventSubscriber/SmsMessageProcessor.php +++ b/src/EventSubscriber/SmsMessageProcessor.php @@ -8,11 +8,16 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\sms\Entity\SmsGatewayInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\sms\Direction; use Drupal\sms\Event\RecipientGatewayEvent; use Drupal\sms\Event\SmsMessageEvent; use Drupal\sms\Exception\RecipientRouteException; +use Drupal\sms\Exception\SmsPluginReportException; use Drupal\sms\Entity\SmsGateway; use Drupal\sms\Event\SmsEvents; +use Drupal\sms\Message\SmsDeliveryReportInterface; +use Drupal\sms\Message\SmsMessageInterface; +use Drupal\sms\Message\SmsMessageResultInterface; /** * Handles messages before they are processed by queue(), send(), or incoming(). @@ -49,6 +54,61 @@ public function __construct(EventDispatcherInterface $event_dispatcher, ConfigFa $this->configFactory = $config_factory; } + /** + * Ensures there is a result, and reports for each recipient. + * + * @param \Drupal\sms\Event\SmsMessageEvent $event + * An SMS message process event. + */ + public function ensureReportsPreprocess(SmsMessageEvent $event) { + $sms_messages = $event->getMessages(); + foreach ($sms_messages as $sms_message) { + // Event can be for any direction. Capture incoming only for preprocess. + if ($sms_message->getDirection() == Direction::INCOMING) { + $this->ensureReports($sms_message); + } + } + } + + /** + * Ensures there is a result, and reports for each recipient. + * + * @param \Drupal\sms\Event\SmsMessageEvent $event + * An SMS message process event. + */ + public function ensureReportsPostprocess(SmsMessageEvent $event) { + $sms_messages = $event->getMessages(); + foreach ($sms_messages as $sms_message) { + $this->ensureReports($sms_message); + } + } + + /** + * Ensures there is a result, and reports for each recipient. + * + * @param \Drupal\sms\Message\SmsMessageInterface $sms_message + * A message to validate. + * + * @throws \Drupal\sms\Exception\SmsPluginReportException + * Thrown if result or reports are invalid. + */ + protected function ensureReports(SmsMessageInterface $sms_message) { + $result = $sms_message->getResult(); + if (!$result instanceof SmsMessageResultInterface) { + throw new SmsPluginReportException('Missing report for message.'); + } + + $message_recipients = $sms_message->getRecipients(); + $result_recipients = array_map(function(SmsDeliveryReportInterface $report) { + return $report->getRecipient(); + }, $result->getReports()); + + $difference_count = count(array_diff($message_recipients, $result_recipients)); + if ($difference_count) { + throw new SmsPluginReportException(sprintf('Missing reports for %s recipient(s).', $difference_count)); + } + } + /** * Ensures there is at least one recipient on the message. * @@ -194,10 +254,17 @@ public function chunkMaxRecipients(SmsMessageEvent $event) { * {@inheritdoc} */ public static function getSubscribedEvents() { + // Ensure reports for incoming messages. + $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureReportsPreprocess', 1024]; + $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureRecipients', 1024]; $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureGateways', 1024]; $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['deliveryReportUrl']; $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['chunkMaxRecipients', -1024]; + + // Ensure reports for outgoing messages. + $events[SmsEvents::MESSAGE_OUTGOING_POST_PROCESS][] = ['ensureReportsPostprocess', 1024]; + return $events; } diff --git a/src/Exception/SmsPluginReportException.php b/src/Exception/SmsPluginReportException.php new file mode 100644 index 0000000..f4335c6 --- /dev/null +++ b/src/Exception/SmsPluginReportException.php @@ -0,0 +1,8 @@ +setDirection(Direction::OUTGOING); + $dispatch = !$sms->getOption('_skip_preprocess_event'); $sms_messages = $dispatch ? $this->dispatchEvent(SmsEvents::MESSAGE_PRE_PROCESS, [$sms])->getMessages() : [$sms]; $sms_messages = $this->dispatchEvent(SmsEvents::MESSAGE_OUTGOING_PRE_PROCESS, $sms_messages)->getMessages(); + // Iterate over messages individually since pre-process can modify the + // gateway used. foreach ($sms_messages as &$sms_message) { $plugin = $sms_message->getGateway()->getPlugin(); @@ -110,23 +114,24 @@ public function send(SmsMessageInterface $sms) { * {@inheritdoc} */ public function incoming(SmsMessageInterface $sms_message) { + $sms_message->setDirection(Direction::INCOMING); + + // Do not iterate over messages individually like outgoing, changing gateway + // in pre-process events do not apply to incoming. + $plugin = $sms_message->getGateway()->getPlugin(); + $dispatch = !$sms_message->getOption('_skip_preprocess_event'); $sms_messages = $dispatch ? $this->dispatchEvent(SmsEvents::MESSAGE_PRE_PROCESS, [$sms_message])->getMessages() : [$sms_message]; $sms_messages = $this->dispatchEvent(SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, $sms_messages)->getMessages(); - foreach ($sms_messages as &$sms_message) { - $plugin = $sms_message->getGateway()->getPlugin(); - if (!$plugin instanceof SmsGatewayPluginIncomingInterface) { - throw new SmsException(sprintf('Gateway plugin `%s` does not support incoming messages', $plugin->getPluginId())); - } - - $result = $plugin->incoming($sms_message); - $sms_message->setResult($result); - - $this->dispatchEvent(SmsEvents::MESSAGE_INCOMING_POST_PROCESS, [$sms_message]); - $this->dispatchEvent(SmsEvents::MESSAGE_POST_PROCESS, [$sms_message]); + if ($plugin instanceof SmsGatewayPluginIncomingEventInterface) { + $event = new SmsMessageEvent($sms_messages); + $plugin->incomingEvent($event); } + $this->dispatchEvent(SmsEvents::MESSAGE_INCOMING_POST_PROCESS, $sms_messages); + $this->dispatchEvent(SmsEvents::MESSAGE_POST_PROCESS, $sms_messages); + return $sms_messages; } diff --git a/src/Provider/SmsProviderInterface.php b/src/Provider/SmsProviderInterface.php index fde5d47..af9f7c7 100644 --- a/src/Provider/SmsProviderInterface.php +++ b/src/Provider/SmsProviderInterface.php @@ -44,15 +44,13 @@ public function queue(SmsMessageInterface $sms_message); public function send(SmsMessageInterface $sms); /** - * Handles a message received by the server. + * Handles a message sent from the gateway to the site. * * @param \Drupal\sms\Message\SmsMessageInterface $sms_message - * The message received. + * The incoming message to process. * * @return \Drupal\sms\Message\SmsMessageInterface[] - * The messages received in this incoming operation. The message received - * can be transformed into multiple messages depending on gateway and event - * subscribers. Therefore this function can return multiple results. + * The messages received in an incoming operation. */ public function incoming(SmsMessageInterface $sms_message); diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php index 2b36b47..52246eb 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php @@ -6,7 +6,8 @@ use Drupal\Component\Utility\Random; use Drupal\sms\Message\SmsDeliveryReport; use Drupal\sms\Plugin\SmsGatewayPluginBase; -use Drupal\sms\Plugin\SmsGatewayPluginIncomingInterface; +use Drupal\sms\Plugin\SmsGatewayPluginIncomingEventInterface; +use Drupal\sms\Event\SmsMessageEvent; use Drupal\sms\Message\SmsMessageInterface; use Drupal\sms\Message\SmsMessageResult; use Drupal\Core\Form\FormStateInterface; @@ -27,7 +28,7 @@ * credit_balance_available = TRUE, * ) */ -class Memory extends SmsGatewayPluginBase implements SmsGatewayPluginIncomingInterface { +class Memory extends SmsGatewayPluginBase implements SmsGatewayPluginIncomingEventInterface { /** * {@inheritdoc} @@ -89,16 +90,12 @@ public function send(SmsMessageInterface $sms_message) { /** * {@inheritdoc} */ - public function incoming(SmsMessageInterface $sms_message) { + public function incomingEvent(SmsMessageEvent $event) { // @todo Contents of this method are subject to proposals made in // https://www.drupal.org/node/2712579 // Set state so we test this method is executed, remove this after above is // addressed. \Drupal::state()->set('sms_test_gateway.memory.incoming', TRUE); - - $new_reports = $this->randomDeliveryReports($sms_message); - return (new SmsMessageResult()) - ->setReports($new_reports); } /** From 6c1edaa0379018e427ee898e287b94523319b0bc Mon Sep 17 00:00:00 2001 From: dpi Date: Sat, 3 Dec 2016 15:08:20 +0800 Subject: [PATCH 02/10] Fixed tests Added results and reports for incoming messages created in tests. Added createMessageResult test helper to create reports and result for a message. --- ...workUserAccountRegistrationServiceTest.php | 1 + src/Tests/SmsFrameworkTestTrait.php | 25 +++++++++++++++++++ .../Kernel/SmsFrameworkGatewayPluginTest.php | 3 ++- tests/src/Kernel/SmsFrameworkProviderTest.php | 21 +++------------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php index 6845588..0296abc 100644 --- a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php +++ b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php @@ -527,6 +527,7 @@ protected function sendIncomingMessage($sender_number, $message) { ->setDirection(Direction::INCOMING) ->setMessage($message) ->addRecipients($this->randomPhoneNumbers(1)); + $incoming->setResult($this->createMessageResult($incoming)); $this->smsProvider->queue($incoming); } diff --git a/src/Tests/SmsFrameworkTestTrait.php b/src/Tests/SmsFrameworkTestTrait.php index 8da2540..facfa2c 100644 --- a/src/Tests/SmsFrameworkTestTrait.php +++ b/src/Tests/SmsFrameworkTestTrait.php @@ -8,6 +8,9 @@ use Drupal\Component\Utility\Unicode; use Drupal\sms\Entity\SmsGatewayInterface; use Drupal\sms\Message\SmsMessage; +use Drupal\sms\Message\SmsDeliveryReport; +use Drupal\sms\Message\SmsMessageInterface; +use Drupal\sms\Message\SmsMessageResult; /** * Shared SMS Framework helpers for kernel and web tests. @@ -218,6 +221,28 @@ protected function getLastVerification() { return reset($verifications); } + /** + * Create a result and reports for a message. + * + * @param \Drupal\sms\Message\SmsMessageInterface $sms_message + * A message object. + * + * @return \Drupal\sms\Message\SmsMessageResult + * A message result with reports for each message recipient. + */ + protected function createMessageResult(SmsMessageInterface $sms_message) { + $reports = array_map( + function($recipient) { + return (new SmsDeliveryReport()) + ->setRecipient($recipient); + }, + $sms_message->getRecipients() + ); + + return (new SmsMessageResult()) + ->setReports($reports); + } + /** * Generates a random SMS message by the specified user. * diff --git a/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php b/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php index bf8f9a8..a5e71c8 100644 --- a/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php +++ b/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php @@ -30,7 +30,7 @@ protected function setUp() { } /** - * Tests if incoming hook is fired on a gateway plugin. + * Tests if incoming event is fired on a gateway plugin. */ public function testIncoming() { $gateway = $this->createMemoryGateway([ @@ -43,6 +43,7 @@ public function testIncoming() { ->setDirection(Direction::INCOMING) ->setMessage($this->randomString()) ->addRecipients($this->randomPhoneNumbers()); + $sms_message->setResult($this->createMessageResult($sms_message)); $this->smsProvider->queue($sms_message); $this->assertTrue(\Drupal::state()->get('sms_test_gateway.memory.incoming')); diff --git a/tests/src/Kernel/SmsFrameworkProviderTest.php b/tests/src/Kernel/SmsFrameworkProviderTest.php index 62458bb..3fae00b 100644 --- a/tests/src/Kernel/SmsFrameworkProviderTest.php +++ b/tests/src/Kernel/SmsFrameworkProviderTest.php @@ -92,7 +92,9 @@ public function testIncoming() { $sms_message = SmsMessage::create() ->setDirection(Direction::INCOMING) ->setMessage($message) - ->addRecipients($this->randomPhoneNumbers()); + ->addRecipients($this->randomPhoneNumbers()) + ->setGateway($this->gateway); + $sms_message->setResult($this->createMessageResult($sms_message)); $sms_messages = $this->smsProvider->incoming($sms_message); @@ -462,23 +464,6 @@ public function testEventsIncoming() { $this->assertEquals($expected, $execution_order); } - /** - * Ensure SMS message is processed by the gateway. - */ - public function testIncomingGatewayProcessed() { - $this->gateway - ->setSkipQueue(TRUE) - ->save(); - - $sms_message = $this->createSmsMessage() - ->setGateway($this->gateway) - ->setDirection(Direction::INCOMING) - ->addRecipients($this->randomPhoneNumbers()); - - $this->smsProvider->queue($sms_message); - $this->assertTrue(\Drupal::state()->get('sms_test_gateway.memory.incoming')); - } - /** * Create a SMS message entity for testing. * From d8c317074451ee6ad4c1de630a4c1197c3dd7f91 Mon Sep 17 00:00:00 2001 From: dpi Date: Sat, 3 Dec 2016 16:12:01 +0800 Subject: [PATCH 03/10] Fixed failing tests due to missing reports --- modules/sms_devel/src/Form/SmsDevelMessageForm.php | 14 ++++++++++++++ .../tests/src/Kernel/SmsFrameworkUserTest.php | 1 + src/EventSubscriber/SmsMessageProcessor.php | 14 +++++++------- src/Tests/SmsFrameworkWebTest.php | 1 + tests/src/Kernel/SmsFrameworkProviderTest.php | 4 ++++ tests/src/Kernel/SmsFrameworkQueueTest.php | 7 +++---- tests/src/Kernel/SmsFrameworkViewsTest.php | 1 + 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/modules/sms_devel/src/Form/SmsDevelMessageForm.php b/modules/sms_devel/src/Form/SmsDevelMessageForm.php index 371e515..067518e 100644 --- a/modules/sms_devel/src/Form/SmsDevelMessageForm.php +++ b/modules/sms_devel/src/Form/SmsDevelMessageForm.php @@ -12,6 +12,8 @@ use Drupal\sms\Direction; use Drupal\sms\Exception\SmsException; use Drupal\sms\Message\SmsMessageResultInterface; +use Drupal\sms\Message\SmsDeliveryReport; +use Drupal\sms\Message\SmsMessageResult; /** * Simulate a message being sent or received. @@ -166,6 +168,18 @@ public function validateForm(array &$form, FormStateInterface $form_state) { public function submitReceive(array &$form, FormStateInterface $form_state) { $this->message->setDirection(Direction::INCOMING); + // Create some fake reports. + $reports = array_map( + function($recipient) { + return (new SmsDeliveryReport()) + ->setRecipient($recipient); + }, + $this->message->getRecipients() + ); + $result = (new SmsMessageResult()) + ->setReports($reports); + $this->message->setResult($result); + if ($form_state->getValue('skip_queue')) { $messages = $this->smsProvider->incoming($this->message); foreach ($messages as $message) { diff --git a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php index 3238e02..a2ea54e 100644 --- a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php +++ b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php @@ -77,6 +77,7 @@ public function testAccountRegistrationNoPhoneSettings() { ->setDirection(Direction::INCOMING) ->setMessage($message) ->addRecipients($this->randomPhoneNumbers(1)); + $incoming->setResult($this->createMessageResult($incoming)); $this->smsProvider->queue($incoming); $this->assertEquals($message, sms_test_gateway_get_incoming()['message']); diff --git a/src/EventSubscriber/SmsMessageProcessor.php b/src/EventSubscriber/SmsMessageProcessor.php index 90ee8f7..bd5ce99 100644 --- a/src/EventSubscriber/SmsMessageProcessor.php +++ b/src/EventSubscriber/SmsMessageProcessor.php @@ -95,13 +95,16 @@ public function ensureReportsPostprocess(SmsMessageEvent $event) { protected function ensureReports(SmsMessageInterface $sms_message) { $result = $sms_message->getResult(); if (!$result instanceof SmsMessageResultInterface) { - throw new SmsPluginReportException('Missing report for message.'); + throw new SmsPluginReportException('Missing result for message.'); } $message_recipients = $sms_message->getRecipients(); - $result_recipients = array_map(function(SmsDeliveryReportInterface $report) { - return $report->getRecipient(); - }, $result->getReports()); + $result_recipients = array_map( + function(SmsDeliveryReportInterface $report) { + return $report->getRecipient(); + }, + $result->getReports() + ); $difference_count = count(array_diff($message_recipients, $result_recipients)); if ($difference_count) { @@ -256,15 +259,12 @@ public function chunkMaxRecipients(SmsMessageEvent $event) { public static function getSubscribedEvents() { // Ensure reports for incoming messages. $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureReportsPreprocess', 1024]; - $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureRecipients', 1024]; $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureGateways', 1024]; $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['deliveryReportUrl']; $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['chunkMaxRecipients', -1024]; - // Ensure reports for outgoing messages. $events[SmsEvents::MESSAGE_OUTGOING_POST_PROCESS][] = ['ensureReportsPostprocess', 1024]; - return $events; } diff --git a/src/Tests/SmsFrameworkWebTest.php b/src/Tests/SmsFrameworkWebTest.php index 26176ed..8fdea2e 100644 --- a/src/Tests/SmsFrameworkWebTest.php +++ b/src/Tests/SmsFrameworkWebTest.php @@ -29,6 +29,7 @@ public function testQueueReport() { for ($i = 0; $i < 2; $i++) { $clone = $sms_message->createDuplicate() ->setDirection(Direction::INCOMING); + $clone->setResult($this->createMessageResult($clone)); $provider->queue($clone); } for ($i = 0; $i < 4; $i++) { diff --git a/tests/src/Kernel/SmsFrameworkProviderTest.php b/tests/src/Kernel/SmsFrameworkProviderTest.php index 3fae00b..67de799 100644 --- a/tests/src/Kernel/SmsFrameworkProviderTest.php +++ b/tests/src/Kernel/SmsFrameworkProviderTest.php @@ -198,6 +198,7 @@ public function testQueueIn() { ->addRecipients($this->randomPhoneNumbers()) ->setMessage($this->randomString()) ->setDirection(Direction::INCOMING); + $sms_message->setResult($this->createMessageResult($sms_message)); $sms_messages = $this->smsStorage ->loadByProperties(['direction' => Direction::INCOMING]); @@ -369,6 +370,7 @@ public function testEventsQueueIncoming() { ->setGateway($this->gateway) ->setDirection(Direction::INCOMING) ->addRecipients($this->randomPhoneNumbers()); + $sms_message->setResult($this->createMessageResult($sms_message)); $this->smsProvider->queue($sms_message); @@ -407,6 +409,7 @@ public function testEventsQueueIncomingSkipQueue() { ->setGateway($this->gateway) ->setDirection(Direction::INCOMING) ->addRecipients($this->randomPhoneNumbers()); + $sms_message->setResult($this->createMessageResult($sms_message)); $this->smsProvider->queue($sms_message); @@ -451,6 +454,7 @@ public function testEventsIncoming() { ->setGateway($this->gateway) ->setDirection(Direction::INCOMING) ->addRecipients($this->randomPhoneNumbers()); + $sms_message->setResult($this->createMessageResult($sms_message)); $this->smsProvider->incoming($sms_message); diff --git a/tests/src/Kernel/SmsFrameworkQueueTest.php b/tests/src/Kernel/SmsFrameworkQueueTest.php index 8246272..54333d5 100644 --- a/tests/src/Kernel/SmsFrameworkQueueTest.php +++ b/tests/src/Kernel/SmsFrameworkQueueTest.php @@ -85,10 +85,9 @@ public function testProcessUnqueued() { */ public function testQueueIncoming() { $sms_message = $this->createSmsMessage() - ->setDirection(Direction::INCOMING); - - // @todo sms_test_gateway_sms_incoming() requires an incoming recipient. - $sms_message->addRecipients($this->randomPhoneNumbers()); + ->setDirection(Direction::INCOMING) + ->addRecipients($this->randomPhoneNumbers()); + $sms_message->setResult($this->createMessageResult($sms_message)); $this->smsProvider->queue($sms_message); $this->assertEquals(0, count($this->getTestMessages($this->gateway)), 'Message not received yet.'); diff --git a/tests/src/Kernel/SmsFrameworkViewsTest.php b/tests/src/Kernel/SmsFrameworkViewsTest.php index d7898af..5df0fe2 100644 --- a/tests/src/Kernel/SmsFrameworkViewsTest.php +++ b/tests/src/Kernel/SmsFrameworkViewsTest.php @@ -110,6 +110,7 @@ public function testSms() { ->setMessage($this->randomMachineName()) ->setAutomated(FALSE) ->setProcessedTime(499488280); + $message2->setResult($this->createMessageResult($message2)); $this->smsProvider->queue($message2); Views::viewsData()->clear(); From 05ccf210234a0be0084d23ceb0fc3e79de797e7a Mon Sep 17 00:00:00 2001 From: dpi Date: Sun, 4 Dec 2016 15:57:05 +0800 Subject: [PATCH 04/10] Test coverage for new result/report exceptions Compacted sms_test_gateway gateway config schema (Derivative types) Added plugin incoming method to execution order tests. New test file covering \Drupal\sms\EventSubscriber\SmsMessageProcessor (some \Drupal\Tests\sms\Kernel\SmsFrameworkProviderTest tests should be moved there) --- .../config/schema/sms_test_gateway.schema.yml | 37 ++--- .../src/Plugin/SmsGateway/Memory.php | 4 + .../SmsGateway/MemoryOutgoingResult.php | 46 ++++++ .../Kernel/SmsFrameworkGatewayPluginTest.php | 2 +- .../src/Kernel/SmsFrameworkProcessorTest.php | 154 ++++++++++++++++++ tests/src/Kernel/SmsFrameworkProviderTest.php | 4 + 6 files changed, 223 insertions(+), 24 deletions(-) create mode 100644 tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php create mode 100644 tests/src/Kernel/SmsFrameworkProcessorTest.php diff --git a/tests/modules/sms_test_gateway/config/schema/sms_test_gateway.schema.yml b/tests/modules/sms_test_gateway/config/schema/sms_test_gateway.schema.yml index 7abb3a9..e5c479a 100644 --- a/tests/modules/sms_test_gateway/config/schema/sms_test_gateway.schema.yml +++ b/tests/modules/sms_test_gateway/config/schema/sms_test_gateway.schema.yml @@ -1,38 +1,29 @@ -sms_gateway.settings.memory: +sms_test_gateway_base: type: sms_gateway_settings - label: 'Memory' mapping: gateway_id: type: string label: 'ID of the gateway config entity.' + +sms_gateway.settings.memory: + type: sms_test_gateway_base + label: 'Memory' + mapping: widget: type: string label: 'Widget' + sms_gateway.settings.memory_chunked: - type: sms_gateway_settings - label: 'Memory Chunked' - mapping: - gateway_id: - type: string - label: 'ID of the gateway config entity.' + type: sms_test_gateway_base sms_gateway.settings.memory_noincoming: - type: sms_gateway_settings - label: 'No incoming' - mapping: - gateway_id: - type: string + type: sms_test_gateway_base + +sms_gateway.settings.memory_outgoing_result: + type: sms_gateway.settings.memory sms_gateway.settings.memory_schedule_aware: - type: sms_gateway_settings - label: 'Schedule aware' - mapping: - gateway_id: - type: string + type: sms_test_gateway_base sms_gateway.settings.capabilities_default: - type: sms_gateway_settings - label: 'Default annotation capabilities' - mapping: - gateway_id: - type: string + type: sms_test_gateway_base diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php index 52246eb..7007921 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php @@ -96,6 +96,10 @@ public function incomingEvent(SmsMessageEvent $event) { // Set state so we test this method is executed, remove this after above is // addressed. \Drupal::state()->set('sms_test_gateway.memory.incoming', TRUE); + + $execution_order = \Drupal::state()->get('sms_test_event_subscriber__execution_order', []); + $execution_order[] = __FUNCTION__; + \Drupal::state()->set('sms_test_event_subscriber__execution_order', $execution_order); } /** diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php new file mode 100644 index 0000000..5f6915c --- /dev/null +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php @@ -0,0 +1,46 @@ +get('sms_test_gateway.memory_outgoing_result.missing_result')) { + return NULL; + } + + $delete_reports = \Drupal::state()->get('sms_test_gateway.memory_outgoing_result.delete_reports'); + if ($delete_reports > 0) { + $reports = $result->getReports(); + + if (!count($reports)) { + throw new \Exception('There are no reports to delete.'); + } + + // Slice of the first {$delete_reports}x reports. + $reports = array_slice($reports, $delete_reports); + + $result->setReports($reports); + return $result; + } + + return $result; + } + +} diff --git a/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php b/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php index a5e71c8..dde050e 100644 --- a/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php +++ b/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php @@ -32,7 +32,7 @@ protected function setUp() { /** * Tests if incoming event is fired on a gateway plugin. */ - public function testIncoming() { + public function testIncomingEvent() { $gateway = $this->createMemoryGateway([ 'plugin' => 'memory', ])->setSkipQueue(TRUE); diff --git a/tests/src/Kernel/SmsFrameworkProcessorTest.php b/tests/src/Kernel/SmsFrameworkProcessorTest.php new file mode 100644 index 0000000..4ce28c1 --- /dev/null +++ b/tests/src/Kernel/SmsFrameworkProcessorTest.php @@ -0,0 +1,154 @@ +installEntitySchema('sms'); + + $this->gatewayMemory = $this->createMemoryGateway(); + $this->gatewayOutgoingResult = $this->createMemoryGateway(['plugin' => 'memory_outgoing_result']); + $this->smsStorage = $this->container->get('entity_type.manager') + ->getStorage('sms'); + $this->smsProvider = $this->container->get('sms.provider'); + } + + /** + * Ensure exception thrown if incoming message is missing a result. + * + * @covers ::ensureReportsPreprocess + */ + public function testIncomingNoResult() { + $sms_message = SmsMessage::create() + ->setDirection(Direction::INCOMING) + ->setMessage($this->randomString()) + ->addRecipients($this->randomPhoneNumbers()) + ->setGateway($this->gatewayMemory); + + $this->setExpectedException(SmsPluginReportException::class, 'Missing result for message.'); + $this->smsProvider->queue($sms_message); + } + + /** + * Ensure exception thrown if incoming message is missing recipient reports. + * + * @covers ::ensureReportsPreprocess + */ + public function testIncomingMissingReports() { + $result = new SmsMessageResult(); + $sms_message = SmsMessage::create() + ->setDirection(Direction::INCOMING) + ->setMessage($this->randomString()) + ->addRecipients($this->randomPhoneNumbers()) + ->setGateway($this->gatewayMemory) + ->setResult($result); + + $recipient_count = count($sms_message->getRecipients()); + $this->setExpectedException(SmsPluginReportException::class, "Missing reports for $recipient_count recipient(s)."); + $this->smsProvider->queue($sms_message); + } + + /** + * Ensure exception thrown if incoming message is missing a result. + * + * @covers ::ensureReportsPreprocess + */ + public function testOutgoingNoResult() { + $this->setFallbackGateway($this->gatewayOutgoingResult); + + \Drupal::state()->set('sms_test_gateway.memory_outgoing_result.missing_result', TRUE); + + // Must skip queue for send() for post-process to run. + $this->gatewayOutgoingResult + ->setSkipQueue(TRUE) + ->save(); + + $sms_message = SmsMessage::create() + ->setDirection(Direction::OUTGOING) + ->setMessage($this->randomString()) + ->addRecipients($this->randomPhoneNumbers()); + + $this->setExpectedException(SmsPluginReportException::class, 'Missing result for message.'); + $this->smsProvider->queue($sms_message); + } + + + /** + * Ensure exception thrown if outgoing message is missing recipient reports. + * + * @covers ::ensureReportsPreprocess + */ + public function testOutgoingMissingReports() { + $this->setFallbackGateway($this->gatewayOutgoingResult); + + $delete_count = rand(1, 5); + \Drupal::state()->set('sms_test_gateway.memory_outgoing_result.delete_reports', $delete_count); + + // Must skip queue for send() for post-process to run. + $this->gatewayOutgoingResult + ->setSkipQueue(TRUE) + ->save(); + + $sms_message = SmsMessage::create() + ->setDirection(Direction::OUTGOING) + ->setMessage($this->randomString()) + ->addRecipients($this->randomPhoneNumbers($delete_count + 1)); + + $this->setExpectedException(SmsPluginReportException::class, "Missing reports for $delete_count recipient(s)."); + $this->smsProvider->queue($sms_message); + } + +} diff --git a/tests/src/Kernel/SmsFrameworkProviderTest.php b/tests/src/Kernel/SmsFrameworkProviderTest.php index 67de799..b4aa589 100644 --- a/tests/src/Kernel/SmsFrameworkProviderTest.php +++ b/tests/src/Kernel/SmsFrameworkProviderTest.php @@ -387,6 +387,7 @@ public function testEventsQueueIncoming() { $this->container->get('cron')->run(); $expected[] = SmsEvents::MESSAGE_INCOMING_PRE_PROCESS; + $expected[] = 'incomingEvent'; // Plugin incoming event. $expected[] = SmsEvents::MESSAGE_INCOMING_POST_PROCESS; $expected[] = SmsEvents::MESSAGE_POST_PROCESS; @@ -417,6 +418,8 @@ public function testEventsQueueIncomingSkipQueue() { SmsEvents::MESSAGE_PRE_PROCESS, SmsEvents::MESSAGE_QUEUE_PRE_PROCESS, SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, + // Plugin incoming event. + 'incomingEvent', SmsEvents::MESSAGE_INCOMING_POST_PROCESS, SmsEvents::MESSAGE_POST_PROCESS, SmsEvents::MESSAGE_QUEUE_POST_PROCESS, @@ -461,6 +464,7 @@ public function testEventsIncoming() { $expected = [ SmsEvents::MESSAGE_PRE_PROCESS, SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, + 'incomingEvent', SmsEvents::MESSAGE_INCOMING_POST_PROCESS, SmsEvents::MESSAGE_POST_PROCESS, ]; From ffbe6a1e1170f81c452f55fa81ddc77a86325816 Mon Sep 17 00:00:00 2001 From: dpi Date: Sun, 4 Dec 2016 17:54:02 +0800 Subject: [PATCH 05/10] Added supports incoming messages gateway annotation property. Added SmsGateway::supportsIncoming() Add preprocess check to incoming messages ensuring they have a gateway set, and the gateway supports incoming messages. Added tests for above. Added missing gateway entity to incoming message tests. --- ...workUserAccountRegistrationServiceTest.php | 3 +- .../tests/src/Kernel/SmsFrameworkUserTest.php | 3 +- src/Annotation/SmsGateway.php | 7 ++++ src/Entity/SmsGateway.php | 9 +++++ src/Entity/SmsGatewayInterface.php | 8 +++++ src/EventSubscriber/SmsMessageProcessor.php | 23 ++++++++++++ src/Tests/SmsFrameworkWebTest.php | 5 ++- .../src/Plugin/SmsGateway/Memory.php | 1 + .../Kernel/SmsFrameworkGatewayEntityTest.php | 20 +++++++++++ .../Kernel/SmsFrameworkGatewayPluginTest.php | 9 +++-- .../src/Kernel/SmsFrameworkProcessorTest.php | 36 +++++++++++++++++++ tests/src/Kernel/SmsFrameworkProviderTest.php | 3 +- tests/src/Kernel/SmsFrameworkQueueTest.php | 3 +- tests/src/Kernel/SmsFrameworkViewsTest.php | 3 +- 14 files changed, 122 insertions(+), 11 deletions(-) diff --git a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php index 0296abc..91f5aa7 100644 --- a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php +++ b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserAccountRegistrationServiceTest.php @@ -526,7 +526,8 @@ protected function sendIncomingMessage($sender_number, $message) { ->setSenderNumber($sender_number) ->setDirection(Direction::INCOMING) ->setMessage($message) - ->addRecipients($this->randomPhoneNumbers(1)); + ->addRecipients($this->randomPhoneNumbers(1)) + ->setGateway($this->gateway); $incoming->setResult($this->createMessageResult($incoming)); $this->smsProvider->queue($incoming); } diff --git a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php index a2ea54e..396e45b 100644 --- a/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php +++ b/modules/sms_user/tests/src/Kernel/SmsFrameworkUserTest.php @@ -76,7 +76,8 @@ public function testAccountRegistrationNoPhoneSettings() { ->setSenderNumber('+123') ->setDirection(Direction::INCOMING) ->setMessage($message) - ->addRecipients($this->randomPhoneNumbers(1)); + ->addRecipients($this->randomPhoneNumbers(1)) + ->setGateway($this->gateway); $incoming->setResult($this->createMessageResult($incoming)); $this->smsProvider->queue($incoming); diff --git a/src/Annotation/SmsGateway.php b/src/Annotation/SmsGateway.php index db9c531..6d34233 100644 --- a/src/Annotation/SmsGateway.php +++ b/src/Annotation/SmsGateway.php @@ -34,6 +34,13 @@ class SmsGateway extends Plugin { */ protected $outgoing_message_max_recipients; + /** + * Whether the gateway supports receiving messages. + * + * @var boolean + */ + protected $incoming; + /** * Whether the gateway is capable of delaying messages until a date. * diff --git a/src/Entity/SmsGateway.php b/src/Entity/SmsGateway.php index c7a7ced..aabaf79 100644 --- a/src/Entity/SmsGateway.php +++ b/src/Entity/SmsGateway.php @@ -248,6 +248,15 @@ public function getMaxRecipientsOutgoing() { return isset($definition['outgoing_message_max_recipients']) ? (int) $definition['outgoing_message_max_recipients'] : 1; } + /** + * {@inheritdoc} + */ + public function supportsIncoming() { + $definition = $this->getPlugin() + ->getPluginDefinition(); + return isset($definition['incoming']) ? (boolean) $definition['incoming'] : FALSE; + } + /** * {@inheritdoc} */ diff --git a/src/Entity/SmsGatewayInterface.php b/src/Entity/SmsGatewayInterface.php index eb92853..497ff57 100644 --- a/src/Entity/SmsGatewayInterface.php +++ b/src/Entity/SmsGatewayInterface.php @@ -105,6 +105,14 @@ public function setRetentionDuration($direction, $retention_duration); */ public function getMaxRecipientsOutgoing(); + /** + * Whether the gateway supports receiving messages. + * + * @return boolean + * Whether the gateway supports receiving messages. + */ + public function supportsIncoming(); + /** * Get whether this gateway is schedule aware. * diff --git a/src/EventSubscriber/SmsMessageProcessor.php b/src/EventSubscriber/SmsMessageProcessor.php index bd5ce99..1ee0524 100644 --- a/src/EventSubscriber/SmsMessageProcessor.php +++ b/src/EventSubscriber/SmsMessageProcessor.php @@ -12,6 +12,7 @@ use Drupal\sms\Event\RecipientGatewayEvent; use Drupal\sms\Event\SmsMessageEvent; use Drupal\sms\Exception\RecipientRouteException; +use Drupal\sms\Exception\SmsException; use Drupal\sms\Exception\SmsPluginReportException; use Drupal\sms\Entity\SmsGateway; use Drupal\sms\Event\SmsEvents; @@ -54,6 +55,27 @@ public function __construct(EventDispatcherInterface $event_dispatcher, ConfigFa $this->configFactory = $config_factory; } + /** + * Ensures gateway supports incoming messages. + * + * @param \Drupal\sms\Event\SmsMessageEvent $event + * An SMS message process event. + */ + public function ensureIncomingSupport(SmsMessageEvent $event) { + $sms_messages = $event->getMessages(); + foreach ($sms_messages as $sms_message) { + if ($sms_message->getDirection() == Direction::INCOMING) { + $gateway = $sms_message->getGateway(); + if (!$gateway instanceof SmsGatewayInterface) { + throw new SmsException('Gateway not set on incoming message'); + } + if (!$gateway->supportsIncoming()) { + throw new SmsException(sprintf('Gateway `%s` does not support incoming messages.', $gateway->id())); + } + } + } + } + /** * Ensures there is a result, and reports for each recipient. * @@ -257,6 +279,7 @@ public function chunkMaxRecipients(SmsMessageEvent $event) { * {@inheritdoc} */ public static function getSubscribedEvents() { + $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureIncomingSupport', 1024]; // Ensure reports for incoming messages. $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureReportsPreprocess', 1024]; $events[SmsEvents::MESSAGE_PRE_PROCESS][] = ['ensureRecipients', 1024]; diff --git a/src/Tests/SmsFrameworkWebTest.php b/src/Tests/SmsFrameworkWebTest.php index 8fdea2e..5014bf9 100644 --- a/src/Tests/SmsFrameworkWebTest.php +++ b/src/Tests/SmsFrameworkWebTest.php @@ -17,6 +17,8 @@ class SmsFrameworkWebTest extends SmsFrameworkWebTestBase { * Tests queue statistics located on Drupal report page. */ public function testQueueReport() { + $gateway = $this->createMemoryGateway(); + /** @var \Drupal\sms\Provider\SmsProviderInterface $provider */ $provider = \Drupal::service('sms.provider'); @@ -28,7 +30,8 @@ public function testQueueReport() { for ($i = 0; $i < 2; $i++) { $clone = $sms_message->createDuplicate() - ->setDirection(Direction::INCOMING); + ->setDirection(Direction::INCOMING) + ->setGateway($gateway); $clone->setResult($this->createMessageResult($clone)); $provider->queue($clone); } diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php index 7007921..401e7d0 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php @@ -22,6 +22,7 @@ * id = "memory", * label = @Translation("Memory"), * outgoing_message_max_recipients = -1, + * incoming = TRUE, * schedule_aware = FALSE, * reports_pull = TRUE, * reports_push = TRUE, diff --git a/tests/src/Kernel/SmsFrameworkGatewayEntityTest.php b/tests/src/Kernel/SmsFrameworkGatewayEntityTest.php index b7f4f91..68869b6 100644 --- a/tests/src/Kernel/SmsFrameworkGatewayEntityTest.php +++ b/tests/src/Kernel/SmsFrameworkGatewayEntityTest.php @@ -151,6 +151,26 @@ public function testGetMaxRecipientsOutgoingDefault() { $this->assertEquals(1, $gateway->getMaxRecipientsOutgoing()); } + /** + * Tests 'incoming' annotation custom value. + */ + public function testSupportsIncomingCustom() { + $gateway = $this->createGateway([ + 'plugin' => 'memory', + ]); + $this->assertEquals(TRUE, $gateway->supportsIncoming()); + } + + /** + * Tests 'incoming' annotation default value. + */ + public function testSupportsIncomingDefault() { + $gateway = $this->createGateway([ + 'plugin' => 'capabilities_default', + ]); + $this->assertEquals(FALSE, $gateway->supportsIncoming()); + } + /** * Tests 'schedule aware annotation' custom value. */ diff --git a/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php b/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php index dde050e..7875e56 100644 --- a/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php +++ b/tests/src/Kernel/SmsFrameworkGatewayPluginTest.php @@ -33,16 +33,15 @@ protected function setUp() { * Tests if incoming event is fired on a gateway plugin. */ public function testIncomingEvent() { - $gateway = $this->createMemoryGateway([ - 'plugin' => 'memory', - ])->setSkipQueue(TRUE); + $gateway = $this->createMemoryGateway() + ->setSkipQueue(TRUE); $gateway->save(); - $this->setFallbackGateway($gateway); $sms_message = SmsMessage::create() ->setDirection(Direction::INCOMING) ->setMessage($this->randomString()) - ->addRecipients($this->randomPhoneNumbers()); + ->addRecipients($this->randomPhoneNumbers()) + ->setGateway($gateway); $sms_message->setResult($this->createMessageResult($sms_message)); $this->smsProvider->queue($sms_message); diff --git a/tests/src/Kernel/SmsFrameworkProcessorTest.php b/tests/src/Kernel/SmsFrameworkProcessorTest.php index 4ce28c1..1a9fa9c 100644 --- a/tests/src/Kernel/SmsFrameworkProcessorTest.php +++ b/tests/src/Kernel/SmsFrameworkProcessorTest.php @@ -4,6 +4,7 @@ use Drupal\sms\Direction; use Drupal\sms\Entity\SmsMessage; +use Drupal\sms\Exception\SmsException; use Drupal\sms\Exception\SmsPluginReportException; use Drupal\sms\Message\SmsMessageResult; @@ -151,4 +152,39 @@ public function testOutgoingMissingReports() { $this->smsProvider->queue($sms_message); } + /** + * Tests exception is thrown if gateway is not set on incoming messages. + * + * @covers ::ensureIncomingSupport + */ + public function testIncomingMissingGateway() { + $sms_message = SmsMessage::create() + ->setDirection(Direction::INCOMING) + ->setMessage($this->randomString()) + ->addRecipients($this->randomPhoneNumbers()); + + $this->setExpectedException(SmsException::class, 'Gateway not set on incoming message'); + $this->smsProvider->queue($sms_message); + } + + /** + * Tests exception is thrown if gateway does not support incoming messages. + * + * @covers ::ensureIncomingSupport + */ + public function testIncomingUnSupported() { + $gateway = $this->createMemoryGateway([ + 'plugin' => 'capabilities_default', + ]); + + $sms_message = SmsMessage::create() + ->setDirection(Direction::INCOMING) + ->setMessage($this->randomString()) + ->addRecipients($this->randomPhoneNumbers()) + ->setGateway($gateway); + + $this->setExpectedException(SmsException::class, "Gateway `" . $gateway->id() . "` does not support incoming messages."); + $this->smsProvider->queue($sms_message); + } + } diff --git a/tests/src/Kernel/SmsFrameworkProviderTest.php b/tests/src/Kernel/SmsFrameworkProviderTest.php index b4aa589..030e36e 100644 --- a/tests/src/Kernel/SmsFrameworkProviderTest.php +++ b/tests/src/Kernel/SmsFrameworkProviderTest.php @@ -197,7 +197,8 @@ public function testQueueIn() { $sms_message ->addRecipients($this->randomPhoneNumbers()) ->setMessage($this->randomString()) - ->setDirection(Direction::INCOMING); + ->setDirection(Direction::INCOMING) + ->setGateway($this->gateway); $sms_message->setResult($this->createMessageResult($sms_message)); $sms_messages = $this->smsStorage diff --git a/tests/src/Kernel/SmsFrameworkQueueTest.php b/tests/src/Kernel/SmsFrameworkQueueTest.php index 54333d5..7a9222e 100644 --- a/tests/src/Kernel/SmsFrameworkQueueTest.php +++ b/tests/src/Kernel/SmsFrameworkQueueTest.php @@ -86,7 +86,8 @@ public function testProcessUnqueued() { public function testQueueIncoming() { $sms_message = $this->createSmsMessage() ->setDirection(Direction::INCOMING) - ->addRecipients($this->randomPhoneNumbers()); + ->addRecipients($this->randomPhoneNumbers()) + ->setGateway($this->gateway); $sms_message->setResult($this->createMessageResult($sms_message)); $this->smsProvider->queue($sms_message); diff --git a/tests/src/Kernel/SmsFrameworkViewsTest.php b/tests/src/Kernel/SmsFrameworkViewsTest.php index 5df0fe2..66f5c6c 100644 --- a/tests/src/Kernel/SmsFrameworkViewsTest.php +++ b/tests/src/Kernel/SmsFrameworkViewsTest.php @@ -109,7 +109,8 @@ public function testSms() { ->setDirection(Direction::INCOMING) ->setMessage($this->randomMachineName()) ->setAutomated(FALSE) - ->setProcessedTime(499488280); + ->setProcessedTime(499488280) + ->setGateway($this->gateway); $message2->setResult($this->createMessageResult($message2)); $this->smsProvider->queue($message2); From f152bfb6e0fba8fc729958d9a4089a9a4003268e Mon Sep 17 00:00:00 2001 From: dpi Date: Sun, 11 Dec 2016 14:13:54 +0800 Subject: [PATCH 06/10] Addressed some documentation nits from first review. --- .../src/Plugin/SmsGateway/MemoryOutgoingResult.php | 2 +- tests/src/Kernel/SmsFrameworkProcessorTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php index 5f6915c..93dc2a0 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryOutgoingResult.php @@ -33,7 +33,7 @@ public function send(SmsMessageInterface $sms_message) { throw new \Exception('There are no reports to delete.'); } - // Slice of the first {$delete_reports}x reports. + // Slice off the first {$delete_reports}x reports. $reports = array_slice($reports, $delete_reports); $result->setReports($reports); diff --git a/tests/src/Kernel/SmsFrameworkProcessorTest.php b/tests/src/Kernel/SmsFrameworkProcessorTest.php index 1a9fa9c..9a1326f 100644 --- a/tests/src/Kernel/SmsFrameworkProcessorTest.php +++ b/tests/src/Kernel/SmsFrameworkProcessorTest.php @@ -103,7 +103,7 @@ public function testIncomingMissingReports() { } /** - * Ensure exception thrown if incoming message is missing a result. + * Ensure exception thrown if gateway send method did not return a result. * * @covers ::ensureReportsPreprocess */ From adb8ce7291ca6f0034a320137be3ae806e105b40 Mon Sep 17 00:00:00 2001 From: dpi Date: Mon, 12 Dec 2016 21:08:45 +0800 Subject: [PATCH 07/10] Rename SmsGatewayPluginIncomingEventInterface to SmsIncomingEventInterface --- .../SmsIncomingEventInterface.php} | 4 ++-- src/Provider/DefaultSmsProvider.php | 4 ++-- .../modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/Plugin/{SmsGatewayPluginIncomingEventInterface.php => SmsGateway/SmsIncomingEventInterface.php} (79%) diff --git a/src/Plugin/SmsGatewayPluginIncomingEventInterface.php b/src/Plugin/SmsGateway/SmsIncomingEventInterface.php similarity index 79% rename from src/Plugin/SmsGatewayPluginIncomingEventInterface.php rename to src/Plugin/SmsGateway/SmsIncomingEventInterface.php index 962e2cf..82b9110 100644 --- a/src/Plugin/SmsGatewayPluginIncomingEventInterface.php +++ b/src/Plugin/SmsGateway/SmsIncomingEventInterface.php @@ -1,13 +1,13 @@ dispatchEvent(SmsEvents::MESSAGE_PRE_PROCESS, [$sms_message])->getMessages() : [$sms_message]; $sms_messages = $this->dispatchEvent(SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, $sms_messages)->getMessages(); - if ($plugin instanceof SmsGatewayPluginIncomingEventInterface) { + if ($plugin instanceof SmsIncomingEventInterface) { $event = new SmsMessageEvent($sms_messages); $plugin->incomingEvent($event); } diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php index 401e7d0..310a83a 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php @@ -6,7 +6,7 @@ use Drupal\Component\Utility\Random; use Drupal\sms\Message\SmsDeliveryReport; use Drupal\sms\Plugin\SmsGatewayPluginBase; -use Drupal\sms\Plugin\SmsGatewayPluginIncomingEventInterface; +use Drupal\sms\Plugin\SmsGateway\SmsIncomingEventInterface; use Drupal\sms\Event\SmsMessageEvent; use Drupal\sms\Message\SmsMessageInterface; use Drupal\sms\Message\SmsMessageResult; @@ -29,7 +29,7 @@ * credit_balance_available = TRUE, * ) */ -class Memory extends SmsGatewayPluginBase implements SmsGatewayPluginIncomingEventInterface { +class Memory extends SmsGatewayPluginBase implements SmsIncomingEventInterface { /** * {@inheritdoc} From f88fdc44a9fbe586ffb02b2e3de73fc3476fe6e8 Mon Sep 17 00:00:00 2001 From: dpi Date: Thu, 15 Dec 2016 20:42:45 +0800 Subject: [PATCH 08/10] Fixed some failing tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Made memory chunked gateway accept incoming for the tests that ensure incoming messages don’t get chunked --- .../sms_test_gateway/src/Plugin/SmsGateway/MemoryChunked.php | 1 + tests/src/Kernel/SmsFrameworkProviderTest.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryChunked.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryChunked.php index ccbad58..86585f1 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryChunked.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/MemoryChunked.php @@ -8,6 +8,7 @@ * @SmsGateway( * id = "memory_chunked", * label = @Translation("Memory Chunked"), + * incoming = TRUE, * outgoing_message_max_recipients = 2, * ) */ diff --git a/tests/src/Kernel/SmsFrameworkProviderTest.php b/tests/src/Kernel/SmsFrameworkProviderTest.php index 9ddebfd..5c62bf1 100644 --- a/tests/src/Kernel/SmsFrameworkProviderTest.php +++ b/tests/src/Kernel/SmsFrameworkProviderTest.php @@ -145,6 +145,7 @@ public function testIncomingNoDirection() { ->setMessage($this->randomString()) ->addRecipients($this->randomPhoneNumbers()) ->setGateway($this->incomingGateway); + $sms_message->setResult($this->createMessageResult($sms_message)); // This method will set direction. $this->smsProvider->incoming($sms_message); @@ -373,6 +374,7 @@ public function testIncomingNotChunked() { ->addRecipients($this->randomPhoneNumbers()) ->setDirection(Direction::INCOMING) ->setGateway($gateway_chunked); + $message->setResult($this->createMessageResult($message)); $this->smsProvider->queue($message); From 9f80bcd47ba70aec9413ca4fa8133fa3b781368f Mon Sep 17 00:00:00 2001 From: dpi Date: Thu, 15 Dec 2016 20:43:12 +0800 Subject: [PATCH 09/10] Assert full namespace+classname+method instead of just method name. https://github.com/dpi/smsframework/pull/60#discussion_r92595865 --- .../sms_test_gateway/src/Plugin/SmsGateway/Memory.php | 2 +- tests/src/Kernel/SmsFrameworkProviderTest.php | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php index 310a83a..f9ecc12 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php @@ -99,7 +99,7 @@ public function incomingEvent(SmsMessageEvent $event) { \Drupal::state()->set('sms_test_gateway.memory.incoming', TRUE); $execution_order = \Drupal::state()->get('sms_test_event_subscriber__execution_order', []); - $execution_order[] = __FUNCTION__; + $execution_order[] = __METHOD__; \Drupal::state()->set('sms_test_event_subscriber__execution_order', $execution_order); } diff --git a/tests/src/Kernel/SmsFrameworkProviderTest.php b/tests/src/Kernel/SmsFrameworkProviderTest.php index 5c62bf1..a9656b0 100644 --- a/tests/src/Kernel/SmsFrameworkProviderTest.php +++ b/tests/src/Kernel/SmsFrameworkProviderTest.php @@ -468,7 +468,7 @@ public function testEventsQueueIncoming() { $this->container->get('cron')->run(); $expected[] = SmsEvents::MESSAGE_INCOMING_PRE_PROCESS; - $expected[] = 'incomingEvent'; // Plugin incoming event. + $expected[] = 'Drupal\sms_test_gateway\Plugin\SmsGateway\Memory::incomingEvent'; $expected[] = SmsEvents::MESSAGE_INCOMING_POST_PROCESS; $expected[] = SmsEvents::MESSAGE_POST_PROCESS; @@ -499,8 +499,7 @@ public function testEventsQueueIncomingSkipQueue() { SmsEvents::MESSAGE_PRE_PROCESS, SmsEvents::MESSAGE_QUEUE_PRE_PROCESS, SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, - // Plugin incoming event. - 'incomingEvent', + 'Drupal\sms_test_gateway\Plugin\SmsGateway\Memory::incomingEvent', SmsEvents::MESSAGE_INCOMING_POST_PROCESS, SmsEvents::MESSAGE_POST_PROCESS, SmsEvents::MESSAGE_QUEUE_POST_PROCESS, @@ -545,7 +544,7 @@ public function testEventsIncoming() { $expected = [ SmsEvents::MESSAGE_PRE_PROCESS, SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, - 'incomingEvent', + 'Drupal\sms_test_gateway\Plugin\SmsGateway\Memory::incomingEvent', SmsEvents::MESSAGE_INCOMING_POST_PROCESS, SmsEvents::MESSAGE_POST_PROCESS, ]; From 957cf521e24a2ad35d4132768b2c1f5348a6dcb9 Mon Sep 17 00:00:00 2001 From: dpi Date: Thu, 15 Dec 2016 20:45:23 +0800 Subject: [PATCH 10/10] Renamed \Drupal\sms\Plugin\SmsGateway\SmsIncomingEventInterface to SmsIncomingEventProcessorInterface; --- ...ntInterface.php => SmsIncomingEventProcessorInterface.php} | 2 +- src/Provider/DefaultSmsProvider.php | 4 ++-- .../modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/Plugin/SmsGateway/{SmsIncomingEventInterface.php => SmsIncomingEventProcessorInterface.php} (88%) diff --git a/src/Plugin/SmsGateway/SmsIncomingEventInterface.php b/src/Plugin/SmsGateway/SmsIncomingEventProcessorInterface.php similarity index 88% rename from src/Plugin/SmsGateway/SmsIncomingEventInterface.php rename to src/Plugin/SmsGateway/SmsIncomingEventProcessorInterface.php index 82b9110..908fb07 100644 --- a/src/Plugin/SmsGateway/SmsIncomingEventInterface.php +++ b/src/Plugin/SmsGateway/SmsIncomingEventProcessorInterface.php @@ -7,7 +7,7 @@ /** * Interface for gateways implementing an incoming event subscriber. */ -interface SmsIncomingEventInterface { +interface SmsIncomingEventProcessorInterface { /** * Process a SMS message from this gateway. diff --git a/src/Provider/DefaultSmsProvider.php b/src/Provider/DefaultSmsProvider.php index d1be5a1..901b862 100644 --- a/src/Provider/DefaultSmsProvider.php +++ b/src/Provider/DefaultSmsProvider.php @@ -9,7 +9,7 @@ use Drupal\sms\Entity\SmsMessageInterface as SmsMessageEntityInterface; use Drupal\sms\Event\SmsMessageEvent; use Drupal\sms\Message\SmsMessageInterface; -use Drupal\sms\Plugin\SmsGateway\SmsIncomingEventInterface; +use Drupal\sms\Plugin\SmsGateway\SmsIncomingEventProcessorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Drupal\sms\Exception\SmsException; @@ -129,7 +129,7 @@ public function incoming(SmsMessageInterface $sms_message) { $sms_messages = $dispatch ? $this->dispatchEvent(SmsEvents::MESSAGE_PRE_PROCESS, [$sms_message])->getMessages() : [$sms_message]; $sms_messages = $this->dispatchEvent(SmsEvents::MESSAGE_INCOMING_PRE_PROCESS, $sms_messages)->getMessages(); - if ($plugin instanceof SmsIncomingEventInterface) { + if ($plugin instanceof SmsIncomingEventProcessorInterface) { $event = new SmsMessageEvent($sms_messages); $plugin->incomingEvent($event); } diff --git a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php index f9ecc12..da97c8d 100644 --- a/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php +++ b/tests/modules/sms_test_gateway/src/Plugin/SmsGateway/Memory.php @@ -6,7 +6,7 @@ use Drupal\Component\Utility\Random; use Drupal\sms\Message\SmsDeliveryReport; use Drupal\sms\Plugin\SmsGatewayPluginBase; -use Drupal\sms\Plugin\SmsGateway\SmsIncomingEventInterface; +use Drupal\sms\Plugin\SmsGateway\SmsIncomingEventProcessorInterface; use Drupal\sms\Event\SmsMessageEvent; use Drupal\sms\Message\SmsMessageInterface; use Drupal\sms\Message\SmsMessageResult; @@ -29,7 +29,7 @@ * credit_balance_available = TRUE, * ) */ -class Memory extends SmsGatewayPluginBase implements SmsIncomingEventInterface { +class Memory extends SmsGatewayPluginBase implements SmsIncomingEventProcessorInterface { /** * {@inheritdoc}