ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ilStudyProgrammeAssignmentRepositoryTest.php
Go to the documentation of this file.
1<?php
2
6class ilStudyProgrammeAssignmentRepositoryTest extends \PHPUnit\Framework\TestCase
7{
8 protected $backupGlobals = false;
9 protected static $created = [];
10 protected static $prg_1;
11 protected static $prg_2;
12 protected static $usr_1;
13 protected static $usr_2;
14
15 public static function setUpBeforeClass() : void
16 {
17 global $DIC;
18 if (!$DIC) {
19 include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
20 try {
21 ilUnitUtil::performInitialisation();
22 } catch (Exception $e) {
23 }
24 }
26 self::$prg_1->putInTree(ROOT_FOLDER_ID);
28 self::$prg_2->putInTree(ROOT_FOLDER_ID);
29 self::$usr_1 = new ilObjUser();
30 self::$usr_1->create();
31 self::$usr_2 = new ilObjUser();
32 self::$usr_2->create();
33 }
34
35
36 protected function setUp() : void
37 {
38 PHPUnit_Framework_Error_Deprecated::$enabled = false;
39
40
41 global $DIC;
42 $this->db = $DIC['ilDB'];
43 }
44
45 public function test_init()
46 {
47 $repo = new ilStudyProgrammeAssignmentDBRepository($this->db);
48 $this->assertInstanceOf(ilStudyProgrammeAssignmentRepository::class, $repo);
49 return $repo;
50 }
51
55 public function test_create($repo)
56 {
57 $ass_1 = $repo->createFor(self::$prg_2->getId(), self::$usr_1->getId(), 6);
58 self::$created[$ass_1->getID()] = $ass_1;
59 $ass_2 = $repo->createFor(self::$prg_2->getId(), self::$usr_1->getId(), 6);
60 self::$created[$ass_2->getID()] = $ass_2;
61 $ass_3 = $repo->createFor(self::$prg_2->getId(), self::$usr_2->getId(), 6);
62 self::$created[$ass_3->getID()] = $ass_3;
63 $ass_4 = $repo->createFor(self::$prg_2->getId(), self::$usr_2->getId(), 6);
64 self::$created[$ass_4->getId()] = $ass_4;
65 $this->assertEquals($ass_1->getRootId(), self::$prg_2->getId());
66 $this->assertEquals($ass_1->getUserId(), self::$usr_1->getId());
67 $this->assertEquals($ass_1->getLastChangeBy(), 6);
68 $this->assertNull($ass_1->getRestartDate());
69 $this->assertEquals(
70 $ass_1->getRestartedAssignmentId(),
72 );
73 }
74
78 public function test_save_and_load()
79 {
80 $repo = new ilStudyProgrammeAssignmentDBRepository($this->db);
81 $ass = $repo->read(current(self::$created)->getId());
82 $this->assertEquals($ass->getId(), current(self::$created)->getId());
83 $this->assertEquals($ass->getRootId(), self::$prg_2->getId());
84 $this->assertEquals($ass->getUserId(), self::$usr_1->getId());
85 $this->assertEquals($ass->getLastChangeBy(), 6);
86 $ass->setRootId(self::$prg_1->getId());
87 $ass->setLastChangeBy(self::$usr_2->getId());
88 $ass->setRestartDate(DateTime::createFromFormat('Ymd', '20210102'));
89 $ass->setRestartedAssignmentId(123);
90 $repo->update($ass);
91
92 $repo = new ilStudyProgrammeAssignmentDBRepository($this->db);
93 $ass = $repo->read(current(self::$created)->getId());
94 $this->assertEquals($ass->getId(), current(self::$created)->getId());
95 $this->assertEquals($ass->getRootId(), self::$prg_1->getId());
96 $this->assertEquals($ass->getUserId(), self::$usr_1->getId());
97 $this->assertEquals($ass->getLastChangeBy(), self::$usr_2->getId());
98 $this->assertEquals($ass->getRestartDate()->format('Ymd'), '20210102');
99 $this->assertEquals($ass->getRestartedAssignmentId(), 123);
100 }
101
105 public function test_read_by_prg_id()
106 {
107 $repo = new ilStudyProgrammeAssignmentDBRepository($this->db);
108 $this->assertCount(0, $repo->readByPrgId(-1));
109
110 $asss = $repo->readByPrgId(self::$prg_1->getId());
111 $this->assertCount(1, $asss);
112 $ass = array_shift($asss);
113 $this->assertEquals($ass->getRootId(), self::$prg_1->getId());
114 $this->assertEquals($ass->getUserId(), self::$usr_1->getId());
115
116 $asss = $repo->readByPrgId(self::$prg_2->getId());
117 $this->assertCount(3, $asss);
118 $this->assertEquals(
119 array_map(function ($ass) {
120 return $ass->getUserId();
121 }, $asss),
122 [self::$usr_1->getId(),self::$usr_2->getId(),self::$usr_2->getId()]
123 );
124 foreach ($asss as $ass) {
125 $this->assertEquals($ass->getRootId(), self::$prg_2->getId());
126 }
127 }
128
129
133 public function test_read_by_usr_id()
134 {
135 $repo = new ilStudyProgrammeAssignmentDBRepository($this->db);
136 $this->assertCount(0, $repo->readByUsrId(-1));
137
138 $asss = $repo->readByUsrId(self::$usr_1->getId());
139 $this->assertCount(2, $asss);
140 $this->assertEquals(
141 array_map(function ($ass) {
142 return $ass->getRootId();
143 }, $asss),
144 [self::$prg_1->getId(),self::$prg_2->getId()]
145 );
146
147 $asss = $repo->readByUsrId(self::$usr_2->getId());
148 $this->assertCount(2, $asss);
149 foreach ($asss as $ass) {
150 $this->assertEquals($ass->getRootId(), self::$prg_2->getId());
151 }
152 }
153
158 {
159 $repo = new ilStudyProgrammeAssignmentDBRepository($this->db);
160 $this->assertCount(0, $repo->readByUsrIdAndPrgId(-1, -2));
161
162 $asss = $repo->readByUsrIdAndPrgId(self::$usr_2->getId(), self::$prg_2->getId());
163 $this->assertCount(2, $asss);
164 foreach ($asss as $ass) {
165 $this->assertEquals($ass->getRootId(), self::$prg_2->getId());
166 $this->assertEquals($ass->getUserId(), self::$usr_2->getId());
167 }
168 }
169
174 public function test_create_error_user($repo)
175 {
176 $repo->createFor(self::$prg_1->getId(), -1, 6);
177 }
178
179
184 public function test_create_error_prg($repo)
185 {
186 $repo->createFor(-1, 6, 6);
187 }
188
189
193 public function test_read_due_to_restart()
194 {
195 $one_day = new DateInterval('P1D');
196 $yesterday = new DateTime();
197 $yesterday->sub($one_day);
198 $today = new DateTime();
199 $tomorrow = new DateTime();
200 $tomorrow->add($one_day);
201 $repo = new ilStudyProgrammeAssignmentDBRepository($this->db);
202 $u_a_repo = ilStudyProgrammeDIC::dic()['ilStudyProgrammeUserAssignmentDB'];
204 $repo->update(array_shift($created)->setRestartDate($yesterday));
205 $repo->update(array_shift($created)->setRestartDate($today));
206 $repo->update(array_shift($created)->setRestartDate($tomorrow));
207 $repo->update(array_shift($created)->setRestartDate(null));
209 $ref = [array_shift($created)->getId(),array_shift($created)->getId()];
210 $this->assertEquals(
211 array_map(function ($ass) {
212 return $ass->getId();
213 }, $repo->readDueToRestart()),
214 $ref
215 );
216 $this->assertEquals(
217 array_map(function ($ass) {
218 return $ass->getId();
219 }, $u_a_repo->getDueToRestartInstances()),
220 $ref
221 );
222
223
225 $repo->update(array_shift($created)->setRestartDate($yesterday));
226 $repo->update(array_shift($created)->setRestartDate($today)->setRestartedAssignmentId(123));
227 $repo->update(array_shift($created)->setRestartDate($tomorrow)->setRestartedAssignmentId(223));
228 $repo->update(array_shift($created)->setRestartDate(null)->setRestartedAssignmentId(323));
230 $ref = [array_shift($created)->getId()];
231 $this->assertEquals(
232 array_map(function ($ass) {
233 return $ass->getId();
234 }, $repo->readDueToRestart()),
235 $ref
236 );
237 $this->assertEquals(
238 array_map(function ($ass) {
239 return $ass->getId();
240 }, $u_a_repo->getDueToRestartInstances()),
241 $ref
242 );
243
245 $repo->update(array_shift($created)->setRestartDate($yesterday)->setRestartedAssignmentId(23));
246 $repo->update(array_shift($created)->setRestartDate($today)->setRestartedAssignmentId(123));
247 $repo->update(array_shift($created)->setRestartDate($tomorrow)->setRestartedAssignmentId(223));
248 $repo->update(array_shift($created)->setRestartDate(null)->setRestartedAssignmentId(323));
249 $this->assertEquals(
250 array_map(function ($ass) {
251 return $ass->getId();
252 }, $repo->readDueToRestart()),
253 []
254 );
255 $this->assertEquals(
256 array_map(function ($ass) {
257 return $ass->getId();
258 }, $u_a_repo->getDueToRestartInstances()),
259 []
260 );
261 }
262
263 public static function tearDownAfterClass() : void
264 {
265 global $DIC;
266 if (!$DIC) {
267 include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
268 ilUnitUtil::performInitialisation();
269 }
270 global $DIC;
271 $db = $DIC['ilDB'];
272 try {
273 self::$prg_1->delete();
274 } catch (Exception $e) {
275 }
276
277 try {
278 self::$prg_2->delete();
279 } catch (Exception $e) {
280 }
281 self::$usr_1->delete();
282 self::$usr_2->delete();
283 if (count(self::$created) > 0) {
284 $db->manipulate(
286 . ' WHERE'
287 . ' ' . $db->in(
289 array_keys(self::$created),
290 false,
291 'integer'
292 )
293
294 );
295 }
296 }
297}
An exception for terminatinating execution or to throw for unit testing.
static createInstance()
Create an instance of ilObjStudyProgramme, put in cache.
test_create_error_prg($repo)
@depends test_init @expectedException ilException
test_create_error_user($repo)
@depends test_init @expectedException ilException
$DIC
Definition: xapitoken.php:46