Skip to content

Commit c7889b3

Browse files
committed
Add pagination for submissions on a backend (try add integration tests)
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent 64c33a2 commit c7889b3

3 files changed

Lines changed: 185 additions & 123 deletions

File tree

tests/Integration/DB/SharedFormsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
9-
namespace OCA\Forms\Tests\Integration\Api;
9+
namespace OCA\Forms\Tests\Integration\Db;
1010

1111
use OCA\Forms\AppInfo\Application;
1212
use OCA\Forms\Constants;
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCA\Forms\Tests\Integration\DB;
10+
11+
use OCA\Forms\Db\SubmissionMapper;
12+
use OCA\Forms\Tests\Integration\IntegrationBase;
13+
use OCP\IDBConnection;
14+
15+
/**
16+
* @group DB
17+
*/
18+
class SubmissionMapperTest extends IntegrationBase {
19+
/** @var SubmissionMapper */
20+
private $submissionMapper;
21+
22+
protected array $users = [
23+
'test' => 'Test user',
24+
'user1' => 'User One',
25+
'user2' => 'User Two',
26+
];
27+
28+
private function setTestForms() {
29+
$this->testForms = [
30+
[
31+
'hash' => 'test_form_1',
32+
'title' => 'Test Form 1',
33+
'description' => 'Form for submission testing',
34+
'owner_id' => 'test',
35+
'access_enum' => 0,
36+
'created' => 12345,
37+
'expires' => 0,
38+
'state' => 0,
39+
'is_anonymous' => false,
40+
'submit_multiple' => true,
41+
'show_expiration' => false,
42+
'last_updated' => 123456789,
43+
'submission_message' => '',
44+
'file_id' => null,
45+
'file_format' => null,
46+
'questions' => [
47+
[
48+
'type' => 'short',
49+
'text' => 'First Question?',
50+
'isRequired' => true,
51+
'name' => '',
52+
'order' => 1,
53+
'options' => [],
54+
'accept' => [],
55+
'description' => 'Please answer this.',
56+
'extraSettings' => []
57+
]
58+
],
59+
'shares' => [],
60+
'submissions' => [
61+
[
62+
'userId' => 'user1',
63+
'timestamp' => 100000,
64+
'answers' => [
65+
[
66+
'questionIndex' => 0,
67+
'text' => 'Answer 1'
68+
]
69+
]
70+
],
71+
[
72+
'userId' => 'user1',
73+
'timestamp' => 100001,
74+
'answers' => [
75+
[
76+
'questionIndex' => 0,
77+
'text' => 'Answer 2'
78+
]
79+
]
80+
],
81+
[
82+
'userId' => 'user2',
83+
'timestamp' => 100002,
84+
'answers' => [
85+
[
86+
'questionIndex' => 0,
87+
'text' => 'Search term'
88+
]
89+
]
90+
]
91+
]
92+
],
93+
[
94+
'hash' => 'test_form_2',
95+
'title' => 'Test Form 2',
96+
'description' => 'Empty form',
97+
'owner_id' => 'test',
98+
'access_enum' => 0,
99+
'created' => 12345,
100+
'expires' => 0,
101+
'state' => 0,
102+
'is_anonymous' => false,
103+
'submit_multiple' => false,
104+
'show_expiration' => false,
105+
'last_updated' => 123456789,
106+
'submission_message' => '',
107+
'file_id' => null,
108+
'file_format' => null,
109+
'questions' => [],
110+
'shares' => [],
111+
'submissions' => []
112+
]
113+
];
114+
}
115+
116+
public function setUp(): void {
117+
$this->setTestForms();
118+
parent::setUp();
119+
120+
$db = \OCP\Server::get(IDBConnection::class);
121+
$answerMapper = \OCP\Server::get(\OCA\Forms\Db\AnswerMapper::class);
122+
$this->submissionMapper = new SubmissionMapper($db, $answerMapper);
123+
}
124+
125+
public function testFindByFormBasic(): void {
126+
$submissions = $this->submissionMapper->findByForm($this->testForms[0]['id']);
127+
128+
$this->assertCount(3, $submissions);
129+
$this->assertEquals('user2', $submissions[0]->getUserId());
130+
$this->assertEquals(100002, $submissions[0]->getTimestamp());
131+
}
132+
133+
public function testFindByFormWithUser(): void {
134+
$submissions = $this->submissionMapper->findByForm($this->testForms[0]['id'], 'user1');
135+
136+
$this->assertCount(2, $submissions);
137+
foreach ($submissions as $submission) {
138+
$this->assertEquals('user1', $submission->getUserId());
139+
}
140+
}
141+
142+
public function testFindByFormWithSearchQuery(): void {
143+
$submissions = $this->submissionMapper->findByForm($this->testForms[0]['id'], null, 'Search term');
144+
145+
$this->assertCount(1, $submissions);
146+
$this->assertEquals('user2', $submissions[0]->getUserId());
147+
}
148+
149+
public function testFindByFormWithLimit(): void {
150+
$submissions = $this->submissionMapper->findByForm($this->testForms[0]['id'], null, null, 2);
151+
152+
$this->assertCount(2, $submissions);
153+
}
154+
155+
public function testFindByFormWithOffset(): void {
156+
$submissions = $this->submissionMapper->findByForm($this->testForms[0]['id'], null, null, null, 1);
157+
158+
$this->assertCount(2, $submissions);
159+
}
160+
161+
public function testCountSubmissionsBasic(): void {
162+
$count = $this->submissionMapper->countSubmissions($this->testForms[0]['id']);
163+
164+
$this->assertEquals(3, $count);
165+
}
166+
167+
public function testCountSubmissionsWithUser(): void {
168+
$count = $this->submissionMapper->countSubmissions($this->testForms[0]['id'], 'user1');
169+
170+
$this->assertEquals(2, $count);
171+
}
172+
173+
public function testCountSubmissionsWithSearch(): void {
174+
$count = $this->submissionMapper->countSubmissions($this->testForms[0]['id'], null, 'Search term');
175+
176+
$this->assertEquals(1, $count);
177+
}
178+
179+
public function testCountSubmissionsEmptyForm(): void {
180+
$count = $this->submissionMapper->countSubmissions($this->testForms[1]['id']);
181+
182+
$this->assertEquals(0, $count);
183+
}
184+
}

tests/Unit/Db/SubmissionMapperTest.php

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)