OCSShareAPIMiddlewareTest.php 3.6 KB

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