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

use PHPUnit\Framework\TestCase;

/**
 * Migrated to SQLite-backed Database. We verify actual DB state instead of stub call logs.
 *
 */
class PermissionDbUpdateTest extends TestCase
{
	protected function setUp(): void
	{
		Database::resetSchema();
	}

	private function loadInc(): void
	{
		require_once __DIR__ . '/../../../modules-available/permissionmanager/inc/permissiondbupdate.inc.php';
	}

	public function testAddAndRemoveRoleFromUser(): void
	{
		$this->loadInc();
		// Ensure target roles exist
		Database::exec('INSERT INTO role (roleid, rolename) VALUES (:id, :n)', ['id' => 10, 'n' => 'R10']);
		Database::exec('INSERT INTO role (roleid, rolename) VALUES (:id, :n)', ['id' => 11, 'n' => 'R11']);
		// add
		PermissionDbUpdate::addRoleToUser([1,2], [10]);
		$rows = Database::queryAll('SELECT * FROM role_x_user WHERE roleid = :r ORDER BY userid, roleid', ['r' => 10]);
		$this->assertSame([[ 'userid' => 1, 'roleid' => 10 ], [ 'userid' => 2, 'roleid' => 10 ]], $rows);

		// remove
		PermissionDbUpdate::removeRoleFromUser([1,2], [10,11]);
		$rows2 = Database::queryAll('SELECT * FROM role_x_user WHERE roleid IN (:r)', ['r' => [10,11]]);
		$this->assertCount(0, $rows2);
	}

	public function testSetRolesForUserDeletesThenAdds(): void
	{
		$this->loadInc();
		Database::exec('INSERT INTO role (roleid, rolename) VALUES (:id, :n)', ['id' => 10, 'n' => 'R10']);
		Database::exec('INSERT INTO role (roleid, rolename) VALUES (:id, :n)', ['id' => 11, 'n' => 'R11']);
		// Prepopulate with an extra role to be removed
		Database::exec('INSERT INTO role_x_user (userid, roleid) VALUES (5, 12)');
		PermissionDbUpdate::setRolesForUser([5], [10, 11]);
		$rows = Database::queryAll('SELECT roleid FROM role_x_user WHERE userid = 5 ORDER BY roleid');
		$this->assertSame([[ 'roleid' => 10 ], [ 'roleid' => 11 ]], $rows);
	}

	public function testSaveRoleUpdateAndInsertPaths(): void
	{
		$this->loadInc();
		// Create existing role 42 with some mappings
		Database::exec('INSERT INTO role (roleid, rolename, roledescription) VALUES (:id, :n, :d)', ['id' => 42, 'n' => 'Old', 'd' => 'old']);
		Database::exec('INSERT INTO role_x_location (roleid, locationid) VALUES (:r, :l)', ['r' => 42, 'l' => 1]);
		Database::exec('INSERT INTO role_x_permission (roleid, permissionid) VALUES (:r, :p)', ['r' => 42, 'p' => 'permissionmanager.users.view']);
		// Update existing role
		PermissionDbUpdate::saveRole('Editors', 'desc', [0, 5], ['permissionmanager.roles.edit'], 42);
		$row = Database::queryFirst('SELECT rolename, roledescription FROM role WHERE roleid = 42');
		$this->assertSame('Editors', $row['rolename']);
		$this->assertSame('desc', $row['roledescription']);
		$locs = Database::queryAll('SELECT locationid FROM role_x_location WHERE roleid = 42 ORDER BY locationid');
		$this->assertSame([[ 'locationid' => 0 ], [ 'locationid' => 5 ]], $locs);
		$perms = Database::queryAll('SELECT permissionid FROM role_x_permission WHERE roleid = 42');
		$this->assertSame([[ 'permissionid' => 'permissionmanager.roles.edit' ]], $perms);

		// Insert new role with mappings
		$this->loadInc();
		PermissionDbUpdate::saveRole('New Role', 'desc2', [1], ['permissionmanager.users.view']);
		// Fetch the last inserted role
		$role = Database::queryFirst('SELECT roleid, rolename, roledescription FROM role WHERE rolename = :n', ['n' => 'New Role']);
		$this->assertNotFalse($role);
		$rid = (int)$role['roleid'];
		$this->assertGreaterThan(0, $rid);
		$this->assertSame('desc2', $role['roledescription']);
		$locs2 = Database::queryAll('SELECT locationid FROM role_x_location WHERE roleid = :r', ['r' => $rid]);
		$this->assertSame([[ 'locationid' => 1 ]], $locs2);
		$perms2 = Database::queryAll('SELECT permissionid FROM role_x_permission WHERE roleid = :r', ['r' => $rid]);
		$this->assertSame([[ 'permissionid' => 'permissionmanager.users.view' ]], $perms2);
	}

	public function testDeleteRole(): void
	{
		$this->loadInc();
		Database::exec('INSERT INTO role (roleid, rolename) VALUES (:id, :n)', ['id' => 77, 'n' => 'Tmp']);
		PermissionDbUpdate::deleteRole(77);
		$row = Database::queryFirst('SELECT * FROM role WHERE roleid = 77');
		$this->assertFalse($row);
	}
}