Skip to content

Commit 7cd561a

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

3 files changed

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

tests/Unit/Db/SubmissionMapperTest.php

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

0 commit comments

Comments
 (0)