|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2021, Mattia Narducci (mattianarducci1@gmail.com) |
| 7 | + * |
| 8 | + * @license GNU AGPL version 3 or any later version |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License as |
| 12 | + * published by the Free Software Foundation, either version 3 of the |
| 13 | + * License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +namespace OCA\DAV\Tests\Command; |
| 26 | + |
| 27 | +use OCA\DAV\CalDAV\BirthdayService; |
| 28 | +use OCA\DAV\CalDav\CalDavBackend; |
| 29 | +use OCA\DAV\Command\DeleteCalendar; |
| 30 | +use OCP\IConfig; |
| 31 | +use OCP\IUserManager; |
| 32 | +use PHPUnit\Framework\MockObject\MockObject; |
| 33 | +use Symfony\Component\Console\Tester\CommandTester; |
| 34 | +use Test\TestCase; |
| 35 | + |
| 36 | +/** |
| 37 | + * Class DeleteCalendarTest |
| 38 | + * |
| 39 | + * @package OCA\DAV\Tests\Command |
| 40 | + */ |
| 41 | +class DeleteCalendarTest extends TestCase { |
| 42 | + public const USER = 'user'; |
| 43 | + public const NAME = 'calendar'; |
| 44 | + |
| 45 | + /** @var CalDavBackend|MockObject */ |
| 46 | + private $calDav; |
| 47 | + |
| 48 | + /** @var IConfig|MockObject */ |
| 49 | + private $config; |
| 50 | + |
| 51 | + /** @var IUserManager|MockObject */ |
| 52 | + private $userManager; |
| 53 | + |
| 54 | + /** @var DeleteCalendar */ |
| 55 | + private $command; |
| 56 | + |
| 57 | + protected function setUp(): void { |
| 58 | + parent::setUp(); |
| 59 | + |
| 60 | + $this->calDav = $this->createMock(CalDavBackend::class); |
| 61 | + $this->config = $this->createMock(IConfig::class); |
| 62 | + $this->userManager = $this->createMock(IUserManager::class); |
| 63 | + |
| 64 | + $this->command = new DeleteCalendar( |
| 65 | + $this->calDav, |
| 66 | + $this->config, |
| 67 | + $this->userManager, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function testInvalidUser() { |
| 72 | + $this->expectException(\InvalidArgumentException::class); |
| 73 | + $this->expectExceptionMessage( |
| 74 | + 'User <' . self::USER . '> is unknown.'); |
| 75 | + |
| 76 | + $this->userManager->expects($this->once()) |
| 77 | + ->method('userExists') |
| 78 | + ->with(self::USER) |
| 79 | + ->willReturn(false); |
| 80 | + |
| 81 | + $commandTester = new CommandTester($this->command); |
| 82 | + $commandTester->execute([ |
| 83 | + 'uid' => self::USER, |
| 84 | + 'name' => self::NAME, |
| 85 | + ]); |
| 86 | + } |
| 87 | + |
| 88 | + public function testNoCalendarName() { |
| 89 | + $this->expectException(\InvalidArgumentException::class); |
| 90 | + $this->expectExceptionMessage( |
| 91 | + 'Please specify a calendar name or --birthday'); |
| 92 | + |
| 93 | + $this->userManager->expects($this->once()) |
| 94 | + ->method('userExists') |
| 95 | + ->with(self::USER) |
| 96 | + ->willReturn(true); |
| 97 | + |
| 98 | + $commandTester = new CommandTester($this->command); |
| 99 | + $commandTester->execute([ |
| 100 | + 'uid' => self::USER, |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + public function testInvalidCalendar() { |
| 105 | + $this->expectException(\InvalidArgumentException::class); |
| 106 | + $this->expectExceptionMessage( |
| 107 | + 'User <' . self::USER . '> has no calendar named <' . self::NAME . '>.'); |
| 108 | + |
| 109 | + $this->userManager->expects($this->once()) |
| 110 | + ->method('userExists') |
| 111 | + ->with(self::USER) |
| 112 | + ->willReturn(true); |
| 113 | + $this->calDav->expects($this->once()) |
| 114 | + ->method('getCalendarByUri') |
| 115 | + ->with( |
| 116 | + 'principals/users/' . self::USER, |
| 117 | + self::NAME |
| 118 | + ) |
| 119 | + ->willReturn(null); |
| 120 | + |
| 121 | + $commandTester = new CommandTester($this->command); |
| 122 | + $commandTester->execute([ |
| 123 | + 'uid' => self::USER, |
| 124 | + 'name' => self::NAME, |
| 125 | + ]); |
| 126 | + } |
| 127 | + |
| 128 | + public function testDelete() { |
| 129 | + $id = 1234; |
| 130 | + |
| 131 | + $this->userManager->expects($this->once()) |
| 132 | + ->method('userExists') |
| 133 | + ->with(self::USER) |
| 134 | + ->willReturn(true); |
| 135 | + $this->calDav->expects($this->once()) |
| 136 | + ->method('getCalendarByUri') |
| 137 | + ->with( |
| 138 | + 'principals/users/' . self::USER, |
| 139 | + self::NAME |
| 140 | + ) |
| 141 | + ->willReturn(['id' => $id]); |
| 142 | + $this->calDav->expects($this->once()) |
| 143 | + ->method('deleteCalendar') |
| 144 | + ->with($id, false); |
| 145 | + |
| 146 | + $commandTester = new CommandTester($this->command); |
| 147 | + $commandTester->execute([ |
| 148 | + 'uid' => self::USER, |
| 149 | + 'name' => self::NAME, |
| 150 | + ]); |
| 151 | + } |
| 152 | + |
| 153 | + public function testForceDelete() { |
| 154 | + $id = 1234; |
| 155 | + |
| 156 | + $this->userManager->expects($this->once()) |
| 157 | + ->method('userExists') |
| 158 | + ->with(self::USER) |
| 159 | + ->willReturn(true); |
| 160 | + $this->calDav->expects($this->once()) |
| 161 | + ->method('getCalendarByUri') |
| 162 | + ->with( |
| 163 | + 'principals/users/' . self::USER, |
| 164 | + self::NAME |
| 165 | + ) |
| 166 | + ->willReturn(['id' => $id]); |
| 167 | + $this->calDav->expects($this->once()) |
| 168 | + ->method('deleteCalendar') |
| 169 | + ->with($id, true); |
| 170 | + |
| 171 | + $commandTester = new CommandTester($this->command); |
| 172 | + $commandTester->execute([ |
| 173 | + 'uid' => self::USER, |
| 174 | + 'name' => self::NAME, |
| 175 | + '-f' => true |
| 176 | + ]); |
| 177 | + } |
| 178 | + |
| 179 | + public function testDeleteBirthday() { |
| 180 | + $id = 1234; |
| 181 | + |
| 182 | + $this->userManager->expects($this->once()) |
| 183 | + ->method('userExists') |
| 184 | + ->with(self::USER) |
| 185 | + ->willReturn(true); |
| 186 | + $this->calDav->expects($this->once()) |
| 187 | + ->method('getCalendarByUri') |
| 188 | + ->with( |
| 189 | + 'principals/users/' . self::USER, |
| 190 | + BirthdayService::BIRTHDAY_CALENDAR_URI |
| 191 | + ) |
| 192 | + ->willReturn(['id' => $id]); |
| 193 | + $this->config->expects($this->once()) |
| 194 | + ->method('setUserValue') |
| 195 | + ->with( |
| 196 | + self::USER, |
| 197 | + 'dav', 'generateBirthdayCalendar', 'no' |
| 198 | + ); |
| 199 | + $this->calDav->expects($this->once()) |
| 200 | + ->method('deleteCalendar') |
| 201 | + ->with($id); |
| 202 | + |
| 203 | + $commandTester = new CommandTester($this->command); |
| 204 | + $commandTester->execute([ |
| 205 | + 'uid' => self::USER, |
| 206 | + '--birthday' => true, |
| 207 | + ]); |
| 208 | + } |
| 209 | + |
| 210 | + public function testBirthdayHasPrecedence() { |
| 211 | + $this->userManager->expects($this->once()) |
| 212 | + ->method('userExists') |
| 213 | + ->with(self::USER) |
| 214 | + ->willReturn(true); |
| 215 | + $this->calDav->expects($this->once()) |
| 216 | + ->method('getCalendarByUri') |
| 217 | + ->with( |
| 218 | + 'principals/users/' . self::USER, |
| 219 | + BirthdayService::BIRTHDAY_CALENDAR_URI |
| 220 | + ) |
| 221 | + ->willReturn(['id' => 1234]); |
| 222 | + |
| 223 | + $commandTester = new CommandTester($this->command); |
| 224 | + $commandTester->execute([ |
| 225 | + 'uid' => self::USER, |
| 226 | + 'name' => self::NAME, |
| 227 | + '--birthday' => true, |
| 228 | + ]); |
| 229 | + } |
| 230 | +} |
0 commit comments