Skip to content

Commit

Permalink
Merge branch '4' into 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Apr 27, 2023
2 parents 882da53 + d13bebf commit 5c7a425
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
39 changes: 38 additions & 1 deletion src/Tasks/ContentReviewEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\ContentReview\Tasks;

use Page;
use RuntimeException;
use SilverStripe\ContentReview\Compatibility\ContentReviewCompatability;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\HTTPRequest;
Expand All @@ -15,18 +16,29 @@
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\View\ArrayData;
use SilverStripe\View\SSViewer;
use SilverStripe\ContentReview\Models\ContentReviewLog;

/**
* Daily task to send emails to the owners of content items when the review date rolls around.
*/
class ContentReviewEmails extends BuildTask
{
private array $invalid_emails = [];

/**
* @param HTTPRequest $request
* @throws RuntimeException
*/
public function run($request)
{
if (!$this->isValidEmail($senderEmail = SiteConfig::current_site_config()->ReviewFrom)) {
throw new RuntimeException(
sprintf(
'Provided sender email address is invalid: "%s".',
$senderEmail
)
);
}

$compatibility = ContentReviewCompatability::start();

// First grab all the pages with a custom setting
Expand All @@ -42,6 +54,16 @@ public function run($request)
}

ContentReviewCompatability::done($compatibility);

if (is_array($this->invalid_emails) && count($this->invalid_emails) > 0) {
$plural = count($this->invalid_emails) > 1 ? 's are' : ' is';
throw new RuntimeException(
sprintf(
'Provided email' . $plural . ' invalid: "%s".',
implode(', ', $this->invalid_emails)
)
);
}
}

/**
Expand Down Expand Up @@ -93,6 +115,13 @@ protected function notifyOwner($ownerID, SS_List $pages)
// Prepare variables
$siteConfig = SiteConfig::current_site_config();
$owner = Member::get()->byID($ownerID);

if (!$this->isValidEmail($owner->Email)) {
$this->invalid_emails[] = $owner->Name . ': ' . $owner->Email;

return;
}

$templateVariables = $this->getTemplateVariables($owner, $siteConfig, $pages);

// Build email
Expand Down Expand Up @@ -159,4 +188,12 @@ protected function getTemplateVariables($recipient, $config, $pages)
'ToEmail' => $recipient->Email,
];
}

/**
* Check validity of email
*/
protected function isValidEmail(?string $email): bool
{
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
}
}
21 changes: 21 additions & 0 deletions tests/php/ContentReviewNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\ContentReview\Tests;

use Page;
use ReflectionClass;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\ContentReview\Extensions\ContentReviewCMSExtension;
Expand Down Expand Up @@ -132,6 +133,26 @@ public function testContentReviewNeeded()
DBDatetime::clear_mock_now();
}

/**
* Test that provided email is valid
*/
public function testIsValidEmail()
{
$class = new ReflectionClass(ContentReviewEmails::class);
$method = $class->getMethod('isValidEmail');
$method->setAccessible(true);

$member = $this->objFromFixture(Member::class, 'author');
$task = new ContentReviewEmails();

$this->assertTrue($method->invokeArgs($task, [$member->Email]));
$this->assertTrue($method->invokeArgs($task, ['[email protected]']));

$this->assertFalse($method->invokeArgs($task, [null]));
$this->assertFalse($method->invokeArgs($task, ['broken.email']));
$this->assertFalse($method->invokeArgs($task, ['broken@email']));
}

/**
* Deletes all pages except those passes in to the $ids parameter
*
Expand Down

0 comments on commit 5c7a425

Please sign in to comment.