Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 Adobe
* Copyright 2026 Adobe
* All Rights Reserved.
*/
namespace Magento\Customer\Model\ResourceModel\Online\Grid;
Expand Down Expand Up @@ -93,12 +93,19 @@ protected function _initSelect(): Collection
$newerSessionExistsSubSelect
);
$this->addFilterToMap('customer_id', 'main_table.customer_id');
$expression = $connection->getCheckSql(
$visitorTypeExpression = $connection->getCheckSql(
'main_table.customer_id IS NOT NULL AND main_table.customer_id != 0',
$connection->quote(Visitor::VISITOR_TYPE_CUSTOMER),
$connection->quote(Visitor::VISITOR_TYPE_VISITOR)
);
$this->getSelect()->columns(['visitor_type' => $expression]);
$websiteIdExpression = new \Zend_Db_Expr(
'COALESCE(main_table.website_id, customer.website_id)'
);
$this->getSelect()->columns([
'visitor_type' => $visitorTypeExpression,
'website_id' => $websiteIdExpression
]);
$this->addFilterToMap('website_id', $websiteIdExpression);
return $this;
}

Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Customer/Model/ResourceModel/Visitor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2015 Adobe
* Copyright 2026 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
Expand Down Expand Up @@ -60,6 +60,7 @@ protected function _prepareDataForSave(\Magento\Framework\Model\AbstractModel $v
{
return [
'customer_id' => $visitor->getCustomerId(),
'website_id' => $visitor->getWebsiteId(),
'last_visit_at' => $visitor->getLastVisitAt()
];
}
Expand Down
13 changes: 12 additions & 1 deletion app/code/Magento/Customer/Model/Visitor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2014 Adobe
* Copyright 2026 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
Expand All @@ -24,6 +24,7 @@
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\DateTime;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class Visitor responsible for initializing visitor's.
Expand Down Expand Up @@ -201,6 +202,16 @@ public function initByRequest($observer)
public function beforeSave()
{
$this->unsetData("session_id");

if (!$this->getWebsiteId()) {
try {
$storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
$this->setWebsiteId($storeManager->getWebsite()->getId());
} catch (\Exception $e) {
$this->_logger->critical($e);
}
}

return parent::beforeSave();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright 2026 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Customer\Test\Unit\Model\ResourceModel;

use Magento\Customer\Model\ResourceModel\Visitor;
use Magento\Customer\Model\Visitor as VisitorModel;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\DateTime as DateTimeDate;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

/**
* Test for Visitor ResourceModel
*/
class VisitorTest extends TestCase
{
/**
* @var Visitor
*/
private $model;

/**
* @var DateTimeDate|\PHPUnit\Framework\MockObject\MockObject
*/
private $dateMock;

/**
* @var DateTime|\PHPUnit\Framework\MockObject\MockObject
*/
private $dateTimeMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->dateMock = $this->createMock(DateTimeDate::class);
$this->dateTimeMock = $this->createMock(DateTime::class);

$contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
$connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
$resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);

$contextMock->method('getResources')->willReturn($resourceMock);
$resourceMock->method('getConnection')->willReturn($connectionMock);

$this->model = $objectManager->getObject(
Visitor::class,
[
'context' => $contextMock,
'date' => $this->dateMock,
'dateTime' => $this->dateTimeMock
]
);
}

/**
* Test that _prepareDataForSave includes website_id
*
* @return void
*/
public function testPrepareDataForSaveIncludesWebsiteId(): void
{
$objectManager = new ObjectManager($this);
$visitorMock = $objectManager->getObject(VisitorModel::class);
$visitorMock->setCustomerId(123);
$visitorMock->setWebsiteId(1);
$visitorMock->setLastVisitAt('2024-01-01 00:00:00');

$reflection = new ReflectionClass($this->model);
$method = $reflection->getMethod('_prepareDataForSave');
$method->setAccessible(true);

$result = $method->invoke($this->model, $visitorMock);

$this->assertArrayHasKey('customer_id', $result);
$this->assertArrayHasKey('website_id', $result);
$this->assertArrayHasKey('last_visit_at', $result);
$this->assertEquals(123, $result['customer_id']);
$this->assertEquals(1, $result['website_id']);
$this->assertEquals('2024-01-01 00:00:00', $result['last_visit_at']);
}

/**
* Test that _prepareDataForSave handles null website_id
*
* @return void
*/
public function testPrepareDataForSaveWithNullWebsiteId(): void
{
$objectManager = new ObjectManager($this);
$visitorMock = $objectManager->getObject(VisitorModel::class);
$visitorMock->setCustomerId(null);
$visitorMock->setWebsiteId(null);
$visitorMock->setLastVisitAt('2024-01-01 00:00:00');

$reflection = new ReflectionClass($this->model);
$method = $reflection->getMethod('_prepareDataForSave');
$method->setAccessible(true);

$result = $method->invoke($this->model, $visitorMock);

$this->assertArrayHasKey('website_id', $result);
$this->assertNull($result['website_id']);
}
}
24 changes: 24 additions & 0 deletions app/code/Magento/Customer/Test/Unit/Model/VisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ public function testBindQuoteDestroy()
$this->assertTrue($this->visitor->getDoQuoteDestroy());
}

public function testBeforeSaveUnsetsSessionId()
{
$this->visitor->setData('session_id', 'test_session_id');
$this->visitor->beforeSave();
$this->assertNull($this->visitor->getData('session_id'));
}

public function testBeforeSaveSetsWebsiteIdWhenNotSet()
{
$this->visitor->unsetData('website_id');
$this->visitor->beforeSave();
// Website ID should be set by StoreManager (or remain null if not available)
// We can't easily mock ObjectManager here, so we just verify the method doesn't throw
$this->assertTrue(true);
}

public function testBeforeSaveDoesNotOverrideExistingWebsiteId()
{
$websiteId = 5;
$this->visitor->setWebsiteId($websiteId);
$this->visitor->beforeSave();
$this->assertEquals($websiteId, $this->visitor->getWebsiteId());
}

public function testClean()
{
$this->visitorResourceModelMock->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright 2026 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Customer\Test\Unit\Ui\Component\Listing\Column\Online\Website;

use Magento\Customer\Ui\Component\Listing\Column\Online\Website\Options;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\System\Store;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Test for Website Options
*/
class OptionsTest extends TestCase
{
/**
* @var Options
*/
private $model;

/**
* @var Store|MockObject
*/
private $systemStoreMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->systemStoreMock = $this->createMock(Store::class);
$this->model = $objectManager->getObject(
Options::class,
['systemStore' => $this->systemStoreMock]
);
}

/**
* Test toOptionArray returns website options
*/
public function testToOptionArray(): void
{
$expectedOptions = [
[
'value' => '1',
'label' => 'Main Website'
],
[
'value' => '2',
'label' => 'Second Website'
]
];

$this->systemStoreMock->expects($this->once())
->method('getWebsiteValuesForForm')
->willReturn($expectedOptions);

$this->assertEquals($expectedOptions, $this->model->toOptionArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright 2026 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Customer\Ui\Component\Listing\Column\Online\Website;

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Store\Model\System\Store;

/**
* Website options for customers online grid
*/
class Options implements OptionSourceInterface
{
/**
* @var Store
*/
private Store $systemStore;

/**
* @param Store $systemStore
*/
public function __construct(Store $systemStore)
{
$this->systemStore = $systemStore;
}

/**
* Get website options
*
* @return array
*/
public function toOptionArray(): array
{
return $this->systemStore->getWebsiteValuesForForm();
}
}
7 changes: 6 additions & 1 deletion app/code/Magento/Customer/etc/db_schema.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!--
/**
* Copyright 2024 Adobe
* Copyright 2026 Adobe
* All rights reserved.
*/
-->
Expand Down Expand Up @@ -515,6 +515,8 @@
comment="Visitor ID"/>
<column xsi:type="int" name="customer_id" unsigned="false" nullable="true" identity="false"
comment="Customer ID"/>
<column xsi:type="smallint" name="website_id" unsigned="true" nullable="true" identity="false"
comment="Website ID"/>
<column xsi:type="varchar" name="session_id" nullable="true" length="1"
comment="Deprecated: Session ID value no longer used"/>
<column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP"
Expand All @@ -527,6 +529,9 @@
<index referenceId="CUSTOMER_VISITOR_CUSTOMER_ID" indexType="btree">
<column name="customer_id"/>
</index>
<index referenceId="CUSTOMER_VISITOR_WEBSITE_ID" indexType="btree">
<column name="website_id"/>
</index>
<index referenceId="CUSTOMER_VISITOR_LAST_VISIT_AT" indexType="btree">
<column name="last_visit_at"/>
</index>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright 2015 Adobe
* Copyright 2026 Adobe
* All Rights Reserved.
*/
-->
Expand Down Expand Up @@ -66,6 +66,14 @@
<label translate="true">Email</label>
</settings>
</column>
<column name="website_id" component="Magento_Ui/js/grid/columns/select">
<settings>
<options class="Magento\Customer\Ui\Component\Listing\Column\Online\Website\Options"/>
<filter>select</filter>
<dataType>select</dataType>
<label translate="true">Website</label>
</settings>
</column>
<column name="last_visit_at" class="Magento\Ui\Component\Listing\Columns\Date" component="Magento_Ui/js/grid/columns/date">
<settings>
<filter>dateRange</filter>
Expand Down