Added missing tests for LogEntryRepository

This commit is contained in:
Jan Böhmer 2024-06-23 16:07:42 +02:00
parent e0e4b74b6f
commit c68a647e75
3 changed files with 55 additions and 5 deletions

View file

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace App\DataFixtures;
use App\Entity\LogSystem\ElementCreatedLogEntry;
use App\Entity\LogSystem\ElementDeletedLogEntry;
use App\Entity\LogSystem\ElementEditedLogEntry;
use App\Entity\Parts\Category;
use App\Entity\UserSystem\User;
@ -37,6 +38,7 @@ class LogEntryFixtures extends Fixture implements DependentFixtureInterface
public function load(ObjectManager $manager)
{
$this->createCategoryEntries($manager);
$this->createDeletedCategory($manager);
}
public function createCategoryEntries(ObjectManager $manager): void
@ -57,8 +59,37 @@ class LogEntryFixtures extends Fixture implements DependentFixtureInterface
$manager->persist($logEntry);
$manager->flush();
}
public function createDeletedCategory(ObjectManager $manager): void
{
//We create a fictive category to test the deletion
$category = new Category();
$category->setName('Node 100');
//Assume a category with id 100 was deleted
$reflClass = new \ReflectionClass($category);
$reflClass->getProperty('id')->setValue($category, 100);
//The whole lifecycle from creation to deletion
$logEntry = new ElementCreatedLogEntry($category);
$logEntry->setUser($this->getReference(UserFixtures::ADMIN, User::class));
$logEntry->setComment('Creation');
$manager->persist($logEntry);
$logEntry = new ElementEditedLogEntry($category);
$logEntry->setUser($this->getReference(UserFixtures::ADMIN, User::class));
$logEntry->setComment('Edit');
$logEntry->setOldData(['name' => 'Test']);
$logEntry->setNewData(['name' => 'Node 100']);
$manager->persist($logEntry);
$logEntry = new ElementDeletedLogEntry($category);
$logEntry->setUser($this->getReference(UserFixtures::ADMIN, User::class));
$logEntry->setOldData(['name' => 'Node 100', 'id' => 100, 'comment' => 'Test comment']);
$manager->persist($logEntry);
$manager->flush();
}
public function getDependencies(): array

View file

@ -111,7 +111,6 @@ class LogEntryRepository extends DBElementRepository
{
$qb = $this->createQueryBuilder('log');
$qb->select('log')
//->where('log INSTANCE OF App\Entity\LogSystem\ElementEditedLogEntry')
->where('log INSTANCE OF '.ElementEditedLogEntry::class)
->orWhere('log INSTANCE OF '.CollectionElementDeleted::class)
->andWhere('log.target_type = :target_type')

View file

@ -21,6 +21,7 @@
namespace App\Tests\Repository;
use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\LogSystem\ElementEditedLogEntry;
use App\Entity\Parts\Category;
use App\Entity\Parts\Part;
use App\Entity\UserSystem\User;
@ -123,15 +124,34 @@ class LogEntryRepositoryTest extends KernelTestCase
$this->assertFalse($this->repo->getElementExistedAtTimestamp($part, new \DateTimeImmutable('2000-01-01')));
}
public function testGetTimetravelDataForElement(): void
{
}
public function testGetElementHistory(): void
{
$category = $this->entityManager->find(Category::class, 1);
$history = $this->repo->getElementHistory($category);
//We have 4 log entries for the category
$this->assertCount(4, $history);
}
public function testGetTimetravelDataForElement(): void
{
$category = $this->entityManager->find(Category::class, 1);
$data = $this->repo->getTimetravelDataForElement($category, new \DateTimeImmutable('2020-01-01'));
//The data must contain only ElementChangedLogEntry
$this->assertCount(2, $data);
$this->assertInstanceOf(ElementEditedLogEntry::class, $data[0]);
$this->assertInstanceOf(ElementEditedLogEntry::class, $data[1]);
}
public function testGetUndeleteDataForElement(): void
{
$undeleteData = $this->repo->getUndeleteDataForElement(Category::class, 100);
//This must be the delete log entry we created in the fixtures
$this->assertSame('Node 100', $undeleteData->getOldName());
}
}