tracking/Tests/Functional/Dashboard/Provider/PageviewsPerPageTest.php
Daniel Siepmann 5f0490f493
WIP|Add flags feature
Allow each tracking record to contain arbitrary tags.
Those tags are generated via an API and can be extended by foreign
extensions or for individual projects.
Existing operating_system was moved to this new feature.
The update command allows to migrate existing records to this new
feature.

Those flags can be used when configuring widgets.

A new flag was added bot:yes and bot:no.
Bots are now tracked but flagged.

That new feature allows to build fine grained reports and makes the
extension way more flexible. Possible new implications:
    - Show visits from none bots
    - Show visits from bots
    - Show visits from specific bot
    - Add color to page views per page (bar chart)
      to color bot or none bot

WIP:
    - Update Yaml file to work like before, no bots in widgets
    - Add documentation (widgets)
    - Add documentation (migration *.yaml)
2022-09-21 17:33:27 +02:00

423 lines
14 KiB
PHP

<?php
declare(strict_types=1);
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace DanielSiepmann\Tracking\Tests\Functional\Dashboard\Provider;
use DanielSiepmann\Tracking\Dashboard\Provider\Demand;
use DanielSiepmann\Tracking\Dashboard\Provider\Demand\Tag;
use DanielSiepmann\Tracking\Dashboard\Provider\PageviewsPerPage;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase as TestCase;
/**
* @covers \DanielSiepmann\Tracking\Dashboard\Provider\PageviewsPerPage
*/
class PageviewsPerPageTest extends TestCase
{
protected $testExtensionsToLoad = [
'typo3conf/ext/tracking',
];
/**
* @test
*/
public function listsSixResultsForLast31DaysByDefault(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
for ($i = 1; $i <= 10; $i++) {
$connection->insert('tx_tracking_pageview', [
'pid' => $i,
'uid' => $i,
'crdate' => time(),
]);
}
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand()
);
$result = $subject->getChartData();
self::assertSame([
'Page 10',
'Page 9',
'Page 8',
'Page 7',
'Page 6',
'Page 5',
], $result['labels']);
self::assertCount(6, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function respectedOrdering(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
$connection->insert('tx_tracking_pageview', [
'pid' => 1,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 2,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 3,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 2,
'crdate' => time(),
]);
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand()
);
$result = $subject->getChartData();
self::assertSame([
'Page 2',
'Page 3',
'Page 1',
], $result['labels']);
self::assertCount(3, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function respectedNumberOfDays(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
$connection->insert('tx_tracking_pageview', [
'pid' => 1,
'crdate' => strtotime('-3 days'),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 2,
'crdate' => strtotime('-2 days'),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 3,
'crdate' => strtotime('-1 days'),
]);
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand(2)
);
$result = $subject->getChartData();
self::assertSame([
'Page 3',
'Page 2',
], $result['labels']);
self::assertCount(2, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function respectedMaxResults(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
for ($i = 1; $i <= 10; $i++) {
$connection->insert('tx_tracking_pageview', [
'pid' => $i,
'crdate' => time(),
]);
}
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand(31, 4)
);
$result = $subject->getChartData();
self::assertSame([
'Page 10',
'Page 9',
'Page 8',
'Page 7',
], $result['labels']);
self::assertCount(4, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function respectedExcludedPages(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
for ($i = 1; $i <= 10; $i++) {
$connection->insert('tx_tracking_pageview', [
'pid' => $i,
'crdate' => time(),
]);
}
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand(31, 6, [1, 2, 3, 4, 5, 6])
);
$result = $subject->getChartData();
self::assertSame([
'Page 10',
'Page 9',
'Page 8',
'Page 7',
], $result['labels']);
self::assertCount(4, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function localizedRecordTitlesIfLimitedToSingleLanguage(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
$connection->insert('tx_tracking_pageview', [
'pid' => 1,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 1,
'sys_language_uid' => 1,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 2,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 2,
'sys_language_uid' => 1,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 3,
'crdate' => time(),
]);
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand(31, 6, [], [1])
);
$result = $subject->getChartData();
self::assertSame([
'Page 2',
'Page 1',
], $result['labels']);
self::assertCount(2, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function defaultLanguageTitleIsUsedIfMultipleLanguagesAreAllowed(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
$connection->insert('tx_tracking_pageview', [
'pid' => 1,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 1,
'sys_language_uid' => 1,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 2,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 2,
'sys_language_uid' => 1,
'crdate' => time(),
]);
$connection->insert('tx_tracking_pageview', [
'pid' => 3,
'crdate' => time(),
]);
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand(31, 6, [], [1, 0])
);
$result = $subject->getChartData();
self::assertSame([
'Page 2',
'Page 1',
'Page 3',
], $result['labels']);
self::assertCount(3, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function respectedConfiguredTagRuleToNotIncludeBots(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
for ($i = 1; $i <= 10; $i++) {
$connection->insert('tx_tracking_pageview', [
'pid' => $i,
'uid' => $i,
'crdate' => time(),
]);
$connection->insert('tx_tracking_tag', [
'pid' => $i,
'uid' => $i,
'record_uid' => $i,
'record_table_name' => 'tx_tracking_pageview',
'name' => 'bot',
'value' => 'no',
'crdate' => time(),
]);
}
for ($i = 11; $i <= 20; $i++) {
$connection->insert('tx_tracking_pageview', [
'pid' => $i,
'uid' => $i,
'crdate' => time(),
]);
$connection->insert('tx_tracking_tag', [
'pid' => $i,
'uid' => $i,
'record_uid' => $i,
'record_table_name' => 'tx_tracking_pageview',
'name' => 'bot',
'value' => 'yes',
'crdate' => time(),
]);
}
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand(31, 6, [], [], [
Tag::createFromArray([
'name' => 'bot',
'value' => 'no',
]),
])
);
$result = $subject->getChartData();
self::assertSame([
'Page 10',
'Page 9',
'Page 8',
'Page 7',
'Page 6',
'Page 5',
], $result['labels']);
self::assertCount(6, $result['datasets'][0]['data']);
}
/**
* @test
*/
public function respectedConfiguredTagRuleToIncludeBots(): void
{
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
for ($i = 1; $i <= 10; $i++) {
$connection->insert('tx_tracking_pageview', [
'pid' => $i,
'uid' => $i,
'crdate' => time(),
]);
$connection->insert('tx_tracking_tag', [
'pid' => $i,
'uid' => $i,
'record_uid' => $i,
'record_table_name' => 'tx_tracking_pageview',
'name' => 'bot',
'value' => 'no',
'crdate' => time(),
]);
}
for ($i = 11; $i <= 20; $i++) {
$connection->insert('tx_tracking_pageview', [
'pid' => $i,
'uid' => $i,
'crdate' => time(),
]);
$connection->insert('tx_tracking_tag', [
'pid' => $i,
'uid' => $i,
'record_uid' => $i,
'record_table_name' => 'tx_tracking_pageview',
'name' => 'bot',
'value' => 'yes',
'crdate' => time(),
]);
}
$subject = new PageviewsPerPage(
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
GeneralUtility::makeInstance(PageRepository::class),
new Demand(31, 6, [], [], [
Tag::createFromArray([
'name' => 'bot',
'value' => 'yes',
]),
])
);
$result = $subject->getChartData();
self::assertSame([
'Page 20',
'Page 19',
'Page 18',
'Page 17',
'Page 16',
'Page 15',
], $result['labels']);
self::assertCount(6, $result['datasets'][0]['data']);
}
}