From 71d95b2692e65b8e12eddb8f22ee7ca809abfdf3 Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Wed, 10 Aug 2022 10:53:07 +0200 Subject: [PATCH 1/3] fix Cleanup Query if no snapshot to keep --- src/Entity/SnapshotManager.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Entity/SnapshotManager.php b/src/Entity/SnapshotManager.php index 7510e510a..8f5c967e6 100644 --- a/src/Entity/SnapshotManager.php +++ b/src/Entity/SnapshotManager.php @@ -256,11 +256,14 @@ public function cleanup(PageInterface $page, $keep) $qb = $this->getRepository()->createQueryBuilder('s'); $expr = $qb->expr(); $qb->delete() - ->where($expr->eq('s.page', $page->getId())) - ->andWhere($expr->notIn( + ->where($expr->eq('s.page', $page->getId())); + + if (!empty($innerArray)) { + $qb->andWhere($expr->notIn( 's.id', $innerArray )); + } return $qb->getQuery()->execute(); } From 95c2b198088ac0c7d1b48086047fba1b720d6de1 Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Wed, 10 Aug 2022 11:21:41 +0200 Subject: [PATCH 2/3] ~ extend Testcase for Query without id to keep --- .../Snapshot/SnapshotManagerTest.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/Functional/Snapshot/SnapshotManagerTest.php b/tests/Functional/Snapshot/SnapshotManagerTest.php index 0f1fdc843..bdc1134d5 100644 --- a/tests/Functional/Snapshot/SnapshotManagerTest.php +++ b/tests/Functional/Snapshot/SnapshotManagerTest.php @@ -180,6 +180,66 @@ public function testCleanupPages(): void static::assertNull($repo->find(789)); } + public function testCleanupAllPages(): void + { + $this->disableAutoIncrement(SonataPagePage::class); + + // Try to write Doctrine Fixture? + $page = new SonataPagePage(); + $page->setId(234); + $page->setName('Name 234'); + $page->setEnabled(true); + $page->setTemplateCode('TemplateCode'); + $page->setRouteName('/page234'); + + $this->entityManager->persist($page); + $this->entityManager->flush(); + + $page2 = new SonataPagePage(); + $page2->setId(456); + $page2->setName('Name 456'); + $page2->setEnabled(true); + $page2->setTemplateCode('TemplateCode'); + $page2->setRouteName('/page456'); + + $this->entityManager->persist($page2); + $this->entityManager->flush(); + + $date = new \DateTime(); + + $this->disableAutoIncrement(SonataPageSnapshot::class); + + $snapshot = new SonataPageSnapshot(); + $snapshot->setId(123); + $snapshot->setPage($page2); + $snapshot->setEnabled(true); + $snapshot->setName('Name 123'); + $snapshot->setRouteName('/snapshot123'); + $snapshot->setPublicationDateEnd($date); + $this->entityManager->persist($snapshot); + $this->entityManager->flush(); + + // this should have no snapshot to keep, because the page doesn't have any + // but also no snapshot should be deleted + $this->snapshotManager->cleanup($page, 1); + + // asking if entity manager contains entity isn't enough, see below + $repo = $this->entityManager->getRepository(SonataPageSnapshot::class); + $all = $repo->findAll(); + + // The Snapshot should still be there + static::assertCount(1, $all); + static::assertContains($snapshot, $all); + + // object still exist in entityManager, that is by design + static::assertTrue($this->entityManager->contains($snapshot)); + + // EntityManager clear is brutal, can't use the entities anymore + $this->entityManager->clear(); + + static::assertNotNull($repo->find(123)); + } + public static function assertDateTimeEquals(\DateTime $expected, \DateTime $actual) { static::assertSame($expected->format('c'), $actual->format('c')); From 9517a65a678dabf583774cf1bd1d0440892b4e66 Mon Sep 17 00:00:00 2001 From: Hans Mackowiak Date: Wed, 10 Aug 2022 12:15:39 +0200 Subject: [PATCH 3/3] ~ fixes --- src/Entity/SnapshotManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entity/SnapshotManager.php b/src/Entity/SnapshotManager.php index 8f5c967e6..e088ae467 100644 --- a/src/Entity/SnapshotManager.php +++ b/src/Entity/SnapshotManager.php @@ -258,7 +258,7 @@ public function cleanup(PageInterface $page, $keep) $qb->delete() ->where($expr->eq('s.page', $page->getId())); - if (!empty($innerArray)) { + if ([] !== $innerArray) { $qb->andWhere($expr->notIn( 's.id', $innerArray