ILIAS  release_8 Revision v8.24
ilTermsOfServiceHelperTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 public function testDocumentCanBeAccepted(): void
28 {
29 $dataGatewayFactory = $this->getMockBuilder(ilTermsOfServiceDataGatewayFactory::class)->getMock();
30 $dataGateway = $this->createMock(ilTermsOfServiceAcceptanceDataGateway::class);
31
32 $dataGateway
33 ->expects($this->once())
34 ->method('trackAcceptance')
35 ->with($this->isInstanceOf(ilTermsOfServiceAcceptanceEntity::class));
36
37 $dataGatewayFactory
38 ->method('getByName')
39 ->willReturn($dataGateway);
40
41 $helper = new ilTermsOfServiceHelper(
42 $dataGatewayFactory,
43 $this->createMock(ilTermsOfServiceDocumentEvaluation::class),
44 $this->createMock(ilTermsOfServiceCriterionTypeFactoryInterface::class),
45 $this->getMockBuilder(ilObjTermsOfService::class)->disableOriginalConstructor()->getMock()
46 );
47
48 $user = $this
49 ->getMockBuilder(ilObjUser::class)
50 ->disableOriginalConstructor()
51 ->onlyMethods(['getLanguage', 'getId', 'getLogin', 'writeAccepted', 'hasToAcceptTermsOfServiceInSession'])
52 ->getMock();
53
54 $user
55 ->method('getId')
56 ->willReturn(-1);
57
58 $user
59 ->method('getLogin')
60 ->willReturn('phpunit');
61
62 $user
63 ->expects($this->once())
64 ->method('writeAccepted');
65
66 $user
67 ->expects($this->once())
68 ->method('hasToAcceptTermsOfServiceInSession')
69 ->with(false);
70
71 $document = $this
72 ->getMockBuilder(ilTermsOfServiceDocument::class)
73 ->disableOriginalConstructor()
74 ->onlyMethods(['content', 'id', 'criteria', 'title'])
75 ->getMock();
76
77 $document
78 ->expects($this->atLeast(1))
79 ->method('content')
80 ->willReturn('phpunit');
81
82 $document
83 ->expects($this->atLeast(1))
84 ->method('title')
85 ->willReturn('phpunit');
86
87 $document
88 ->expects($this->atLeast(1))
89 ->method('id')
90 ->willReturn(1);
91
92 $document
93 ->expects($this->atLeast(1))
94 ->method('criteria')
95 ->willReturn([]);
96
97 $helper->trackAcceptance($user, $document);
98 }
99
100 public function testAcceptanceHistoryCanBeDeleted(): void
101 {
102 $dataGatewayFactory = $this->getMockBuilder(ilTermsOfServiceDataGatewayFactory::class)->getMock();
103 $dataGateway = $this->createMock(ilTermsOfServiceAcceptanceDataGateway::class);
104
105 $dataGateway
106 ->expects($this->once())
107 ->method('deleteAcceptanceHistoryByUser')
108 ->with($this->isInstanceOf(ilTermsOfServiceAcceptanceEntity::class));
109
110 $dataGatewayFactory
111 ->method('getByName')
112 ->willReturn($dataGateway);
113
114 $helper = new ilTermsOfServiceHelper(
115 $dataGatewayFactory,
116 $this->createMock(ilTermsOfServiceDocumentEvaluation::class),
117 $this->createMock(ilTermsOfServiceCriterionTypeFactoryInterface::class),
118 $this->getMockBuilder(ilObjTermsOfService::class)->disableOriginalConstructor()->getMock()
119 );
120
121 $user = $this
122 ->getMockBuilder(ilObjUser::class)
123 ->disableOriginalConstructor()
124 ->onlyMethods(['getId', 'getLogin'])
125 ->getMock();
126
127 $user
128 ->method('getId')
129 ->willReturn(-1);
130
131 $user
132 ->method('getLogin')
133 ->willReturn('phpunit');
134
135 $helper->deleteAcceptanceHistoryByUser($user->getId());
136 }
137
139 {
140 $dataGatewayFactory = $this->getMockBuilder(ilTermsOfServiceDataGatewayFactory::class)->getMock();
141 $dataGateway = $this->createMock(ilTermsOfServiceAcceptanceDataGateway::class);
142
143 $entity = new ilTermsOfServiceAcceptanceEntity();
144 $entity = $entity->withId(4711);
145
146 $dataGateway
147 ->expects($this->atLeast(1))
148 ->method('loadCurrentAcceptanceOfUser')
149 ->with($this->isInstanceOf(ilTermsOfServiceAcceptanceEntity::class))
150 ->willReturn($entity);
151
152 $dataGatewayFactory
153 ->method('getByName')
154 ->willReturn($dataGateway);
155
156 $helper = new ilTermsOfServiceHelper(
157 $dataGatewayFactory,
158 $this->createMock(ilTermsOfServiceDocumentEvaluation::class),
159 $this->createMock(ilTermsOfServiceCriterionTypeFactoryInterface::class),
160 $this->getMockBuilder(ilObjTermsOfService::class)->disableOriginalConstructor()->getMock()
161 );
162
163 $user = $this
164 ->getMockBuilder(ilObjUser::class)
165 ->disableOriginalConstructor()
166 ->onlyMethods(['getId', 'getLogin'])
167 ->getMock();
168
169 $user
170 ->method('getId')
171 ->willReturn(-1);
172
173 $user
174 ->method('getLogin')
175 ->willReturn('phpunit');
176
177 $this->assertInstanceOf(ilTermsOfServiceAcceptanceEntity::class, $helper->getCurrentAcceptanceForUser($user));
178 $this->assertSame($entity, $helper->getCurrentAcceptanceForUser($user));
179 }
180
182 {
183 $dataGatewayFactory = $this->getMockBuilder(ilTermsOfServiceDataGatewayFactory::class)->getMock();
184 $dataGateway = $this->createMock(ilTermsOfServiceAcceptanceDataGateway::class);
185
186 $entity = new ilTermsOfServiceAcceptanceEntity();
187 $entity = $entity->withId(4711);
188
189 $dataGateway
190 ->expects($this->atLeast(1))
191 ->method('loadById')
192 ->willReturn($entity);
193
194 $dataGatewayFactory
195 ->method('getByName')
196 ->willReturn($dataGateway);
197
198 $helper = new ilTermsOfServiceHelper(
199 $dataGatewayFactory,
200 $this->createMock(ilTermsOfServiceDocumentEvaluation::class),
201 $this->createMock(ilTermsOfServiceCriterionTypeFactoryInterface::class),
202 $this->getMockBuilder(ilObjTermsOfService::class)->disableOriginalConstructor()->getMock()
203 );
204
205 $user = $this
206 ->getMockBuilder(ilObjUser::class)
207 ->disableOriginalConstructor()
208 ->onlyMethods(['getId', 'getLogin'])
209 ->getMock();
210
211 $user
212 ->method('getId')
213 ->willReturn(-1);
214
215 $user
216 ->method('getLogin')
217 ->willReturn('phpunit');
218
219 $this->assertInstanceOf(ilTermsOfServiceAcceptanceEntity::class, $helper->getById($entity->getId()));
220 $this->assertSame($entity, $helper->getById($entity->getId()));
221 }
222
223 public function testDocumentMustBeResigned(): void
224 {
225 $dataGatewayFactory = $this->getMockBuilder(ilTermsOfServiceDataGatewayFactory::class)->getMock();
226 $dataGateway = $this->createMock(ilTermsOfServiceAcceptanceDataGateway::class);
227
228 $entity1 = new ilTermsOfServiceAcceptanceEntity();
229 $entity1 = $entity1
230 ->withId(4711)
231 ->withSerializedCriteria(
232 (new ilTermsOfServiceAcceptanceHistoryCriteriaBag('[' . implode(',', [
233 '{"id":"usr_language","value":{"lng":"de"}}',
234 '{"id":"usr_global_role","value":{"role_id":4711}}'
235 ]) . ']'))->toJson()
236 );
237
238 $entity2 = $entity1->withId(0);
239
240 $dataGateway
241 ->expects($this->exactly(3))
242 ->method('loadCurrentAcceptanceOfUser')
243 ->willReturnOnConsecutiveCalls($entity1, $entity2, $entity1);
244 $dataGatewayFactory
245 ->method('getByName')
246 ->willReturn($dataGateway);
247
248 $tos_object = $this->getMockBuilder(ilObjTermsOfService::class)->disableOriginalConstructor()->getMock();
249 $tos_object->method('getStatus')->willReturn(true);
250 $tos_object->method('shouldReevaluateOnLogin')->willReturn(true);
251
252 $user = $this
253 ->getMockBuilder(ilObjUser::class)
254 ->disableOriginalConstructor()
255 ->onlyMethods(['getId', 'getLogin', 'isAnonymous', 'getAgreeDate'])
256 ->getMock();
257 $user
258 ->method('getId')
259 ->willReturn(4711);
260 $user
261 ->method('getLogin')
262 ->willReturn('phpunit');
263 $user
264 ->method('isAnonymous')
265 ->willReturn(false);
266 $user
267 ->method('getAgreeDate')
268 ->willReturn((new ILIAS\Data\Factory())->clock()->system()->now()->format('Y-m-d H:i:s'));
269
270 $logger = $logger = $this
271 ->getMockBuilder(ilLogger::class)
272 ->disableOriginalConstructor()
273 ->getMock();
274
275 $document_evaluation = $this->createMock(ilTermsOfServiceDocumentEvaluation::class);
276 $document_evaluation->expects($this->exactly(4))
277 ->method('hasDocument')
278 ->willReturnOnConsecutiveCalls(true, false, true, true);
279 $document_evaluation->method('withContextUser')->with($user)->willReturn($document_evaluation);
280 $document_evaluation->expects($this->exactly(2))->method('evaluateDocument')
281 ->with($this->isInstanceOf(ilTermsOfServiceHistorizedDocument::class))
282 ->willReturnOnConsecutiveCalls(false, true);
283
284 $helper = new ilTermsOfServiceHelper(
285 $dataGatewayFactory,
286 $document_evaluation,
287 $this->createMock(ilTermsOfServiceCriterionTypeFactoryInterface::class),
288 $tos_object
289 );
290
291 $this->assertTrue($helper->hasToResignAcceptance($user, $logger));
292 $this->assertFalse($helper->hasToResignAcceptance($user, $logger));
293 $this->assertFalse($helper->hasToResignAcceptance($user, $logger));
294 $this->assertFalse($helper->hasToResignAcceptance($user, $logger));
295 }
296}
Class ilTermsOfServiceBaseTest.
Class ilTermsOfServiceHelperTest.
Class ilTermsOfServiceHelper.
Class ChatMainBarProvider \MainMenu\Provider.