-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support password protection * Fix PHPCS * Add filter to allow changing the password protect template * Use TimberResponse when returning view * Assert that response is instance of `TimberResponse` --------- Co-authored-by: Joe Lambert <[email protected]> Co-authored-by: Alice Williams <[email protected]>
- Loading branch information
1 parent
ba1daec
commit c445c31
Showing
4 changed files
with
230 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Rareloop\Lumberjack\Http\Middleware; | ||
|
||
use Timber\Timber; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Rareloop\Lumberjack\Exceptions\TwigTemplateNotFoundException; | ||
use Rareloop\Lumberjack\Http\Responses\TimberResponse; | ||
use Rareloop\Lumberjack\Post; | ||
|
||
class PasswordProtected implements MiddlewareInterface | ||
{ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$passwordRequestResponse = $this->handlePasswordProtected(); | ||
|
||
if ($passwordRequestResponse) { | ||
return $passwordRequestResponse; | ||
} | ||
|
||
return $handler->handle($request); | ||
} | ||
|
||
protected function handlePasswordProtected(): ?ResponseInterface | ||
{ | ||
if (!post_password_required()) { | ||
return null; | ||
} | ||
|
||
$context = Timber::context(); | ||
$context['post'] = new Post(); | ||
|
||
$template = apply_filters('lumberjack/password_protect_template', 'single-password.twig'); | ||
|
||
try { | ||
return new TimberResponse($template, $context); | ||
} catch (TwigTemplateNotFoundException $e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Rareloop\Lumberjack\Test\Http\Middleware; | ||
|
||
use Mockery; | ||
use Timber\Timber; | ||
use Brain\Monkey\Functions; | ||
use Brain\Monkey\Filters; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Rareloop\Lumberjack\Http\Middleware\PasswordProtected; | ||
use Rareloop\Lumberjack\Http\Responses\TimberResponse; | ||
use Rareloop\Lumberjack\Test\Unit\BrainMonkeyPHPUnitIntegration; | ||
|
||
/** | ||
* @runTestsInSeparateProcesses | ||
* @preserveGlobalState disabled | ||
*/ | ||
class PasswordProtectTest extends TestCase | ||
{ | ||
use BrainMonkeyPHPUnitIntegration; | ||
|
||
/** @test */ | ||
public function it_does_nothing_when_password_is_not_required() | ||
{ | ||
$middleware = new PasswordProtected; | ||
|
||
Functions\expect('post_password_required') | ||
->once() | ||
->andReturn(false); | ||
|
||
$request = Mockery::mock(ServerRequestInterface::class); | ||
$response = Mockery::mock(ResponseInterface::class); | ||
$handler = Mockery::mock(RequestHandlerInterface::class); | ||
|
||
$handler->shouldReceive('handle')->once()->with($request)->andReturn($response); | ||
|
||
$this->assertSame($response, $middleware->process($request, $handler)); | ||
} | ||
|
||
/** @test */ | ||
public function it_does_nothing_when_the_password_twig_file_is_not_found() | ||
{ | ||
$middleware = new PasswordProtected; | ||
|
||
Functions\expect('post_password_required') | ||
->once() | ||
->andReturn(true); | ||
|
||
Functions\expect('get_the_ID') | ||
->once() | ||
->andReturn(123); | ||
|
||
Functions\expect('get_post') | ||
->once(); | ||
|
||
$timber = \Mockery::mock('alias:' . Timber::class); | ||
$timber->shouldReceive('compile') | ||
->withArgs(function ($template) { | ||
return $template === 'single-password.twig'; | ||
}) | ||
->once() | ||
->andReturn(false); | ||
|
||
$timber->shouldReceive('context') | ||
->once() | ||
->andReturn([]); | ||
|
||
$request = Mockery::mock(ServerRequestInterface::class); | ||
$response = Mockery::mock(ResponseInterface::class); | ||
$handler = Mockery::mock(RequestHandlerInterface::class); | ||
|
||
$handler->shouldReceive('handle')->once()->with($request)->andReturn($response); | ||
|
||
$this->assertSame($response, $middleware->process($request, $handler)); | ||
} | ||
|
||
/** @test */ | ||
public function it_renders_the_password_template_when_needed() | ||
{ | ||
Functions\expect('post_password_required') | ||
->once() | ||
->andReturn(true); | ||
|
||
Functions\expect('get_the_ID') | ||
->once() | ||
->andReturn(123); | ||
|
||
Functions\expect('get_post') | ||
->once(); | ||
|
||
$request = Mockery::mock(ServerRequestInterface::class); | ||
$handler = Mockery::mock(RequestHandlerInterface::class); | ||
|
||
$timber = \Mockery::mock('alias:' . Timber::class); | ||
$timber->shouldReceive('compile') | ||
->withArgs(function ($template) { | ||
return $template === 'single-password.twig'; | ||
}) | ||
->once() | ||
->andReturn('testing123'); | ||
|
||
$timber->shouldReceive('context') | ||
->once() | ||
->andReturn([]); | ||
|
||
$handler->shouldReceive('handle')->never(); | ||
|
||
$middleware = new PasswordProtected; | ||
$response = $middleware->process($request, $handler); | ||
|
||
$this->assertInstanceOf(TimberResponse::class, $response); | ||
$this->assertSame('testing123', $response->getBody()->getContents()); | ||
$this->assertTrue(Filters\applied('lumberjack/password_protect_template') > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters