From 9ae696e71f8d01dc329fdcc0546170816d7d17fe 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 | 43 +++++++++----------- 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index afe1e315c..931e6d9c4 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -39,7 +39,6 @@ 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 @@ public function __construct(FormMapper $formMapper, AnswerMapper $answerMapper, IRootFolder $storage, IConfig $config, - IDateTimeFormatter $dateTimeFormatter, IL10N $l10n, LoggerInterface $logger, IUserManager $userManager, @@ -104,7 +102,6 @@ public function __construct(FormMapper $formMapper, $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 @@ public function getSubmissionsCsv(string $hash): array { } // 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 560c2f034..69025374d 100644 --- a/tests/Integration/Api/ApiV2Test.php +++ b/tests/Integration/Api/ApiV2Test.php @@ -1204,9 +1204,9 @@ public function dataExportSubmissions() { '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 838bd1e4d..560814025 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -40,7 +40,6 @@ 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 @@ public function setUp(): void { $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 @@ public function setUp(): void { $this->answerMapper, $this->storage, $this->config, - $this->dateTimeFormatter, $this->l10n, $this->logger, $this->userManager, @@ -288,7 +282,8 @@ public function dataGetSubmissionsCsv() { 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'], ['questionId' => 2, 'text' => 'Q2A1'] - ] + ], + 'timestamp' => 123456789 ], [ 'id' => 2, @@ -296,14 +291,15 @@ public function dataGetSubmissionsCsv() { 'answers' => [ ['questionId' => 1, 'text' => 'Q1A2'], ['questionId' => 2, 'text' => 'Q2A2'] - ] + ], + 'timestamp' => 123456789 ], ], // 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' => [ @@ -320,13 +316,14 @@ public function dataGetSubmissionsCsv() { ['questionId' => 1, 'text' => 'Q1A1'], ['questionId' => 1, 'text' => 'Q1A2'], ['questionId' => 1, 'text' => 'Q1A3'], - ] + ], + 'timestamp' => 123456789 ], ], // 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' => [ @@ -341,13 +338,14 @@ public function dataGetSubmissionsCsv() { 'userId' => 'anon-user-xyz', 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'], - ] + ], + 'timestamp' => 123456789 ], ], // 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' => [ @@ -364,13 +362,14 @@ public function dataGetSubmissionsCsv() { 'userId' => 'user1', 'answers' => [ ['questionId' => 2, 'text' => 'Q2A1'] - ] + ], + 'timestamp' => 123456789 ], ], // 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 */ @@ -397,13 +396,14 @@ public function dataGetSubmissionsCsv() { 'userId' => 'anon-user-xyz', 'answers' => [ ['questionId' => 1, 'text' => 'Q1A1'], - ] + ], + 'timestamp' => 123456789 ], ], // 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" ' ], ]; @@ -446,7 +446,7 @@ private function setUpSimpleCsvTest(): string { // Expected CSV-Result ' "User ID","User display name","Timestamp","Question 1" - "user1","User 1","01.01.01, 01:01","Q1A1" + "user1","User 1","1970-01-01T01:00:00+01:00","Q1A1" ' ); } @@ -510,11 +510,6 @@ private function setUpCsvTest(array $questions, array $submissions, string $csvT ['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