Skip to content

Commit

Permalink
Merge pull request #24 from webandco/bugfix/update-hitcounter-via-hash
Browse files Browse the repository at this point in the history
BUGFIX: RedirectRepository updates hitcounter using sourceuripathhash
  • Loading branch information
kdambekalns authored May 4, 2022
2 parents 7fbbaad + 7b7c38f commit f8e5ea1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Classes/Domain/Repository/RedirectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
77 changes: 77 additions & 0 deletions Tests/Functional/Domain/Repository/RedirectRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace Neos\RedirectHandler\DatabaseStorage\Tests\Functional\Domain\Repository;

/*
* This file is part of the Neos.RedirectHandler.DatabaseStorage package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Tests\FunctionalTestCase;
use Neos\RedirectHandler\DatabaseStorage\Domain\Repository\RedirectRepository;
use Neos\RedirectHandler\Storage\RedirectStorageInterface;

/**
* Functional tests for the RedirectRepository and dependant classes
*/
class RedirectRepositoryTest extends FunctionalTestCase
{
/**
* @var boolean
*/
protected static $testablePersistenceEnabled = true;


/**
* @var RedirectStorageInterface
*/
protected $redirectStorage;

/**
* @var RedirectRepository
*/
protected $redirectRepository;

public function setUp(): void
{
parent::setUp();
$this->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());
}
}

0 comments on commit f8e5ea1

Please sign in to comment.