2019-03-26 23:34:40 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-03-26 23:34:40 +01:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-03-26 23:34:40 +01:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-03-26 23:34:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-11-09 00:31:42 +01:00
|
|
|
namespace App\Tests\Entity\UserSystem;
|
2019-03-26 23:34:40 +01:00
|
|
|
|
2019-08-16 16:43:31 +02:00
|
|
|
use App\Entity\UserSystem\PermissionsEmbed;
|
2019-03-26 23:34:40 +01:00
|
|
|
use Doctrine\ORM\Mapping\Embedded;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
2019-03-26 23:34:40 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-01-05 22:49:00 +01:00
|
|
|
use ReflectionClass;
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
class PermissionsEmbedTest extends TestCase
|
|
|
|
{
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetPermissionValue(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
//For newly created embedded, all things should be set to inherit => null
|
|
|
|
//Test both normal name and constants
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::CONFIG, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::ATTACHMENT_TYPES, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::CATEGORIES, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICE_PARTS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICES, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::FOOTRPINTS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::GROUPS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::LABELS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::MANUFACTURERS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ATTACHMENTS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_COMMENT, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_DESCRIPTION, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_FOOTPRINT, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MANUFACTURER, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MINAMOUNT, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_NAME, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDER, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDERDETAILS, 0));
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::USERS, 0));
|
2019-03-26 23:34:40 +01:00
|
|
|
|
|
|
|
//Set a value for testing to the part property
|
2020-01-05 22:49:00 +01:00
|
|
|
$reflection = new ReflectionClass($embed);
|
2019-03-26 23:34:40 +01:00
|
|
|
$property = $reflection->getProperty('parts');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
|
|
|
$property->setValue($embed, 0b11011000); // 11 01 10 00
|
|
|
|
|
|
|
|
//Test if function is working correctly
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
|
|
|
|
$this->assertFalse($embed->getPermissionValue(PermissionsEmbed::PARTS, 2));
|
|
|
|
$this->assertTrue($embed->getPermissionValue(PermissionsEmbed::PARTS, 4));
|
|
|
|
// 11 is reserved, but it should be also treat as INHERIT.
|
|
|
|
$this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 6));
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetBitValue(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
|
|
|
|
//Set a value for testing to the part property
|
2020-01-05 22:49:00 +01:00
|
|
|
$reflection = new ReflectionClass($embed);
|
2019-03-26 23:34:40 +01:00
|
|
|
$property = $reflection->getProperty('parts');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
|
|
|
|
$property->setValue($embed, 0b11011000); // 11 01 10 00
|
|
|
|
|
|
|
|
//Test if function is working correctly
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame(PermissionsEmbed::INHERIT, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
|
|
|
|
$this->assertSame(PermissionsEmbed::DISALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 2));
|
|
|
|
$this->assertSame(PermissionsEmbed::ALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 4));
|
2019-03-26 23:34:40 +01:00
|
|
|
// 11 is reserved, but it should be also treat as INHERIT.
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame(0b11, $embed->getBitValue(PermissionsEmbed::PARTS, 6));
|
2019-03-26 23:34:40 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testInvalidPermissionName(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
//When encoutering an unknown permission name the class must throw an exception
|
2020-01-05 22:49:00 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2019-03-26 23:34:40 +01:00
|
|
|
$embed->getPermissionValue('invalid', 0);
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testInvalidBit1(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
//When encoutering an negative bit the class must throw an exception
|
2020-01-05 22:49:00 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2019-03-26 23:34:40 +01:00
|
|
|
$embed->getPermissionValue('parts', -1);
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testInvalidBit2(): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
//When encoutering an odd bit number it must throw an error.
|
2020-01-05 22:49:00 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2019-03-26 23:34:40 +01:00
|
|
|
$embed->getPermissionValue('parts', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStatesBINARY()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'ALLOW' => [PermissionsEmbed::ALLOW],
|
|
|
|
'DISALLOW' => [PermissionsEmbed::DISALLOW],
|
|
|
|
'INHERIT' => [PermissionsEmbed::INHERIT],
|
2019-11-09 00:47:20 +01:00
|
|
|
'0b11' => [0b11],
|
2019-03-26 23:34:40 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStatesBOOL()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'ALLOW' => [true],
|
|
|
|
'DISALLOW' => [false],
|
|
|
|
'INHERIT' => [null],
|
2019-11-09 00:47:20 +01:00
|
|
|
'0b11' => [null],
|
2019-03-26 23:34:40 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getStatesBINARY
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testTestsetBitValue($value): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
//Check if it returns itself, for chaining.
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($embed, $embed->setBitValue(PermissionsEmbed::PARTS, 0, $value));
|
|
|
|
$this->assertSame($value, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
|
2019-03-26 23:34:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getStatesBOOL
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testSetPermissionValue($value): void
|
2019-03-26 23:34:40 +01:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
//Check if it returns itself, for chaining.
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($embed, $embed->setPermissionValue(PermissionsEmbed::PARTS, 0, $value));
|
|
|
|
$this->assertSame($value, $embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
|
2019-10-26 22:27:04 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testSetRawPermissionValue(): void
|
2019-10-26 22:27:04 +02:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
$embed->setRawPermissionValue(PermissionsEmbed::PARTS, 10);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame(10, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
|
2019-10-26 22:27:04 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testSetRawPermissionValues(): void
|
2019-10-26 22:27:04 +02:00
|
|
|
{
|
|
|
|
$embed = new PermissionsEmbed();
|
|
|
|
$embed->setRawPermissionValues([
|
|
|
|
PermissionsEmbed::PARTS => 0,
|
|
|
|
PermissionsEmbed::USERS => 100,
|
2019-11-09 00:47:20 +01:00
|
|
|
PermissionsEmbed::CATEGORIES => 1304,
|
2019-10-26 22:27:04 +02:00
|
|
|
]);
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
|
|
|
|
$this->assertSame(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
|
|
|
|
$this->assertSame(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
|
2019-10-26 22:27:04 +02:00
|
|
|
|
|
|
|
//Test second method to pass perm names and values
|
|
|
|
$embed->setRawPermissionValues(
|
|
|
|
[PermissionsEmbed::PARTS, PermissionsEmbed::USERS, PermissionsEmbed::CATEGORIES],
|
|
|
|
[0, 100, 1304]
|
|
|
|
);
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
|
|
|
|
$this->assertSame(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
|
|
|
|
$this->assertSame(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
|
2019-03-26 23:34:40 +01:00
|
|
|
}
|
|
|
|
}
|