OCSShareAPIMiddlewareTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Tests\Middleware;
  25. use OCA\Files_Sharing\Controller\ShareAPIController;
  26. use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\OCS\OCSNotFoundException;
  29. use OCP\AppFramework\OCSController;
  30. use OCP\IL10N;
  31. use OCP\Share\IManager;
  32. /**
  33. * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
  34. */
  35. class OCSShareAPIMiddlewareTest extends \Test\TestCase {
  36. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  37. private $shareManager;
  38. /** @var IL10N */
  39. private $l;
  40. /** @var OCSShareAPIMiddleware */
  41. private $middleware;
  42. public function setUp() {
  43. parent::setUp();
  44. $this->shareManager = $this->createMock(IManager::class);
  45. $this->l = $this->createMock(IL10N::class);
  46. $this->l->method('t')->will($this->returnArgument(0));
  47. $this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l);
  48. }
  49. public function dataBeforeController() {
  50. return [
  51. [
  52. $this->createMock(Controller::class),
  53. false,
  54. false
  55. ],
  56. [
  57. $this->createMock(Controller::class),
  58. true,
  59. false
  60. ],
  61. [
  62. $this->createMock(OCSController::class),
  63. false,
  64. false
  65. ],
  66. [
  67. $this->createMock(OCSController::class),
  68. true,
  69. false
  70. ],
  71. [
  72. $this->createMock(ShareAPIController::class),
  73. false,
  74. true
  75. ],
  76. [
  77. $this->createMock(ShareAPIController::class),
  78. true,
  79. false
  80. ],
  81. ];
  82. }
  83. /**
  84. * @dataProvider dataBeforeController
  85. *
  86. * @param Controller $controller
  87. * @param bool $enabled
  88. * @param bool $exception
  89. */
  90. public function testBeforeController(Controller $controller, $enabled, $exception) {
  91. $this->shareManager->method('shareApiEnabled')->willReturn($enabled);
  92. try {
  93. $this->middleware->beforeController($controller, 'foo');
  94. $this->assertFalse($exception);
  95. } catch (OCSNotFoundException $e) {
  96. $this->assertTrue($exception);
  97. }
  98. }
  99. public function dataAfterController() {
  100. return [
  101. [
  102. $this->createMock(Controller::class),
  103. ],
  104. [
  105. $this->createMock(OCSController::class),
  106. ],
  107. [
  108. $this->createMock(ShareAPIController::class),
  109. ],
  110. ];
  111. }
  112. /**
  113. * @dataProvider dataAfterController
  114. *
  115. * @param Controller $controller
  116. * @param bool $called
  117. */
  118. public function testAfterController(Controller $controller) {
  119. if ($controller instanceof ShareAPIController) {
  120. $controller->expects($this->once())->method('cleanup');
  121. }
  122. $response = $this->getMockBuilder('OCP\AppFramework\Http\Response')
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $this->middleware->afterController($controller, 'foo', $response);
  126. $this->addToAssertionCount(1);
  127. }
  128. }