ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilTermsOfServiceAcceptanceDatabaseGatewayTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
13  public function testInstanceCanBeCreated()
14  {
15  $database = $this->getMockBuilder(\ilDBInterface::class)->getMock();
16  $gateway = new \ilTermsOfServiceAcceptanceDatabaseGateway($database);
17 
18  $this->assertInstanceOf(\ilTermsOfServiceAcceptanceDatabaseGateway::class, $gateway);
19  }
20 
25  {
26  $entity = new \ilTermsOfServiceAcceptanceEntity();
27  $entity->withUserId(666);
28  $entity->withDocumentId(4711);
29  $entity->withTitle('Document PHP Unit');
30  $entity->withSerializedCriteria('');
31  $entity->withText('PHP Unit');
32  $entity->withTimestamp(time());
33  $entity->withHash(md5($entity->getText()));
34 
35  $expected_id = 4711;
36 
37  $database = $this->getMockBuilder(\ilDBInterface::class)->getMock();
38  $result = $this->getMockBuilder(\ilDBStatement::class)->getMock();
39 
40  $database
41  ->expects($this->once())
42  ->method('queryF')
43  ->with(
44  'SELECT id FROM tos_versions WHERE hash = %s AND doc_id = %s',
45  ['text', 'integer'],
46  [$entity->getHash(), $entity->getDocumentId()]
47  )->will($this->returnValue($result));
48 
49  $database
50  ->expects($this->once())
51  ->method('numRows')
52  ->with($result)->
53  will($this->returnValue(0));
54 
55  $database
56  ->expects($this->once())
57  ->method('nextId')
58  ->with('tos_versions')
59  ->will($this->returnValue($expected_id));
60 
61  $expectedVersions = [
62  'id' => ['integer', $expected_id],
63  'doc_id' => ['integer', $entity->getDocumentId()],
64  'title' => ['text', $entity->getTitle()],
65  'text' => ['clob', $entity->getText()],
66  'hash' => ['text', $entity->getHash()],
67  'ts' => ['integer', $entity->getTimestamp()]
68  ];
69  $expectedTracking = [
70  'tosv_id' => ['integer', $expected_id],
71  'usr_id' => ['integer', $entity->getUserId()],
72  'criteria' => ['clob', $entity->getSerializedCriteria()],
73  'ts' => ['integer', $entity->getTimestamp()]
74  ];
75 
76  $database
77  ->expects($this->exactly(2))
78  ->method('insert')
79  ->with(
80  $this->logicalOr('tos_versions', 'tos_acceptance_track'),
81  $this->logicalOr($expectedVersions, $expectedTracking)
82  );
83 
84  $gateway = new \ilTermsOfServiceAcceptanceDatabaseGateway($database);
85  $gateway->trackAcceptance($entity);
86  }
87 
92  {
93  $entity = new \ilTermsOfServiceAcceptanceEntity();
94  $entity->withUserId(666);
95  $entity->withDocumentId(4711);
96  $entity->withTitle('Document PHP Unit');
97  $entity->withSerializedCriteria('');
98  $entity->withText('PHP Unit');
99  $entity->withTimestamp(time());
100  $entity->withHash(md5($entity->getText()));
101 
102  $expected_id = 4711;
103 
104  $database = $this->getMockBuilder(\ilDBInterface::class)->getMock();
105  $result = $this->getMockBuilder(\ilDBStatement::class)->getMock();
106 
107  $database
108  ->expects($this->once())
109  ->method('queryF')
110  ->with(
111  'SELECT id FROM tos_versions WHERE hash = %s AND doc_id = %s',
112  ['text', 'integer'],
113  [$entity->getHash(), $entity->getDocumentId()]
114  )->will($this->returnValue($result));
115 
116  $database
117  ->expects($this->once())
118  ->method('numRows')
119  ->with($result)
120  ->will($this->returnValue(1));
121 
122  $database
123  ->expects($this->once())
124  ->method('fetchAssoc')
125  ->with($result)
126  ->will($this->returnValue(['id' => $expected_id]));
127 
128  $expectedTracking = [
129  'tosv_id' => ['integer', $expected_id],
130  'usr_id' => ['integer', $entity->getUserId()],
131  'criteria' => ['clob', $entity->getSerializedCriteria()],
132  'ts' => ['integer', $entity->getTimestamp()]
133  ];
134  $database
135  ->expects($this->once())
136  ->method('insert')
137  ->with('tos_acceptance_track', $expectedTracking);
138 
139  $gateway = new \ilTermsOfServiceAcceptanceDatabaseGateway($database);
140  $gateway->trackAcceptance($entity);
141  }
142 
147  {
148  $entity = new \ilTermsOfServiceAcceptanceEntity();
149 
150  $expected = [
151  'id' => 4711,
152  'usr_id' => 6,
153  'title' => 'Document PHP Unit',
154  'doc_id' => 4711,
155  'criteria' => '',
156  'text' => 'PHP Unit',
157  'hash' => md5('PHP Unit'),
158  'accepted_ts' => time()
159  ];
160 
161  $database = $this->getMockBuilder(\ilDBInterface::class)->getMock();
162  $database
163  ->expects($this->once())
164  ->method('fetchAssoc')
165  ->will($this->onConsecutiveCalls($expected));
166 
167  $gateway = new \ilTermsOfServiceAcceptanceDatabaseGateway($database);
168  $entity = $gateway->loadCurrentAcceptanceOfUser($entity);
169 
170  $this->assertEquals($expected['id'], $entity->getId());
171  $this->assertEquals($expected['usr_id'], $entity->getUserId());
172  $this->assertEquals($expected['doc_id'], $entity->getDocumentId());
173  $this->assertEquals($expected['title'], $entity->getTitle());
174  $this->assertEquals($expected['criteria'], $entity->getSerializedCriteria());
175  $this->assertEquals($expected['text'], $entity->getText());
176  $this->assertEquals($expected['accepted_ts'], $entity->getTimestamp());
177  $this->assertEquals($expected['hash'], $entity->getHash());
178  }
179 
184  {
185  $entity = new \ilTermsOfServiceAcceptanceEntity();
186  $entity->withUserId(4711);
187 
188  $database = $this->getMockBuilder(\ilDBInterface::class)->getMock();
189 
190  $database
191  ->expects($this->once())
192  ->method('quote')
193  ->with($entity->getUserId(), 'integer')
194  ->will($this->returnValue($entity->getUserId()));
195 
196  $database
197  ->expects($this->once())
198  ->method('manipulate')
199  ->with('DELETE FROM tos_acceptance_track WHERE usr_id = ' . $entity->getUserId());
200 
201  $gateway = new \ilTermsOfServiceAcceptanceDatabaseGateway($database);
202  $gateway->deleteAcceptanceHistoryByUser($entity);
203  }
204 
209  {
210  $entity = new \ilTermsOfServiceAcceptanceEntity();
211 
212  $expected = [
213  'id' => 4711,
214  'title' => 'Document PHP Unit',
215  'doc_id' => 4711,
216  'criteria' => '',
217  'text' => 'PHP Unit',
218  'hash' => md5('PHP Unit'),
219  ];
220 
221  $database = $this->getMockBuilder(\ilDBInterface::class)->getMock();
222  $database
223  ->expects($this->once())
224  ->method('fetchAssoc')
225  ->will($this->onConsecutiveCalls($expected));
226 
227  $gateway = new \ilTermsOfServiceAcceptanceDatabaseGateway($database);
228  $entity = $gateway->loadById($entity);
229 
230  $this->assertEquals($expected['id'], $entity->getId());
231  $this->assertEquals($expected['doc_id'], $entity->getDocumentId());
232  $this->assertEquals($expected['title'], $entity->getTitle());
233  $this->assertEquals($expected['criteria'], $entity->getSerializedCriteria());
234  $this->assertEquals($expected['text'], $entity->getText());
235  $this->assertEquals($expected['hash'], $entity->getHash());
236  }
237 }
$result
Class ilTermsOfServiceAcceptanceDatabaseGatewayTest.
Class ilTermsOfServiceBaseTest.
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.