| 
 | 1 | +<?php  | 
 | 2 | + | 
 | 3 | +declare(strict_types=1);  | 
 | 4 | + | 
 | 5 | +/**  | 
 | 6 | + * @copyright Copyright (c) 2021 Thomas Citharel <nextcloud@tcit.fr>  | 
 | 7 | + *  | 
 | 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\unit\DAV\Migration;  | 
 | 27 | + | 
 | 28 | +use OCA\DAV\Migration\RemoveDeletedUsersCalendarSubscriptions;  | 
 | 29 | +use OCP\DB\IResult;  | 
 | 30 | +use OCP\DB\QueryBuilder\IExpressionBuilder;  | 
 | 31 | +use OCP\DB\QueryBuilder\IFunctionBuilder;  | 
 | 32 | +use OCP\DB\QueryBuilder\IParameter;  | 
 | 33 | +use OCP\DB\QueryBuilder\IQueryBuilder;  | 
 | 34 | +use OCP\DB\QueryBuilder\IQueryFunction;  | 
 | 35 | +use OCP\IDBConnection;  | 
 | 36 | +use OCP\IUserManager;  | 
 | 37 | +use OCP\Migration\IOutput;  | 
 | 38 | +use PHPUnit\Framework\MockObject\MockObject;  | 
 | 39 | +use Test\TestCase;  | 
 | 40 | + | 
 | 41 | +class RemoveDeletedUsersCalendarSubscriptionsTest extends TestCase {  | 
 | 42 | +	/**  | 
 | 43 | +	 * @var IDBConnection|MockObject  | 
 | 44 | +	 */  | 
 | 45 | +	private $dbConnection;  | 
 | 46 | +	/**  | 
 | 47 | +	 * @var IUserManager|MockObject  | 
 | 48 | +	 */  | 
 | 49 | +	private $userManager;  | 
 | 50 | + | 
 | 51 | +	/**  | 
 | 52 | +	 * @var IOutput|MockObject  | 
 | 53 | +	 */  | 
 | 54 | +	private $output;  | 
 | 55 | +	/**  | 
 | 56 | +	 * @var RemoveDeletedUsersCalendarSubscriptions  | 
 | 57 | +	 */  | 
 | 58 | +	private $migration;  | 
 | 59 | + | 
 | 60 | + | 
 | 61 | +	protected function setUp(): void {  | 
 | 62 | +		parent::setUp();  | 
 | 63 | + | 
 | 64 | +		$this->dbConnection = $this->createMock(IDBConnection::class);  | 
 | 65 | +		$this->userManager = $this->createMock(IUserManager::class);  | 
 | 66 | +		$this->output = $this->createMock(IOutput::class);  | 
 | 67 | + | 
 | 68 | +		$this->migration = new RemoveDeletedUsersCalendarSubscriptions($this->dbConnection, $this->userManager);  | 
 | 69 | +	}  | 
 | 70 | + | 
 | 71 | +	public function testGetName(): void {  | 
 | 72 | +		$this->assertEquals(  | 
 | 73 | +			'Clean up old calendar subscriptions from deleted users that were not cleaned-up',  | 
 | 74 | +			$this->migration->getName()  | 
 | 75 | +		);  | 
 | 76 | +	}  | 
 | 77 | + | 
 | 78 | +	/**  | 
 | 79 | +	 * @dataProvider dataTestRun  | 
 | 80 | +	 * @param array $subscriptions  | 
 | 81 | +	 * @param array $userExists  | 
 | 82 | +	 * @param int $deletions  | 
 | 83 | +	 * @throws \Exception  | 
 | 84 | +	 */  | 
 | 85 | +	public function testRun(array $subscriptions, array $userExists, int $deletions): void {  | 
 | 86 | +		$qb = $this->createMock(IQueryBuilder::class);  | 
 | 87 | + | 
 | 88 | +		$qb->method('select')->willReturn($qb);  | 
 | 89 | + | 
 | 90 | +		$functionBuilder = $this->createMock(IFunctionBuilder::class);  | 
 | 91 | + | 
 | 92 | +		$qb->method('func')->willReturn($functionBuilder);  | 
 | 93 | +		$functionBuilder->method('count')->willReturn($this->createMock(IQueryFunction::class));  | 
 | 94 | + | 
 | 95 | +		$qb->method('selectDistinct')  | 
 | 96 | +			->with(['id', 'principaluri'])  | 
 | 97 | +			->willReturn($qb);  | 
 | 98 | + | 
 | 99 | +		$qb->method('from')  | 
 | 100 | +			->with('calendarsubscriptions')  | 
 | 101 | +			->willReturn($qb);  | 
 | 102 | + | 
 | 103 | +		$qb->method('setMaxResults')  | 
 | 104 | +			->willReturn($qb);  | 
 | 105 | + | 
 | 106 | +		$qb->method('setFirstResult')  | 
 | 107 | +			->willReturn($qb);  | 
 | 108 | + | 
 | 109 | +		$result = $this->createMock(IResult::class);  | 
 | 110 | + | 
 | 111 | +		$qb->method('execute')  | 
 | 112 | +			->willReturn($result);  | 
 | 113 | + | 
 | 114 | +		$result->expects($this->at(0))  | 
 | 115 | +			->method('fetchOne')  | 
 | 116 | +			->willReturn(count($subscriptions));  | 
 | 117 | + | 
 | 118 | +		$result  | 
 | 119 | +			->method('fetch')  | 
 | 120 | +			->willReturnOnConsecutiveCalls(...$subscriptions);  | 
 | 121 | + | 
 | 122 | +		$qb->method('delete')  | 
 | 123 | +			->with('calendarsubscriptions')  | 
 | 124 | +			->willReturn($qb);  | 
 | 125 | + | 
 | 126 | +		$expr = $this->createMock(IExpressionBuilder::class);  | 
 | 127 | + | 
 | 128 | +		$qb->method('expr')->willReturn($expr);  | 
 | 129 | +		$qb->method('createNamedParameter')->willReturn($this->createMock(IParameter::class));  | 
 | 130 | +		$qb->method('where')->willReturn($qb);  | 
 | 131 | +		// Only when user exists  | 
 | 132 | +		$qb->expects($this->exactly($deletions))->method('executeStatement');  | 
 | 133 | + | 
 | 134 | +		$this->dbConnection->method('getQueryBuilder')->willReturn($qb);  | 
 | 135 | + | 
 | 136 | + | 
 | 137 | +		$this->output->expects($this->once())->method('startProgress');  | 
 | 138 | + | 
 | 139 | +		$this->output->expects($subscriptions === [] ? $this->never(): $this->once())->method('advance');  | 
 | 140 | +		if (count($subscriptions)) {  | 
 | 141 | +			$this->userManager->method('userExists')  | 
 | 142 | +				->willReturnCallback(function (string $username) use ($userExists) {  | 
 | 143 | +					return $userExists[$username];  | 
 | 144 | +				});  | 
 | 145 | +		}  | 
 | 146 | +		$this->output->expects($this->once())->method('finishProgress');  | 
 | 147 | +		$this->output->expects($this->once())->method('info')->with(sprintf('%d calendar subscriptions without an user have been cleaned up', $deletions));  | 
 | 148 | + | 
 | 149 | +		$this->migration->run($this->output);  | 
 | 150 | +	}  | 
 | 151 | + | 
 | 152 | +	public function dataTestRun(): array {  | 
 | 153 | +		return [  | 
 | 154 | +			[[], [], 0],  | 
 | 155 | +			[[[  | 
 | 156 | +				'id' => 1,  | 
 | 157 | +				'principaluri' => 'users/principals/foo1',  | 
 | 158 | +			],  | 
 | 159 | +				[  | 
 | 160 | +					'id' => 2,  | 
 | 161 | +					'principaluri' => 'users/principals/bar1',  | 
 | 162 | +				],  | 
 | 163 | +				[  | 
 | 164 | +					'id' => 3,  | 
 | 165 | +					'principaluri' => 'users/principals/bar1',  | 
 | 166 | +				]], ['foo1' => true, 'bar1' => false], 2]  | 
 | 167 | +		];  | 
 | 168 | +	}  | 
 | 169 | +}  | 
0 commit comments