From d872b9ec27d42ed3f4c70df66b1cc3a95bdc9451 Mon Sep 17 00:00:00 2001 From: Gawain Lynch Date: Thu, 22 Dec 2016 16:32:26 +0100 Subject: [PATCH 1/2] Check if session is started before accessing --- src/Controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller.php b/src/Controller.php index 9f6f6dc..ba64dbb 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -147,9 +147,9 @@ protected function serve(Application $app, Request $request, $file, $action, $wi protected function isRestricted(Application $app, Request $request) { $session = $request->getSession(); - $auth = (isset($session)) ? $session->get('authentication') : null; + $auth = $session && $session->isStarted() ? $session->get('authentication') : null; - if ($auth && ($auth->getUser()->getEnabled())) { + if ($auth && $auth->getUser()->getEnabled()) { return false; } From db6b2bd6e09df9af557b3f859fee9cff615c9846 Mon Sep 17 00:00:00 2001 From: Gawain Lynch Date: Sun, 25 Dec 2016 01:29:04 +0100 Subject: [PATCH 2/2] [Tests] Mock expectation on Session::isStarted() --- tests/ControllerTest.php | 43 ++++++++++++++++++++++++++++++---------- tests/ResponderTest.php | 3 ++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 69c8265..cfbe792 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -7,12 +7,15 @@ use Bolt\Filesystem\Handler\Image; use Bolt\Filesystem\Handler\Image\Dimensions; use Bolt\Thumbs\Controller; +use Bolt\Thumbs; use Bolt\Thumbs\Thumbnail; use Bolt\Thumbs\Transaction; use Silex\Application; use Silex\Provider\ServiceControllerServiceProvider; use Silex\WebTestCase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpKernel\Exception\HttpException; class ControllerTest extends WebTestCase { @@ -52,10 +55,10 @@ public function testIsRestricted() $app['thumbnails.only_aliases'] = false; $controller = new Controller(); $request = Request::create('/thumbs/123x456c/herp/derp.png'); - $this->assertInstanceOf('Bolt\Thumbs\Response', $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456)); + $this->assertInstanceOf(Thumbs\Response::class, $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456)); $app['thumbnails.only_aliases'] = true; - $this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException'); + $this->setExpectedException(HttpException::class); $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456); } @@ -65,23 +68,39 @@ public function testNotIsRestrictedWhenLoggedIn() $controller = new Controller(); $request = Request::create('/thumbs/123x456c/herp/derp.png'); - $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session'); - $user = $this->getMock('stdClass', ['getEnabled']); + $user = $this->getMockBuilder('stdClass') + ->setMethods(['getEnabled']) + ->getMock() + ; $user->expects($this->any()) ->method('getEnabled') - ->willReturn(true); - $auth = $this->getMock('stdClass', ['getUser']); + ->willReturn(true) + ; + + $auth = $this->getMockBuilder('stdClass') + ->setMethods(['getUser']) + ->getMock() + ; $auth->expects($this->any()) ->method('getUser') - ->willReturn($user); + ->willReturn($user) + ; + + $session = $this->getMockBuilder(Session::class)->getMock(); $session->expects($this->any()) ->method('get') ->with('authentication') - ->willReturn($auth); + ->willReturn($auth) + ; + $session->expects($this->atLeastOnce()) + ->method('isStarted') + ->willReturn(true) + ; + /** @var Session $session */ $request->setSession($session); $app['thumbnails.only_aliases'] = true; - $this->assertInstanceOf('Bolt\Thumbs\Response', $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456)); + $this->assertInstanceOf(Thumbs\Response::class, $controller->thumbnail($app, $request, 'herp/derp.png', 'c', 123, 456)); } /** @@ -94,7 +113,11 @@ public function createApplication() $app->mount('/thumbs', $app['controller.thumbnails']); $app->register(new ServiceControllerServiceProvider()); - $mock = $this->getMock('Bolt\Thumbs\ThumbnailResponder', ['respond'], [], '', false); + $mock = $this->getMockBuilder(Thumbs\Responder::class) + ->setMethods(['respond']) + ->disableOriginalConstructor() + ->getMock() + ; $mock ->expects($this->any()) ->method('respond') diff --git a/tests/ResponderTest.php b/tests/ResponderTest.php index 1bc5843..cff2158 100644 --- a/tests/ResponderTest.php +++ b/tests/ResponderTest.php @@ -4,6 +4,7 @@ use Bolt\Filesystem; use Bolt\Filesystem\Adapter\Local; use Bolt\Filesystem\Handler\Image; +use Bolt\Thumbs\Creator; use Bolt\Thumbs\CreatorInterface; use Bolt\Thumbs\FinderInterface; use Bolt\Thumbs\Responder; @@ -70,7 +71,7 @@ public function testSrcImage() public function testCaching() { - $this->creator = $this->getMock('\Bolt\Thumbs\CreatorInterface'); + $this->creator = $this->getMockBuilder(Creator::class)->getMock(); $this->creator->expects($this->once()) ->method('create') ->willReturnCallback(