Skip to content

Commit

Permalink
Merge pull request #1531 from nextcloud/fix/exportCsvTimestamp
Browse files Browse the repository at this point in the history
Make timestamp in csv export ISO 8601 compliant
  • Loading branch information
jotoeri authored Mar 5, 2023
2 parents 077f359 + 68b8140 commit b6597d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
5 changes: 1 addition & 4 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,7 +92,6 @@ public function __construct(FormMapper $formMapper,
AnswerMapper $answerMapper,
IRootFolder $storage,
IConfig $config,
IDateTimeFormatter $dateTimeFormatter,
IL10N $l10n,
LoggerInterface $logger,
IUserManager $userManager,
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/Api/ApiV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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","",""'
]
];
}
Expand Down
32 changes: 14 additions & 18 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,9 +73,6 @@ class SubmissionServiceTest extends TestCase {
/** @var IConfig|MockObject */
private $config;

/** @var IDateTimeFormatter|MockObject */
private $dateTimeFormatter;

/** @var IL10N|MockObject */
private $l10n;

Expand All @@ -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);
Expand All @@ -121,7 +116,6 @@ public function setUp(): void {
$this->answerMapper,
$this->storage,
$this->config,
$this->dateTimeFormatter,
$this->l10n,
$this->logger,
$this->userManager,
Expand Down Expand Up @@ -285,6 +279,7 @@ public function dataGetSubmissionsCsv() {
[
'id' => 1,
'userId' => 'user1',
'timestamp' => 123456789,
'answers' => [
['questionId' => 1, 'text' => 'Q1A1'],
['questionId' => 2, 'text' => 'Q2A1']
Expand All @@ -293,6 +288,7 @@ public function dataGetSubmissionsCsv() {
[
'id' => 2,
'userId' => 'user2',
'timestamp' => 123456789,
'answers' => [
['questionId' => 1, 'text' => 'Q1A2'],
['questionId' => 2, 'text' => 'Q2A2']
Expand All @@ -302,8 +298,8 @@ public function dataGetSubmissionsCsv() {
// 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' => [
Expand All @@ -316,6 +312,7 @@ public function dataGetSubmissionsCsv() {
[
'id' => 1,
'userId' => 'user1',
'timestamp' => 123456789,
'answers' => [
['questionId' => 1, 'text' => 'Q1A1'],
['questionId' => 1, 'text' => 'Q1A2'],
Expand All @@ -326,7 +323,7 @@ public function dataGetSubmissionsCsv() {
// 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' => [
Expand All @@ -339,6 +336,7 @@ public function dataGetSubmissionsCsv() {
[
'id' => 1,
'userId' => 'anon-user-xyz',
'timestamp' => 123456789,
'answers' => [
['questionId' => 1, 'text' => 'Q1A1'],
]
Expand All @@ -347,7 +345,7 @@ public function dataGetSubmissionsCsv() {
// 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' => [
Expand All @@ -362,6 +360,7 @@ public function dataGetSubmissionsCsv() {
[
'id' => 1,
'userId' => 'user1',
'timestamp' => 123456789,
'answers' => [
['questionId' => 2, 'text' => 'Q2A1']
]
Expand All @@ -370,7 +369,7 @@ public function dataGetSubmissionsCsv() {
// 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 */
Expand All @@ -395,6 +394,7 @@ public function dataGetSubmissionsCsv() {
[
'id' => 1,
'userId' => 'anon-user-xyz',
'timestamp' => 123456789,
'answers' => [
['questionId' => 1, 'text' => 'Q1A1'],
]
Expand All @@ -403,7 +403,7 @@ public function dataGetSubmissionsCsv() {
// 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"
'
],
];
Expand Down Expand Up @@ -438,6 +438,7 @@ private function setUpSimpleCsvTest(): string {
[
'id' => 1,
'userId' => 'user1',
'timestamp' => 123456789,
'answers' => [
['questionId' => 1, 'text' => 'Q1A1']
]
Expand All @@ -446,7 +447,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","1973-11-29T22:33:09+01:00","Q1A1"
'
);
}
Expand Down Expand Up @@ -510,11 +511,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
Expand Down

0 comments on commit b6597d1

Please sign in to comment.