From 959b41ec50b5437450ea3bb3c8cb181043117fac Mon Sep 17 00:00:00 2001 From: Lapiu Dev Date: Thu, 2 Mar 2023 08:36:39 -0600 Subject: [PATCH 1/3] Fixed issue #18630: Survey list showing wrong icon for surveys. Time adjust considered for icon generation. --- application/models/Survey.php | 10 +- tests/unit/models/SurveyTest.php | 347 ++++++++++++++++++++++++++++++- 2 files changed, 351 insertions(+), 6 deletions(-) diff --git a/application/models/Survey.php b/application/models/Survey.php index 3f2a750f863..82c34d15212 100755 --- a/application/models/Survey.php +++ b/application/models/Survey.php @@ -1154,7 +1154,7 @@ public function getRunning() // If it's active, then we check if not expired // Time adjust $sNow = date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime(date("Y-m-d H:i:s")))); - $sStop = ($this->expires != '') ? date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime($this->expires))) : null; + $sStop = ($this->expires != '') ? date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime($this->expires))) : $sNow; $sStart = ($this->startdate != '') ? date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime($this->startdate))) : $sNow; // Time comparaison @@ -1169,17 +1169,17 @@ public function getRunning() $sStart = convertToGlobalSettingFormat($sStart); // Icon generaton (for CGridView) - $sIconRunNoEx = 'SS' . gT('End: Never') . ''; - $sIconRunning = '' . sprintf(gT('End: %s'), $sStop) . ''; + $sIconRunNoEx = 'SS' . gT('End: Never') . ''; + $sIconRunning = '' . sprintf(gT('End: %s'), $sStop) . ''; $sIconExpired = '' . sprintf(gT('Expired: %s'), $sStop) . ''; - $sIconFuture = '' . sprintf(gT('Start: %s'), $sStart) . ''; + $sIconFuture = '' . sprintf(gT('Start: %s'), $sStart) . ''; // Icon parsing if ($bExpired || $bWillRun) { // Expire prior to will start $running = ($bExpired) ? $sIconExpired : $sIconFuture; } else { - if ($sStop == null) { + if ($this->expires == '') { $running = $sIconRunNoEx; } else { $running = $sIconRunning; diff --git a/tests/unit/models/SurveyTest.php b/tests/unit/models/SurveyTest.php index 2422399f896..32ce930d9f1 100644 --- a/tests/unit/models/SurveyTest.php +++ b/tests/unit/models/SurveyTest.php @@ -2,8 +2,353 @@ namespace ls\tests; - class SurveyTest extends BaseModelTestCase { protected $modelClassName = \Survey::class; + private static $intervals; + + public static function setUpBeforeClass(): void + { + parent::setupBeforeClass(); + + \Yii::import('application.helpers.surveytranslator_helper', true); + + \Yii::app()->session['dateformat'] = 6; + + //Set time intervals. + self::$intervals = array( + 'oneDay' => \DateInterval::createFromDateString('1 days'), + 'twoDays' => \DateInterval::createFromDateString('2 days'), + 'threeDays' => \DateInterval::createFromDateString('3 days'), + 'fourDays' => \DateInterval::createFromDateString('4 days'), + 'fiveDays' => \DateInterval::createFromDateString('5 days'), + 'sixDays' => \DateInterval::createFromDateString('6 days'), + 'sevenDays' => \DateInterval::createFromDateString('7 days'), + ); + } + + public function setUp(): void + { + \SettingGlobal::setSetting('timeadjust', '+0 minutes'); + } + + /** + * Survey state: inactive. + */ + public function testInactiveSurveyState(): void + { + $survey = new \Survey(); + $survey->active = 'N'; + + $state = $survey->getState(); + + $this->assertSame('inactive', $state, 'Survey active property is ' . $survey->active); + } + + /** + * Survey state: expired. + */ + public function testExpiredSurveyState(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $twoDaysAgo = date_create()->sub(self::$intervals['twoDays'])->format('Y-m-d H:i:s'); + $survey->expires = $twoDaysAgo; + + $state = $survey->getState(); + + $this->assertSame('expired', $state, 'Survey expires property is ' . $survey->expires); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+420 minutes'); + + $state = $survey->getState(); + + $this->assertSame('expired', $state, 'Survey expires property is ' . $survey->expires . ' (time adjust test)'); + } + + /** + * Survey state: willRun. + */ + public function testWillRunSurveyState(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $inFourDays = date_create()->add(self::$intervals['fourDays'])->format('Y-m-d H:i:s'); + $survey->startdate = $inFourDays; + + $state = $survey->getState(); + + $this->assertSame('willRun', $state, 'Survey startdate property is ' . $survey->startdate); + } + + /** + * Survey state: willExpire (the survey is active and it has an expiredate). + */ + public function testWillExpireSurveyState(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $inFiveDays = date_create()->add(self::$intervals['fiveDays'])->format('Y-m-d H:i:s'); + $survey->expires = $inFiveDays; + + $state = $survey->getState(); + + $this->assertSame('willExpire', $state, 'Survey expires property is ' . $survey->expires); + + // Testing for both start and expire date. + $inSevenDays = date_create()->add(self::$intervals['sevenDays'])->format('Y-m-d H:i:s'); + $oneDayAgo = date_create()->sub(self::$intervals['oneDay'])->format('Y-m-d H:i:s'); + + $survey->startdate = $oneDayAgo; + $survey->expires = $inSevenDays; + + $state = $survey->getState(); + + $this->assertSame('willExpire', $state, 'Survey expires property is ' . $survey->expires . '. Survey startdate property is ' . $survey->startdate); + } + + /** + * Survey state: running (the survey is active but it does not have an expire date). + */ + public function testRunningSurveyState(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $state = $survey->getState(); + + $this->assertSame('running', $state, 'Survey active property is ' . $survey->active . ', no dates set.'); + } + + /** + * The survey is not active. + */ + public function testInactiveSurveyIcon(): void + { + $survey = new \Survey(); + $survey->active = 'N'; + + $icon = $survey->getRunning(); + + $this->assertStringContainsString(gT('Inactive'), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-stop text-warning', $icon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey is active but it has no start or expire dates set. + */ + public function testActiveSurveyIconNoDates(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $icon = $survey->getRunning(); + + $this->assertStringContainsString(gT('Active'), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-play text-success', $icon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey is active, it has a start date in the past but no expire date. + */ + public function testActiveSurveyIconNoExpireDate(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $threeDaysAgo = date_create()->sub(self::$intervals['threeDays'])->format('Y-m-d H:i:s'); + + $survey->startdate = $threeDaysAgo; + + $icon = $survey->getRunning(); + + $this->assertStringContainsString(gT('End: Never'), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-play text-success', $icon, 'The icon link does not have the right css classes.'); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+420 minutes'); + + $newIcon = $survey->getRunning(); + + $this->assertStringContainsString(gT('End: Never'), $newIcon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-play text-success', $newIcon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey is active, it has an expire date in the future but no start date. + */ + public function testActiveSurveyIconNoStartDate(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $inFourDays = date_create()->add(self::$intervals['fourDays'])->format('Y-m-d H:i:s'); + + $survey->expires = $inFourDays; + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); + + $icon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('End: %s'), $sExpires), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-play text-success', $icon, 'The icon link does not have the right css classes.'); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+120 minutes'); + + $newIcon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('End: %s'), $sExpires), $newIcon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-play text-success', $newIcon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey is active, it has an expire date in the future and a start date in the past. + */ + public function testActiveSurveyIconExpireDateInTheFutureStartDateInThePast(): void + { + + $survey = new \Survey(); + $survey->active = 'Y'; + + $oneDayAgo = date_create()->sub(self::$intervals['oneDay'])->format('Y-m-d H:i:s'); + $inFiveDays = date_create()->add(self::$intervals['fiveDays'])->format('Y-m-d H:i:s'); + + $survey->startdate = $oneDayAgo; + $survey->expires = $inFiveDays; + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); + + $icon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('End: %s'), $sExpires), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-play text-success', $icon, 'The icon link does not have the right css classes.'); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+180 minutes'); + + $newIcon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('End: %s'), $sExpires), $newIcon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-play text-success', $newIcon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey has a start date in the future and no expire date. + */ + public function testSurveyIconWillStartNoExpireDate(): void + { + + $survey = new \Survey(); + $survey->active = 'Y'; + + $inSixDays = date_create()->add(self::$intervals['sixDays'])->format('Y-m-d H:i:s'); + + $survey->startdate = $inSixDays; + + $sStart = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->startdate)))); + $icon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Start: %s'), $sStart), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-clock-o text-warning', $icon, 'The icon link does not have the right css classes.'); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+240 minutes'); + + $newIcon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Start: %s'), $sStart), $newIcon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-clock-o text-warning', $newIcon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey has a start date in the future and an expire date in the future. + */ + public function testSurveyIconWillStart(): void + { + + $survey = new \Survey(); + $survey->active = 'Y'; + + $inFourDays = date_create()->add(self::$intervals['fourDays'])->format('Y-m-d H:i:s'); + $inSevenDays = date_create()->add(self::$intervals['sevenDays'])->format('Y-m-d H:i:s'); + + $survey->startdate = $inFourDays; + $survey->expires = $inSevenDays; + + $sStart = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->startdate)))); + $icon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Start: %s'), $sStart), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-clock-o text-warning', $icon, 'The icon link does not have the right css classes.'); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+300 minutes'); + + $newIcon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Start: %s'), $sStart), $newIcon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa-clock-o text-warning', $newIcon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey has an expire date in the past and no start date. + */ + public function testSurveyIconExpiredNoStartdate(): void + { + + $survey = new \Survey(); + $survey->active = 'Y'; + + $threeDaysAgo = date_create()->sub(self::$intervals['threeDays'])->format('Y-m-d H:i:s'); + + $survey->expires = $threeDaysAgo; + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); + $icon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Expired: %s'), $sExpires), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa fa-step-forward text-warning', $icon, 'The icon link does not have the right css classes.'); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+360 minutes'); + + $newIcon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Expired: %s'), $sExpires), $newIcon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa fa-step-forward text-warning', $newIcon, 'The icon link does not have the right css classes.'); + } + + /** + * The survey has an expire date in the past and a start date in the past. + */ + public function testSurveyIconExpired(): void + { + $survey = new \Survey(); + $survey->active = 'Y'; + + $fiveDaysAgo = date_create()->sub(self::$intervals['fiveDays'])->format('Y-m-d H:i:s'); + $sevenDaysAgo = date_create()->sub(self::$intervals['sevenDays'])->format('Y-m-d H:i:s'); + + $survey->startdate = $sevenDaysAgo; + $survey->expires = $fiveDaysAgo; + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); + $icon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Expired: %s'), $sExpires), $icon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa fa-step-forward text-warning', $icon, 'The icon link does not have the right css classes.'); + + //Test with time adjust. + \SettingGlobal::setSetting('timeadjust', '+60 minutes'); + + $newIcon = $survey->getRunning(); + + $this->assertStringContainsString(sprintf(gT('Expired: %s'), $sExpires), $newIcon, 'The icon link does not have the right text.'); + $this->assertStringContainsString('fa fa fa-step-forward text-warning', $newIcon, 'The icon link does not have the right css classes.'); + } } From 21a47000b50ee3d09b9c5a60420521e4baa278a4 Mon Sep 17 00:00:00 2001 From: Gabriel Jenik Date: Thu, 2 Mar 2023 15:22:08 -0300 Subject: [PATCH 2/3] Fixed issue #18630: Survey list showing wrong icon for surveys. Porting stuff from getState() --- application/models/Survey.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/application/models/Survey.php b/application/models/Survey.php index 82c34d15212..3e901ab2c3b 100755 --- a/application/models/Survey.php +++ b/application/models/Survey.php @@ -1154,16 +1154,16 @@ public function getRunning() // If it's active, then we check if not expired // Time adjust $sNow = date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime(date("Y-m-d H:i:s")))); - $sStop = ($this->expires != '') ? date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime($this->expires))) : $sNow; - $sStart = ($this->startdate != '') ? date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime($this->startdate))) : $sNow; + $sStop = ($this->expires != '') ? date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime($this->expires))) : null; + $sStart = ($this->startdate != '') ? date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime($this->startdate))) : null; // Time comparaison $oNow = new DateTime($sNow); $oStop = new DateTime($sStop); $oStart = new DateTime($sStart); - $bExpired = ($oStop < $oNow); - $bWillRun = ($oStart > $oNow); + $bExpired = (!is_null($sStop) && $oStop < $oNow); + $bWillRun = (!is_null($sStart) && $oStart > $oNow); $sStop = $sStop != null ? convertToGlobalSettingFormat($sStop) : null; $sStart = convertToGlobalSettingFormat($sStart); @@ -1179,7 +1179,7 @@ public function getRunning() // Expire prior to will start $running = ($bExpired) ? $sIconExpired : $sIconFuture; } else { - if ($this->expires == '') { + if (is_null($sStop)) { $running = $sIconRunNoEx; } else { $running = $sIconRunning; From 73f7142609109940d970f3966b243ee657d1b582 Mon Sep 17 00:00:00 2001 From: Gabriel Jenik Date: Thu, 2 Mar 2023 16:02:24 -0300 Subject: [PATCH 3/3] Fixed issue #18630: Survey list showing wrong icon for surveys. --- tests/unit/models/SurveyTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/models/SurveyTest.php b/tests/unit/models/SurveyTest.php index 32ce930d9f1..5b37cf0a0cb 100644 --- a/tests/unit/models/SurveyTest.php +++ b/tests/unit/models/SurveyTest.php @@ -199,6 +199,8 @@ public function testActiveSurveyIconNoStartDate(): void //Test with time adjust. \SettingGlobal::setSetting('timeadjust', '+120 minutes'); + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); $newIcon = $survey->getRunning(); @@ -230,6 +232,8 @@ public function testActiveSurveyIconExpireDateInTheFutureStartDateInThePast(): v //Test with time adjust. \SettingGlobal::setSetting('timeadjust', '+180 minutes'); + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); $newIcon = $survey->getRunning(); @@ -258,6 +262,8 @@ public function testSurveyIconWillStartNoExpireDate(): void //Test with time adjust. \SettingGlobal::setSetting('timeadjust', '+240 minutes'); + + $sStart = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->startdate)))); $newIcon = $survey->getRunning(); @@ -288,6 +294,8 @@ public function testSurveyIconWillStart(): void //Test with time adjust. \SettingGlobal::setSetting('timeadjust', '+300 minutes'); + + $sStart = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->startdate)))); $newIcon = $survey->getRunning(); @@ -316,6 +324,8 @@ public function testSurveyIconExpiredNoStartdate(): void //Test with time adjust. \SettingGlobal::setSetting('timeadjust', '+360 minutes'); + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); $newIcon = $survey->getRunning(); @@ -345,6 +355,8 @@ public function testSurveyIconExpired(): void //Test with time adjust. \SettingGlobal::setSetting('timeadjust', '+60 minutes'); + + $sExpires = convertToGlobalSettingFormat(date("Y-m-d H:i:s", strtotime(\Yii::app()->getConfig('timeadjust'), strtotime($survey->expires)))); $newIcon = $survey->getRunning();