|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2021 Daniel Rudolf <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Daniel Rudolf <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Polls\Tests\Integration\Command\Share; |
|
25
|
|
|
|
|
26
|
|
|
use OCA\Polls\Db\Poll; |
|
27
|
|
|
use OCA\Polls\Db\PollMapper; |
|
28
|
|
|
use OCA\Polls\Db\Share; |
|
29
|
|
|
use OCA\Polls\Db\ShareMapper; |
|
30
|
|
|
use OCA\Polls\Service\ShareService; |
|
31
|
|
|
use OCP\IGroupManager; |
|
32
|
|
|
use OCP\IUserManager; |
|
33
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
34
|
|
|
|
|
35
|
|
|
trait TShareCommandTest { |
|
36
|
|
|
/** @var PollMapper|MockObject */ |
|
37
|
|
|
protected $pollMapper; |
|
38
|
|
|
|
|
39
|
|
|
/** @var ShareMapper|MockObject */ |
|
40
|
|
|
protected $shareMapper; |
|
41
|
|
|
|
|
42
|
|
|
/** @var ShareService|MockObject */ |
|
43
|
|
|
protected $shareService; |
|
44
|
|
|
|
|
45
|
|
|
/** @var IUserManager|MockObject */ |
|
46
|
|
|
protected $userManager; |
|
47
|
|
|
|
|
48
|
|
|
/** @var IGroupManager|MockObject */ |
|
49
|
|
|
protected $groupManager; |
|
50
|
|
|
|
|
51
|
|
|
/** @var int */ |
|
52
|
|
|
protected $lastShareId = 0; |
|
53
|
|
|
|
|
54
|
|
|
protected function setUpMocks(): void { |
|
55
|
|
|
$this->pollMapper = $this->createMock(PollMapper::class); |
|
56
|
|
|
$this->shareMapper = $this->createMock(ShareMapper::class); |
|
57
|
|
|
$this->shareService = $this->createMock(ShareService::class); |
|
58
|
|
|
$this->userManager = $this->createMock(IUserManager::class); |
|
59
|
|
|
$this->groupManager = $this->createMock(IGroupManager::class); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function createPollMock(int $id): Poll { |
|
63
|
|
|
/** @var Poll|MockObject $poll */ |
|
64
|
|
|
$poll = $this->createMock( |
|
65
|
|
|
Poll::class, |
|
66
|
|
|
['getId'] |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$poll->method('getId') |
|
70
|
|
|
->willReturn($id); |
|
71
|
|
|
|
|
72
|
|
|
return $poll; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function createShareMock(int $pollId, string $type, string $userId): Share { |
|
76
|
|
|
/** @var Share|MockObject $share */ |
|
77
|
|
|
$share = $this->createMock( |
|
78
|
|
|
Share::class, |
|
79
|
|
|
['getId', 'getPollId', 'getType', 'getEmailAddress', 'getToken'], |
|
80
|
|
|
['getUserId'] |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$id = ++$this->lastShareId; |
|
84
|
|
|
$token = $this->getShareToken($pollId, $type, $userId); |
|
85
|
|
|
|
|
86
|
|
|
$share->method('getId') |
|
87
|
|
|
->willReturn($id); |
|
88
|
|
|
|
|
89
|
|
|
$share->method('getPollId') |
|
90
|
|
|
->willReturn($pollId); |
|
91
|
|
|
|
|
92
|
|
|
$share->method('getType') |
|
93
|
|
|
->willReturn($type); |
|
94
|
|
|
|
|
95
|
|
|
$share->method('getUserId') |
|
96
|
|
|
->willReturn($userId); |
|
97
|
|
|
|
|
98
|
|
|
$share->method('getEmailAddress') |
|
99
|
|
|
->willReturn($userId); |
|
100
|
|
|
|
|
101
|
|
|
$share->method('getToken') |
|
102
|
|
|
->willReturn($token); |
|
103
|
|
|
|
|
104
|
|
|
return $share; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getShareToken(int $pollId, string $type, string $userId): string { |
|
108
|
|
|
return substr(md5($pollId . '_' . $type . '_' . $userId), 0, 16); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function createMock($originalClassName, array $addMethods = null, array $onlyMethods = null): MockObject { |
|
112
|
|
|
$mockBuilder = $this->getMockBuilder($originalClassName) |
|
|
|
|
|
|
113
|
|
|
->disableOriginalConstructor() |
|
114
|
|
|
->disableOriginalClone() |
|
115
|
|
|
->disableArgumentCloning() |
|
116
|
|
|
->disallowMockingUnknownTypes() |
|
117
|
|
|
->disableAutoReturnValueGeneration(); |
|
118
|
|
|
|
|
119
|
|
|
if ($addMethods !== null) { |
|
120
|
|
|
$mockBuilder->addMethods($addMethods); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($onlyMethods !== null) { |
|
124
|
|
|
$mockBuilder->onlyMethods($onlyMethods); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $mockBuilder->getMock(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|