1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-22 15:07:44 +00:00
Files
matomo/tests/PHPUnit/Fixtures/TwoVisitsWithCustomVariables.php
Stefan Giehl 95270a8d24 Update Omnifixture and improve various test fixtures (#23465)
* set correct cookie domain for tracker

* Log a warning if conversion fails to insert

* Make Fixtures more realistic

* update OmniFixture

* updates expected UI test files

* updates expected test files

* update ui tests

* another omnifixture update

* add note

* submodule update

* check sanitized value
2025-08-05 18:33:58 +02:00

172 خطوط
8.3 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Fixtures;
use Piwik\Date;
use Piwik\Plugins\Goals\API;
use Piwik\Tests\Framework\Fixture;
/**
* Adds one site with two goals and tracks two visits with custom variables.
*/
class TwoVisitsWithCustomVariables extends Fixture
{
public $dateTime = '2010-01-03 11:22:33';
public $idSite = 1;
public $idGoal1 = 1;
public $idGoal2 = 2;
public $visitorId = '61e8cc2d51fea26d';
public $useEscapedQuotes = true;
public $doExtraQuoteTests = true;
public $resolutionWidthToUse = 1111;
public $resolutionHeightToUse = 222;
public function setUp(): void
{
$this->setUpWebsitesAndGoals();
$this->trackVisits();
}
public function tearDown(): void
{
// empty
}
private function setUpWebsitesAndGoals()
{
// tests run in UTC, the Tracker in UTC
if (!self::siteCreated($idSite = 1)) {
self::createWebsite($this->dateTime);
}
if (!self::goalExists($idSite = 1, $idGoal = 1)) {
API::getInstance()->addGoal($this->idSite, 'triggered js', 'manually', '', '');
}
if (!self::goalExists($idSite = 1, $idGoal = 2)) {
API::getInstance()->addGoal($this->idSite, 'second goal', 'manually', '', '');
}
}
private function trackVisits()
{
$dateTime = $this->dateTime;
$idSite = $this->idSite;
$idGoal = $this->idGoal1;
$idGoal2 = $this->idGoal2;
$visitorA = self::getTracker($this->idSite, $this->dateTime, $defaultInit = true);
// Used to test actual referrer + keyword position in Live!
$visitorA->setUrlReferrer(urldecode('http://www.google.com/url?sa=t&source=web&cd=1&ved=0CB4QFjAA&url=http%3A%2F%2Fpiwik.org%2F&rct=j&q=this%20keyword%20should%20be%20ranked&ei=V8WfTePkKKLfiALrpZWGAw&usg=AFQjCNF_MGJRqKPvaKuUokHtZ3VvNG9ALw&sig2=BvKAdCtNixsmfNWXjsNyMw'));
// no campaign, but a search engine to attribute the goal conversion to
$attribution = array(
'',
'',
1302306504,
'http://www.google.com/search?q=piwik&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a'
);
$visitorA->setAttributionInfo(json_encode($attribution));
$visitorA->setResolution($this->resolutionWidthToUse, $this->resolutionHeightToUse);
// At first, visitor custom var is set to LoggedOut
$visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)->getDatetime());
$visitorA->setUrl('http://example.org/homepage');
$visitorA->setCustomVariable($id = 2, $name = 'VisitorType', $value = 'LoggedOut');
self::checkResponse($visitorA->doTrackPageView('Homepage'));
$visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)->addPeriod(1, 'second')->getDatetime());
self::checkResponse($visitorA->doTrackGoal($idGoal2));
// After login, set to LoggedIn, should overwrite previous value
$visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->getDatetime());
$visitorA->setUrl('http://example.org/user/profile');
$visitorA->setCustomVariable($id = 1, $name = 'VisitorType', $value = 'LoggedIn');
$visitorA->setCustomVariable($id = 4, $name = 'Status user', $value = 'Loggedin', $scope = 'page');
if ($this->useEscapedQuotes) {
$lookingAtProfile = 'looking at &quot;profile page&quot;';
} else {
$lookingAtProfile = 'looking at profile page';
}
$visitorA->setCustomVariable($id = 5, $name = 'Status user', $value = $lookingAtProfile, $scope = 'page');
self::checkResponse($visitorA->doTrackPageView('Profile page'));
$visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->addPeriod(1, 'second')->getDatetime());
$visitorA->setCustomVariable($id = 2, $name = 'SET WITH EMPTY VALUE', $value = '');
$visitorA->setCustomVariable($id = 1, $name = 'Language', $value = 'FR', $scope = 'page');
$visitorA->setCustomVariable($id = 2, $name = 'SET WITH EMPTY VALUE PAGE SCOPE', $value = '', $scope = 'page');
$visitorA->setCustomVariable($id = 4, $name = 'Status user', $value = "looking at \"profile page\"", $scope = 'page');
$visitorA->setCustomVariable($id = 3, $name = 'Value will be VERY long and truncated', $value = 'abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----');
self::checkResponse($visitorA->doTrackPageView('Profile page for user *_)%'));
$visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->addPeriod(2, 'second')->getDatetime());
self::checkResponse($visitorA->doTrackGoal($idGoal));
if ($this->doExtraQuoteTests) {
$visitorA->setCustomVariable(
$id = 2,
$name = 'var1',
$value = 'looking at "profile page"',
$scope = 'page'
);
$visitorA->setCustomVariable(
$id = 3,
$name = 'var2',
$value = '\'looking at "\profile page"\'',
$scope = 'page'
);
$visitorA->setCustomVariable(
$id = 4,
$name = 'var3',
$value = '\\looking at "\profile page"\\',
$scope = 'page'
);
$visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->addPeriod(3, 'second')->getDatetime());
self::checkResponse($visitorA->doTrackPageView('Concurrent page views'));
}
// -
// Second new visitor on Idsite 1: one page view
$visitorB = self::getTracker($idSite, $dateTime, $defaultInit = true);
if (!empty($this->visitorId)) {
$visitorB->setVisitorId($this->visitorId);
}
$visitorB->setUrlReferrer('');
// Test campaigns that are specified using the _rcn tracker parameter, only conversions will be attributed to the campaign
$attribution = array(
' CAMPAIGN NAME -%20YEAH! ',
' CAMPAIGN%20KEYWORD - RIGHT... ',
1302306504,
'http://www.example.org/test/really?q=yes'
);
$visitorB->setAttributionInfo(json_encode($attribution));
$visitorB->setResolution($this->resolutionWidthToUse, $this->resolutionHeightToUse);
$visitorB->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6');
$visitorB->setForceVisitDateTime(Date::factory($dateTime)->addHour(1)->getDatetime());
$visitorB->setCustomVariable($id = 1, $name = 'VisitorType', $value = 'LoggedOut');
$visitorB->setCustomVariable($id = 2, $name = 'Othercustom value which should be truncated abcdefghijklmnopqrstuvwxyz', $value = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz');
$visitorB->setCustomVariable($id = -2, $name = 'not tracked', $value = 'not tracked');
$visitorB->setCustomVariable($id = 6, $name = 'not tracked', $value = 'not tracked');
$visitorB->setCustomVariable($id = 6, $name = array('not tracked'), $value = 'not tracked');
$visitorB->setUrl('http://example.org/homepage');
self::checkResponse($visitorB->doTrackGoal($idGoal, 1000));
$visitorB->setForceVisitDateTime(Date::factory($dateTime)->addHour(1.1)->getDatetime());
self::checkResponse($visitorB->doTrackPageView('Homepage'));
// DIFFERENT test -
// testing that starting the visit with an outlink works (doesn't trigger errors)
$visitorB->setForceVisitDateTime(Date::factory($dateTime)->addHour(2)->getDatetime());
self::checkResponse($visitorB->doTrackAction('http://test.com', 'link'));
}
}