summaryrefslogtreecommitdiffstats
path: root/tests/Modules/PermissionManager/PermissionManagerPageTest.php
blob: 835e2484f9080138492002e7b3be21d70c2e8adb (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
<?php

use PHPUnit\Framework\TestCase;

/**
 * Adapted to use the SQLite-backed Database test backend and real Location class.
 *
 */
class PermissionManagerPageTest extends TestCase
{
	protected function setUp(): void
	{
		Database::resetSchema();
		Render::reset();
		User::reset();
		$_GET = $_POST = $_REQUEST = $_SERVER = [];
		// Logged in user id (not used directly by this module flow)
		User::$loggedIn = true;
		User::$id = 123;
		$_SERVER['REQUEST_METHOD'] = 'GET';
		// Use real Location implementation for helper functions used by the module
		$GLOBALS['__TEST_USE_REAL_CLASSES'] = ['Location'];
		require_once 'modules-available/locations/inc/location.inc.php';
	}

	private function newPage(): Page_PermissionManager
	{
		require_once 'modules-available/permissionmanager/inc/getpermissiondata.inc.php';
		require_once 'modules-available/permissionmanager/page.inc.php';
		return new Page_PermissionManager('permissionmanager');
	}

	public function testRenderRolesTable(): void
	{
		User::$permissions = ['roles.*' => true];
		$_GET['show'] = 'roles';
		$_REQUEST = $_GET;

		$page = $this->newPage();
		$page->preprocess();
		$page->render();

		$names = array_column(Render::$templates, 'name');
		$this->assertContains('rolestable', $names);
	}

	public function testRenderUsersTableWithAllRoles(): void
	{
		User::$permissions = ['users.*' => true, 'users.edit-roles' => true];
		$_GET['show'] = 'users';
		$_REQUEST = $_GET;

		$page = $this->newPage();
		$page->preprocess();
		$page->render();

		$names = array_column(Render::$templates, 'name');
		$this->assertContains('role-filter-selectize', $names);
		$this->assertContains('userstable', $names);
	}

	public function testRenderLocationsTable(): void
	{
		User::$permissions = ['locations.*' => true];
		$_GET['show'] = 'locations';
		$_REQUEST = $_GET;

		$page = $this->newPage();
		$page->preprocess();
		$page->render();

		$names = array_column(Render::$templates, 'name');
		$this->assertContains('locationstable', $names);
	}
}