From d9418c105e66028d1399b43bad3c1e0122c2aa25 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 22 Jul 2016 14:44:16 +0200 Subject: [PATCH] Add OCSShareAPIMiddleware tests --- .../lib/Middleware/OCSShareAPIMiddleware.php | 1 - .../Middleware/OCSShareAPIMiddlewareTest.php | 115 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php diff --git a/apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php b/apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php index b42cf6b00ad..04a6545c9a7 100644 --- a/apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php +++ b/apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php @@ -29,7 +29,6 @@ class OCSShareAPIMiddleware extends Middleware { */ public function beforeController($controller, $methodName) { if ($controller instanceof Share20OCS) { - /** @var Share20OCS $controller */ if (!$this->shareManager->shareApiEnabled()) { throw new OCSNotFoundException($this->l->t('Share API is disabled')); } diff --git a/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php b/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php new file mode 100644 index 00000000000..6a2460396de --- /dev/null +++ b/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php @@ -0,0 +1,115 @@ +shareManager = $this->getMockBuilder('OCP\Share\IManager')->getMock(); + $this->l = $this->getMockBuilder('OCP\IL10N')->getMock(); + + $this->l->method('t')->will($this->returnArgument(0)); + + $this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l); + } + + public function dataBeforeController() { + return [ + [ + $this->getMockBuilder('OCP\AppFramework\Controller')->disableOriginalConstructor()->getMock(), + false, + false + ], + [ + $this->getMockBuilder('OCP\AppFramework\Controller')->disableOriginalConstructor()->getMock(), + true, + false + ], + [ + $this->getMockBuilder('OCP\AppFramework\OCSController')->disableOriginalConstructor()->getMock(), + false, + false + ], + [ + $this->getMockBuilder('OCP\AppFramework\OCSController')->disableOriginalConstructor()->getMock(), + true, + false + ], + [ + $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS')->disableOriginalConstructor()->getMock(), + false, + true + ], + [ + $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS')->disableOriginalConstructor()->getMock(), + true, + false + ], + ]; + } + + /** + * @dataProvider dataBeforeController + * + * @param Controller $controller + * @param bool $enabled + * @param bool $exception + */ + public function testBeforeController(Controller $controller, $enabled, $exception) { + $this->shareManager->method('shareApiEnabled')->willReturn($enabled); + + try { + $this->middleware->beforeController($controller, 'foo'); + $this->assertFalse($exception); + } catch (OCSNotFoundException $e) { + $this->assertTrue($exception); + } + } + + public function dataAfterController() { + return [ + [ + $this->getMockBuilder('OCP\AppFramework\Controller')->disableOriginalConstructor()->getMock(), + ], + [ + $this->getMockBuilder('OCP\AppFramework\OCSController')->disableOriginalConstructor()->getMock(), + ], + [ + $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS')->disableOriginalConstructor()->getMock(), + ], + ]; + } + + /** + * @dataProvider dataAfterController + * + * @param Controller $controller + * @param bool $called + */ + public function testAfterController(Controller $controller) { + if ($controller instanceof OCA\Files_Sharing\API\Share20OCS) { + $controller->expects($this->once())->method('cleanup'); + } + + $response = $this->getMockBuilder('OCP\AppFramework\Http\Response') + ->disableOriginalConstructor() + ->getMock(); + $this->middleware->afterController($controller, 'foo', $response); + } +} -- 2.39.5