Skip to content

Commit

Permalink
Simulate app request contenxt when rendering mail previews
Browse files Browse the repository at this point in the history
Mailers will not expect the request context to be inside debugkit.
Instead we should simulate the app context so that mailer links don't
need `plugin => null` everywhere.

Refs #600
  • Loading branch information
markstory committed Apr 28, 2018
1 parent fc57dad commit b271f3e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Controller/MailPreviewController.php
Expand Up @@ -20,6 +20,7 @@
use Cake\Core\Plugin;
use Cake\Event\Event;
use Cake\Http\Exception\NotFoundException;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use DebugKit\Mailer\AbstractResult;
use DebugKit\Mailer\PreviewResult;
Expand Down Expand Up @@ -114,6 +115,11 @@ public function sent($panelId, $number)
*/
public function email($name, $method)
{
// Clear the plugin attribute from the request instance
// Router is holding onto so that we can render mail previews
// in a plugin less request context.
Router::pushRequest($this->request->withParam('plugin', null));

$plugin = $this->request->getQuery('plugin');
$email = $this->findPreview($name, $method, $plugin);
$partType = $this->request->getQuery('part');
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase/Controller/MailPreviewControllerTest.php
Expand Up @@ -44,6 +44,10 @@ public function setUp()
{
parent::setUp();
Plugin::load('DebugkitTestPlugin', ['path' => APP . 'Plugin' . DS . 'DebugkitTestPlugin' . DS]);
Router::scope('/', function ($routes) {
$routes->connect('/users/:action/*', ['controller' => 'Users']);
});

Router::plugin('DebugKit', function (RouteBuilder $routes) {
$routes->scope(
'/mail_preview',
Expand Down Expand Up @@ -80,6 +84,7 @@ public function testEmailPartTextContent()

$this->assertResponseOk();
$this->assertResponseContains('Testing email action.');
$this->assertResponseContains('/users/verify/token', 'Should contain URL from app context');
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/Template/Email/text/test_email.ctp
@@ -1 +1,2 @@
Testing email action.
<?= $this->Url->build(['controller' => 'Users', 'action' => 'verify', 'token']) ?>

0 comments on commit b271f3e

Please sign in to comment.