ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
4require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceAcceptanceDatabaseGateway.php';
5require_once 'Services/TermsOfService/classes/class.ilTermsOfServiceAcceptanceEntity.php';
6require_once 'Services/TermsOfService/test/ilTermsOfServiceBaseTest.php';
7
13{
17 protected $backupGlobals = false;
18
22 public function setUp()
23 {
24 parent::setUp();
25 }
26
30 public function testInstanceCanBeCreated()
31 {
32 $database = $this->getMockBuilder('ilDBInterface')->getMock();
33 $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
34 $this->assertInstanceOf('ilTermsOfServiceAcceptanceDatabaseGateway', $gateway);
35 }
36
41 {
43 $entity->setUserId(666);
44 $entity->setIso2LanguageCode('de');
45 $entity->setSource('/path/to/file');
46 $entity->setSourceType(0);
47 $entity->setText('PHP Unit');
48 $entity->setTimestamp(time());
49 $entity->setHash(md5($entity->getText()));
50
51 $expected_id = 4711;
52
53 $database = $this->getMockBuilder('ilDBInterface')->getMock();
54 $result = $this->getMockBuilder('ilDBStatement')->getMock();
55
56 $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));
57 $database->expects($this->once())->method('numRows')->with($result)->will($this->returnValue(0));
58 $database->expects($this->once())->method('nextId')->with('tos_versions')->will($this->returnValue($expected_id));
59
60 $expectedVersions = array(
61 'id' => array('integer', $expected_id),
62 'lng' => array('text', $entity->getIso2LanguageCode()),
63 'src' => array('text', $entity->getSource()),
64 'src_type' => array('integer', $entity->getSourceType()),
65 'text' => array('clob', $entity->getText()),
66 'hash' => array('text', $entity->getHash()),
67 'ts' => array('integer', $entity->getTimestamp())
68 );
69 $expectedTracking = array(
70 'tosv_id' => array('integer', $expected_id),
71 'usr_id' => array('integer', $entity->getUserId()),
72 'ts' => array('integer', $entity->getTimestamp())
73 );
74 $database->expects($this->exactly(2))->method('insert')->with(
75 $this->logicalOr('tos_versions', 'tos_acceptance_track'),
76 $this->logicalOr($expectedVersions, $expectedTracking)
77 );
78
79 $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
80 $gateway->trackAcceptance($entity);
81 }
82
87 {
89 $entity->setUserId(666);
90 $entity->setIso2LanguageCode('de');
91 $entity->setSource('/path/to/file');
92 $entity->setSourceType(0);
93 $entity->setText('PHP Unit');
94 $entity->setTimestamp(time());
95 $entity->setHash(md5($entity->getText()));
96
97 $expected_id = 4711;
98
99 $database = $this->getMockBuilder('ilDBInterface')->getMock();
100 $result = $this->getMockBuilder('ilDBStatement')->getMock();
101
102 $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));
103 $database->expects($this->once())->method('numRows')->with($result)->will($this->returnValue(1));
104 $database->expects($this->once())->method('fetchAssoc')->with($result)->will($this->returnValue(array('id' => $expected_id)));
105
106 $expectedTracking = array(
107 'tosv_id' => array('integer', $expected_id),
108 'usr_id' => array('integer', $entity->getUserId()),
109 'ts' => array('integer', $entity->getTimestamp())
110 );
111 $database->expects($this->once())->method('insert')->with('tos_acceptance_track', $expectedTracking);
112
113 $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
114 $gateway->trackAcceptance($entity);
115 }
116
121 {
122 $entity = new ilTermsOfServiceAcceptanceEntity();
123
124 $expected = array(
125 'id' => 4711,
126 'usr_id' => 6,
127 'lng' => 'de',
128 'src' => '/path/to/file',
129 'src_type' => 0,
130 'text' => 'PHP Unit',
131 'hash' => md5('PHP Unit'),
132 'accepted_ts' => time()
133 );
134
135 $database = $this->getMockBuilder('ilDBInterface')->getMock();
136 $database->expects($this->once())->method('fetchAssoc')->will($this->onConsecutiveCalls($expected));
137 $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
138 $gateway->loadCurrentAcceptanceOfUser($entity);
139
140 $this->assertEquals($expected['id'], $entity->getId());
141 $this->assertEquals($expected['usr_id'], $entity->getUserId());
142 $this->assertEquals($expected['lng'], $entity->getIso2LanguageCode());
143 $this->assertEquals($expected['src'], $entity->getSource());
144 $this->assertEquals($expected['src_type'], $entity->getSourceType());
145 $this->assertEquals($expected['text'], $entity->getText());
146 $this->assertEquals($expected['accepted_ts'], $entity->getTimestamp());
147 $this->assertEquals($expected['hash'], $entity->getHash());
148 }
149
154 {
155 $entity = new ilTermsOfServiceAcceptanceEntity();
156 $entity->setUserId(4711);
157
158 $database = $this->getMockBuilder('ilDBInterface')->getMock();
159 $database->expects($this->once())->method('quote')->with($entity->getUserId(), 'integer')->will($this->returnValue($entity->getUserId()));
160 $database->expects($this->once())->method('manipulate')->with('DELETE FROM tos_acceptance_track WHERE usr_id = ' . $entity->getUserId());
161 $gateway = new ilTermsOfServiceAcceptanceDatabaseGateway($database);
162 $gateway->deleteAcceptanceHistoryByUser($entity);
163 }
164}
$result
An exception for terminatinating execution or to throw for unit testing.