OCSShareAPIMiddlewareTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Tests\Middleware;
  7. use OCA\Files_Sharing\Controller\ShareAPIController;
  8. use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\OCS\OCSNotFoundException;
  11. use OCP\AppFramework\OCSController;
  12. use OCP\IL10N;
  13. use OCP\Share\IManager;
  14. /**
  15. * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
  16. */
  17. class OCSShareAPIMiddlewareTest extends \Test\TestCase {
  18. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  19. private $shareManager;
  20. /** @var IL10N */
  21. private $l;
  22. /** @var OCSShareAPIMiddleware */
  23. private $middleware;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->shareManager = $this->createMock(IManager::class);
  27. $this->l = $this->createMock(IL10N::class);
  28. $this->l->method('t')->willReturnArgument(0);
  29. $this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l);
  30. }
  31. public function dataBeforeController() {
  32. return [
  33. [
  34. $this->createMock(Controller::class),
  35. false,
  36. false
  37. ],
  38. [
  39. $this->createMock(Controller::class),
  40. true,
  41. false
  42. ],
  43. [
  44. $this->createMock(OCSController::class),
  45. false,
  46. false
  47. ],
  48. [
  49. $this->createMock(OCSController::class),
  50. true,
  51. false
  52. ],
  53. [
  54. $this->createMock(ShareAPIController::class),
  55. false,
  56. true
  57. ],
  58. [
  59. $this->createMock(ShareAPIController::class),
  60. true,
  61. false
  62. ],
  63. ];
  64. }
  65. /**
  66. * @dataProvider dataBeforeController
  67. *
  68. * @param Controller $controller
  69. * @param bool $enabled
  70. * @param bool $exception
  71. */
  72. public function testBeforeController(Controller $controller, $enabled, $exception) {
  73. $this->shareManager->method('shareApiEnabled')->willReturn($enabled);
  74. try {
  75. $this->middleware->beforeController($controller, 'foo');
  76. $this->assertFalse($exception);
  77. } catch (OCSNotFoundException $e) {
  78. $this->assertTrue($exception);
  79. }
  80. }
  81. public function dataAfterController() {
  82. return [
  83. [
  84. $this->createMock(Controller::class),
  85. ],
  86. [
  87. $this->createMock(OCSController::class),
  88. ],
  89. [
  90. $this->createMock(ShareAPIController::class),
  91. ],
  92. ];
  93. }
  94. /**
  95. * @dataProvider dataAfterController
  96. *
  97. * @param Controller $controller
  98. * @param bool $called
  99. */
  100. public function testAfterController(Controller $controller) {
  101. if ($controller instanceof ShareAPIController) {
  102. $controller->expects($this->once())->method('cleanup');
  103. }
  104. $response = $this->getMockBuilder('OCP\AppFramework\Http\Response')
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. $this->middleware->afterController($controller, 'foo', $response);
  108. $this->addToAssertionCount(1);
  109. }
  110. }