ListCalendarsTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  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\DAV\Tests\Command;
  27. use OCA\DAV\CalDAV\BirthdayService;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\Command\ListCalendars;
  30. use OCP\IUserManager;
  31. use Symfony\Component\Console\Tester\CommandTester;
  32. use Test\TestCase;
  33. /**
  34. * Class ListCalendarsTest
  35. *
  36. * @package OCA\DAV\Tests\Command
  37. */
  38. class ListCalendarsTest extends TestCase {
  39. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */
  40. private $userManager;
  41. /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject $l10n */
  42. private $calDav;
  43. /** @var ListCalendars */
  44. private $command;
  45. public const USERNAME = 'username';
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->userManager = $this->createMock(IUserManager::class);
  49. $this->calDav = $this->createMock(CalDavBackend::class);
  50. $this->command = new ListCalendars(
  51. $this->userManager,
  52. $this->calDav
  53. );
  54. }
  55. public function testWithBadUser() {
  56. $this->expectException(\InvalidArgumentException::class);
  57. $this->userManager->expects($this->once())
  58. ->method('userExists')
  59. ->with(self::USERNAME)
  60. ->willReturn(false);
  61. $commandTester = new CommandTester($this->command);
  62. $commandTester->execute([
  63. 'uid' => self::USERNAME,
  64. ]);
  65. $this->assertStringContainsString("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay());
  66. }
  67. public function testWithCorrectUserWithNoCalendars() {
  68. $this->userManager->expects($this->once())
  69. ->method('userExists')
  70. ->with(self::USERNAME)
  71. ->willReturn(true);
  72. $this->calDav->expects($this->once())
  73. ->method('getCalendarsForUser')
  74. ->with('principals/users/' . self::USERNAME)
  75. ->willReturn([]);
  76. $commandTester = new CommandTester($this->command);
  77. $commandTester->execute([
  78. 'uid' => self::USERNAME,
  79. ]);
  80. $this->assertStringContainsString("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay());
  81. }
  82. public function dataExecute() {
  83. return [
  84. [false, '✓'],
  85. [true, 'x']
  86. ];
  87. }
  88. /**
  89. * @dataProvider dataExecute
  90. */
  91. public function testWithCorrectUser(bool $readOnly, string $output) {
  92. $this->userManager->expects($this->once())
  93. ->method('userExists')
  94. ->with(self::USERNAME)
  95. ->willReturn(true);
  96. $this->calDav->expects($this->once())
  97. ->method('getCalendarsForUser')
  98. ->with('principals/users/' . self::USERNAME)
  99. ->willReturn([
  100. [
  101. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI,
  102. ],
  103. [
  104. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $readOnly,
  105. 'uri' => 'test',
  106. '{DAV:}displayname' => 'dp',
  107. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => 'owner-principal',
  108. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname' => 'owner-dp',
  109. ]
  110. ]);
  111. $commandTester = new CommandTester($this->command);
  112. $commandTester->execute([
  113. 'uid' => self::USERNAME,
  114. ]);
  115. $this->assertStringContainsString($output, $commandTester->getDisplay());
  116. $this->assertStringNotContainsString(BirthdayService::BIRTHDAY_CALENDAR_URI, $commandTester->getDisplay());
  117. }
  118. }