1
0

ListCalendarsTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\Command;
  28. use OCA\DAV\CalDAV\BirthdayService;
  29. use OCA\DAV\CalDAV\CalDavBackend;
  30. use OCA\DAV\Command\ListCalendars;
  31. use OCP\IUserManager;
  32. use Symfony\Component\Console\Tester\CommandTester;
  33. use Test\TestCase;
  34. /**
  35. * Class ListCalendarsTest
  36. *
  37. * @package OCA\DAV\Tests\Command
  38. */
  39. class ListCalendarsTest extends TestCase {
  40. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */
  41. private $userManager;
  42. /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject $l10n */
  43. private $calDav;
  44. /** @var ListCalendars */
  45. private $command;
  46. public const USERNAME = 'username';
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->userManager = $this->createMock(IUserManager::class);
  50. $this->calDav = $this->createMock(CalDavBackend::class);
  51. $this->command = new ListCalendars(
  52. $this->userManager,
  53. $this->calDav
  54. );
  55. }
  56. public function testWithBadUser(): void {
  57. $this->expectException(\InvalidArgumentException::class);
  58. $this->userManager->expects($this->once())
  59. ->method('userExists')
  60. ->with(self::USERNAME)
  61. ->willReturn(false);
  62. $commandTester = new CommandTester($this->command);
  63. $commandTester->execute([
  64. 'uid' => self::USERNAME,
  65. ]);
  66. $this->assertStringContainsString("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay());
  67. }
  68. public function testWithCorrectUserWithNoCalendars(): void {
  69. $this->userManager->expects($this->once())
  70. ->method('userExists')
  71. ->with(self::USERNAME)
  72. ->willReturn(true);
  73. $this->calDav->expects($this->once())
  74. ->method('getCalendarsForUser')
  75. ->with('principals/users/' . self::USERNAME)
  76. ->willReturn([]);
  77. $commandTester = new CommandTester($this->command);
  78. $commandTester->execute([
  79. 'uid' => self::USERNAME,
  80. ]);
  81. $this->assertStringContainsString("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay());
  82. }
  83. public function dataExecute() {
  84. return [
  85. [false, '✓'],
  86. [true, 'x']
  87. ];
  88. }
  89. /**
  90. * @dataProvider dataExecute
  91. */
  92. public function testWithCorrectUser(bool $readOnly, string $output): void {
  93. $this->userManager->expects($this->once())
  94. ->method('userExists')
  95. ->with(self::USERNAME)
  96. ->willReturn(true);
  97. $this->calDav->expects($this->once())
  98. ->method('getCalendarsForUser')
  99. ->with('principals/users/' . self::USERNAME)
  100. ->willReturn([
  101. [
  102. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI,
  103. ],
  104. [
  105. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $readOnly,
  106. 'uri' => 'test',
  107. '{DAV:}displayname' => 'dp',
  108. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => 'owner-principal',
  109. '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname' => 'owner-dp',
  110. ]
  111. ]);
  112. $commandTester = new CommandTester($this->command);
  113. $commandTester->execute([
  114. 'uid' => self::USERNAME,
  115. ]);
  116. $this->assertStringContainsString($output, $commandTester->getDisplay());
  117. $this->assertStringNotContainsString(BirthdayService::BIRTHDAY_CALENDAR_URI, $commandTester->getDisplay());
  118. }
  119. }