Skip to content

Commit

Permalink
Add functional tests regarding actual frontend request tracking
Browse files Browse the repository at this point in the history
Ensures pageview and recordview are tracked as expected.

Relates: #46
  • Loading branch information
DanielSiepmann committed Aug 16, 2021
1 parent 721b6e5 commit 73e2ec0
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function localizedRecordTitlesIfLimitedToSingleLanguage(): void
$result = $subject->getChartData();
static::assertSame([
'Page 2',
'Seite 1',
'Page 1',
], $result['labels']);
static::assertCount(2, $result['datasets'][0]['data']);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

DanielSiepmann\Tracking\Middleware\Recordview:
public: true
arguments:
$rules:
topics:
matches: 'request.getQueryParams()["topic_id"] > 0'
recordUid: 'request.getQueryParams()["topic_id"]'
tableName: 'sys_category'
23 changes: 23 additions & 0 deletions Tests/Functional/Fixtures/Extensions/recordview/ext_emconf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$EM_CONF[$_EXTKEY] = [
'title' => 'TESTING: Tracking recordview',
'description' => 'Used by functional tests',
'category' => 'fe',
'state' => 'stable',
'uploadfolder' => 0,
'createDirs' => '',
'clearCacheOnLoad' => 0,
'author' => 'Daniel Siepmann',
'author_email' => '[email protected]',
'author_company' => '',
'version' => '1.1.2',
'constraints' => [
'depends' => [
'core' => '',
'tracking' => '',
],
'conflicts' => [],
'suggests' => [],
],
];
3 changes: 2 additions & 1 deletion Tests/Functional/Fixtures/Pages.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<pages>
<pid>1</pid>
<pid>0</pid>
<uid>1</uid>
<title>Page 1</title>
<slug>/</slug>
</pages>
<pages>
<pid>1</pid>
Expand Down
5 changes: 5 additions & 0 deletions Tests/Functional/Fixtures/Rendering.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
page = PAGE
page {
10 = TEXT
10.value = Output
}
2 changes: 1 addition & 1 deletion Tests/Functional/Fixtures/sites/example/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
base: 'https://example.com'
base: '/'
languages:
-
title: English
Expand Down
91 changes: 91 additions & 0 deletions Tests/Functional/PageviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace DanielSiepmann\Tracking\Tests\Functional;

/*
* Copyright (C) 2021 Daniel Siepmann <[email protected]>
*
* 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.
*/

use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase as TestCase;

/**
* @testdox Pageviews are
*/
class PageviewTest extends TestCase
{
protected $testExtensionsToLoad = [
'typo3conf/ext/tracking',
];

protected $pathsToLinkInTestInstance = [
'typo3conf/ext/tracking/Tests/Functional/Fixtures/sites' => 'typo3conf/sites',
];

protected function setUp(): void
{
parent::setUp();

$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$this->setUpFrontendRootPage(1, [
'EXT:tracking/Tests/Functional/Fixtures/Rendering.typoscript',
]);
}

/**
* @test
*/
public function trackedWhenAllowed(): void
{
$request = new InternalRequest();
$request = $request->withPageId(1);
$request = $request->withHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0');
$response = $this->executeFrontendRequest($request);

self::assertSame(200, $response->getStatusCode());

$records = $this->getAllRecords('tx_tracking_pageview');
self::assertCount(1, $records);
self::assertSame('1', (string)$records[0]['pid']);
self::assertSame('1', (string)$records[0]['uid']);
self::assertSame('http://localhost/?id=1', $records[0]['url']);
self::assertSame('Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0', $records[0]['user_agent']);
self::assertSame('Macintosh', $records[0]['operating_system']);
self::assertSame('0', (string)$records[0]['type']);
}

/**
* @test
*/
public function notTrackedWhenDisallowed(): void
{
$this->setUpBackendUserFromFixture(1);

$request = new InternalRequest();
$request = $request->withPageId(1);
$context = new InternalRequestContext();
$context = $context->withBackendUserId(1);
$response = $this->executeFrontendRequest($request, $context);

self::assertSame(200, $response->getStatusCode());

$records = $this->getAllRecords('tx_tracking_pageview');
self::assertCount(0, $records);
}
}
91 changes: 91 additions & 0 deletions Tests/Functional/RecordviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace DanielSiepmann\Tracking\Tests\Functional;

/*
* Copyright (C) 2021 Daniel Siepmann <[email protected]>
*
* 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.
*/

use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase as TestCase;

/**
* @testdox Recordviews are
*/
class RecordviewTest extends TestCase
{
protected $testExtensionsToLoad = [
'typo3conf/ext/tracking',
'typo3conf/ext/tracking/Tests/Functional/Fixtures/Extensions/recordview',
];

protected $pathsToLinkInTestInstance = [
'typo3conf/ext/tracking/Tests/Functional/Fixtures/sites' => 'typo3conf/sites',
];

protected function setUp(): void
{
parent::setUp();

$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
$this->setUpFrontendRootPage(1, [
'EXT:tracking/Tests/Functional/Fixtures/Rendering.typoscript',
]);
}

/**
* @test
*/
public function trackedWhenAllowed(): void
{
$request = new InternalRequest();
$request = $request->withPageId(1);
$request = $request->withQueryParameter('topic_id', 1);
$request = $request->withHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0');
$response = $this->executeFrontendRequest($request);

self::assertSame(200, $response->getStatusCode());

$records = $this->getAllRecords('tx_tracking_recordview');
self::assertCount(1, $records);
self::assertSame('1', (string)$records[0]['pid']);
self::assertSame('1', (string)$records[0]['uid']);
self::assertSame('http://localhost/?id=1&topic_id=1', $records[0]['url']);
self::assertSame('Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0', $records[0]['user_agent']);
self::assertSame('Macintosh', $records[0]['operating_system']);
self::assertSame('sys_category_1', $records[0]['record']);
self::assertSame('1', (string)$records[0]['record_uid']);
self::assertSame('sys_category', $records[0]['record_table_name']);
}

/**
* @test
*/
public function notTrackedWhenNotDetected(): void
{
$request = new InternalRequest();
$request = $request->withPageId(1);
$response = $this->executeFrontendRequest($request);

self::assertSame(200, $response->getStatusCode());

$records = $this->getAllRecords('tx_tracking_recordview');
self::assertCount(0, $records);
}
}
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"phpspec/prophecy-phpunit": "^2.0",
"typo3/testing-framework": "^6.8.2",
"saschaegerer/phpstan-typo3": "^0.13.1",
"symplify/easy-coding-standard": "^9.3"
"symplify/easy-coding-standard": "^9.3",
"cweagans/composer-patches": "^1.7"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -62,6 +63,12 @@
"extension-key": "tracking",
"web-dir": ".Build/web"
},
"composer-exit-on-patch-failure": true,
"patches": {
"typo3/testing-framework": {
"[FEATURE] Support custom user agent in functional tests #257": "https://patch-diff.githubusercontent.com/raw/TYPO3/testing-framework/pull/257.patch"
}
},
"branch-alias": {
"dev-main": "1.0.x-dev"
}
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ parameters:
- '#Cannot call method fetchAll\(\) on Doctrine\\DBAL\\Driver\\ResultStatement\|int\.#'
- '#Cannot call method fetchColumn\(\) on Doctrine\\DBAL\\Driver\\Statement\|int\.#'
- '#Cannot call method fetchColumn\(\) on Doctrine\\DBAL\\Driver\\ResultStatement\|int\.#'
- '#^Variable \$_EXTKEY might not be defined\.$#'

0 comments on commit 73e2ec0

Please sign in to comment.