diff --git a/Classes/Domain/Repository/RedirectRepository.php b/Classes/Domain/Repository/RedirectRepository.php index c123391..6a7be60 100644 --- a/Classes/Domain/Repository/RedirectRepository.php +++ b/Classes/Domain/Repository/RedirectRepository.php @@ -240,12 +240,12 @@ public function incrementHitCount(RedirectInterface $redirect): void $host = $redirect->getHost(); /** @var Query $query */ if ($host === null) { - $query = $this->entityManager->createQuery('UPDATE Neos\RedirectHandler\DatabaseStorage\Domain\Model\Redirect r SET r.hitCounter = r.hitCounter + 1, r.lastHit = CURRENT_TIMESTAMP() WHERE r.sourceUriPath = :sourceUriPath and r.host IS NULL'); + $query = $this->entityManager->createQuery('UPDATE Neos\RedirectHandler\DatabaseStorage\Domain\Model\Redirect r SET r.hitCounter = r.hitCounter + 1, r.lastHit = CURRENT_TIMESTAMP() WHERE r.sourceUriPathHash = :sourceUriPathHash and r.host IS NULL'); } else { - $query = $this->entityManager->createQuery('UPDATE Neos\RedirectHandler\DatabaseStorage\Domain\Model\Redirect r SET r.hitCounter = r.hitCounter + 1, r.lastHit = CURRENT_TIMESTAMP() WHERE r.sourceUriPath = :sourceUriPath and r.host = :host'); + $query = $this->entityManager->createQuery('UPDATE Neos\RedirectHandler\DatabaseStorage\Domain\Model\Redirect r SET r.hitCounter = r.hitCounter + 1, r.lastHit = CURRENT_TIMESTAMP() WHERE r.sourceUriPathHash = :sourceUriPathHash and r.host = :host'); $query->setParameter('host', $host); } - $query->setParameter('sourceUriPath', $redirect->getSourceUriPath()) + $query->setParameter('sourceUriPathHash', md5($redirect->getSourceUriPath())) ->execute(); } diff --git a/Tests/Functional/Domain/Repository/RedirectRepositoryTest.php b/Tests/Functional/Domain/Repository/RedirectRepositoryTest.php new file mode 100644 index 0000000..b29a9fe --- /dev/null +++ b/Tests/Functional/Domain/Repository/RedirectRepositoryTest.php @@ -0,0 +1,77 @@ +redirectStorage = $this->objectManager->get(RedirectStorageInterface::class); + $this->redirectRepository = $this->objectManager->get(RedirectRepository::class); + } + + /** + * @test + */ + public function incrementHitcounter() + { + $sourceHost = 'example.org'; + $sourcePath = 'some/old/product'; + $oldTargetUri = "https://www.$sourceHost/productA"; + + // create a new redirect entry + $this->redirectStorage->addRedirect($sourcePath, $oldTargetUri); + // and persist it + $this->redirectRepository->persistEntities(); + + // query the redirect model + $absoluteRedirect = $this->redirectRepository->findOneBySourceUriPathAndHost($sourcePath); + // save the hitcounter + $oldHitcounter = $absoluteRedirect->getHitCounter(); + + // increment the hitcounter + $this->redirectRepository->incrementHitCount($absoluteRedirect); + + // clearState forces the persistenceManager to fetch the redirect from the database + $this->persistenceManager->clearState(); + + // query the hitcounter again + $absoluteRedirect = $this->redirectRepository->findOneBySourceUriPathAndHost($sourcePath); + + // check if old hitcounter+1 equals the new hitcounter + $this->assertSame($oldHitcounter+1, $absoluteRedirect->getHitCounter()); + } +}