|
| 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 | +}; |
0 commit comments