Permalink
Browse files

Fixed #385: Insert token valid dates in survey system

  • Loading branch information...
mennodekker committed Dec 17, 2018
1 parent b4a800f commit b072e42748dacb3eafd19ee9cd3054437c22edcc
@@ -701,10 +701,11 @@ public function copyTokenToSource(\Gems_Tracker_Token $token, $language, $survey
/****************************************
* Insert token in table (if not there) *
****************************************/
****************************************/
$validDates = $this->getValidDates($token);
// Get the mapped values
$values = $this->_fillAttributeMap($token);
$values = $this->_fillAttributeMap($token) + $validDates;
// Apparently it is possible to have this value filled without a survey questionnaire.
if ($token->isCompleted()) {
$values['completed'] = $token->getCompletionTime()->toString(self::LS_DB_COMPLETION_FORMAT);
@@ -1296,7 +1297,39 @@ public function getTokenUrl(\Gems_Tracker_Token $token, $language, $surveyId, $s
$baseurl = $this->getBaseUrl();
return $baseurl . ('/' == substr($baseurl, -1) ? '' : '/') . 'index.php?sid=' . $sourceSurveyId . '&token=' . $tokenId . $langUrl;
}
/**
* Get valid from/to dates to send to LimeSurvey depending on the dates of the token
*
* @param \Gems_Tracker_Token $token
* @return []
*/
public function getValidDates(\Gems_Tracker_Token $token)
{
$now = new \MUtil_Date();
// For extra protection, we add valid from/to dates as needed instead of leaving them in GemsTracker only
$tokenFrom = $token->getValidFrom();
$tokenUntil = $token->getValidUntil();
// Always set a date, so LimeSurvey will check the token
$lsFrom = is_null($tokenFrom) ? new \MUtil_Date('1900-01-01') : $tokenFrom;
if (!is_null($tokenUntil) && $tokenUntil->isEarlier($now)) {
$lsUntil = $tokenUntil;
} elseif (!is_null($tokenFrom)) {
// To end of day. If entering via GemsTracker it will always be updated as long as the token is still valid
$lsUntil = clone $now;
$lsUntil->setTimeToDayEnd();
} else {
// No valid from date, use same date as until
$lsUntil = $lsFrom;
}
$values = [
'validfrom' => $lsFrom->toString(self::LS_DB_DATETIME_FORMAT),
'validuntil' => $lsUntil->toString(self::LS_DB_DATETIME_FORMAT)
];
return $values;
}
/**
* Checks whether the token is in the source.
@@ -0,0 +1,143 @@
<?php
namespace Gems\Tracker\Source;
class LimeSurveyDatabaseTest extends \Gems_Test_DbTestAbstract
{
/**
*
* @var \Gems_Tracker_Source_LimeSurvey1m9Database
*/
protected $object;
public function setUp()
{
parent::setUp();
$sourceData = [];
$this->object = new \Gems_Tracker_Source_LimeSurvey1m9Database($sourceData, $this->db);
}
public function filterKeys($a)
{
$out = [];
foreach($a as $key => $value)
{
$new = str_replace('_', '', $key);
// Prevent accidentally overwriting
if (!array_key_exists($new, $a)) {
$out[$new] = $value;
} else {
$out[$key] = $value;
}
}
return $out;
}
/**
*
* @param \MUtil_Date|null $fromDate
* @param \MUtil_Date|null $untilDate
* @param [] $expected
* @dataProvider validDatesProvider
*/
public function testValidDates($fromDate, $untilDate, $expected)
{
$a = [
'key_a'=>'value',
'hey_b'=>'value',
'keya'=>'value',
];
$b = $this->filterKeys($a);
$token = $this->getMockBuilder('Gems_Tracker_Token')
->disableOriginalConstructor()
->getMock();
$token->expects($this->any())
->method('getValidFrom')
->will($this->returnValue($fromDate));
$token->expects($this->any())
->method('getValidUntil')
->will($this->returnValue($untilDate));
$actual = $this->object->getValidDates($token);
$this->assertEquals($expected, $actual);
}
/**
* There is a risk that the time attribute will not match due to the second changing
* Address that if the test fails but leave for now
*
* @return []
*/
public function validDatesProvider()
{
$now = new \MUtil_Date();
$now->setTimeToDayStart();
$nextWeek = clone $now;
$nextWeek->addDay(7);
$lastWeek = clone $now;
$lastWeek->subDay(7);
$tomorrow = clone $now;
$tomorrow->addDay(1);
return [
'futureOpen' => [
$nextWeek,
null,
[
'validfrom' => $nextWeek->toString('yyyy-MM-dd HH:mm:ss'),
'validuntil' => $now->toString('yyyy-MM-dd 23:59:59'),
]
],
'futureClosed' => [
$tomorrow,
$nextWeek,
[
'validfrom' => $tomorrow->toString('yyyy-MM-dd HH:mm:ss'),
'validuntil' => $now->toString('yyyy-MM-dd 23:59:59'),
]
],
'open' => [
$lastWeek,
$nextWeek,
[
'validfrom' => $lastWeek->toString('yyyy-MM-dd HH:mm:ss'),
'validuntil' => $now->toString('yyyy-MM-dd 23:59:59'),
]
],
'unknown' => [
null,
null,
[
'validfrom' => '1900-01-01 00:00:00',
'validuntil' => '1900-01-01 00:00:00',
]
],
'onlyend' => [
null,
$nextWeek,
[
'validfrom' => '1900-01-01 00:00:00',
'validuntil' => '1900-01-01 00:00:00',
]
],
'past' => [
$lastWeek,
$lastWeek,
[
'validfrom' => $lastWeek->toString('yyyy-MM-dd HH:mm:ss'),
'validuntil' => $lastWeek->toString('yyyy-MM-dd HH:mm:ss'),
]
],
];
}
protected function getDataSet()
{
return new \PHPUnit_Extensions_Database_DataSet_ArrayDataSet([]);
}
}

0 comments on commit b072e42

Please sign in to comment.