Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 4 additions & 9 deletions Classes/Domain/Model/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Visitor extends AbstractModel
protected bool $blacklisted = false;

protected ?FrontendUser $frontenduser = null;
protected ?Pagevisit $lastPageVisit = null;

public function __construct()
{
Expand Down Expand Up @@ -603,16 +604,10 @@ public function removePagevisit(Pagevisit $pagevisit): self

public function getLastPagevisit(): ?Pagevisit
{
static $lastPagevisit = null;
if ($lastPagevisit === null) {
$pagevisits = $this->getPagevisits();
$lastPagevisit = null;
foreach ($pagevisits as $pagevisit) {
$lastPagevisit = $pagevisit;
break;
}
if ($this->lastPageVisit === null) {
$this->lastPageVisit = array_last($this->getPagevisits());
}
return $lastPagevisit;
return $this->lastPageVisit;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions Tests/Unit/Domain/Model/VisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

namespace In2code\Lux\Tests\Unit\Domain\Model;

use DateTime;
use In2code\Lux\Domain\Model\Attribute;
use In2code\Lux\Domain\Model\Categoryscoring;
use In2code\Lux\Domain\Model\Pagevisit;
use In2code\Lux\Domain\Model\Visitor;
use In2code\Lux\Tests\Helper\TestingHelper;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

#[CoversClass(Visitor::class)]
#[CoversMethod(Visitor::class, 'getCategoryscoringsSortedByScoring')]
#[CoversMethod(Visitor::class, 'getFullName')]
#[CoversMethod(Visitor::class, 'getNameCombination')]
#[CoversMethod(Visitor::class, 'getLastPagevisit')]
class VisitorTest extends UnitTestCase
{
protected array $testFilesToDelete = [];
Expand Down Expand Up @@ -123,4 +127,36 @@ public function testGetCategoryscoringsSortedByScoring(array $sortings, array $e
}
self::assertSame($expectedSortings, $newScoringArray);
}

#[Test]
public function getLastPagevisitCachesResultPerVisitor(): void
{
$visitor = new Visitor();
$pagevisit = new Pagevisit();
$pagevisit->setCrdate(new DateTime('2026-01-01 10:00:00'));
$visitor->addPagevisit($pagevisit);

self::assertSame($pagevisit, $visitor->getLastPagevisit());

$newerPagevisit = new Pagevisit();
$newerPagevisit->setCrdate(new DateTime('2026-01-02 10:00:00'));
$visitor->addPagevisit($newerPagevisit);

self::assertSame($pagevisit, $visitor->getLastPagevisit());
}

#[Test]
public function getLastPagevisitIsNotSharedBetweenVisitors(): void
{
$firstPagevisit = new Pagevisit();
$firstVisitor = new Visitor();
$firstVisitor->addPagevisit($firstPagevisit);

$secondPagevisit = new Pagevisit();
$secondVisitor = new Visitor();
$secondVisitor->addPagevisit($secondPagevisit);

self::assertSame($firstPagevisit, $firstVisitor->getLastPagevisit());
self::assertSame($secondPagevisit, $secondVisitor->getLastPagevisit());
}
}
Loading