Skip to content

Commit

Permalink
Merge pull request #12 from nswdpc/maint-stan
Browse files Browse the repository at this point in the history
Module maintenance
  • Loading branch information
tardinha authored Jul 26, 2023
2 parents 6aa5f37 + 863414c commit 63fc79a
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 63 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/Connector/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
10 changes: 4 additions & 6 deletions src/Connector/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -127,7 +127,7 @@ public function send($parameters)
protected function sendMessage(array $parameters) {

/**
* @var Mailgun\Mailgun
* @var \Mailgun\Mailgun
*/
$client = $this->getClient();
/**
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/Connector/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
Expand All @@ -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'])
Expand Down
14 changes: 8 additions & 6 deletions src/Controllers/MailgunModelAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Email/MailgunEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions src/Email/MailgunMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -77,7 +77,7 @@ protected function assignCustomParameters(MailgunEmail &$email, MessageConnector
}

/**
* @param Email
* @param Email $email
* @returns mixed
*/
public function send($email)
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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) {

Expand Down Expand Up @@ -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)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Jobs/SendJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = [])
{
Expand Down
4 changes: 2 additions & 2 deletions src/Models/MailgunEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/FieldType/DBLongText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
49 changes: 24 additions & 25 deletions tests/MailgunSyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ class MailgunSyncTest extends SapphireTest
. "<p>consulting support conversation advertisements policy promotional request.</p>"
. "<p>Option purpose programming</p>";

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);

Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -86,15 +85,15 @@ 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
$this->assertEquals(constant(Base::class . "::{$value}"), $connector->getApiEndpointRegion());

// 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
Expand Down Expand Up @@ -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);
Expand All @@ -230,7 +229,7 @@ public function testJobMailerDelivery() {
public function testAlwaysFrom() {

$alwaysFromEmail = '[email protected]';
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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 ]);
Expand All @@ -333,10 +332,10 @@ public function testSendWithDefaultConfiguration() {
$overrideBcc = '[email protected]';
$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');
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down
Loading

0 comments on commit 63fc79a

Please sign in to comment.