ListCalendarsTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\DAV\Tests\Command;
  7. use OCA\DAV\CalDAV\BirthdayService;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\DAV\Command\ListCalendars;
  10. use OCP\IUserManager;
  11. use Symfony\Component\Console\Tester\CommandTester;
  12. use Test\TestCase;
  13. /**
  14. * Class ListCalendarsTest
  15. *
  16. * @package OCA\DAV\Tests\Command
  17. */
  18. class ListCalendarsTest extends TestCase {
  19. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */
  20. private $userManager;
  21. /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject $l10n */
  22. private $calDav;
  23. /** @var ListCalendars */
  24. private $command;
  25. public const USERNAME = 'username';
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->userManager = $this->createMock(IUserManager::class);
  29. $this->calDav = $this->createMock(CalDavBackend::class);
  30. $this->command = new ListCalendars(
  31. $this->userManager,
  32. $this->calDav
  33. );
  34. }
  35. public function testWithBadUser(): void {
  36. $this->expectException(\InvalidArgumentException::class);
  37. $this->userManager->expects($this->once())
  38. ->method('userExists')
  39. ->with(self::USERNAME)
  40. ->willReturn(false);
  41. $commandTester = new CommandTester($this->command);
  42. $commandTester->execute([
  43. 'uid' => self::USERNAME,
  44. ]);
  45. $this->assertStringContainsString("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay());
  46. }
  47. public function testWithCorrectUserWithNoCalendars(): void {
  48. $this->userManager->expects($this->once())
  49. ->method('userExists')
  50. ->with(self::USERNAME)
  51. ->willReturn(true);
  52. $this->calDav->expects($this->once())
  53. ->method('getCalendarsForUser')
  54. ->with('principals/users/' . self::USERNAME)
  55. ->willReturn([]);
  56. $commandTester = new CommandTester($this->command);
  57. $commandTester->execute([
  58. 'uid' => self::USERNAME,
  59. ]);
  60. $this->assertStringContainsString("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay());
  61. }
  62. public function dataExecute() {
  63. return [
  64. [false, '✓'],
  65. [true, 'x']
  66. ];
  67. }
  68. /**
  69. * @dataProvider dataExecute
  70. */
  71. public function testWithCorrectUser(bool $readOnly, string $output): void {
  72. $this->userManager->expects($this->once())
  73. ->method('userExists')
  74. ->with(self::USERNAME)
  75. ->willReturn(true);
  76. $this->calDav->expects($this->once())
  77. ->method('getCalendarsForUser')
  78. ->with('principals/users/' . self::USERNAME)
  79. ->willReturn([
  80. [
  81. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI,
  82. ],
  83. [
  84. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $readOnly,
  85. 'uri' => 'test',
  86. '{DAV:}displayname' => 'dp',
  87. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => 'owner-principal',
  88. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname' => 'owner-dp',
  89. ]
  90. ]);
  91. $commandTester = new CommandTester($this->command);
  92. $commandTester->execute([
  93. 'uid' => self::USERNAME,
  94. ]);
  95. $this->assertStringContainsString($output, $commandTester->getDisplay());
  96. $this->assertStringNotContainsString(BirthdayService::BIRTHDAY_CALENDAR_URI, $commandTester->getDisplay());
  97. }
  98. }