Skip to content

Commit c3092e1

Browse files
committed
fixup
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 2dbbee2 commit c3092e1

File tree

3 files changed

+150
-4
lines changed

3 files changed

+150
-4
lines changed

tests/Integration/Api/ApiV3Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function setUp(): void {
245245

246246
// Set up http Client
247247
$this->http = new Client([
248-
'base_uri' => 'http://localhost:8080/ocs/v2.php/apps/forms/',
248+
'base_uri' => 'http://localhost:80/ocs/v2.php/apps/forms/',
249249
'auth' => ['test', 'test'],
250250
'headers' => [
251251
'OCS-ApiRequest' => 'true',
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
namespace OCA\Forms\Tests\Integration\Api;
9+
10+
use GuzzleHttp\Client;
11+
12+
use OCA\Forms\Tests\Integration\IntegrationBase;
13+
14+
/**
15+
* This tests that the API respects all admin settings
16+
* @group DB
17+
*/
18+
class RespectAdminSettingsTest extends IntegrationBase {
19+
/** @var GuzzleHttp\Client */
20+
private $http;
21+
22+
protected array $users = [
23+
'test' => 'Test user',
24+
];
25+
26+
/**
27+
* Store Test Forms Array.
28+
* Necessary as function due to object type-casting.
29+
*/
30+
private function setTestForms() {
31+
$this->testForms = [
32+
[
33+
'hash' => 'abcdefghij123456',
34+
'title' => 'Title of a test Form',
35+
'description' => '',
36+
'owner_id' => 'test',
37+
'access_enum' => 0,
38+
'created' => 12345,
39+
'expires' => 0,
40+
'state' => 0,
41+
'is_anonymous' => false,
42+
'submit_multiple' => false,
43+
'show_expiration' => false,
44+
'last_updated' => 123456789,
45+
'submission_message' => '',
46+
'file_id' => null,
47+
'file_format' => null,
48+
'questions' => [
49+
[
50+
'type' => 'short',
51+
'text' => 'Third Question?',
52+
'description' => '',
53+
'isRequired' => false,
54+
'name' => '',
55+
'order' => 1,
56+
'options' => [],
57+
'accept' => [],
58+
'extraSettings' => []
59+
],
60+
],
61+
'shares' => [
62+
[
63+
'shareType' => 0,
64+
'shareWith' => 'user2',
65+
],
66+
],
67+
'submissions' => []
68+
],
69+
];
70+
}
71+
72+
/**
73+
* Set up test environment.
74+
* Writing testforms into db, preparing http request
75+
*/
76+
public function setUp(): void {
77+
$this->setTestForms();
78+
$this->users = [
79+
'test' => 'Test Displayname',
80+
'user1' => 'User No. 1',
81+
];
82+
83+
parent::setUp();
84+
85+
// Set up http Client
86+
$this->http = new Client([
87+
'base_uri' => 'http://localhost/ocs/v2.php/apps/forms/',
88+
'auth' => ['test', 'test'],
89+
'headers' => [
90+
'OCS-ApiRequest' => 'true',
91+
'Accept' => 'application/json'
92+
],
93+
]);
94+
}
95+
96+
public function tearDown(): void {
97+
parent::tearDown();
98+
}
99+
100+
// Small Wrapper for OCS-Response
101+
private function OcsResponse2Data($resp) {
102+
$arr = json_decode($resp->getBody()->getContents(), true);
103+
return $arr['ocs']['data'];
104+
}
105+
106+
// Unset Id, as we can not control it on the tests.
107+
private function arrayUnsetId(array $arr): array {
108+
foreach ($arr as $index => $elem) {
109+
unset($arr[$index]['id']);
110+
}
111+
return $arr;
112+
}
113+
114+
/**
115+
* Allow to update form if there are no admin settings
116+
*/
117+
public function testAllowUpdate(): void {
118+
$resp = $this->http->request(
119+
'PATCH',
120+
"api/v3/forms/{$this->testForms[0]['id']}",
121+
[ 'keyValuePairs' => ['access' => ['permitAllUsers' => true, 'showToAllUsers' => true]]],
122+
);
123+
$this->assertEquals(200, $resp->getStatusCode());
124+
125+
$resp = $this->http->request(
126+
'GET',
127+
"api/v3/forms/{$this->testForms[0]['id']}",
128+
);
129+
$data = $this->OcsResponse2Data($resp);
130+
$this->assertEquals(200, $resp->getStatusCode());
131+
$this->assertEquals($this->testForms[0], $data);
132+
}
133+
};

tests/Integration/IntegrationBase.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
*/
88
namespace OCA\Forms\Tests\Integration;
99

10+
use OCA\Forms\AppInfo\Application;
1011
use OCP\DB\QueryBuilder\IQueryBuilder;
12+
use OCP\IConfig;
13+
use OCP\IDBConnection;
14+
use OCP\IUserManager;
1115
use Test\TestCase;
1216

1317
/**
@@ -30,7 +34,10 @@ class IntegrationBase extends TestCase {
3034
public function setUp(): void {
3135
parent::setUp();
3236

33-
$userManager = \OC::$server->getUserManager();
37+
$config = \OCP\Server::get(IConfig::class);
38+
$config->deleteAppValues(Application::APP_ID);
39+
40+
$userManager = \OCP\Server::get(IUserManager::class);
3441
foreach ($this->users as $userId => $displayName) {
3542
$user = $userManager->get($userId);
3643
if ($user === null) {
@@ -39,7 +46,7 @@ public function setUp(): void {
3946
$user->setDisplayName($displayName);
4047
}
4148

42-
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
49+
$qb = \OCP\Server::get(IDBConnection::class)->getQueryBuilder();
4350

4451
// Write our test forms into db
4552
foreach ($this->testForms as $index => $form) {
@@ -136,38 +143,44 @@ public function setUp(): void {
136143

137144
/** Clean up database from testforms */
138145
public function tearDown(): void {
139-
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
146+
$db = \OCP\Server::get(IDBConnection::class);
140147

141148
foreach ($this->testForms as $form) {
149+
$qb = $db->getQueryBuilder();
142150
$qb->delete('forms_v2_forms')
143151
->where($qb->expr()->eq('id', $qb->createNamedParameter($form['id'], IQueryBuilder::PARAM_INT)));
144152
$qb->executeStatement();
145153

146154
foreach ($form['questions'] as $question) {
155+
$qb = $db->getQueryBuilder();
147156
$qb->delete('forms_v2_questions')
148157
->where($qb->expr()->eq('id', $qb->createNamedParameter($question['id'], IQueryBuilder::PARAM_INT)));
149158
$qb->executeStatement();
150159

151160
foreach ($question['options'] as $option) {
161+
$qb = $db->getQueryBuilder();
152162
$qb->delete('forms_v2_options')
153163
->where($qb->expr()->eq('id', $qb->createNamedParameter($option['id'], IQueryBuilder::PARAM_INT)));
154164
$qb->executeStatement();
155165
}
156166
}
157167

158168
foreach ($form['shares'] as $share) {
169+
$qb = $db->getQueryBuilder();
159170
$qb->delete('forms_v2_shares')
160171
->where($qb->expr()->eq('id', $qb->createNamedParameter($share['id'], IQueryBuilder::PARAM_INT)));
161172
$qb->executeStatement();
162173
}
163174

164175
if (isset($form['submissions'])) {
165176
foreach ($form['submissions'] as $submission) {
177+
$qb = $db->getQueryBuilder();
166178
$qb->delete('forms_v2_submissions')
167179
->where($qb->expr()->eq('id', $qb->createNamedParameter($submission['id'], IQueryBuilder::PARAM_INT)));
168180
$qb->executeStatement();
169181

170182
foreach ($submission['answers'] as $answer) {
183+
$qb = $db->getQueryBuilder();
171184
$qb->delete('forms_v2_answers')
172185
->where($qb->expr()->eq('id', $qb->createNamedParameter($answer['id'], IQueryBuilder::PARAM_INT)));
173186
$qb->executeStatement();

0 commit comments

Comments
 (0)