Skip to content

Commit

Permalink
pipeline fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Myakshin <molodchick@gmail.com>
  • Loading branch information
Koc committed Jan 3, 2024
1 parent 197d47a commit cd5cdd5
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/phpunit-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ jobs:
run: php -S localhost:8080 &

- name: PHPUnit integration
env:
NC_datadirectory: ${{ env.GITHUB_WORKSPACE }}/data
# Only run if phpunit integration config file exists
if: steps.check_integration.outcome == 'success'
working-directory: apps/${{ env.APP_NAME }}
Expand Down
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@
// Submissions linking with file in cloud
[
'name' => 'api#linkFile',
'url' => '/api/{apiVersion}/link/link/{fileFormat}',
'url' => '/api/{apiVersion}/form/link/{fileFormat}',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v2(\.3)?',
Expand Down
5 changes: 3 additions & 2 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use OCA\Forms\Db\QuestionMapper;
use OCA\Forms\Db\SubmissionMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\OCS\OCSException;;
use OCP\AppFramework\OCS\OCSException;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
Expand All @@ -48,12 +48,13 @@
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use Psr\Log\LoggerInterface;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv;

use Psr\Log\LoggerInterface;

class SubmissionService {
/** @var FormMapper */
private $formMapper;
Expand Down
33 changes: 24 additions & 9 deletions tests/Integration/Api/ApiV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use OCA\Forms\Db\FormMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IRootFolder;
use Test\TestCase;

/**
Expand Down Expand Up @@ -68,8 +69,8 @@ private function setTestForms() {
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => 'Back to website',
'file_id' => null,
'file_format' => null,
'file_id' => 'to be inserted in setUp method',
'file_format' => 'csv',
'questions' => [
[
'type' => 'short',
Expand Down Expand Up @@ -173,8 +174,8 @@ private function setTestForms() {
'show_expiration' => false,
'last_updated' => 123456789,
'submission_message' => '',
'file_id' => 12,
'file_format' => 'csv',
'file_id' => null,
'file_format' => null,
'questions' => [
[
'type' => 'short',
Expand Down Expand Up @@ -211,6 +212,14 @@ public function setUp(): void {
}
$user->setDisplayName('Test Displayname');

/** @var IRootFolder $root */
$root = \OC::$server->get(IRootFolder::class);
$userFolder = $root->getUserFolder($user->getUID());
$userFolder->newFolder('foo');
$linkedFile = $userFolder->newFile('foo/bar.csv');

$this->testForms[0]['file_id'] = $linkedFile->getId();

// We also have user2 and user3 but those accounts are "deleted"
$user = $userManager->get("user1");
if ($user === null) {
Expand Down Expand Up @@ -1284,17 +1293,23 @@ public function testExportSubmissions(string $expected) {
$this->assertEquals($arr_txt_expected, $arr_txt_data);
}

public function testLinkedFileRespondsWithHttp404WhenFormNotLinkedYet() {
$resp = $this->http->request('GET', "api/v2.3/form/linkedFile/{$this->testForms[1]['hash']}");

$this->assertEquals(404, $resp->getStatusCode());
}

public function testLinkedFile() {
$resp = $this->http->request('GET', "api/v2.2/submissions/linkedFile/{$this->testForms[0]['hash']}");
$resp = $this->http->request('GET', "api/v2.3/form/linkedFile/{$this->testForms[0]['hash']}");

$this->assertEquals(200, $resp->getStatusCode());
$data = $this->OcsResponse2Data($resp);

$this->assertEquals(['fileId' => null, 'path' => null, 'fileFormat' => null], $data);
$this->assertEquals(['fileId' => $this->testForms[0]['file_id'], 'path' => 'foo', 'fileFormat' => 'csv'], $data);
}

public function testLinkFile() {
$resp = $this->http->request('POST', 'api/v2.2/submissions/link/csv', [
$resp = $this->http->request('POST', 'api/v2.3/form/link/csv', [
'json' => [
'hash' => $this->testForms[0]['hash'],
'path' => ''
Expand All @@ -1307,9 +1322,9 @@ public function testLinkFile() {
}

public function testUnlinkFile() {
$resp = $this->http->request('POST', 'api/v2.2/submissions/unlink', [
$resp = $this->http->request('POST', 'api/v2.3/form/unlink', [
'json' => [
'hash' => $this->testForms[1]['hash'],
'hash' => $this->testForms[0]['hash'],
'path' => ''
]]
);
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function testExportSubmissions() {
->willReturn($csv);

$fileName = 'foo.csv';
$this->submissionService->expects($this->once())
$this->formsService->expects($this->once())
->method('getFileName')
->with($form, 'csv')
->willReturn($fileName);
Expand Down Expand Up @@ -362,7 +362,7 @@ public function testGetLinkedFile() {
->with('hash')
->willReturn($form);

$this->submissionService->expects($this->once())
$this->formsService->expects($this->once())
->method('getFilePath')
->with($form)
->willReturn('foo/bar');
Expand Down Expand Up @@ -744,7 +744,7 @@ public function testInsertSubmission_answers() {
->method('notifyNewSubmission')
->with($form, 'currentUser');

$this->submissionService->expects($this->once())
$this->formsService->expects($this->once())
->method('getFilePath')
->willReturn('foo/bar');

Expand Down
35 changes: 31 additions & 4 deletions tests/Unit/Service/FormsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
use OCA\Forms\Service\CirclesService;
use OCA\Forms\Service\ConfigService;
use OCA\Forms\Service\FormsService;
use OCP\Files\IRootFolder;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
Expand Down Expand Up @@ -93,6 +95,12 @@ class FormsServiceTest extends TestCase {
/** @var CirclesService|MockObject */
private $circlesService;

/** @var IRootFolder|MockObject */
private $storage;

/** @var IL10N|MockObject */
private $l10n;

public function setUp(): void {
parent::setUp();
$this->activityManager = $this->createMock(ActivityManager::class);
Expand All @@ -118,6 +126,15 @@ public function setUp(): void {
->method('getUser')
->willReturn($user);

$this->storage = $this->createMock(IRootFolder::class);

$this->l10n = $this->createMock(IL10N::class);
$this->l10n->expects($this->any())
->method('t')
->will($this->returnCallback(function (string $identity) {
return $identity;
}));

$this->formsService = new FormsService(
$userSession,
$this->activityManager,
Expand All @@ -131,7 +148,9 @@ public function setUp(): void {
$this->logger,
$this->userManager,
$this->secureRandom,
$this->circlesService
$this->circlesService,
$this->storage,
$this->l10n
);
}

Expand Down Expand Up @@ -594,7 +613,9 @@ public function testGetPermissions_NotLoggedIn() {
$this->logger,
$this->userManager,
$this->secureRandom,
$this->circlesService
$this->circlesService,
$this->storage,
$this->l10n
);

$form = new Form();
Expand Down Expand Up @@ -831,7 +852,9 @@ public function testPublicCanSubmit() {
$this->logger,
$this->userManager,
$this->secureRandom,
$this->circlesService
$this->circlesService,
$this->storage,
$this->l10n
);

$this->assertEquals(true, $formsService->canSubmit($form));
Expand Down Expand Up @@ -991,7 +1014,9 @@ public function testHasUserAccess_NotLoggedIn() {
$this->logger,
$this->userManager,
$this->secureRandom,
$this->circlesService
$this->circlesService,
$this->storage,
$this->l10n
);

$form = new Form();
Expand Down Expand Up @@ -1408,6 +1433,8 @@ public function testNotifyNewSubmission($shares, $shareNotifications) {
$this->userManager,
$this->secureRandom,
$this->circlesService,
$this->storage,
$this->l10n
])
->getMock();

Expand Down
21 changes: 16 additions & 5 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\Forms\Db\QuestionMapper;
use OCA\Forms\Db\Submission;
use OCA\Forms\Db\SubmissionMapper;
use OCA\Forms\Service\FormsService;
use OCA\Forms\Service\SubmissionService;

use OCP\Files\File;
Expand Down Expand Up @@ -89,6 +90,9 @@ class SubmissionServiceTest extends TestCase {
/** @var ITempManager|MockObject */
private $tempManager;

/** @var FormsService|MockObject */
private $formsService;

public function setUp(): void {
parent::setUp();
$this->formMapper = $this->createMock(FormMapper::class);
Expand Down Expand Up @@ -118,6 +122,8 @@ public function setUp(): void {
return $identity;
}));

$this->formsService = $this->createMock(FormsService::class);

$this->submissionService = new SubmissionService(
$this->formMapper,
$this->questionMapper,
Expand All @@ -130,7 +136,8 @@ public function setUp(): void {
$this->userManager,
$userSession,
$this->mailer,
$this->tempManager
$this->tempManager,
$this->formsService
);
}

Expand Down Expand Up @@ -204,11 +211,10 @@ public function testGetSubmissions() {

public function dataWriteFileToCloud() {
return [
'rootFolder' => ['Some nice Form Title', '', Folder::class, '', 'Some nice Form Title (responses).csv', false],
'subFolder' => ['Some nice Form Title', '/folder path', Folder::class, '', 'Some nice Form Title (responses).csv', false],
'nonCsv-file' => ['Some nice Form Title', '/fileName.txt', File::class, 'txt', 'Some nice Form Title (responses).csv', false],
'rootFolder' => ['Some nice Form Title', '', Folder::class, 'csv', 'Some nice Form Title (responses).csv', false],
'subFolder' => ['Some nice Form Title', '/folder path', Folder::class, 'csv', 'Some nice Form Title (responses).csv', false],
'csv-file' => ['Some nice Form Title', '/fileName.csv', File::class, 'csv', 'fileName.csv', true],
'invalidFormTitle' => ['Form 1 / 2', '', Folder::class, '', 'Form 1 - 2 (responses).csv', false],
'invalidFormTitle' => ['Form 1 / 2', '', Folder::class, 'csv', 'Form 1 - 2 (responses).csv', false],
];
}

Expand Down Expand Up @@ -285,6 +291,11 @@ public function testWriteFileToCloud(string $formTitle, string $path, string $pa

$form = $this->formMapper->findByHash('abcdefg');

$this->formsService->expects($this->once())
->method('getFileName')
->with($form, $pathExtension)
->willReturn($expectedFileName);

$this->submissionService->writeFileToCloud($form, $path, 'csv');
}

Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit.integration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<testsuite name="Forms integration tests">
<directory>./Integration</directory>
</testsuite>
<php>
<!-- <env name="NC_datadirectory" value="/tmp" force="true"/>-->
<env name="NC_check_data_directory_permissions" value="0" force="true"/>
</php>
<coverage>
<!-- filters for code coverage -->
<include>
Expand Down

0 comments on commit cd5cdd5

Please sign in to comment.