diff --git a/composer.json b/composer.json index 8fef9dd..c8a0056 100644 --- a/composer.json +++ b/composer.json @@ -42,5 +42,9 @@ "kriswallsmith/buzz" : "^1.1", "nyholm/psr7" : "^1.3", "silverstripe/framework" : "^4" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "friendsofphp/php-cs-fixer": "^3" } } diff --git a/src/Connector/Event.php b/src/Connector/Event.php index 6fea3d2..13d6bfc 100644 --- a/src/Connector/Event.php +++ b/src/Connector/Event.php @@ -17,7 +17,7 @@ class Event extends Base /** * @param string $begin an RFC 2822 formatted UTC datetime OR empty string for no begin datetime * @param string $event_filter see https://documentation.mailgun.com/en/latest/api-events.html#event-types can also be a filter expression e.g "failed OR rejected" - * @param boolean $resubmit whether to resubmit events if possible + * @param array $extra_params extra parameters for API request * @return array */ public function pollEvents($begin = null, $event_filter = "", $extra_params = array()) diff --git a/src/Connector/Message.php b/src/Connector/Message.php index 973f522..3ce2191 100644 --- a/src/Connector/Message.php +++ b/src/Connector/Message.php @@ -4,6 +4,7 @@ use Mailgun\Mailgun; use NSWDPC\Messaging\Mailgun\Log; use NSWDPC\Messaging\Mailgun\Connector\Event as EventConnector; +use Mailgun\Model\Message\SendResponse; use Mailgun\Model\Message\ShowResponse; use NSWDPC\Messaging\Mailgun\SendJob; use NSWDPC\Messaging\Mailgun\MailgunEvent; @@ -84,7 +85,6 @@ public function getMime(MailgunEvent $event) * See: http://mailgun-documentation.readthedocs.io/en/latest/api-sending.html#sending * @return SendResponse|QueuedJobDescriptor|null * @param array $parameters an array of parameters for the Mailgun API - * @param string $in a strtotime value added to 'now' being the time that the message will be sent via a queued job, if enabled */ public function send($parameters) { @@ -127,7 +127,7 @@ public function send($parameters) protected function sendMessage(array $parameters) { /** - * @var Mailgun\Mailgun + * @var \Mailgun\Mailgun */ $client = $this->getClient(); /** @@ -245,8 +245,7 @@ public function isDelivered(MailgunEvent $event, $cleanup = true) 'recipient' => $event->Recipient,// match against the recipient of the event ]; - // calling pollEvents will store matching local MailgunEvent record(s) - $events = $connector->pollEvents($begin, $event_filter, $resubmit, $extra_params); + $events = $connector->pollEvents($begin, $event_filter, $extra_params); $is_delivered = !empty($events); return $is_delivered; @@ -255,7 +254,7 @@ public function isDelivered(MailgunEvent $event, $cleanup = true) /** * Trim < and > from message id * @return string - * @param string + * @param string $message_id */ public static function cleanMessageId($message_id) { @@ -356,7 +355,6 @@ public function getVariables() { /** * Based on options set in {@link NSWDPC\Messaging\Mailgun\MailgunEmail} set Mailgun options, params, headers and variables - * @param NSWDPC\Messaging\Mailgun\MailgunEmail $email * @param array $parameters */ protected function addCustomParameters(&$parameters) diff --git a/src/Connector/Webhook.php b/src/Connector/Webhook.php index 32a3796..ab53fd5 100644 --- a/src/Connector/Webhook.php +++ b/src/Connector/Webhook.php @@ -15,7 +15,7 @@ class Webhook extends Base { /** * verify signature * @return true returns true if signature is valid - * @param array + * @param array $signature */ public function verify_signature($signature) { @@ -27,7 +27,7 @@ public function verify_signature($signature) /** * Sign the token based on timestamp and signature in request - * @param array + * @param array $signature */ public function sign_token($signature) { $webhook_signing_key = $this->getWebhookSigningKey(); @@ -39,7 +39,7 @@ public function sign_token($signature) { /** * Based on Mailgun docs, determine if the signature is correct - * @param array + * @param array $signature */ public function is_valid_signature($signature) { return isset($signature['timestamp']) diff --git a/src/Controllers/MailgunModelAdmin.php b/src/Controllers/MailgunModelAdmin.php index 41a9868..e92f16b 100644 --- a/src/Controllers/MailgunModelAdmin.php +++ b/src/Controllers/MailgunModelAdmin.php @@ -32,14 +32,16 @@ public function getEditForm($id = null, $fields = null) $form = parent::getEditForm($id, $fields); $grid = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass)); - $config = $grid->getConfig(); + if($grid instanceof GridField) { + $config = $grid->getConfig(); - $config->removeComponentsByType(GridFieldAddNewButton::class); - $config->removeComponentsByType(GridFieldPrintButton::class); + $config->removeComponentsByType(GridFieldAddNewButton::class); + $config->removeComponentsByType(GridFieldPrintButton::class); - if(! Permission::check( MailgunEvent::PERMISSIONS_DELETE, 'any', Member::currentUser()) ) { - $config->removeComponentsByType(GridFieldEditButton::class); - $config->removeComponentsByType(GridFieldDeleteAction::class); + if(! Permission::check( MailgunEvent::PERMISSIONS_DELETE, 'any', Member::currentUser()) ) { + $config->removeComponentsByType(GridFieldEditButton::class); + $config->removeComponentsByType(GridFieldDeleteAction::class); + } } return $form; diff --git a/src/Email/MailgunEmail.php b/src/Email/MailgunEmail.php index 2782a13..4e3ff0b 100644 --- a/src/Email/MailgunEmail.php +++ b/src/Email/MailgunEmail.php @@ -37,7 +37,7 @@ class MailgunEmail extends Email { /** * Retrieve the connector instance - * @return NSWDPC\Messaging\Mailgun\Connector\Message + * @return \NSWDPC\Messaging\Mailgun\Connector\Message * @deprecated */ public function getConnector() : Message { diff --git a/src/Email/MailgunMailer.php b/src/Email/MailgunMailer.php index 9a6251f..9cb862b 100644 --- a/src/Email/MailgunMailer.php +++ b/src/Email/MailgunMailer.php @@ -60,7 +60,7 @@ public function getAlwaysFrom() { /** * Retrieve and set custom parameters on the API connector * @param MailgunEmail $email - * @param MessageConnector connector instance for this send attempt + * @param MessageConnector $connector instance for this send attempt * @return MessageConnector */ protected function assignCustomParameters(MailgunEmail &$email, MessageConnector &$connector) : MessageConnector { @@ -77,7 +77,7 @@ protected function assignCustomParameters(MailgunEmail &$email, MessageConnector } /** - * @param Email + * @param Email $email * @returns mixed */ public function send($email) @@ -103,7 +103,7 @@ public function send($email) } else { throw new \Exception("Tried to send, expected a SendResponse or a QueuedJobDescriptor but got type=" . gettype($response)); } - } catch (Exception $e) { + } catch (\Exception $e) { Log::log('Mailgun-Sync / Mailgun error: ' . $e->getMessage(), \Psr\Log\LogLevel::NOTICE); } return false; @@ -212,11 +212,9 @@ public function prepareParameters(Email $email, MessageConnector $connector) : a // if Cc and Bcc have been provided if (isset($headers['Cc'])) { $parameters['cc'] = $headers['Cc']; - unset($parameters['h:Cc']);//avoid double Cc header } if (isset($headers['Bcc'])) { $parameters['bcc'] = $headers['Bcc']; - unset($parameters['h:Bcc']);//avoid sending double Bcc header } // Provide Mailgun the Attachments. Keys are 'fileContent' (the bytes) and filename (the file name) @@ -241,7 +239,7 @@ public function prepareParameters(Email $email, MessageConnector $connector) : a /** * Given {@link \SilverStripe\Control\Email\Email} configuration, apply relevant values - * @param array + * @param array $parameters */ public function assignDefaultParameters(&$parameters) { @@ -348,7 +346,7 @@ protected function prepareHeaders(Swift_Mime_SimpleHeaderSet $header_set) * 'contents' => $data, * 'filename' => $filename, * 'mimetype' => $mimetype, - * @param array $attachements Each value is a {@link Swift_Attachment} + * @param array $attachments Each value is a {@link Swift_Attachment} */ protected function prepareAttachments(array $attachments) { diff --git a/src/Jobs/SendJob.php b/src/Jobs/SendJob.php index bad43ac..f51d5a5 100644 --- a/src/Jobs/SendJob.php +++ b/src/Jobs/SendJob.php @@ -62,7 +62,7 @@ public function getSignature() // these simple message params $parts = ['to','from','cc','bcc','subject']; foreach($parts as $part) { - $params[ $part ] = isset($parameters[ $part ]) ? $parameters[ $part ] : ''; + $params[ $part ] = isset($this->parameters[ $part ]) ? $this->parameters[ $part ] : ''; } // at this time $params['sendtime'] = microtime(true); @@ -71,8 +71,8 @@ public function getSignature() /** * Create the job - * @param string domain DEPRECATED - * @param array parameters for Mailgun API + * @param string $domain DEPRECATED + * @param array $parameters for Mailgun API */ public function __construct($domain = "", $parameters = []) { diff --git a/src/Models/MailgunEvent.php b/src/Models/MailgunEvent.php index 9234afe..d67fa53 100644 --- a/src/Models/MailgunEvent.php +++ b/src/Models/MailgunEvent.php @@ -262,7 +262,7 @@ public function getCmsFields() /** * Events that are sibling to this event (sharing the smae MessageId) - * @return DataList + * @return \SilverStripe\ORM\DataList */ public function getSiblingEvents() { @@ -428,7 +428,7 @@ private function saveDeliveryStatus(array $delivery_status) /** * Given a Mailgun\Model\Event\Event, store if possible - * @param Mailgun\Model\Event\Event $event + * @param MailgunEventModel $event * @return MailgunEvent|boolean */ public function storeEvent(MailgunEventModel $event) diff --git a/src/ORM/FieldType/DBLongText.php b/src/ORM/FieldType/DBLongText.php index fab0426..d9be872 100644 --- a/src/ORM/FieldType/DBLongText.php +++ b/src/ORM/FieldType/DBLongText.php @@ -34,7 +34,7 @@ public function requireField() $charset = Config::inst()->get(MySQLDatabase::class, 'charset'); $collation = Config::inst()->get(MySQLDatabase::class, 'collation'); $values = "longtext character set {$charset} collate {$collation}"; - DB::require_field($this->tableName, $this->name, $values, $this->default); + DB::require_field($this->tableName, $this->name, $values); } else { // different manager e.g SQLite3 TEXT = 2^31 - 1 length parent::requireField(); diff --git a/tests/MailgunSyncTest.php b/tests/MailgunSyncTest.php index b829271..fbdf929 100644 --- a/tests/MailgunSyncTest.php +++ b/tests/MailgunSyncTest.php @@ -47,12 +47,11 @@ class MailgunSyncTest extends SapphireTest . "
consulting support conversation advertisements policy promotional request.
" . "Option purpose programming
"; - public function setUp() + public function setUp() : void { parent::setUp(); // Avoid using TestMailer for this test - $this->mailer = MailgunMailer::create(); - Injector::inst()->registerService($this->mailer, Mailer::class); + Injector::inst()->registerService(new MailgunMailer(), Mailer::class); // use MailgunEmail Injector::inst()->registerService(MailgunEmail::create(), Email::class); @@ -61,10 +60,10 @@ public function setUp() // modify some config values for tests // never send via a job - Config::inst()->update(Base::class, 'api_domain', $this->test_api_domain); - Config::inst()->update(Base::class, 'api_key', $this->test_api_key); - Config::inst()->update(Base::class, 'send_via_job', 'no'); - Config::inst()->update(Base::class, 'api_testmode', true); + Config::modify()->set(Base::class, 'api_domain', $this->test_api_domain); + Config::modify()->set(Base::class, 'api_key', $this->test_api_key); + Config::modify()->set(Base::class, 'send_via_job', 'no'); + Config::modify()->set(Base::class, 'api_testmode', true); } /** @@ -73,11 +72,11 @@ public function setUp() public function testApiDomain() { $currentValue = Config::inst()->get(Base::class, 'api_domain'); $value = "testing.example.org"; - Config::inst()->update(Base::class, 'api_domain', $value); + Config::modify()->set(Base::class, 'api_domain', $value); $connector = MessageConnector::create(); $result = $connector->getApiDomain(); $this->assertEquals($value, $result); - Config::inst()->update(Base::class, 'api_domain', $currentValue); + Config::modify()->set(Base::class, 'api_domain', $currentValue); } /** @@ -86,7 +85,7 @@ public function testApiDomain() { public function testApiEndpoint() { $value = 'API_ENDPOINT_EU'; - Config::inst()->update(Base::class, 'api_endpoint_region', $value); + Config::modify()->set(Base::class, 'api_endpoint_region', $value); $connector = MessageConnector::create(); $domains = $connector->getClient(); // assert that the expected URL value is what was set on the client @@ -94,7 +93,7 @@ public function testApiEndpoint() { // switch to default region $value = ''; - Config::inst()->update(Base::class, 'api_endpoint_region', $value); + Config::modify()->set(Base::class, 'api_endpoint_region', $value); $connector = MessageConnector::create(); $domains = $connector->getClient(); // when no value is set, the default region URL is used @@ -216,7 +215,7 @@ public function testMailerDelivery($subject = "test_mailer_delivery") * Test delivery via a Job */ public function testJobMailerDelivery() { - Config::inst()->update(Base::class, 'send_via_job', 'yes'); + Config::modify()->set(Base::class, 'send_via_job', 'yes'); // send message $subject = "test_mailer_delivery_job"; $sendData = $this->testMailerDelivery($subject); @@ -230,7 +229,7 @@ public function testJobMailerDelivery() { public function testAlwaysFrom() { $alwaysFromEmail = 'alwaysfrom@example.com'; - Config::inst()->update(MailgunMailer::class, 'always_from', $alwaysFromEmail); + Config::modify()->set(MailgunMailer::class, 'always_from', $alwaysFromEmail); $to_address = $this->config()->get('to_address'); $to_name = $this->config()->get('to_name'); @@ -273,7 +272,7 @@ public function testAlwaysFrom() { public function testAPIDelivery() { - Config::inst()->update(Base::class, 'send_via_job', 'no'); + Config::modify()->set(Base::class, 'send_via_job', 'no'); $connector = MessageConnector::create(); $to = $to_address = $this->config()->get('to_address'); @@ -315,7 +314,7 @@ public function testAPIDelivery() $this->assertEquals(TestMessage::MSG_ID, $response->getId()); $sendData = TestMessage::getSendData(); - $this->arrayHasKey('parameters', $sendData); + $this->assertArrayHasKey('parameters', $sendData); foreach(['o:testmode','o:tag','from','to','subject','text','html'] as $key) { $this->assertEquals($parameters[ $key ], $sendData['parameters'][ $key ]); @@ -333,10 +332,10 @@ public function testSendWithDefaultConfiguration() { $overrideBcc = 'bccallemailsto@example.com'; $overrideBccName = 'bcc person'; - Config::inst()->update(Email::class, 'send_all_emails_to', $overrideTo); - Config::inst()->update(Email::class, 'send_all_emails_from', $overrideFrom); - Config::inst()->update(Email::class, 'cc_all_emails_to', $overrideCc); - Config::inst()->update(Email::class, 'bcc_all_emails_to', [ $overrideBcc => $overrideBccName ]); + Config::modify()->set(Email::class, 'send_all_emails_to', $overrideTo); + Config::modify()->set(Email::class, 'send_all_emails_from', $overrideFrom); + Config::modify()->set(Email::class, 'cc_all_emails_to', $overrideCc); + Config::modify()->set(Email::class, 'bcc_all_emails_to', [ $overrideBcc => $overrideBccName ]); $to_address = $this->config()->get('to_address'); $to_name = $this->config()->get('to_name'); @@ -367,7 +366,7 @@ public function testSendWithDefaultConfiguration() { $sendData = TestMessage::getSendData(); foreach(['domain','parameters','sentVia','client','in'] as $key) { - $this->arrayHasKey($key, $sendData); + $this->assertArrayHasKey($key, $sendData); } $this->assertEquals($this->test_api_domain, $sendData['domain']); @@ -429,16 +428,16 @@ public function testAttachmentDelivery() { $sendData = TestMessage::getSendData(); - $this->arrayHasKey('parameters', $sendData); - $this->arrayHasKey('attachment', $sendData['parameters']); + $this->assertArrayHasKey('parameters', $sendData); + $this->assertArrayHasKey('attachment', $sendData['parameters']); $attachments = $sendData['parameters']['attachment']; $f = 1; $this->assertEquals(count($files), count($attachments)); foreach($attachments as $attachment) { - $this->arrayHasKey( 'filename', $attachment ); - $this->arrayHasKey( 'mimetype', $attachment ); - $this->arrayHasKey( 'fileContent', $attachment ); + $this->assertArrayHasKey( 'filename', $attachment ); + $this->assertArrayHasKey( 'mimetype', $attachment ); + $this->assertArrayHasKey( 'fileContent', $attachment ); foreach($files as $file => $mimetype) { if($file == $attachment['filename']) { $this->assertEquals($mimetype, $attachment['mimetype']); diff --git a/tests/WebhookTest.php b/tests/WebhookTest.php index 1c02b35..46a0e80 100644 --- a/tests/WebhookTest.php +++ b/tests/WebhookTest.php @@ -21,11 +21,11 @@ class WebhookTest extends FunctionalTest protected $usesDatabase = true; - public function setUp() { + public function setUp() : void { parent::setUp(); - Config::inst()->set(Base::class, 'webhook_filter_variable', $this->webhook_filter_variable); - Config::inst()->set(Base::class, 'webhook_previous_filter_variable', $this->webhook_previous_filter_variable); - Config::inst()->set(Base::class, 'webhooks_enabled', true); + Config::modify()->set(Base::class, 'webhook_filter_variable', $this->webhook_filter_variable); + Config::modify()->set(Base::class, 'webhook_previous_filter_variable', $this->webhook_previous_filter_variable); + Config::modify()->set(Base::class, 'webhooks_enabled', true); } /** @@ -51,10 +51,10 @@ protected function getConnector() { /** * Set a signing key in Configuration - * @param string + * @param string $signing_key */ protected function setSigningKey($signing_key) { - Config::inst()->set(Base::class, 'webhook_signing_key', $signing_key); + Config::modify()->set(Base::class, 'webhook_signing_key', $signing_key); } /** @@ -131,8 +131,8 @@ protected function sendWebhookRequest($type) { // remove webhook variable and test unset( $data['event-data']['user-variables']['wfv'] ); - Config::inst()->set(Base::class, 'webhook_filter_variable', ''); - Config::inst()->set(Base::class, 'webhook_previous_filter_variable', ''); + Config::modify()->set(Base::class, 'webhook_filter_variable', ''); + Config::modify()->set(Base::class, 'webhook_previous_filter_variable', ''); // change the signing key in config, it should fail now $signing_key = "YOU_SHALL_NOT_PASS";