Skip to content

Commit

Permalink
Merge pull request #1 from nswdpc/upgrade-requirements
Browse files Browse the repository at this point in the history
Upgrade requirements
  • Loading branch information
JamesDPC authored Feb 3, 2023
2 parents 024d900 + 3aebe6a commit 529753f
Show file tree
Hide file tree
Showing 145 changed files with 4,088 additions and 610 deletions.
24 changes: 13 additions & 11 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/tests export-ignore
/docs export-ignore
/client/src export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php_cs.dist
/phpcs.xml.dist
/phpstan.neon.dist
/phpunit.xml.dist
/.waratah export-ignore
/README.md export-ignore
/tests export-ignore
/docs export-ignore
/client/src export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php-cs-fixer.dist.php export-ignore
/phpunit.xml.dist export-ignore
/.waratah export-ignore
/code-of-conduct.md export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
/thirdparty/postmark-templates export-ignore
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/client/node_modules
/vendor/
.DS_Store
/.php_cs.cache
/resources/
/_resources/
/public/
/assets/
/.php-cs-fixer.cache
/tests/__output/
4 changes: 2 additions & 2 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__);

return PhpCsFixer\Config::create()
->setRules([
$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR2' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
## Structured email handling for Silverstripe

The goal of this module is to produce simple and easy-to-digest emails ([more](./docs/en/001_index.md)). The templates used are based on the [Postmark Transactional Email Templates](https://github.com/wildbit/postmark-templates).
The goal of this module is to produce simple and easy-to-digest emails ([more](./docs/en/001_index.md)). The templates used are based on the [Postmark Transactional Email Templates](https://github.com/ActiveCampaign/postmark-templates).

> :warning: This module is under active development and shouldn't be used in production just yet as there are probably bugs. Test results, pull requests and feedback are welcome.
The `StructuredEmail` extends the `Email` class. You can use it standalone or you may wish to inject StructuredEmail as the Email class for your project using `Injector`.
The `StructuredEmail` class extends the `Email` class. You can use it standalone or you may wish to inject StructuredEmail as the Email class for your project using `Injector`.

For specific core emails, it will attempt to detect the purpose of the email based on the template name:

Expand All @@ -20,7 +18,7 @@ TODO:

### Existing templates

If the class encounters a complete HTML document in the email, it will use HTML contained within the `<body>` tag as the email content.
If the class encounters a complete HTML document in the email, it will use HTML contained within the `<body>` tag of that document as the email content.

## Schema.org

Expand All @@ -29,7 +27,7 @@ If the class encounters a complete HTML document in the email, it will use HTML
## Resources

+ E-mail support across clients: https://www.caniemail.com
+ Postmark templates: https://github.com/wildbit/postmark-templates
+ Postmark templates: https://github.com/ActiveCampaign/postmark-templates

## Quick example

Expand Down Expand Up @@ -75,7 +73,7 @@ composer require nswdpc/silverstripe-structured-email

+ [dpcdigital@NSWDPC:~$](https://dpc.nsw.gov.au)

The source of the HTML email templates is the [Postmark templates project](https://github.com/wildbit/postmark-templates)
The source of the HTML email templates is the [Postmark templates project](https://github.com/ActiveCampaign/postmark-templates)

## Bugtracker

Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@
}
],
"require": {
"silverstripe/framework": "^4",
"silverstripe/framework": "^4.10",
"league/html-to-markdown": "^5",
"spatie/schema-org": "^3.4",
"nswdpc/silverstripe-taggable-notifications": "dev-master"
"spatie/schema-org": "^3.5",
"nswdpc/silverstripe-taggable-notifications": "^0.3"
},
"suggest": {
"ext-xml": "Use DOMDocument to process HTML documents",
"ext-tidy": "Use tidy to process HTML documents"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "^2.18",
"symbiote/silverstripe-phpstan": "^4",
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3",
"silverstripe/sqlite3": "^2"
}
}
6 changes: 3 additions & 3 deletions docs/en/001_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ This module handles the templating of the email, leaving you to figure out what

We add:

+ Standard, proven email templates based on https://github.com/wildbit/postmark-templates
+ Standard, proven email templates based on https://github.com/ActiveCampaign/postmark-templates
+ A Structured Email class
+ An Injectable email decorator containing standard CSS values
+ Structured template parts allowing you to override as required
+ 🧫 Automated text/plain content creation (as markdown)

### Planned features

+ ⚠️ 🧫 Schema.org support
+ ⚠️ 🧫 Automated text/plain content creation (as markdown)
+ ⚠️ 🧫 improved Schema.org support

### Stretch goals

Expand Down
2 changes: 1 addition & 1 deletion docs/en/003_decorator.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ print get_class($decorator);

### Set the layout type

A layout type allows you some control over the HTML width ([more](https://github.com/wildbit/mailmason/wiki/Project-Structure#layouts))
A layout type allows you some control over the HTML width ([more](https://github.com/ActiveCampaign/mailmason/wiki/Project-Structure#layouts))

+ basic-full = full width
+ basic = single centred column
Expand Down
2 changes: 2 additions & 0 deletions docs/en/004_schemaorg.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $email->setViewAction(
'https://confirm.example.com?token=suitably-long-token'
);
```
Internally, a `\Spatie\SchemaOrg\ViewAction` will be created using the name and URL provided.

This will result in the following HTML snippet in the template:

Expand Down Expand Up @@ -50,6 +51,7 @@ You can also define a generic action:

```php
<?php
/* @var Spatie\SchemaOrg\Action */
$action = Schema::action()
->name('Carry out this action')
->handler(
Expand Down
11 changes: 0 additions & 11 deletions phpcs.xml.dist

This file was deleted.

6 changes: 0 additions & 6 deletions phpstan.neon.dist

This file was deleted.

15 changes: 1 addition & 14 deletions templates/NSWDPC/StructuredEmail/Includes/WithThanks.ss
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
<p>
<%t StructuredEmail.THANKS 'Thanks '%>
<br>
<% if $SiteConfig %>
<br>
{$SiteConfig.Title}
<% end_if %>
<% with $Top.EmailDecorator %>
<% if $SignOffLink %>
<br>
{$SignOffLink.XML}
<% end_if %>
<% end_with %>
</p>
<%-- override this template in your project to provide a generic sign-off to the email, if required --%>
71 changes: 56 additions & 15 deletions tests/EmailTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class EmailTemplateTest extends SapphireTest {
Member::class => '*',
];

protected $usesDatabase = false;
protected $usesDatabase = null;

private function saveOutput($html, $template, $ext = ".html") {
$full = dirname(__FILE__) . '/__output/' . $template;
Expand All @@ -29,13 +29,15 @@ public function testTemplate() {
'Body' => file_get_contents(dirname(__FILE__) . '/data/template.html')
];

$subject = 'Welcome to the show';

$email = StructuredEmail::create();
$email->setTo("[email protected]");
$email->setCc("[email protected]");
$email->setBcc("[email protected]");
$email->setFrom(["[email protected]" => "Jiminy Crickets", "[email protected]" => "Bob Pokemon"]);
$email->addFrom("[email protected]");
$email->setSubject('Welcome to the show');
$email->setSubject($subject);
$email->setPreheader('Test generic email that needs your attention');
$email->setData($data);
$email->setViewAction('Confirm your identify', 'https://confirm.example.com?token=suitably-long-token');
Expand All @@ -48,6 +50,14 @@ public function testTemplate() {
$message = $email->getSwiftMessage();
$this->saveOutput($message, "StructuredEmail", ".txt");


// assert email contains subject
$this->assertStringContainsString(
"Subject: " . $subject,
$message
);


}

/**
Expand All @@ -57,16 +67,18 @@ public function testForgotEmail() {
$template = 'SilverStripe/Control/Email/ForgotPasswordEmail';
$token = "really-bad-token";
$member = $this->objFromFixture(Member::class, 'forgotpassword');
$resetPasswordLink = Security::getPasswordResetLink($member, $token);
$subject = _t(
'SilverStripe\\Security\\Member.SUBJECTPASSWORDRESET',
"Your password reset link",
'Email subject'
);
/** @var StructuredEmail $email */
$email = StructuredEmail::create()
->setHTMLTemplate($template)
->setData($member)
->setSubject(_t(
'SilverStripe\\Security\\Member.SUBJECTPASSWORDRESET',
"Your password reset link",
'Email subject'
))
->addData('PasswordResetLink', Security::getPasswordResetLink($member, $token))
->setSubject($subject)
->addData('PasswordResetLink', $resetPasswordLink)
->setTo($member->Email);
// test preheader
$email->setPreHeader(
Expand All @@ -81,31 +93,52 @@ public function testForgotEmail() {
$message = $email->getSwiftMessage();
$this->saveOutput($message, "ForgotPasswordEmail", ".txt");

// test that email is rendered with link and in structured email
// test that email is rendered with reset link and in structured email
$this->assertStringContainsString(
htmlspecialchars($resetPasswordLink), // entitised link
$html // in the HTML
);

// assert email contains subject
$this->assertStringContainsString(
"Subject: " . $subject,
$message
);

}

public function testChangePassword() {
$member = $this->objFromFixture(Member::class, 'forgotpassword');
$subject = _t(
'SilverStripe\\Security\\Member.SUBJECTPASSWORDCHANGED',
"Your password has been changed",
'Email subject'
);
$email = StructuredEmail::create()
->setHTMLTemplate('SilverStripe\\Control\\Email\\ChangePasswordEmail')
->setData($member)
->setTo($member->Email)
->setFrom('[email protected]')
->setSubject(_t(
'SilverStripe\\Security\\Member.SUBJECTPASSWORDCHANGED',
"Your password has been changed",
'Email subject'
));
->setSubject($subject);
$email->send();

$this->saveOutput($email->getBody(), "ChangePasswordEmail");

$message = $email->getSwiftMessage();
$this->saveOutput($message, "ChangePasswordEmail", ".txt");

// assert email contains subject
$this->assertStringContainsString(
"Subject: " . $subject,
$message
);


}

public function testStandardEmail() {
$member = $this->objFromFixture(Member::class, 'forgotpassword');
$subject = 'Subject of an important message';
$email = StructuredEmail::create()
->setHTMLTemplate('SilverStripe\\Control\\Email\\Email')
->setData([
Expand All @@ -114,13 +147,21 @@ public function testStandardEmail() {
->setPreHeader('An important message')
->setTo($member->Email)
->setFrom('[email protected]')
->setSubject('Subject of an important message');
->setSubject($subject);
$email->send();

$this->saveOutput($email->getBody(), "StandardEmail");

$message = $email->getSwiftMessage();
$this->saveOutput($message, "StandardEmail", ".txt");

// assert email contains subject
$this->assertStringContainsString(
"Subject: " . $subject,
$message
);


}

}
Loading

0 comments on commit 529753f

Please sign in to comment.