Skip to content

Commit

Permalink
fix(tests): Add database test for shared forms and reuse integration …
Browse files Browse the repository at this point in the history
…test class

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Apr 15, 2024
1 parent 72d2125 commit afe24e0
Show file tree
Hide file tree
Showing 3 changed files with 396 additions and 158 deletions.
165 changes: 7 additions & 158 deletions tests/Integration/Api/ApiV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,15 @@
use GuzzleHttp\Exception\ClientException;

use OCA\Forms\Constants;
use OCA\Forms\Db\FormMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Test\TestCase;
use OCA\Forms\Tests\Integration\IntegrationBase;

/**
* @group DB
*/
class ApiV2Test extends TestCase {
class ApiV2Test extends IntegrationBase {
/** @var GuzzleHttp\Client */
private $http;

/** @var FormMapper */
private $formMapper;

/** @var Array */
private $testForms;

/**
* Store Test Forms Array.
* Necessary as function due to object type-casting.
Expand Down Expand Up @@ -234,115 +226,13 @@ private function setTestForms() {
* Writing testforms into db, preparing http request
*/
public function setUp(): void {
parent::setUp();
$userManager = \OC::$server->getUserManager();
$user = $userManager->get('test');
if ($user === null) {
$user = $userManager->createUser('test', 'test');
}
$user->setDisplayName('Test Displayname');

// We also have user2 and user3 but those accounts are "deleted"
$user = $userManager->get("user1");
if ($user === null) {
$user = $userManager->createUser("user1", "user1");
}
$user->setDisplayName("User No. 1");

$this->setTestForms();
$this->users = [
'test' => 'Test Displayname',
'user1' => 'User No. 1',
];

$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();

// Write our test forms into db
foreach ($this->testForms as $index => $form) {
$qb->insert('forms_v2_forms')
->values([
'hash' => $qb->createNamedParameter($form['hash'], IQueryBuilder::PARAM_STR),
'title' => $qb->createNamedParameter($form['title'], IQueryBuilder::PARAM_STR),
'description' => $qb->createNamedParameter($form['description'], IQueryBuilder::PARAM_STR),
'owner_id' => $qb->createNamedParameter($form['owner_id'], IQueryBuilder::PARAM_STR),
'access_enum' => $qb->createNamedParameter($form['access_enum'], IQueryBuilder::PARAM_INT),
'created' => $qb->createNamedParameter($form['created'], IQueryBuilder::PARAM_INT),
'expires' => $qb->createNamedParameter($form['expires'], IQueryBuilder::PARAM_INT),
'state' => $qb->createNamedParameter($form['state'], IQueryBuilder::PARAM_INT),
'is_anonymous' => $qb->createNamedParameter($form['is_anonymous'], IQueryBuilder::PARAM_BOOL),
'submit_multiple' => $qb->createNamedParameter($form['submit_multiple'], IQueryBuilder::PARAM_BOOL),
'show_expiration' => $qb->createNamedParameter($form['show_expiration'], IQueryBuilder::PARAM_BOOL),
'last_updated' => $qb->createNamedParameter($form['last_updated'], IQueryBuilder::PARAM_INT),
'submission_message' => $qb->createNamedParameter($form['submission_message'], IQueryBuilder::PARAM_STR),
'file_id' => $qb->createNamedParameter($form['file_id'], IQueryBuilder::PARAM_INT),
'file_format' => $qb->createNamedParameter($form['file_format'], IQueryBuilder::PARAM_STR),
]);
$qb->executeStatement();
$formId = $qb->getLastInsertId();
$this->testForms[$index]['id'] = $formId;

// Insert Questions into DB
foreach ($form['questions'] as $qIndex => $question) {
$qb->insert('forms_v2_questions')
->values([
'form_id' => $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT),
'order' => $qb->createNamedParameter($question['order'], IQueryBuilder::PARAM_INT),
'type' => $qb->createNamedParameter($question['type'], IQueryBuilder::PARAM_STR),
'is_required' => $qb->createNamedParameter($question['isRequired'], IQueryBuilder::PARAM_BOOL),
'text' => $qb->createNamedParameter($question['text'], IQueryBuilder::PARAM_STR),
'name' => $qb->createNamedParameter($question['name'], IQueryBuilder::PARAM_STR),
'description' => $qb->createNamedParameter($question['description'], IQueryBuilder::PARAM_STR),
'extra_settings_json' => $qb->createNamedParameter(json_encode($question['extraSettings']), IQueryBuilder::PARAM_STR),
]);
$qb->executeStatement();
$questionId = $qb->getLastInsertId();
$this->testForms[$index]['questions'][$qIndex]['id'] = $questionId;

// Insert Options into DB
foreach ($question['options'] as $oIndex => $option) {
$qb->insert('forms_v2_options')
->values([
'question_id' => $qb->createNamedParameter($questionId, IQueryBuilder::PARAM_INT),
'text' => $qb->createNamedParameter($option['text'], IQueryBuilder::PARAM_STR)
]);
$qb->executeStatement();
$this->testForms[$index]['questions'][$qIndex]['options'][$oIndex]['id'] = $qb->getLastInsertId();
}
}

// Insert Shares into DB
foreach ($form['shares'] as $sIndex => $share) {
$qb->insert('forms_v2_shares')
->values([
'form_id' => $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT),
'share_type' => $qb->createNamedParameter($share['shareType'], IQueryBuilder::PARAM_STR),
'share_with' => $qb->createNamedParameter($share['shareWith'], IQueryBuilder::PARAM_STR),
'permissions_json' => $qb->createNamedParameter(json_encode($share['permissions'] ?? null), IQueryBuilder::PARAM_STR),
]);
$qb->executeStatement();
$this->testForms[$index]['shares'][$sIndex]['id'] = $qb->getLastInsertId();
}

// Insert Submissions into DB
foreach ($form['submissions'] as $suIndex => $submission) {
$qb->insert('forms_v2_submissions')
->values([
'form_id' => $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT),
'user_id' => $qb->createNamedParameter($submission['userId'], IQueryBuilder::PARAM_STR),
'timestamp' => $qb->createNamedParameter($submission['timestamp'], IQueryBuilder::PARAM_INT)
]);
$qb->executeStatement();
$submissionId = $qb->getLastInsertId();
$this->testForms[$index]['submissions'][$suIndex]['id'] = $submissionId;

foreach ($submission['answers'] as $aIndex => $answer) {
$qb->insert('forms_v2_answers')
->values([
'submission_id' => $qb->createNamedParameter($submissionId, IQueryBuilder::PARAM_INT),
'question_id' => $qb->createNamedParameter($this->testForms[$index]['questions'][$answer['questionIndex']]['id'], IQueryBuilder::PARAM_INT),
'text' => $qb->createNamedParameter($answer['text'], IQueryBuilder::PARAM_STR)
]);
$qb->executeStatement();
$this->testForms[$index]['submissions'][$suIndex]['answers'][$aIndex]['id'] = $qb->getLastInsertId();
}
}
}
parent::setUp();

// Set up http Client
$this->http = new Client([
Expand All @@ -355,48 +245,7 @@ public function setUp(): void {
]);
}

/** Clean up database from testforms */
public function tearDown(): void {
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();

foreach ($this->testForms as $form) {
$qb->delete('forms_v2_forms')
->where($qb->expr()->eq('id', $qb->createNamedParameter($form['id'], IQueryBuilder::PARAM_INT)));
$qb->executeStatement();

foreach ($form['questions'] as $question) {
$qb->delete('forms_v2_questions')
->where($qb->expr()->eq('id', $qb->createNamedParameter($question['id'], IQueryBuilder::PARAM_INT)));
$qb->executeStatement();

foreach ($question['options'] as $option) {
$qb->delete('forms_v2_options')
->where($qb->expr()->eq('id', $qb->createNamedParameter($option['id'], IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}
}

foreach ($form['shares'] as $share) {
$qb->delete('forms_v2_shares')
->where($qb->expr()->eq('id', $qb->createNamedParameter($share['id'], IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}

if (isset($form['submissions'])) {
foreach ($form['submissions'] as $submission) {
$qb->delete('forms_v2_submissions')
->where($qb->expr()->eq('id', $qb->createNamedParameter($submission['id'], IQueryBuilder::PARAM_INT)));
$qb->executeStatement();

foreach ($submission['answers'] as $answer) {
$qb->delete('forms_v2_answers')
->where($qb->expr()->eq('id', $qb->createNamedParameter($answer['id'], IQueryBuilder::PARAM_INT)));
$qb->executeStatement();
}
}
}
}

parent::tearDown();
}

Expand Down
192 changes: 192 additions & 0 deletions tests/Integration/DB/SharedFormsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Ferdinand Thiessen <[email protected]>
*
* @author Ferdinand Thiessen <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Forms\Tests\Integration\Api;

use OCA\Forms\Constants;
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Tests\Integration\IntegrationBase;

/**
* @group DB
*/
class SharedFormsTest extends IntegrationBase {

public function setUp(): void {
$this->users = ['test' => 'Test user', 'user1' => 'User no. 1'];

$this->testForms = [
[
'hash' => 'aaaa',
'title' => 'Title of a Form',
'description' => 'Just a simple form.',
'owner_id' => 'test',
'access_enum' => Constants::FORM_ACCESS_NOPUBLICSHARE,
'created' => 12345,
'expires' => 0,
'state' => 0,
'is_anonymous' => false,
'submit_multiple' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => 'Back to website',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [
[
'shareType' => 0,
'shareWith' => 'user1',
'permissions' => ['submit'],
],
],
],
[
'hash' => 'bbbb',
'title' => 'Title of a public Form',
'description' => '',
'owner_id' => 'test',
'access_enum' => Constants::FORM_ACCESS_SHOWTOALLUSERS,
'created' => 12345,
'expires' => 0,
'state' => 0,
'is_anonymous' => false,
'submit_multiple' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => 'Back to website',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [],
],
[
'hash' => 'cccc',
'title' => 'Title of a public invisible Form',
'description' => '',
'owner_id' => 'test',
'access_enum' => Constants::FORM_ACCESS_PERMITALLUSERS,
'created' => 12345,
'expires' => 0,
'state' => 0,
'is_anonymous' => false,
'submit_multiple' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => 'Back to website',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [],
],
[
'hash' => 'dddd',
'title' => 'Shown AND shared form',
'description' => 'Just a simple form.',
'owner_id' => 'test',
'access_enum' => Constants::FORM_ACCESS_SHOWTOALLUSERS,
'created' => 12345,
'expires' => 0,
'state' => 0,
'is_anonymous' => false,
'submit_multiple' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => 'Back to website',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [
[
'shareType' => 0,
'shareWith' => 'user1',
'permissions' => ['submit'],
],
// two shares to the same user - valid but should result in just one result entry
[
'shareType' => 0,
'shareWith' => 'user1',
'permissions' => ['submit'],
],
],
],
[
'hash' => 'eeee',
'title' => 'Unrelated form',
'description' => 'Just a simple form.',
'owner_id' => 'test',
'access_enum' => Constants::FORM_ACCESS_NOPUBLICSHARE,
'created' => 12345,
'expires' => 0,
'state' => 0,
'is_anonymous' => false,
'submit_multiple' => false,
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => 'Back to website',
'file_id' => null,
'file_format' => null,
'questions' => [],
'shares' => [
[
'shareType' => 0,
'shareWith' => 'user2',
'permissions' => ['submit'],
],
],
],
];

parent::setUp();
}

/**
* Test that only shared forms that are shown to user are listed
*/
public function testShownSharedForms() {
$formMapper = \OCP\Server::get(FormMapper::class);
$forms = $formMapper->findSharedForms('user1');

$this->assertEquals(3, count($forms));
$this->assertEqualsCanonicalizing(
['aaaa', 'bbbb', 'dddd'],
array_map(fn ($form) => $form->read()['hash'], $forms),
);
}

/**
* Test that all forms shared and public permit are shown without the `filterShown` parameter
*/
public function testPublicSharedForms() {
$formMapper = \OCP\Server::get(FormMapper::class);
$forms = $formMapper->findSharedForms('user1', filterShown: false);

$this->assertEquals(4, count($forms));
$this->assertEqualsCanonicalizing(
['aaaa', 'bbbb', 'cccc', 'dddd'],
array_map(fn ($form) => $form->read()['hash'], $forms),
);
}
}
Loading

0 comments on commit afe24e0

Please sign in to comment.