From 74a6bb09477a175f06bffbef14cf533ba8744b69 Mon Sep 17 00:00:00 2001 From: Ben Burgess <88810029+bx80@users.noreply.github.com> Date: Wed, 1 Dec 2021 15:27:48 +1300 Subject: Configuration option to disable transition periods (#18366) * Added config option to disable transition periods * Config section checks * Hide transitions row action if period is not allowed * Added system test * Add default values for requests vars * Do allowed period checks for transition row actions in javascript * Code tidy up, fix for range get day count bug, improved tests * Added UI test for disabled period --- .../TransitionsMaxAllowedPeriodTest.php | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 plugins/Transitions/tests/Integration/TransitionsMaxAllowedPeriodTest.php (limited to 'plugins/Transitions/tests/Integration') diff --git a/plugins/Transitions/tests/Integration/TransitionsMaxAllowedPeriodTest.php b/plugins/Transitions/tests/Integration/TransitionsMaxAllowedPeriodTest.php new file mode 100644 index 0000000000..b074e7d3ab --- /dev/null +++ b/plugins/Transitions/tests/Integration/TransitionsMaxAllowedPeriodTest.php @@ -0,0 +1,133 @@ +createSuperUser = true; + } + + public function setUp(): void + { + parent::setUp(); + Fixture::createWebsite('2010-02-03 00:00:00'); + $this->api = API::getInstance(); + + $t = Fixture::getTracker(1, '2012-08-09 01:02:03', $defaultInit = true, $useLocalTracker = false); + + $t->setUrl('http://example.org/page/one.html'); + $t->doTrackPageView('incredible title '); + } + + public function test_ShouldThrowException_IfPeriodNotAllowed() + { + $invalidPeriods = [ + 'day' => ['week', 'month', 'year'], + 'week' => ['month', 'year'], + 'month' => ['year'], + ]; + foreach ($invalidPeriods as $period => $invalids) { + Config::setSetting('Transitions_1', 'max_period_allowed', $period); + foreach ($invalids as $ip) { + try { + $this->api->getTransitionsForAction('http://example.org/page/one.html', 'url', + 1, $ip, '2012-08-09'); + $this->fail("Failed asserting that exception 'PeriodNotAllowed' was thrown"); + } catch (\Exception $e) { + $this->assertEquals('PeriodNotAllowed', $e->getMessage()); + } + } + } + } + + public function test_ShouldReturnData_IfPeriodAllowed() + { + $validPeriods = [ + 'day' => ['day'], + 'week' => ['day', 'week'], + 'month' => ['day', 'week', 'month'], + 'year' => ['day', 'week', 'month', 'year'], + 'all' => ['day', 'week', 'month', 'year'], + ]; + foreach ($validPeriods as $period => $valids) { + Config::setSetting('Transitions_1', 'max_period_allowed', $period); + foreach ($valids as $vp) { + $r = $this->api->getTransitionsForAction('http://example.org/page/one.html', 'url', + 1, $vp, '2012-08-09'); + self::assertEquals(1, $r['pageMetrics']['pageviews']); + } + } + } + + public function test_ShouldThrowException_IfRangeDayCountIsLargerThanDayPeriod() + { + Config::setSetting('Transitions_1', 'max_period_allowed', 'day'); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('PeriodNotAllowed'); + $this->api->getTransitionsForAction('http://example.org/page/one.html', 'url', 1, + 'range','2012-08-09,2012-08-10'); + } + + public function test_ShouldThrowException_IfRangeDayCountIsLargerThanWeekPeriod() + { + Config::setSetting('Transitions_1', 'max_period_allowed', 'day'); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('PeriodNotAllowed'); + $this->api->getTransitionsForAction('http://example.org/page/one.html', 'url', 1, + 'range','2012-08-09,2012-08-17'); + } + + public function test_ShouldThrowException_IfRangeDayCountIsLargerThanMonthPeriod() + { + Config::setSetting('Transitions_1', 'max_period_allowed', 'day'); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('PeriodNotAllowed'); + $this->api->getTransitionsForAction('http://example.org/page/one.html', 'url', 1, + 'range','2012-08-09,2012-09-10'); + } + + public function test_ShouldThrowException_IfRangeDayCountIsLargerThanYearPeriod() + { + Config::setSetting('Transitions_1', 'max_period_allowed', 'day'); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('PeriodNotAllowed'); + $this->api->getTransitionsForAction('http://example.org/page/one.html', 'url', 1, + 'range','2012-08-09,2013-08-10'); + } + + public function test_ShouldUseSiteConfigInsteadOfGeneral_IfSiteConfigExists() + { + Config::setSetting('Transitions_1', 'max_period_allowed', null); + Config::setSetting('Transitions', 'max_period_allowed', 'month'); + $maxAllowedPeriod = Transitions::getPeriodAllowedConfig(1); + $this->assertEquals('month', $maxAllowedPeriod); + + Config::setSetting('Transitions_1', 'max_period_allowed', 'week'); + $maxAllowedPeriod = Transitions::getPeriodAllowedConfig(1); + $this->assertEquals('week', $maxAllowedPeriod); + } + +} -- cgit v1.2.3