1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
|
<?php
use PHPUnit\Framework\TestCase;
/**
* Tests for modules-available/exams/page.inc.php using SQLite DB and stubs.
*
*/
class ExamsPageTest extends TestCase
{
protected function setUp(): void
{
Database::resetSchema();
Render::reset();
Message::reset();
User::reset();
$_GET = $_POST = $_REQUEST = [];
// Use real Location
$GLOBALS['__TEST_USE_REAL_CLASSES'] = ['Location'];
require_once __DIR__ . '/../../../modules-available/locations/inc/location.inc.php';
require_once __DIR__ . '/../../../modules-available/exams/inc/exams.inc.php';
require_once __DIR__ . '/../../../modules-available/exams/page.inc.php';
// Logged in
User::$loggedIn = true;
User::$id = 1;
}
private function seedBasicExamAndLecture(int $locationId, string $examDesc = 'Exam 1'): void
{
$now = time();
Database::exec('INSERT INTO exams (examid, lectureid, autologin, starttime, endtime, description) VALUES (100, :l, :a, :s, :e, :d)', [
'l' => 'LEC-1', 'a' => '', 's' => $now - 3600, 'e' => $now + 3600, 'd' => $examDesc
]);
Database::exec('INSERT INTO exams_x_location (examid, locationid) VALUES (100, :lid)', ['lid' => $locationId]);
Database::exec('INSERT INTO sat_user (userid, firstname, lastname, email) VALUES (1, "T", "U", "t@u")');
Database::exec('INSERT INTO sat_lecture (lectureid, ownerid, displayname, starttime, endtime, isexam, isenabled) VALUES (:id, 1, :n, :ls, :le, 1, 1)', [
'id' => 'LEC-1', 'n' => 'Lecture 1', 'ls' => $now - 7200, 'le' => $now + 7200
]);
Database::exec('INSERT INTO sat_lecture_x_location (lectureid, locationid) VALUES (:id, :lid)', ['id' => 'LEC-1', 'lid' => $locationId]);
}
public function testShowRendersListsAndVisWithPermissions(): void
{
// Permissions: view on all, edit only for location 2
User::$permissions = ['exams.view' => true, 'exams.edit' => true];
User::$allowedLocations = [2];
$this->seedBasicExamAndLecture(2, 'My Exam');
$_GET = ['action' => 'show'];
$_REQUEST = $_GET;
$page = new Page_Exams('exams');
$page->preprocess();
$page->render();
$names = array_column(Render::$templates, 'name');
$this->assertContains('page-main-heading', $names);
$this->assertContains('page-exams', $names);
$this->assertContains('page-exams-vis', $names);
// Find page-exams-vis payload and verify JSON fields decode
$vis = null; foreach (Render::$templates as $t) { if ($t['name'] === 'page-exams-vis') { $vis = $t; break; } }
$this->assertNotNull($vis);
$data = $vis['data'];
$items = json_decode($data['exams_json'], true);
$groups = json_decode($data['rooms_json'], true);
$this->assertIsArray($items);
$this->assertIsArray($groups);
$this->assertNotEmpty($items);
$this->assertNotEmpty($groups);
$groupIds = array_column($groups, 'id');
$this->assertContains(2, $groupIds);
}
public function testShowFiltersByViewLocationsAndDisablesEditWhenCannotEditAll(): void
{
// User can view location 2 but cannot edit location 3
User::$permissions = ['exams.view' => true, 'exams.edit' => false];
User::$allowedLocations = [2];
$this->seedBasicExamAndLecture(3, 'Other Exam');
$_GET = ['action' => 'show'];
$_REQUEST = $_GET;
$page = new Page_Exams('exams');
$page->preprocess();
$page->render();
// page-exams rows payload should mark edit disabled when user lacks edit perms for locations
$tpl = null;
foreach (Render::$templates as $t) {
if ($t['name'] === 'page-exams') {
$tpl = $t;
break;
}
}
$this->assertNotNull($tpl);
$rows = $tpl['data']['exams'] ?? [];
if (!empty($rows)) {
$this->assertArrayHasKey('edit', $rows[0]);
$this->assertSame('disabled', $rows[0]['edit']['disabled'] ?? '');
}
}
}
|