Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Cleanup Query if no snapshot to keep #1536

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Entity/SnapshotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Hanmac marked this conversation as resolved.
Show resolved Hide resolved
$qb->andWhere($expr->notIn(
's.id',
$innerArray
));
}

return $qb->getQuery()->execute();
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Functional/Snapshot/SnapshotManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down