From 73e2ec0214fd2f1cf9bbd46914a90d101bb990d5 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 16 Aug 2021 10:33:25 +0200 Subject: [PATCH] Add functional tests regarding actual frontend request tracking Ensures pageview and recordview are tracked as expected. Relates: #46 --- .../Provider/PageviewsPerPageTest.php | 2 +- .../recordview/Configuration/Services.yaml | 14 +++ .../Extensions/recordview/ext_emconf.php | 23 +++++ Tests/Functional/Fixtures/Pages.xml | 3 +- .../Functional/Fixtures/Rendering.typoscript | 5 + .../Fixtures/sites/example/config.yaml | 2 +- Tests/Functional/PageviewTest.php | 91 +++++++++++++++++++ Tests/Functional/RecordviewTest.php | 91 +++++++++++++++++++ composer.json | 9 +- phpstan.neon | 1 + 10 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 Tests/Functional/Fixtures/Extensions/recordview/Configuration/Services.yaml create mode 100644 Tests/Functional/Fixtures/Extensions/recordview/ext_emconf.php create mode 100644 Tests/Functional/Fixtures/Rendering.typoscript create mode 100644 Tests/Functional/PageviewTest.php create mode 100644 Tests/Functional/RecordviewTest.php diff --git a/Tests/Functional/Dashboard/Provider/PageviewsPerPageTest.php b/Tests/Functional/Dashboard/Provider/PageviewsPerPageTest.php index bdec086..74084de 100644 --- a/Tests/Functional/Dashboard/Provider/PageviewsPerPageTest.php +++ b/Tests/Functional/Dashboard/Provider/PageviewsPerPageTest.php @@ -244,7 +244,7 @@ class PageviewsPerPageTest extends TestCase $result = $subject->getChartData(); static::assertSame([ 'Page 2', - 'Seite 1', + 'Page 1', ], $result['labels']); static::assertCount(2, $result['datasets'][0]['data']); } diff --git a/Tests/Functional/Fixtures/Extensions/recordview/Configuration/Services.yaml b/Tests/Functional/Fixtures/Extensions/recordview/Configuration/Services.yaml new file mode 100644 index 0000000..18ca140 --- /dev/null +++ b/Tests/Functional/Fixtures/Extensions/recordview/Configuration/Services.yaml @@ -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' diff --git a/Tests/Functional/Fixtures/Extensions/recordview/ext_emconf.php b/Tests/Functional/Fixtures/Extensions/recordview/ext_emconf.php new file mode 100644 index 0000000..8d502f3 --- /dev/null +++ b/Tests/Functional/Fixtures/Extensions/recordview/ext_emconf.php @@ -0,0 +1,23 @@ + 'TESTING: Tracking recordview', + 'description' => 'Used by functional tests', + 'category' => 'fe', + 'state' => 'stable', + 'uploadfolder' => 0, + 'createDirs' => '', + 'clearCacheOnLoad' => 0, + 'author' => 'Daniel Siepmann', + 'author_email' => 'coding@daniel-siepmann.de', + 'author_company' => '', + 'version' => '1.1.2', + 'constraints' => [ + 'depends' => [ + 'core' => '', + 'tracking' => '', + ], + 'conflicts' => [], + 'suggests' => [], + ], +]; diff --git a/Tests/Functional/Fixtures/Pages.xml b/Tests/Functional/Fixtures/Pages.xml index de52539..20d3076 100644 --- a/Tests/Functional/Fixtures/Pages.xml +++ b/Tests/Functional/Fixtures/Pages.xml @@ -1,9 +1,10 @@ - 1 + 0 1 Page 1 + / 1 diff --git a/Tests/Functional/Fixtures/Rendering.typoscript b/Tests/Functional/Fixtures/Rendering.typoscript new file mode 100644 index 0000000..81f64d9 --- /dev/null +++ b/Tests/Functional/Fixtures/Rendering.typoscript @@ -0,0 +1,5 @@ +page = PAGE +page { + 10 = TEXT + 10.value = Output +} diff --git a/Tests/Functional/Fixtures/sites/example/config.yaml b/Tests/Functional/Fixtures/sites/example/config.yaml index 53e8851..7eecb62 100644 --- a/Tests/Functional/Fixtures/sites/example/config.yaml +++ b/Tests/Functional/Fixtures/sites/example/config.yaml @@ -1,4 +1,4 @@ -base: 'https://example.com' +base: '/' languages: - title: English diff --git a/Tests/Functional/PageviewTest.php b/Tests/Functional/PageviewTest.php new file mode 100644 index 0000000..6e9e34d --- /dev/null +++ b/Tests/Functional/PageviewTest.php @@ -0,0 +1,91 @@ + + * + * 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); + } +} diff --git a/Tests/Functional/RecordviewTest.php b/Tests/Functional/RecordviewTest.php new file mode 100644 index 0000000..ce72cf0 --- /dev/null +++ b/Tests/Functional/RecordviewTest.php @@ -0,0 +1,91 @@ + + * + * 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); + } +} diff --git a/composer.json b/composer.json index 4e70043..783bf0c 100644 --- a/composer.json +++ b/composer.json @@ -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, @@ -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" } diff --git a/phpstan.neon b/phpstan.neon index 63517d9..ffcbb26 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -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\.$#'