Newer
Older
<?php
class ResourceTest extends \Codeception\Test\Unit
{
protected $db_handle;
protected $oldPerm, $oldUser;
protected function _before()
{
//First we must initialise the StudipPDO database connection:
$this->db_handle = new \StudipPDO(
'mysql:host='
. $GLOBALS['DB_STUDIP_HOST']
. ';dbname='
. $GLOBALS['DB_STUDIP_DATABASE'],
$GLOBALS['DB_STUDIP_USER'],
$GLOBALS['DB_STUDIP_PASSWORD']
);
//Then we must start a transaction before we access the database,
//otherwise we would spam the live database with test data!
$this->db_handle->beginTransaction();
//Now we tell the DBManager about the connection
//we have established to the Stud.IP database:
\DBManager::getInstance()->setConnection('studip', $this->db_handle);
// Workaround old-style Stud.IP-API using $GLOBALS['user']
$this->oldUser = $GLOBALS['user'];
$GLOBALS['user'] = new \Seminar_User(
\User::findByUsername('root@studip')
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
);
$this->oldPerm = $GLOBALS['perm'];
$GLOBALS['perm'] = new \Seminar_Perm();
//As a final step we create the SORM objects for our test cases:
$this->test_user_username = 'test_user_' . date('YmdHis');
$this->test_user = new User();
$this->test_user->username = $this->test_user_username;
$this->test_user->vorname = 'Test';
$this->test_user->nachname = 'User';
$this->test_user->perms = 'admin';
$this->test_user->store();
$this->test_property_name = 'test_' . date('YmdHis');
$this->test_property2_name = 'some_test_user_' . date('YmdHis');
$this->resource_cat = new ResourceCategory();
$this->resource_cat->name = 'Test Category';
$this->resource_cat->class_name = 'Resource';
$this->resource_cat->iconnr = '1';
$this->resource_cat->store();
$def = new ResourcePropertyDefinition();
$def->name = $this->test_property_name;
$def->type = 'text';
$def->searchable = '0';
$def->range_search = '0';
$def->store();
$def2 = new ResourcePropertyDefinition();
$def2->name = $this->test_property2_name;
$def2->type = 'user';
$def2->searchable = '0';
$def2->range_search = '0';
$def2->store();
$this->test_property = $this->resource_cat->addProperty(
$this->test_property_name,
'text',
true,
true
);
$this->test_property2 = $this->resource_cat->addProperty(
$this->test_property2_name,
'user',
false,
true
);
$this->test_resource_name = 'Test Resource ' . date('YmdHis');
$this->resource = $this->resource_cat->createResource(
$this->test_resource_name,
'Resource Description 20171013'
);
$this->resource->requestable = '1';
$this->resource->store();
$this->resource->setProperty(
$this->test_property_name,
'test'
);
$this->resource->setPropertyRelatedObject(
$this->test_property2_name,
$this->test_user
);
$permission = new ResourcePermission();
$permission->user_id = $this->test_user->id;
$permission->resource_id = $this->resource->id;
$permission->perms = 'admin';
$permission->store();
$this->booking_start_time = new DateTime('2017-10-02 8:00:00 +0000');
$this->booking_end_time = new DateTime('2017-10-02 10:00:00 +0000');
$this->booking_repeat_end = new DateTime('2017-10-06 10:00:00 +0000');
$this->booking = $this->resource->createBooking(
$this->test_user,
$this->test_user->id,
[
[
'begin' => $this->booking_start_time,
'end' => $this->booking_end_time
]
],
new DateInterval('P2D'),
2, //Shall not be regarded since a repetition end date is set.
$this->booking_repeat_end
);
$this->lock_start_time = new DateTime('2017-11-01 0:00:00 +0000');
$this->lock_end_time = new DateTime('2017-11-02 23:59:59 +0000');
$this->lock = $this->resource->createLock(
$this->test_user,
$this->lock_start_time,
$this->lock_end_time
);
$this->course = new Course();
$this->course->name = 'Test Resource Course ' . date('YmdHis');
$this->course->store();
$this->course_date = new CourseDate();
$this->course_date->range_id = $this->course->id;
$this->course_date->autor_id = $this->test_user->id;
$this->course_date->date = strtotime('2017-10-02 11:00:00 +0000');
$this->course_date->end_time = strtotime('2017-10-02 12:00:00 +0000');
$this->course_date->store();
$this->request = $this->resource->createRequest(
$this->test_user,
$this->course->id,
'test createRequest',
[
$this->test_property_name => 'test'
]
);
$this->user2 = new User();
$this->user2->username = 'test_user2_' . date('YmdHis');
$this->user2->perms = 'tutor';
$this->user2->vorname = 'Test';
$this->user2->nachname = 'User 2';
$this->user2->store();
$this->resource->setUserPermission(
$this->user2,
'admin'
);
//Everything is set up for the test cases.
}
protected function _after()
{
//We must roll back the changes we made in this test
//so that the live database remains unchanged after
//all the test cases of this test have been finished:
$this->db_handle->rollBack();
// Workaround old-style Stud.IP-API using $GLOBALS['user']
$GLOBALS['perm'] = $this->oldPerm;
$GLOBALS['user'] = $this->oldUser;
}
public function testCreateResourceWithoutCategory()
{
$this->expectException(
InvalidResourceException::class
);
$r = new Resource();
$r->name = 'Invalid Resource';
$r->store();
}
public function testCreateResource()
{
//$this->resource has been created in the _before() method
$this->assertEquals(
$this->test_resource_name,
$this->resource->name
);
$this->assertEquals(
'Resource Description 20171013',
$this->resource->description
);
}
public function testCreateBooking()
{
//The booking has been created in the _before() method.
$this->assertEquals(
$this->resource->id,
$this->booking->resource_id
);
$this->assertEquals(
$this->test_user->id,
$this->booking->range_id
);
$this->assertEquals(
$this->booking_start_time->getTimestamp(),
$this->booking->begin
);
$this->assertEquals(
$this->booking_end_time->getTimestamp(),
$this->booking->end
);
$this->assertEquals(
$this->booking_repeat_end->getTimestamp(),
$this->booking->repeat_end
);
$this->assertEquals(
null,
$this->booking->repeat_quantity
);
$this->assertEquals(
'P00Y00M02D',
$this->booking->repetition_interval
);
}
public function testCreateLock()
{
//The resource lock has been created in the _before() method.
$this->assertEquals(
$this->resource->id,
$this->lock->resource_id
);
$this->assertEquals(
$this->lock_start_time->getTimestamp(),
$this->lock->begin
);
$this->assertEquals(
$this->lock_end_time->getTimestamp(),
$this->lock->end
);
}
public function testPropertyExists()
{
//The property has been created in the _before() method.
$this->assertTrue(
$this->resource->propertyExists(
$this->test_property_name
)
);
}
public function testGetPropertyObject()
{
//The property has been created in the _before() method.
$property = $this->resource->getPropertyObject(
$this->test_property_name
);
$this->assertEquals(
$this->test_property_name,
$property->name
);
$this->assertEquals(
'test',
$property->state
);
}
public function testGetProperty()
{
//The property has been created in the _before() method.
$state = $this->resource->getProperty(
$this->test_property_name
);
$this->assertEquals(
'test',
$state
);
}
public function testNonexistingProperty()
{
//The resource has been created in the _before() method.
$this->assertFalse(
$this->resource->propertyExists(
'nonexistant_property_' . date('YmdHis')
)
);
}
public function testGetNonexistingPropertyObject()
{
//The resource has been created in the _before() method.
$no_object = $this->resource->getPropertyObject(
'nonexistant_property_' . date('YmdHis')
);
$this->assertNull(
$no_object
);
}
public function testGetNonexistingProperty()
{
//The resource has been created in the _before() method.
$no_object = $this->resource->getProperty(
'nonexistant_property_' . date('YmdHis')
);
$this->assertNull(
$no_object
);
}
public function testGetPropertyObjectWithoutName()
{
$this->expectException(TypeError::class);
//The resource has been created in the _before() method.
$no_object = $this->resource->getPropertyObject();
$this->assertEquals(
null,
$no_object
);
}
public function testGetPropertyWithoutName()
{
$this->expectException(TypeError::class);
//The resource has been created in the _before() method.
$this->assertEquals(
null,
$this->resource->getProperty()
);
}
public function testGetPropertyRelatedObject()
{
$user = $this->resource->getPropertyRelatedObject(
$this->test_property2_name
);
$this->assertEquals(
true,
($user instanceof User)
);
$this->assertEquals(
$this->test_user_username,
$user->username
);
}
public function testGetNonexistantPropertyRelatedObject()
{
$no_object = $this->resource->getPropertyRelatedObject(
'nonexistant_property' . date('YmdHis')
);
$this->assertNull(
$no_object
);
}
//Resource::setProperty and Resource::setPropertyRelatedObject
//don't need to be tested since they are called in the _before() method.
//If those set methods wouldn't work then the tests where
//properties or property objects are retrieved would fail.
public function testGetPropertyArray()
{
$properties = $this->resource->getPropertyArray();
$this->assertEquals(
2,
count($properties)
);
}
public function testGetOnlyRequestablePropertyArray()
{
$properties = $this->resource->getPropertyArray(true);
$this->assertEquals(
1,
count($properties)
);
$this->assertEquals(
$this->test_property_name,
$properties[0]['name']
);
$this->assertEquals(
'text',
$properties[0]['type']
);
$this->assertEquals(
'test',
$properties[0]['state']
);
$this->assertTrue(
$properties[0]['requestable']
);
}
public function testResourceAddChild()
{
//$this->resource has been created in the _before() method.
$this->child = $this->resource_cat->createResource(
'Test Resource Child 20171013',
'Resource Child Description 20171013'
);
$this->resource->addChild($this->child);
$this->assertEquals(
$this->resource->children[0]->id,
$this->child->id
);
$this->assertEquals(
$this->child->parent_id,
$this->resource->id
);
$this->assertEquals(
$this->child->level,
($this->resource->level+1)
);
}
public function testResourceIsAssigned()
{
//$this->resource has been created
//in the _before()-Method.
$this->assertTrue(
$this->resource->isAssigned(
$this->booking_start_time,
$this->booking_end_time
)
);
}
public function testResourceIsUnassigned()
{
$this->assertFalse(
$this->resource->isAssigned(
new DateTime('2016-10-02 7:30:00 +0000'),
new DateTime('2016-10-02 7:59:59 +0000')
)
);
}
public function testResourceIsAssignedWithInvalidTimeRange()
{
$this->expectException(
InvalidArgumentException::class
);
$this->resource->isAssigned(
new DateTime('2017-10-02 23:59:59 +0000'),
new DateTime('2017-10-02 0:00:00 +0000')
);
}
public function testGetResourceBookings()
{
//$this->resource has been created in the _before() method.
$bookings = $this->resource->getResourceBookings(
new DateTime('2017-01-01 0:00:00 +0000'),
new DateTime('2017-12-31 23:59:59 +0000')
);
$this->assertEquals(
count($bookings),
1
);
$this->assertEquals(
$bookings[0]->id,
$this->booking->id
);
$this->assertEquals(
$bookings[0]->begin,
$this->booking->begin
);
$this->assertEquals(
$bookings[0]->end,
$this->booking->end
);
}
public function testResourceIsLocked()
{
$this->assertTrue(
$this->resource->isLocked(
new DateTime('2017-11-01 8:00:00 +0000'),
new DateTime('2017-11-02 18:00:00 +0000')
)
);
}
public function testResourceIsNotLocked()
{
$this->assertFalse(
$this->resource->isLocked(
new DateTime('2017-11-20 8:00:00 +0000'),
new DateTime('2017-11-20 18:00:00 +0000')
)
);
}
public function testResourceIsLockedWithInvalidTimeRange()
{
$this->expectException(
InvalidArgumentException::class
);
$this->resource->isLocked(
new DateTime('2017-11-02 23:59:59 +0000'),
new DateTime('2017-11-01 0:00:00 +0000')
);
}
//Resource::isAvailable does not need to be tested since
//failure of one of the tests for isAvailable and isLocked would
//imply that a test for isAvailable would also fail.
public function testGetFullName()
{
$this->assertEquals(
'Ressource ' . $this->test_resource_name,
$this->resource->getFullName()
);
}
//testUserPermissions tests all user permission methods.
public function testUserPermissions()
{
//$this->test_user should have admin permissions
//on $this->resource since $this->test_user is the owner
//of $this->resource.
$this->assertEquals(
'admin',
$this->resource->getUserPermission(
$this->test_user
)
);
//user2 is set up in the _before() method and user2's
//permission level on $this->resource is set to admin.
$user2_perms = $this->resource->getUserPermission($this->user2);
$this->assertEquals(
'admin',
$user2_perms
);
//user2 should have all lower privileges since user2
//has admin permission on $this->resource:
$this->assertTrue(
$this->resource->userHasPermission(
$this->user2,
'user'
)
);
$this->assertTrue(
$this->resource->userHasPermission(
$this->user2,
'autor'
)
);
$this->assertTrue(
$this->resource->userHasPermission(
$this->user2,
'tutor'
)
);
$this->assertTrue(
$this->resource->userHasPermission(
$this->user2,
'admin'
)
);
//now we delete user2's permission and give user2
//another permission level:
$this->assertTrue(
$this->resource->deleteUserPermission($this->user2)
);
$this->assertTrue(
$this->resource->setUserPermission(
$this->user2,
'tutor'
)
);
$this->assertEquals(
'tutor',
$this->resource->getUserPermission($this->user2)
);
//Ok, we now delete every permission for
//$this->resource:
$this->assertTrue(
$this->resource->deleteAllPermissions()
);
//Now user2 should have no permissions on $this->resource:
$this->assertEquals(
'',
$this->resource->getUserPermission($this->user2)
);
}
public function testGetURL()
{
//By testing the getURL method we have automatically tested
//the methods getLink, getURLForAction and getLinkForAction
//since all those methods generate the same URL. The "link"-methods
//just generate a HTML compliant representation of them.
$link = $this->resource->getActionURL('show');
$this->assertEquals(
'dispatch.php/resources/resource/index/' . $this->resource->id,
$link
);
$link = $this->resource->getActionURL('show', ['test' => '1']);
$this->assertEquals(
'dispatch.php/resources/resource/index/' . $this->resource->id . '?test=1',
$link
);
$link = $this->resource->getActionURL('delete');
$this->assertEquals(
'dispatch.php/resources/resource/delete/' . $this->resource->id,
$link
);
}
}