Skip to content

Commit ffd258b

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 ffd258b

3 files changed

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

tests/Unit/Db/SubmissionMapperTest.php

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

0 commit comments

Comments
 (0)