summaryrefslogtreecommitdiffstats
path: root/tests/Modules/SyslogPageTest.php
blob: c9b6a3ea216ec3f3851e0080ada8d43340e2f932 (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

use PHPUnit\Framework\TestCase;

/**
 * Syslog page tests adapted to use the SQLite-backed Database backend and real Paginate.
 *
 */
class SyslogPageTest extends TestCase
{
	protected function setUp(): void
	{
		Database::resetSchema();

		// Reset stubs and superglobals for each test
		Render::reset();
		Message::reset();
		Session::reset();
		Property::reset();
		User::reset();
		$_GET = $_POST = $_REQUEST = [];

		// Default: logged in user
		User::$loggedIn = true;
		User::$id = 1;
	}

	private function newSyslogPage(): Page_SysLog
	{
		// Load the module's page class
		require_once __DIR__ . '/../../modules-available/syslog/page.inc.php';
		return new Page_SysLog('syslog');
	}

	public function testRenderDeniesWithoutViewPermission(): void
	{
		// No 'view' permission
		User::$permissions = ['view' => false];
		$mod = $this->newSyslogPage();
		$mod->preprocess();
		$mod->render();

		$this->assertContains('main.no-permission', Message::$errors, 'Should add no-permission error');
		// Heading template still added, but no 'page-syslog' template
		$names = array_column(Render::$templates, 'name');
		$this->assertNotEmpty($names);
		$this->assertSame('heading', Render::$templates[0]['name']);
		$this->assertNotContains('page-syslog', $names);
	}

	public function testRenderWithFiltersAndTypes(): void
	{
		User::$permissions = ['view' => true];
		// Apply filters via GET (we search for 'login' which exists in seed)
		$_GET['filter'] = 'session-open,foo-custom'; // includes one unknown type
		$_GET['search'] = 'login';
		$_GET['not'] = '0';
		$_REQUEST = $_GET;

		$mod = $this->newSyslogPage();
		$mod->preprocess();
		$mod->render();

		// Verify render payload from real Paginate
		$names = array_column(Render::$templates, 'name');
		$this->assertContains('page-syslog', $names);
		$tpl = null;
		foreach (Render::$templates as $t) {
			if ($t['name'] === 'page-syslog') { $tpl = $t; break; }
		}
		$this->assertNotNull($tpl);
		$data = $tpl['data'];
		$this->assertSame('session-open,foo-custom', $data['filter']);
		$this->assertSame('login', $data['search']);
		$this->assertSame(false, $data['not']);
		$this->assertIsString($data['types']);
		$typesArray = json_decode($data['types'], true);
		$this->assertIsArray($typesArray);
		$ids = array_column($typesArray, 'logtypeid');
		// Should include both DB types and unknown filter value
		$this->assertContains('session-open', $ids);
		$this->assertContains('partition-temp', $ids);
		$this->assertContains('foo-custom', $ids);

		// Each list row enriched with 'date' and 'icon'
		$this->assertArrayHasKey('list', $data);
		$this->assertNotEmpty($data['list']);
		$this->assertArrayHasKey('date', $data['list'][0]);
		$this->assertArrayHasKey('icon', $data['list'][0]);
	}

	public function testRenderWithAllowedLocationsRestrictionFiltersRows(): void
	{
		User::$permissions = ['view' => true];
		// Restrict allowed locations (no 0) -> only logs from machines in these locations remain
		User::$allowedLocations = [5, 6];
		$_REQUEST = $_GET = [];

		$mod = $this->newSyslogPage();
		$mod->preprocess();
		$mod->render();

		$tpl = null;
		foreach (Render::$templates as $t) {
			if ($t['name'] === 'page-syslog') { $tpl = $t; break; }
		}
		$this->assertNotNull($tpl);
		$data = $tpl['data'];
		$this->assertArrayHasKey('list', $data);
		// With allowedLocations [5,6] and seed machines m1@1, m2@5, only m2 should remain
		$this->assertNotEmpty($data['list']);
		$uuids = array_column($data['list'], 'machineuuid');
		$this->assertContains('m2', $uuids);
		$this->assertNotContains('m1', $uuids);
	}
}