diff --git a/src/DataFixtures/LogEntryFixtures.php b/src/DataFixtures/LogEntryFixtures.php index e61a5b77..cb5d823f 100644 --- a/src/DataFixtures/LogEntryFixtures.php +++ b/src/DataFixtures/LogEntryFixtures.php @@ -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 diff --git a/src/Repository/LogEntryRepository.php b/src/Repository/LogEntryRepository.php index 38a4d98b..daad1a81 100644 --- a/src/Repository/LogEntryRepository.php +++ b/src/Repository/LogEntryRepository.php @@ -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') diff --git a/tests/Repository/LogEntryRepositoryTest.php b/tests/Repository/LogEntryRepositoryTest.php index 1231ee8b..c2329bc7 100644 --- a/tests/Repository/LogEntryRepositoryTest.php +++ b/tests/Repository/LogEntryRepositoryTest.php @@ -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()); } }