2020-01-24 22:57:04 +01:00
|
|
|
<?php
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 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-03-15 13:57:50 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-24 22:57:04 +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)
|
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* 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.
|
2020-01-24 22:57:04 +01:00
|
|
|
*
|
|
|
|
* 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
|
2020-02-22 18:14:36 +01:00
|
|
|
* GNU Affero General Public License for more details.
|
2020-01-24 22:57:04 +01:00
|
|
|
*
|
2020-02-22 18:14:36 +01:00
|
|
|
* 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-24 22:57:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Tests\Entity\LogSystem;
|
|
|
|
|
|
|
|
use App\Entity\Attachments\Attachment;
|
|
|
|
use App\Entity\Attachments\AttachmentType;
|
|
|
|
use App\Entity\Attachments\PartAttachment;
|
2023-04-08 01:07:59 +02:00
|
|
|
use App\Entity\LogSystem\UserLoginLogEntry;
|
2022-12-18 20:34:25 +01:00
|
|
|
use App\Entity\ProjectSystem\Project;
|
|
|
|
use App\Entity\ProjectSystem\ProjectBOMEntry;
|
2020-01-24 22:57:04 +01:00
|
|
|
use App\Entity\LogSystem\AbstractLogEntry;
|
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Entity\Parts\Footprint;
|
|
|
|
use App\Entity\Parts\Manufacturer;
|
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Entity\Parts\Storelocation;
|
|
|
|
use App\Entity\Parts\Supplier;
|
|
|
|
use App\Entity\UserSystem\Group;
|
|
|
|
use App\Entity\UserSystem\User;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class AbstractLogEntryTest extends TestCase
|
|
|
|
{
|
|
|
|
public function levelDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[0, 'emergency'],
|
|
|
|
[1, 'alert'],
|
|
|
|
[2, 'critical'],
|
|
|
|
[3, 'error'],
|
|
|
|
[4, 'warning'],
|
|
|
|
[5, 'notice'],
|
|
|
|
[6, 'info'],
|
|
|
|
[7, 'debug'],
|
|
|
|
[8, 'blabla', true],
|
2020-03-15 13:57:50 +01:00
|
|
|
[-1, 'test', true],
|
2020-01-24 22:57:04 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function targetTypeDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[1, User::class],
|
|
|
|
[2, Attachment::class],
|
|
|
|
[3, AttachmentType::class],
|
|
|
|
[4, Category::class],
|
2022-12-18 20:34:25 +01:00
|
|
|
[5, Project::class],
|
|
|
|
[6, ProjectBOMEntry::class],
|
2020-01-24 22:57:04 +01:00
|
|
|
[7, Footprint::class],
|
|
|
|
[8, Group::class],
|
|
|
|
[9, Manufacturer::class],
|
|
|
|
[10, Part::class],
|
|
|
|
[11, Storelocation::class],
|
|
|
|
[12, Supplier::class],
|
2020-03-15 13:57:50 +01:00
|
|
|
[-1, 'blablub', true],
|
2020-01-24 22:57:04 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider levelDataProvider
|
|
|
|
*/
|
2020-03-15 13:57:50 +01:00
|
|
|
public function testLevelIntToString(int $int, string $expected_string, bool $expect_exception = false): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
if ($expect_exception) {
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
}
|
|
|
|
$this->assertSame($expected_string, AbstractLogEntry::levelIntToString($int));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider levelDataProvider
|
|
|
|
*/
|
2020-03-15 13:57:50 +01:00
|
|
|
public function testLevelStringToInt(int $expected_int, string $string, bool $expect_exception = false): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
if ($expect_exception) {
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
}
|
|
|
|
$this->assertSame($expected_int, AbstractLogEntry::levelStringToInt($string));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider targetTypeDataProvider
|
|
|
|
*/
|
2020-03-15 13:57:50 +01:00
|
|
|
public function testTargetTypeIdToClass(int $int, string $expected_class, bool $expect_exception = false): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
if ($expect_exception) {
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
}
|
|
|
|
$this->assertSame($expected_class, AbstractLogEntry::targetTypeIdToClass($int));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider targetTypeDataProvider
|
|
|
|
*/
|
2020-03-15 13:57:50 +01:00
|
|
|
public function testTypeClassToID(int $expected_id, string $class, bool $expect_exception = false): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
if ($expect_exception) {
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
}
|
|
|
|
$this->assertSame($expected_id, AbstractLogEntry::targetTypeClassToID($class));
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:57:50 +01:00
|
|
|
public function testTypeClassToIDSubclasses(): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
//Test if class mapping works for subclasses
|
|
|
|
$this->assertSame(2, AbstractLogEntry::targetTypeClassToID(PartAttachment::class));
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:57:50 +01:00
|
|
|
public function testSetGetTarget(): void
|
2020-01-24 22:57:04 +01:00
|
|
|
{
|
|
|
|
$part = $this->createMock(Part::class);
|
|
|
|
$part->method('getID')->willReturn(10);
|
|
|
|
|
2020-03-15 13:57:50 +01:00
|
|
|
$log = new class() extends AbstractLogEntry {
|
|
|
|
};
|
2020-01-24 22:57:04 +01:00
|
|
|
$log->setTargetElement($part);
|
|
|
|
|
|
|
|
$this->assertSame(Part::class, $log->getTargetClass());
|
|
|
|
$this->assertSame(10, $log->getTargetID());
|
|
|
|
|
|
|
|
$log->setTargetElement(null);
|
2020-03-29 22:47:25 +02:00
|
|
|
$this->assertNull($log->getTargetClass());
|
|
|
|
$this->assertNull($log->getTargetID());
|
2020-01-24 22:57:04 +01:00
|
|
|
}
|
2023-04-08 01:07:59 +02:00
|
|
|
|
|
|
|
public function testCLIUsername(): void
|
|
|
|
{
|
|
|
|
$log = new UserLoginLogEntry('1.1.1.1');
|
|
|
|
|
2023-04-15 23:14:53 +02:00
|
|
|
//By default, no CLI username is set
|
2023-04-08 01:07:59 +02:00
|
|
|
$this->assertNull($log->getCLIUsername());
|
2023-04-08 01:13:13 +02:00
|
|
|
$this->assertFalse($log->isCLIEntry());
|
2023-04-08 01:07:59 +02:00
|
|
|
|
|
|
|
$user = new User();
|
|
|
|
$user->setName('test');
|
|
|
|
$log->setUser($user);
|
|
|
|
|
|
|
|
//Set a CLI username
|
2023-04-08 01:13:13 +02:00
|
|
|
$log->setCLIUsername('root');
|
2023-04-08 01:07:59 +02:00
|
|
|
$this->assertSame('root', $log->getCLIUsername());
|
2023-04-08 01:13:13 +02:00
|
|
|
$this->assertTrue($log->isCLIEntry());
|
2023-04-08 01:07:59 +02:00
|
|
|
|
|
|
|
//Normal user must be null now
|
|
|
|
$this->assertNull($log->getUser());
|
|
|
|
}
|
2020-01-24 22:57:04 +01:00
|
|
|
}
|