UserPlaceholderHandlerTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\files_external\tests\Config;
  7. use OCA\Files_External\Config\UserPlaceholderHandler;
  8. use OCP\IRequest;
  9. use OCP\IUser;
  10. use OCP\IUserManager;
  11. use OCP\IUserSession;
  12. use OCP\Share\Exceptions\ShareNotFound;
  13. use OCP\Share\IManager;
  14. class UserPlaceholderHandlerTest extends \Test\TestCase {
  15. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  16. protected $user;
  17. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  18. protected $session;
  19. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  20. private $shareManager;
  21. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  22. private $request;
  23. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  24. private $userManager;
  25. /** @var UserPlaceholderHandler */
  26. protected $handler;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->user = $this->createMock(IUser::class);
  30. $this->user->expects($this->any())
  31. ->method('getUid')
  32. ->willReturn('alice');
  33. $this->session = $this->createMock(IUserSession::class);
  34. $this->shareManager = $this->createMock(IManager::class);
  35. $this->request = $this->createMock(IRequest::class);
  36. $this->userManager = $this->createMock(IUserManager::class);
  37. $this->handler = new UserPlaceholderHandler($this->session, $this->shareManager, $this->request, $this->userManager);
  38. }
  39. protected function setUser() {
  40. $this->session->expects($this->any())
  41. ->method('getUser')
  42. ->willReturn($this->user);
  43. }
  44. public function optionProvider() {
  45. return [
  46. ['/foo/bar/$user/foobar', '/foo/bar/alice/foobar'],
  47. [['/foo/bar/$user/foobar'], ['/foo/bar/alice/foobar']],
  48. [['/FOO/BAR/$USER/FOOBAR'], ['/FOO/BAR/alice/FOOBAR']],
  49. ];
  50. }
  51. /**
  52. * @dataProvider optionProvider
  53. */
  54. public function testHandle($option, $expected) {
  55. $this->setUser();
  56. $this->assertSame($expected, $this->handler->handle($option));
  57. }
  58. /**
  59. * @dataProvider optionProvider
  60. */
  61. public function testHandleNoUser($option) {
  62. $this->shareManager->expects($this->once())
  63. ->method('getShareByToken')
  64. ->willThrowException(new ShareNotFound());
  65. $this->assertSame($option, $this->handler->handle($option));
  66. }
  67. }