diff options
author | Ben Burgess <88810029+bx80@users.noreply.github.com> | 2021-12-01 05:27:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-01 05:27:48 +0300 |
commit | 74a6bb09477a175f06bffbef14cf533ba8744b69 (patch) | |
tree | d582cea51ba22234ab35a3f86d9d98bf2e061378 | |
parent | 9efa63a654145fcb315d16c5e6d21ab3b96c6f01 (diff) |
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
-rw-r--r-- | core/Period/Range.php | 10 | ||||
-rw-r--r-- | plugins/CoreHome/vue/dist/CoreHome.umd.js | 5 | ||||
-rw-r--r-- | plugins/CoreHome/vue/dist/CoreHome.umd.min.js | 2 | ||||
-rw-r--r-- | plugins/CoreHome/vue/src/Periods/Range.ts | 5 | ||||
-rw-r--r-- | plugins/Transitions/API.php | 53 | ||||
-rw-r--r-- | plugins/Transitions/Controller.php | 3 | ||||
-rw-r--r-- | plugins/Transitions/Transitions.php | 42 | ||||
-rw-r--r-- | plugins/Transitions/javascripts/transitions.js | 43 | ||||
-rw-r--r-- | plugins/Transitions/lang/en.json | 2 | ||||
-rw-r--r-- | plugins/Transitions/tests/Integration/TransitionsMaxAllowedPeriodTest.php | 133 | ||||
-rw-r--r-- | plugins/Transitions/tests/UI/Transitions_spec.js | 16 |
11 files changed, 309 insertions, 5 deletions
diff --git a/core/Period/Range.php b/core/Period/Range.php index 1a4e381dc6..e6b3ae1357 100644 --- a/core/Period/Range.php +++ b/core/Period/Range.php @@ -498,6 +498,16 @@ class Range extends Period return array($strLastDate, $lastPeriod); } + /** + * Return the number of days contained in this range + * + * @return int + * @throws Exception + */ + public function getDayCount() + { + return (self::getNumDaysDifference($this->getDateStart(), $this->getDateEnd()) + 1); + } private static function getNumDaysDifference(Date $date1, Date $date2) { diff --git a/plugins/CoreHome/vue/dist/CoreHome.umd.js b/plugins/CoreHome/vue/dist/CoreHome.umd.js index 22a055e0e4..190b5027e1 100644 --- a/plugins/CoreHome/vue/dist/CoreHome.umd.js +++ b/plugins/CoreHome/vue/dist/CoreHome.umd.js @@ -546,6 +546,11 @@ var Range_RangePeriod = /*#__PURE__*/function () { value: function containsToday() { return todayIsInRange(this.getDateRange()); } + }, { + key: "getDayCount", + value: function getDayCount() { + return Math.ceil((this.endDate.getTime() - this.startDate.getTime()) / (1000 * 3600 * 24)) + 1; + } }], [{ key: "getLastNRange", value: function getLastNRange(childPeriodType, strAmount, strEndDate) { diff --git a/plugins/CoreHome/vue/dist/CoreHome.umd.min.js b/plugins/CoreHome/vue/dist/CoreHome.umd.min.js index 7c71a545a3..4850e32df6 100644 --- a/plugins/CoreHome/vue/dist/CoreHome.umd.min.js +++ b/plugins/CoreHome/vue/dist/CoreHome.umd.min.js @@ -35,7 +35,7 @@ function D(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r- * * @link https://matomo.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */var _=function(){function e(t,n,r){V(this,e),F(this,"startDate",void 0),F(this,"endDate",void 0),F(this,"childPeriodType",void 0),this.startDate=t,this.endDate=n,this.childPeriodType=r}return H(e,[{key:"getPrettyString",value:function(){var e=C(this.startDate),t=C(this.endDate);return D("General_DateRangeFromTo",[e,t])}},{key:"getDateRange",value:function(){return[this.startDate,this.endDate]}},{key:"containsToday",value:function(){return T(this.getDateRange())}}],[{key:"getLastNRange",value:function(t,n,r){var i=Math.max(parseInt(n.toString(),10)-1,0);if(Number.isNaN(i))throw new Error("Invalid range strAmount");var a=r?P(r):E(),o=new Date(a.getTime());if("day"===t)o.setDate(o.getDate()-i);else if("week"===t)o.setDate(o.getDate()-7*i);else if("month"===t)o.setDate(1),o.setMonth(o.getMonth()-i);else{if("year"!==t)throw new Error("Unknown period type '".concat(t,"'."));o.setFullYear(o.getFullYear()-i)}if("day"!==t){var c=f.periods[t].parse(o),s=f.periods[t].parse(a),l=c.getDateRange(),u=I(l,1);o=u[0];var d=s.getDateRange(),p=I(d,2);a=p[1]}var m=new Date(1991,7,6);if(o.getTime()-m.getTime()<0)switch(t){case"year":o=new Date(1992,0,1);break;case"month":o=new Date(1991,8,1);break;case"week":o=new Date(1991,8,12);break;case"day":default:o=m;break}return new e(o,a,t)}},{key:"getLastNRangeChild",value:function(t,n,r){var i=n?P(n):E(),a=new Date(i.getTime()),o=new Date(i.getTime());if("day"===t)a.setDate(a.getDate()-r),o.setDate(o.getDate()-r);else if("week"===t)a.setDate(a.getDate()-7*r),o.setDate(o.getDate()-7*r);else if("month"===t)a.setDate(1),a.setMonth(a.getMonth()-r),o.setDate(1),o.setMonth(o.getMonth()-r);else{if("year"!==t)throw new Error("Unknown period type '".concat(t,"'."));a.setFullYear(a.getFullYear()-r),o.setFullYear(o.getFullYear()-r)}if("day"!==t){var c=f.periods[t].parse(a),s=f.periods[t].parse(o),l=c.getDateRange(),u=I(l,1);a=u[0];var d=s.getDateRange(),p=I(d,2);o=p[1]}var m=new Date(1991,7,6);if(a.getTime()-m.getTime()<0)switch(t){case"year":a=new Date(1992,0,1);break;case"month":a=new Date(1991,8,1);break;case"week":a=new Date(1991,8,12);break;case"day":default:a=m;break}return new e(a,o,t)}},{key:"parse",value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"day";if(/^previous/.test(t)){var r=e.getLastNRange(n,"2").startDate;return e.getLastNRange(n,t.substring(8),r)}if(/^last/.test(t))return e.getLastNRange(n,t.substring(4));var i=decodeURIComponent(t).split(",");return new e(P(i[0]),P(i[1]),n)}},{key:"getDisplayText",value:function(){return D("General_DateRangeInPeriodList")}}]),e}();function R(){return{getAllLabels:f.getAllLabels.bind(f),isRecognizedPeriod:f.isRecognizedPeriod.bind(f),get:f.get.bind(f),parse:f.parse.bind(f),parseDate:P,format:C,RangePeriod:_,todayIsInRange:T}}function U(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function q(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function J(e,t,n){return t&&q(e.prototype,t),n&&q(e,n),e}function G(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e} + */var _=function(){function e(t,n,r){V(this,e),F(this,"startDate",void 0),F(this,"endDate",void 0),F(this,"childPeriodType",void 0),this.startDate=t,this.endDate=n,this.childPeriodType=r}return H(e,[{key:"getPrettyString",value:function(){var e=C(this.startDate),t=C(this.endDate);return D("General_DateRangeFromTo",[e,t])}},{key:"getDateRange",value:function(){return[this.startDate,this.endDate]}},{key:"containsToday",value:function(){return T(this.getDateRange())}},{key:"getDayCount",value:function(){return Math.ceil((this.endDate.getTime()-this.startDate.getTime())/864e5)+1}}],[{key:"getLastNRange",value:function(t,n,r){var i=Math.max(parseInt(n.toString(),10)-1,0);if(Number.isNaN(i))throw new Error("Invalid range strAmount");var a=r?P(r):E(),o=new Date(a.getTime());if("day"===t)o.setDate(o.getDate()-i);else if("week"===t)o.setDate(o.getDate()-7*i);else if("month"===t)o.setDate(1),o.setMonth(o.getMonth()-i);else{if("year"!==t)throw new Error("Unknown period type '".concat(t,"'."));o.setFullYear(o.getFullYear()-i)}if("day"!==t){var c=f.periods[t].parse(o),s=f.periods[t].parse(a),l=c.getDateRange(),u=I(l,1);o=u[0];var d=s.getDateRange(),p=I(d,2);a=p[1]}var m=new Date(1991,7,6);if(o.getTime()-m.getTime()<0)switch(t){case"year":o=new Date(1992,0,1);break;case"month":o=new Date(1991,8,1);break;case"week":o=new Date(1991,8,12);break;case"day":default:o=m;break}return new e(o,a,t)}},{key:"getLastNRangeChild",value:function(t,n,r){var i=n?P(n):E(),a=new Date(i.getTime()),o=new Date(i.getTime());if("day"===t)a.setDate(a.getDate()-r),o.setDate(o.getDate()-r);else if("week"===t)a.setDate(a.getDate()-7*r),o.setDate(o.getDate()-7*r);else if("month"===t)a.setDate(1),a.setMonth(a.getMonth()-r),o.setDate(1),o.setMonth(o.getMonth()-r);else{if("year"!==t)throw new Error("Unknown period type '".concat(t,"'."));a.setFullYear(a.getFullYear()-r),o.setFullYear(o.getFullYear()-r)}if("day"!==t){var c=f.periods[t].parse(a),s=f.periods[t].parse(o),l=c.getDateRange(),u=I(l,1);a=u[0];var d=s.getDateRange(),p=I(d,2);o=p[1]}var m=new Date(1991,7,6);if(a.getTime()-m.getTime()<0)switch(t){case"year":a=new Date(1992,0,1);break;case"month":a=new Date(1991,8,1);break;case"week":a=new Date(1991,8,12);break;case"day":default:a=m;break}return new e(a,o,t)}},{key:"parse",value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"day";if(/^previous/.test(t)){var r=e.getLastNRange(n,"2").startDate;return e.getLastNRange(n,t.substring(8),r)}if(/^last/.test(t))return e.getLastNRange(n,t.substring(4));var i=decodeURIComponent(t).split(",");return new e(P(i[0]),P(i[1]),n)}},{key:"getDisplayText",value:function(){return D("General_DateRangeInPeriodList")}}]),e}();function R(){return{getAllLabels:f.getAllLabels.bind(f),isRecognizedPeriod:f.isRecognizedPeriod.bind(f),get:f.get.bind(f),parse:f.parse.bind(f),parseDate:P,format:C,RangePeriod:_,todayIsInRange:T}}function U(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function q(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function J(e,t,n){return t&&q(e.prototype,t),n&&q(e,n),e}function G(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e} /*! * Matomo - free/libre analytics platform * diff --git a/plugins/CoreHome/vue/src/Periods/Range.ts b/plugins/CoreHome/vue/src/Periods/Range.ts index 07660dae5e..f1c092d7da 100644 --- a/plugins/CoreHome/vue/src/Periods/Range.ts +++ b/plugins/CoreHome/vue/src/Periods/Range.ts @@ -176,6 +176,11 @@ export default class RangePeriod { containsToday(): boolean { return todayIsInRange(this.getDateRange()); } + + getDayCount(): number { + return (Math.ceil((this.endDate.getTime() - this.startDate.getTime()) / (1000 * 3600 * 24)) + + 1); + } } Periods.addCustomPeriod('range', RangePeriod); diff --git a/plugins/Transitions/API.php b/plugins/Transitions/API.php index 67a388eb8a..0dc1c726c5 100644 --- a/plugins/Transitions/API.php +++ b/plugins/Transitions/API.php @@ -66,6 +66,10 @@ class API extends \Piwik\Plugin\API { Piwik::checkUserHasViewAccess($idSite); + if (!$this->isPeriodAllowed($idSite, $period, $date)) { + throw new Exception('PeriodNotAllowed'); + } + // get idaction of the requested action $idaction = $this->deriveIdAction($actionName, $actionType); if ($idaction < 0) { @@ -656,4 +660,53 @@ class API extends \Piwik\Plugin\API $this->totalTransitionsToFollowingPages += $actions; } } + + + /** + * Check if a period is allowed by config settings + * + * @param $idSite + * @param $period + * @param $date + * + * @return bool + */ + public function isPeriodAllowed($idSite, $period, $date) : bool + { + $maxPeriodAllowed = Transitions::getPeriodAllowedConfig($idSite); + if ($maxPeriodAllowed === 'all') { + return true; + } + + // If the period is a range then the number of days in the range must be less or equal to the max period allowed + if ($period === 'range') { + + $range = new Period\Range($period, $date); + $rangeDays = $range->getDayCount(); + + switch ($maxPeriodAllowed) { + case 'day': + return $rangeDays == 1; + case 'week': + return $rangeDays <= 7; + case 'month': + return $rangeDays <= 31; + case 'year': + return $rangeDays <= 365; + } + } + + switch ($maxPeriodAllowed) { + case 'day': + return $period === 'day'; + case 'week': + return in_array($period, ['day', 'week']); + case 'month': + return in_array($period, ['day', 'week', 'month']); + case 'year': + return in_array($period, ['day', 'week', 'month', 'year']); + } + + return false; + } } diff --git a/plugins/Transitions/Controller.php b/plugins/Transitions/Controller.php index 2cabbc62d2..a8a85e0115 100644 --- a/plugins/Transitions/Controller.php +++ b/plugins/Transitions/Controller.php @@ -60,6 +60,9 @@ class Controller extends \Piwik\Plugin\Controller 'NoDataForAction' => 'Transitions_NoDataForAction', 'NoDataForActionDetails' => 'Transitions_NoDataForActionDetails', 'NoDataForActionBack' => 'Transitions_ErrorBack', + 'PeriodNotAllowed' => 'Transitions_PeriodNotAllowed', + 'PeriodNotAllowedDetails'=> 'Transitions_PeriodNotAllowedDetails', + 'PeriodNotAllowedBack' => 'Transitions_ErrorBack', 'ShareOfAllPageviews' => 'Transitions_ShareOfAllPageviews', 'DateRange' => 'General_DateRange' ); diff --git a/plugins/Transitions/Transitions.php b/plugins/Transitions/Transitions.php index 67d5be7f25..afd5f3a2fc 100644 --- a/plugins/Transitions/Transitions.php +++ b/plugins/Transitions/Transitions.php @@ -9,6 +9,10 @@ namespace Piwik\Plugins\Transitions; +use Piwik\Common; +use Piwik\Plugins\Transitions\API; +use Piwik\Config; + /** */ class Transitions extends \Piwik\Plugin @@ -22,7 +26,8 @@ class Transitions extends \Piwik\Plugin 'AssetManager.getStylesheetFiles' => 'getStylesheetFiles', 'AssetManager.getJavaScriptFiles' => 'getJsFiles', 'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys', - 'API.getPagesComparisonsDisabledFor' => 'getPagesComparisonsDisabledFor', + 'API.getPagesComparisonsDisabledFor' => 'getPagesComparisonsDisabledFor', + 'Template.jsGlobalVariables' => 'addJsGlobalVariables', ); } @@ -55,4 +60,39 @@ class Transitions extends \Piwik\Plugin $translationKeys[] = 'CoreHome_ThereIsNoDataForThisReport'; $translationKeys[] = 'General_Others'; } + + public function addJsGlobalVariables(&$out) + { + $idSite = Common::getRequestVar('idSite', 1, 'int'); + $maxPeriodAllowed = self::getPeriodAllowedConfig($idSite); + + $out .= ' piwik.transitionsMaxPeriodAllowed = "'.($maxPeriodAllowed ? $maxPeriodAllowed : 'all').'"'."\n"; + } + + /** + * Retrieve the period allowed config setting for a site or all sites if null + * + * @param $idSite + * + * @return string + */ + public static function getPeriodAllowedConfig($idSite) : string + { + $transitionsGeneralConfig = Config::getInstance()->Transitions; + $generalMaxPeriodAllowed = ($transitionsGeneralConfig && !empty($transitionsGeneralConfig['max_period_allowed']) ? $transitionsGeneralConfig['max_period_allowed']: null); + + $siteMaxPeriodAllowed = null; + if ($idSite) { + $sectionName = 'Transitions_'.$idSite; + $transitionsSiteConfig = Config::getInstance()->$sectionName; + $siteMaxPeriodAllowed = ($transitionsSiteConfig && !empty($transitionsSiteConfig['max_period_allowed']) ? $transitionsSiteConfig['max_period_allowed'] : null); + } + + if (!$generalMaxPeriodAllowed && !$siteMaxPeriodAllowed) { + return 'all'; // No config setting, so all periods are valid + } + + // Site setting overrides general, if it exists + return $siteMaxPeriodAllowed ?? $generalMaxPeriodAllowed; + } } diff --git a/plugins/Transitions/javascripts/transitions.js b/plugins/Transitions/javascripts/transitions.js index 19bd467b14..caabaa0b69 100644 --- a/plugins/Transitions/javascripts/transitions.js +++ b/plugins/Transitions/javascripts/transitions.js @@ -123,6 +123,45 @@ DataTable_RowActions_Registry.register({ }, isAvailableOnReport: function (dataTableParams) { + + if (piwik.transitionsMaxPeriodAllowed && dataTableParams['period']) { + + if (dataTableParams['period'] === 'range') { + + var piwikPeriods = piwikHelper.getAngularDependency('piwikPeriods'); + if (piwikPeriods) { + var range = piwikPeriods.parse(dataTableParams['period'], dataTableParams['date']); + if (range) { + var rangeDays = range.getDayCount(); + if ((piwik.transitionsMaxPeriodAllowed === 'day' && rangeDays > 1) || + (piwik.transitionsMaxPeriodAllowed === 'week' && rangeDays > 7) || + (piwik.transitionsMaxPeriodAllowed === 'month' && rangeDays > 31) || + (piwik.transitionsMaxPeriodAllowed === 'year' && rangeDays > 365)) + { + return false; + } + } + } + } else { + if (piwik.transitionsMaxPeriodAllowed === 'day' && dataTableParams['period'] !== 'day') { + return false; + } + if (piwik.transitionsMaxPeriodAllowed === 'week' && dataTableParams['period'] !== 'day' + && dataTableParams['period'] !== 'week') { + return false; + } + if (piwik.transitionsMaxPeriodAllowed === 'month' && dataTableParams['period'] !== 'day' + && dataTableParams['period'] !== 'week' && dataTableParams['period'] !== 'month') { + return false; + } + if (piwik.transitionsMaxPeriodAllowed === 'year' && dataTableParams['period'] !== 'day' + && dataTableParams['period'] !== 'week' && dataTableParams['period'] !== 'month' + && dataTableParams['period'] !== 'year' + ) { + return false; + } + } + } var i = 0; for (i; i < DataTable_RowActions_Transitions.registeredReports.length; i++) { var report = DataTable_RowActions_Transitions.registeredReports[i]; @@ -1586,8 +1625,8 @@ Piwik_Transitions_Ajax.prototype.callApi = function (method, params, callback) { if (typeof Piwik_Transitions_Translations == 'undefined') { self.callApi('Transitions.getTranslations', {}, function (response) { - if (typeof response[0] == 'object') { - Piwik_Transitions_Translations = response[0]; + if (typeof response == 'object') { + Piwik_Transitions_Translations = response; } else { Piwik_Transitions_Translations = {}; } diff --git a/plugins/Transitions/lang/en.json b/plugins/Transitions/lang/en.json index 321557d613..71cdb25bb5 100644 --- a/plugins/Transitions/lang/en.json +++ b/plugins/Transitions/lang/en.json @@ -24,6 +24,8 @@ "LoopsInline": "%s page reloads", "NoDataForAction": "There's no data for %s", "NoDataForActionDetails": "Either the action had no pageviews during the period %s or it is invalid.", + "PeriodNotAllowed": "Time period not allowed", + "PeriodNotAllowedDetails": "This feature is not available for this period, try selecting a period with fewer days", "OutgoingTraffic": "Outgoing traffic", "PluginDescription": "Reports previous and following actions for each page URL in a new Transitions report, available in the Actions reports via a new icon.", "ShareOfAllPageviews": "This page had %1$s pageviews (%2$s of all pageviews)", 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 @@ +<?php +/** + * Matomo - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ +namespace Piwik\Plugins\Transitions\tests\Integration; + +use Piwik\Plugins\Transitions\Transitions; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; +use Piwik\Config; +use Piwik\Tests\Framework\Fixture; +use Piwik\Plugins\Transitions\API; + +/** + * Tests the transitions plugin max_period_allowed setting + * + * @group TransitionsMaxAllowedPeriodTest + * @group Plugins + */ +class TransitionsMaxAllowedPeriodTest extends IntegrationTestCase +{ + + public $api; + + protected static function configureFixture($fixture) + { + parent::configureFixture($fixture); + $fixture->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); + } + +} diff --git a/plugins/Transitions/tests/UI/Transitions_spec.js b/plugins/Transitions/tests/UI/Transitions_spec.js index ed125f3078..4e0d716d09 100644 --- a/plugins/Transitions/tests/UI/Transitions_spec.js +++ b/plugins/Transitions/tests/UI/Transitions_spec.js @@ -84,4 +84,18 @@ describe("Transitions", function () { expect(await page.screenshotSelector('body')).to.matchImage('transitions_report_switch_type_title'); }); -});
\ No newline at end of file + it('should show period not allowed for disabled periods', async function () { + + testEnvironment.overrideConfig('Transitions_1', 'max_period_allowed', 'day'); + testEnvironment.save(); + + await page.goto("?" + urlBase + "#?" + generalParams + "&category=General_Actions&subcategory=Transitions_Transitions"); + await page.waitForNetworkIdle(); + expect(await page.screenshotSelector('.pageWrap')).to.matchImage('transitions_report_period_not_allowed'); + + testEnvironment.overrideConfig('Transitions_1', 'max_period_allowed', 'all'); + testEnvironment.save(); + }); + + +}); |