|
10 | 10 |
|
11 | 11 | use OCA\DAV\Command\RemoveInvalidShares; |
12 | 12 | use OCA\DAV\Connector\Sabre\Principal; |
| 13 | +use OCA\DAV\DAV\RemoteUserPrincipalBackend; |
| 14 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
13 | 15 | use OCP\IDBConnection; |
14 | 16 | use OCP\Server; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
15 | 18 | use Symfony\Component\Console\Input\InputInterface; |
16 | 19 | use Symfony\Component\Console\Output\OutputInterface; |
17 | 20 | use Test\TestCase; |
|
23 | 26 | * @group DB |
24 | 27 | */ |
25 | 28 | class RemoveInvalidSharesTest extends TestCase { |
| 29 | + private RemoveInvalidShares $command; |
| 30 | + |
| 31 | + private IDBConnection $db; |
| 32 | + private Principal&MockObject $principalBackend; |
| 33 | + private RemoteUserPrincipalBackend&MockObject $remoteUserPrincipalBackend; |
| 34 | + |
26 | 35 | protected function setUp(): void { |
27 | 36 | parent::setUp(); |
28 | | - $db = Server::get(IDBConnection::class); |
29 | 37 |
|
30 | | - $db->insertIfNotExist('*PREFIX*dav_shares', [ |
| 38 | + $this->db = Server::get(IDBConnection::class); |
| 39 | + $this->principalBackend = $this->createMock(Principal::class); |
| 40 | + $this->remoteUserPrincipalBackend = $this->createMock(RemoteUserPrincipalBackend::class); |
| 41 | + |
| 42 | + $this->db->insertIfNotExist('*PREFIX*dav_shares', [ |
31 | 43 | 'principaluri' => 'principal:unknown', |
32 | 44 | 'type' => 'calendar', |
33 | 45 | 'access' => 2, |
34 | 46 | 'resourceid' => 666, |
35 | 47 | ]); |
36 | | - } |
37 | | - |
38 | | - public function test(): void { |
39 | | - $db = Server::get(IDBConnection::class); |
40 | | - $principal = $this->createMock(Principal::class); |
| 48 | + $this->db->insertIfNotExist('*PREFIX*dav_shares', [ |
| 49 | + 'principaluri' => 'principals/remote-users/foobar', |
| 50 | + 'type' => 'calendar', |
| 51 | + 'access' => 2, |
| 52 | + 'resourceid' => 666, |
| 53 | + ]); |
41 | 54 |
|
42 | | - $repair = new RemoveInvalidShares($db, $principal); |
43 | | - $this->invokePrivate($repair, 'run', [$this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)]); |
| 55 | + $this->command = new RemoveInvalidShares( |
| 56 | + $this->db, |
| 57 | + $this->principalBackend, |
| 58 | + $this->remoteUserPrincipalBackend, |
| 59 | + ); |
| 60 | + } |
44 | 61 |
|
45 | | - $query = $db->getQueryBuilder(); |
| 62 | + private function selectShares(): array { |
| 63 | + $query = $this->db->getQueryBuilder(); |
46 | 64 | $query->select('*') |
47 | 65 | ->from('dav_shares') |
48 | | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter('principal:unknown'))); |
| 66 | + ->where($query->expr()->in( |
| 67 | + 'principaluri', |
| 68 | + $query->createNamedParameter( |
| 69 | + ['principal:unknown', 'principals/remote-users/foobar'], |
| 70 | + IQueryBuilder::PARAM_STR_ARRAY, |
| 71 | + ), |
| 72 | + )); |
49 | 73 | $result = $query->executeQuery(); |
50 | 74 | $data = $result->fetchAll(); |
51 | 75 | $result->closeCursor(); |
52 | | - $this->assertEquals(0, count($data)); |
| 76 | + |
| 77 | + return $data; |
| 78 | + } |
| 79 | + |
| 80 | + public function testWithoutPrincipals(): void { |
| 81 | + $this->principalBackend->method('getPrincipalByPath') |
| 82 | + ->willReturnMap([ |
| 83 | + ['principal:unknown', null], |
| 84 | + ['principals/remote-users/foobar', null], |
| 85 | + ]); |
| 86 | + $this->remoteUserPrincipalBackend->method('getPrincipalByPath') |
| 87 | + ->willReturnMap([ |
| 88 | + ['principal:unknown', null], |
| 89 | + ['principals/remote-users/foobar', null], |
| 90 | + ]); |
| 91 | + |
| 92 | + $this->command->run( |
| 93 | + $this->createMock(InputInterface::class), |
| 94 | + $this->createMock(OutputInterface::class), |
| 95 | + ); |
| 96 | + |
| 97 | + $data = $this->selectShares(); |
| 98 | + $this->assertCount(0, $data); |
| 99 | + } |
| 100 | + |
| 101 | + public function testWithLocalPrincipal(): void { |
| 102 | + $this->principalBackend->method('getPrincipalByPath') |
| 103 | + ->willReturnMap([ |
| 104 | + ['principal:unknown', ['uri' => 'principal:unknown']], |
| 105 | + ['principals/remote-users/foobar', null], |
| 106 | + ]); |
| 107 | + $this->remoteUserPrincipalBackend->method('getPrincipalByPath') |
| 108 | + ->willReturnMap([ |
| 109 | + ['principals/remote-users/foobar', null], |
| 110 | + ]); |
| 111 | + |
| 112 | + $this->command->run( |
| 113 | + $this->createMock(InputInterface::class), |
| 114 | + $this->createMock(OutputInterface::class), |
| 115 | + ); |
| 116 | + |
| 117 | + $data = $this->selectShares(); |
| 118 | + $this->assertCount(1, $data); |
| 119 | + $this->assertEquals('principal:unknown', $data[0]['principaluri']); |
| 120 | + } |
| 121 | + |
| 122 | + public function testWithRemotePrincipal() { |
| 123 | + $this->principalBackend->method('getPrincipalByPath') |
| 124 | + ->willReturnMap([ |
| 125 | + ['principal:unknown', null], |
| 126 | + ['principals/remote-users/foobar', null], |
| 127 | + ]); |
| 128 | + $this->remoteUserPrincipalBackend->method('getPrincipalByPath') |
| 129 | + ->willReturnMap([ |
| 130 | + ['principal:unknown', null], |
| 131 | + ['principals/remote-users/foobar', ['uri' => 'principals/remote-users/foobar']], |
| 132 | + ]); |
| 133 | + |
| 134 | + $this->command->run( |
| 135 | + $this->createMock(InputInterface::class), |
| 136 | + $this->createMock(OutputInterface::class), |
| 137 | + ); |
| 138 | + |
| 139 | + $data = $this->selectShares(); |
| 140 | + $this->assertCount(1, $data); |
| 141 | + $this->assertEquals('principals/remote-users/foobar', $data[0]['principaluri']); |
53 | 142 | } |
54 | 143 | } |
0 commit comments