ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ilTermsOfServiceAcceptanceDatabaseGatewayTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceAcceptanceDatabaseGateway.php';
5 require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceAcceptanceEntity.php';
6 
12 {
16  protected $backupGlobals = false;
17 
21  public function setUp()
22  {
23  }
24 
28  public function testInstanceCanBeCreated()
29  {
30  $database = $this->getMockBuilder('ilDB')->disableOriginalConstructor()->getMock();
31  $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
32  $this->assertInstanceOf('ilTermsOfServiceAcceptanceDatabaseGateway', $gateway);
33  }
34 
39  {
40  $entity = new ilTermsOfServiceAcceptanceEntity();
41  $entity->setUserId(666);
42  $entity->setIso2LanguageCode('de');
43  $entity->setSource('/path/to/file');
44  $entity->setSourceType(0);
45  $entity->setText('PHP Unit');
46  $entity->setTimestamp(time());
47  $entity->setHash(md5($entity->getText()));
48 
49  $expected_id = 4711;
50 
51  $database = $this->getMockBuilder('ilDB')->disableOriginalConstructor()->getMock();
52  $result = $this->getMockBuilder('MDB2_BufferedResult_mysqli')->disableOriginalConstructor()->getMock();
53 
54  $database->expects($this->once())->method('queryF')->with('SELECT id FROM tos_versions WHERE hash = %s AND lng = %s', array('text', 'text'), array($entity->getHash(), $entity->getIso2LanguageCode()))->will($this->returnValue($result));
55  $database->expects($this->once())->method('numRows')->with($result)->will($this->returnValue(0));
56  $database->expects($this->once())->method('nextId')->with('tos_versions')->will($this->returnValue($expected_id));
57 
58  $expectedVersions = array(
59  'id' => array('integer', $expected_id),
60  'lng' => array('text', $entity->getIso2LanguageCode()),
61  'src' => array('text', $entity->getSource()),
62  'src_type' => array('integer', $entity->getSourceType()),
63  'text' => array('clob', $entity->getText()),
64  'hash' => array('text', $entity->getHash()),
65  'ts' => array('integer', $entity->getTimestamp())
66  );
67  $expectedTracking = array(
68  'tosv_id' => array('integer', $expected_id),
69  'usr_id' => array('integer', $entity->getUserId()),
70  'ts' => array('integer', $entity->getTimestamp())
71  );
72  $database->expects($this->exactly(2))->method('insert')->with(
73  $this->logicalOr('tos_versions', 'tos_acceptance_track'),
74  $this->logicalOr($expectedVersions, $expectedTracking)
75  );
76 
77  $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
78  $gateway->trackAcceptance($entity);
79  }
80 
85  {
86  $entity = new ilTermsOfServiceAcceptanceEntity();
87  $entity->setUserId(666);
88  $entity->setIso2LanguageCode('de');
89  $entity->setSource('/path/to/file');
90  $entity->setSourceType(0);
91  $entity->setText('PHP Unit');
92  $entity->setTimestamp(time());
93  $entity->setHash(md5($entity->getText()));
94 
95  $expected_id = 4711;
96 
97  $database = $this->getMockBuilder('ilDB')->disableOriginalConstructor()->getMock();
98  $result = $this->getMockBuilder('MDB2_BufferedResult_mysqli')->disableOriginalConstructor()->getMock();
99 
100  $database->expects($this->once())->method('queryF')->with('SELECT id FROM tos_versions WHERE hash = %s AND lng = %s', array('text', 'text'), array($entity->getHash(), $entity->getIso2LanguageCode()))->will($this->returnValue($result));
101  $database->expects($this->once())->method('numRows')->with($result)->will($this->returnValue(1));
102  $database->expects($this->once())->method('fetchAssoc')->with($result)->will($this->returnValue(array('id' => $expected_id)));
103 
104  $expectedTracking = array(
105  'tosv_id' => array('integer', $expected_id),
106  'usr_id' => array('integer', $entity->getUserId()),
107  'ts' => array('integer', $entity->getTimestamp())
108  );
109  $database->expects($this->once())->method('insert')->with('tos_acceptance_track', $expectedTracking);
110 
111  $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
112  $gateway->trackAcceptance($entity);
113  }
114 
119  {
120  $entity = new ilTermsOfServiceAcceptanceEntity();
121 
122  $expected = array(
123  'id' => 4711,
124  'usr_id' => 6,
125  'lng' => 'de',
126  'src' => '/path/to/file',
127  'src_type' => 0,
128  'text' => 'PHP Unit',
129  'accepted_ts' => time()
130  );
131 
132  $database = $this->getMockBuilder('ilDB')->disableOriginalConstructor()->getMock();
133  $database->expects($this->once())->method('fetchAssoc')->will($this->onConsecutiveCalls($expected));
134  $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
135  $gateway->loadCurrentAcceptanceOfUser($entity);
136 
137  $this->assertEquals($expected['id'], $entity->getId());
138  $this->assertEquals($expected['usr_id'], $entity->getUserId());
139  $this->assertEquals($expected['lng'], $entity->getIso2LanguageCode());
140  $this->assertEquals($expected['src'], $entity->getSource());
141  $this->assertEquals($expected['src_type'], $entity->getSourceType());
142  $this->assertEquals($expected['text'], $entity->getText());
143  $this->assertEquals($expected['accepted_ts'], $entity->getTimestamp());
144  }
145 
150  {
151  $entity = new ilTermsOfServiceAcceptanceEntity();
152  $entity->setUserId(4711);
153 
154  $database = $this->getMockBuilder('ilDB')->disableOriginalConstructor()->getMock();
155  $database->expects($this->once())->method('quote')->with($entity->getUserId(), 'integer')->will($this->returnValue($entity->getUserId()));
156  $database->expects($this->once())->method('manipulate')->with('DELETE FROM tos_acceptance_track WHERE usr_id = ' . $entity->getUserId());
157  $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
158  $gateway->deleteAcceptanceHistoryByUser($entity);
159  }
160 }