-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathFixOwncloudGroupSubshareStatusTest.php
More file actions
131 lines (102 loc) · 4.84 KB
/
Copy pathFixOwncloudGroupSubshareStatusTest.php
File metadata and controls
131 lines (102 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Sharing\Tests\Command;
use OCA\Files_Sharing\Command\FixOwncloudGroupSubshareStatus;
use OCA\Files_Sharing\Tests\TestCase;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Server;
use OCP\Share\IShare;
use Symfony\Component\Console\Tester\CommandTester;
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
class FixOwncloudGroupSubshareStatusTest extends TestCase {
private IDBConnection $connection;
private CommandTester $commandTester;
protected function setUp(): void {
parent::setUp();
$this->connection = Server::get(IDBConnection::class);
$this->commandTester = new CommandTester(new FixOwncloudGroupSubshareStatus($this->connection));
$this->cleanDB();
}
protected function tearDown(): void {
$this->cleanDB();
parent::tearDown();
}
private function cleanDB(): void {
$this->connection->getQueryBuilder()->delete('share')->executeStatement();
}
private function insertShare(int $shareType, int $accepted, int $permissions): int {
$qb = $this->connection->getQueryBuilder();
$qb->insert('share')
->values([
'share_type' => $qb->createNamedParameter($shareType, IQueryBuilder::PARAM_INT),
'share_with' => $qb->createNamedParameter('user1'),
'uid_owner' => $qb->createNamedParameter('owner'),
'uid_initiator' => $qb->createNamedParameter('owner'),
'parent' => $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT),
'item_type' => $qb->createNamedParameter('file'),
'item_source' => $qb->createNamedParameter('42'),
'item_target' => $qb->createNamedParameter('/file'),
'file_source' => $qb->createNamedParameter(42, IQueryBuilder::PARAM_INT),
'file_target' => $qb->createNamedParameter('/file'),
'permissions' => $qb->createNamedParameter($permissions, IQueryBuilder::PARAM_INT),
'stime' => $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT),
'accepted' => $qb->createNamedParameter($accepted, IQueryBuilder::PARAM_INT),
])
->executeStatement();
return (int)$this->connection->lastInsertId('*PREFIX*share');
}
private function getAccepted(int $id): int {
$qb = $this->connection->getQueryBuilder();
return (int)$qb->select('accepted')
->from('share')
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
->executeQuery()
->fetchOne();
}
public function testFixesPendingSubshareWithPermissions(): void {
$id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 31);
$this->commandTester->execute([]);
$this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id));
$this->assertStringContainsString('Fixed', $this->commandTester->getDisplay());
}
public function testDryRunShowsCountWithoutChanging(): void {
$id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 31);
$this->commandTester->execute(['--dry-run' => true]);
$this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($id));
$this->assertStringContainsString('dry-run', $this->commandTester->getDisplay());
}
public function testDoesNotTouchDeclinedSubshare(): void {
// permissions = 0 means the user explicitly declined the share
$id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 0);
$this->commandTester->execute([]);
$this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($id));
$this->assertStringContainsString('No affected', $this->commandTester->getDisplay());
}
public function testDoesNotTouchAlreadyAcceptedSubshare(): void {
$id = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_ACCEPTED, 31);
$this->commandTester->execute([]);
$this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id));
$this->assertStringContainsString('No affected', $this->commandTester->getDisplay());
}
public function testDoesNotTouchNonUsergroupShares(): void {
$id = $this->insertShare(IShare::TYPE_GROUP, IShare::STATUS_PENDING, 31);
$this->commandTester->execute([]);
$this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($id));
$this->assertStringContainsString('No affected', $this->commandTester->getDisplay());
}
public function testFixesMultipleAffectedRows(): void {
$id1 = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 31);
$id2 = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 17);
$idDeclined = $this->insertShare(IShare::TYPE_USERGROUP, IShare::STATUS_PENDING, 0);
$this->commandTester->execute([]);
$this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id1));
$this->assertSame(IShare::STATUS_ACCEPTED, $this->getAccepted($id2));
$this->assertSame(IShare::STATUS_PENDING, $this->getAccepted($idDeclined));
$this->assertStringContainsString('2', $this->commandTester->getDisplay());
}
}