From 68b8140e7cff9b8e37176f596f14fbbce57cb685 Mon Sep 17 00:00:00 2001 From: Christian Hartmann Date: Tue, 21 Feb 2023 17:17:22 +0100 Subject: [PATCH] Make timestamp in csv export ISO 8601 compliant Signed-off-by: Christian Hartmann --- lib/Service/SubmissionService.php | 5 +-- tests/Integration/Api/ApiV2Test.php | 6 ++-- tests/Unit/Service/SubmissionServiceTest.php | 32 +++++++++----------- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index afe1e315..931e6d9c 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -39,7 +39,6 @@ use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\Files\NotPermittedException; use OCP\IConfig; -use OCP\IDateTimeFormatter; use OCP\IL10N; use OCP\IUser; use OCP\IUserManager; @@ -93,7 +92,6 @@ class SubmissionService { AnswerMapper $answerMapper, IRootFolder $storage, IConfig $config, - IDateTimeFormatter $dateTimeFormatter, IL10N $l10n, LoggerInterface $logger, IUserManager $userManager, @@ -104,7 +102,6 @@ class SubmissionService { $this->answerMapper = $answerMapper; $this->storage = $storage; $this->config = $config; - $this->dateTimeFormatter = $dateTimeFormatter; $this->l10n = $l10n; $this->logger = $logger; $this->userManager = $userManager; @@ -236,7 +233,7 @@ class SubmissionService { } // Date - $row[] = $this->dateTimeFormatter->formatDateTime($submission->getTimestamp(), 'full', 'full', new DateTimeZone($userTimezone), $this->l10n); + $row[] = date_format(date_timestamp_set(new DateTime(), $submission->getTimestamp())->setTimezone(new DateTimeZone($userTimezone)), 'c'); // Answers, make sure we keep the question order $answers = array_reduce($this->answerMapper->findBySubmission($submission->getId()), function (array $carry, Answer $answer) { diff --git a/tests/Integration/Api/ApiV2Test.php b/tests/Integration/Api/ApiV2Test.php index 560c2f03..69025374 100644 --- a/tests/Integration/Api/ApiV2Test.php +++ b/tests/Integration/Api/ApiV2Test.php @@ -1204,9 +1204,9 @@ class ApiV2Test extends TestCase { 'exportSubmissions' => [ 'expected' => ' "User ID","User display name","Timestamp","First Question?","Second Question?" - "user1","User No. 1","Friday, January 2, 1970 at 10:17:36 AM GMT+0:00","This is a short answer.","Option 1" - "","Anonymous user","Thursday, January 1, 1970 at 3:25:45 AM GMT+0:00","This is another short answer.","Option 2" - "","Anonymous user","Thursday, January 1, 1970 at 12:20:34 AM GMT+0:00","",""' + "user1","User No. 1","1970-01-02T10:17:36+00:00","This is a short answer.","Option 1" + "","Anonymous user","1970-01-01T03:25:45+00:00","This is another short answer.","Option 2" + "","Anonymous user","1970-01-01T00:20:34+00:00","",""' ] ]; } diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 838bd1e4..e555e75f 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -40,7 +40,6 @@ use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IConfig; -use OCP\IDateTimeFormatter; use OCP\IL10N; use OCP\IUser; use OCP\IUserManager; @@ -74,9 +73,6 @@ class SubmissionServiceTest extends TestCase { /** @var IConfig|MockObject */ private $config; - /** @var IDateTimeFormatter|MockObject */ - private $dateTimeFormatter; - /** @var IL10N|MockObject */ private $l10n; @@ -94,7 +90,6 @@ class SubmissionServiceTest extends TestCase { $this->answerMapper = $this->createMock(AnswerMapper::class); $this->storage = $this->createMock(IRootFolder::class); $this->config = $this->createMock(IConfig::class); - $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); $this->l10n = $this->createMock(IL10N::class); $this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(); $this->userManager = $this->createMock(IUserManager::class); @@ -121,7 +116,6 @@ class SubmissionServiceTest extends TestCase { $this->answerMapper, $this->storage, $this->config, - $this->dateTimeFormatter, $this->l10n, $this->logger, $this->userManager, @@ -285,6 +279,7 @@ class SubmissionServiceTest extends TestCase { [ 'id' => 1, 'userId' => 'user1', + 'timestamp' => 123456789, 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'], ['questionId' => 2, 'text' => 'Q2A1'] @@ -293,6 +288,7 @@ class SubmissionServiceTest extends TestCase { [ 'id' => 2, 'userId' => 'user2', + 'timestamp' => 123456789, 'answers' => [ ['questionId' => 1, 'text' => 'Q1A2'], ['questionId' => 2, 'text' => 'Q2A2'] @@ -302,8 +298,8 @@ class SubmissionServiceTest extends TestCase { // Expected CSV-Result ' "User ID","User display name","Timestamp","Question 1","Question 2" - "user1","User 1","01.01.01, 01:01","Q1A1","Q2A1" - "user2","User 2","01.01.01, 01:01","Q1A2","Q2A2" + "user1","User 1","1973-11-29T22:33:09+01:00","Q1A1","Q2A1" + "user2","User 2","1973-11-29T22:33:09+01:00","Q1A2","Q2A2" ' ], 'checkbox-multi-answers' => [ @@ -316,6 +312,7 @@ class SubmissionServiceTest extends TestCase { [ 'id' => 1, 'userId' => 'user1', + 'timestamp' => 123456789, 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'], ['questionId' => 1, 'text' => 'Q1A2'], @@ -326,7 +323,7 @@ class SubmissionServiceTest extends TestCase { // Expected CSV-Result ' "User ID","User display name","Timestamp","Question 1" - "user1","User 1","01.01.01, 01:01","Q1A1; Q1A2; Q1A3" + "user1","User 1","1973-11-29T22:33:09+01:00","Q1A1; Q1A2; Q1A3" ' ], 'anonymous-user' => [ @@ -339,6 +336,7 @@ class SubmissionServiceTest extends TestCase { [ 'id' => 1, 'userId' => 'anon-user-xyz', + 'timestamp' => 123456789, 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'], ] @@ -347,7 +345,7 @@ class SubmissionServiceTest extends TestCase { // Expected CSV-Result ' "User ID","User display name","Timestamp","Question 1" - "","Anonymous user","01.01.01, 01:01","Q1A1" + "","Anonymous user","1973-11-29T22:33:09+01:00","Q1A1" ' ], 'questions-not-answered' => [ @@ -362,6 +360,7 @@ class SubmissionServiceTest extends TestCase { [ 'id' => 1, 'userId' => 'user1', + 'timestamp' => 123456789, 'answers' => [ ['questionId' => 2, 'text' => 'Q2A1'] ] @@ -370,7 +369,7 @@ class SubmissionServiceTest extends TestCase { // Expected CSV-Result ' "User ID","User display name","Timestamp","Question 1","Question 2","Question 3" - "user1","User 1","01.01.01, 01:01","","Q2A1","" + "user1","User 1","1973-11-29T22:33:09+01:00","","Q2A1","" ' ], /* No submissions, but request via api */ @@ -395,6 +394,7 @@ class SubmissionServiceTest extends TestCase { [ 'id' => 1, 'userId' => 'anon-user-xyz', + 'timestamp' => 123456789, 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'], ] @@ -403,7 +403,7 @@ class SubmissionServiceTest extends TestCase { // Expected CSV-Result ' "User ID","User display name","Timestamp" - "","Anonymous user","01.01.01, 01:01" + "","Anonymous user","1973-11-29T22:33:09+01:00" ' ], ]; @@ -438,6 +438,7 @@ class SubmissionServiceTest extends TestCase { [ 'id' => 1, 'userId' => 'user1', + 'timestamp' => 123456789, 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'] ] @@ -446,7 +447,7 @@ class SubmissionServiceTest extends TestCase { // Expected CSV-Result ' "User ID","User display name","Timestamp","Question 1" - "user1","User 1","01.01.01, 01:01","Q1A1" + "user1","User 1","1973-11-29T22:33:09+01:00","Q1A1" ' ); } @@ -510,11 +511,6 @@ class SubmissionServiceTest extends TestCase { ['unknown', null] ])); - // Just using any timestamp here - $this->dateTimeFormatter->expects($this->any()) - ->method('formatDateTime') - ->willReturn('01.01.01, 01:01'); - $this->answerMapper->expects($this->any()) ->method('findBySubmission') // Return AnswerObjects for corresponding submission