Merge pull request #1488 from nextcloud/tests/api-controller-clone

Added test for `cloneForm` in `ApiController`
This commit is contained in:
Ferdinand Thiessen 2023-02-21 16:58:08 +01:00 коммит произвёл GitHub
Родитель 3181423ee0 ee9a9ad6fd
Коммит 96a099c9ee
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 161 добавлений и 0 удалений

Просмотреть файл

@ -57,6 +57,7 @@ use OCA\Forms\Db\SubmissionMapper;
use OCA\Forms\Service\ConfigService;
use OCA\Forms\Service\FormsService;
use OCA\Forms\Service\SubmissionService;
use OCA\Forms\Tests\Unit\MockedMapperException;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
@ -173,6 +174,14 @@ class ApiControllerTest extends TestCase {
};
}
/**
* Helper function to throw exceptions
* Required as PHP 7 does not allow throwing in expressions (e.g. `fn(): foo => throw ...`)
*/
public function throwMockedException(string $class) {
throw $this->createMock($class);
}
public function testGetSubmissions_invalidForm() {
$exception = $this->createMock(MapperException::class);
$this->formMapper->expects($this->once())
@ -403,4 +412,145 @@ class ApiControllerTest extends TestCase {
->willReturn(new DataResponse('succeeded'));
$this->assertEquals(new DataResponse('succeeded'), $apiController->newForm());
}
public function dataCloneForm_exceptions() {
return [
'disabled' => [
'canCreate' => false,
'callback' => fn ($id): Form => new Form(),
'exception' => OCSForbiddenException::class
],
'not found' => [
'canCreate' => true,
'callback' => fn ($id): Form => $this->throwMockedException(MockedMapperException::class),
'exception' => OCSBadRequestException::class
],
'not owned' => [
'canCreate' => true,
'callback' => function ($id): Form {
$form = new Form();
$form->setId($id);
$form->setOwnerId('otherUser');
return $form;
},
'exception' => OCSForbiddenException::class
]
];
}
/**
* @dataProvider dataCloneForm_exceptions()
*/
public function testCloneForm_exceptions(bool $canCreate, $callback, string $exception) {
$this->configService->expects($this->once())
->method('canCreateForms')
->willReturn($canCreate);
$this->formMapper->expects($canCreate ? $this->once() : $this->never())
->method('findById')
->with(7)
->willReturnCallback($callback);
$this->expectException($exception);
$this->apiController->cloneForm(7);
}
public function dataCloneForm() {
return [
'works' => [
'old' => [
'id' => 7,
'title' => '',
'hash' => 'old hash',
'created' => null,
'access' => [
'permitAllUsers' => false,
'showToAllUsers' => false,
],
'ownerId' => 'currentUser',
'description' => '',
'expires' => 0,
'isAnonymous' => false,
'submitMultiple' => false,
'showExpiration' => false
],
'new' => [
'id' => 14,
'title' => ' - Copy',
'hash' => 'new hash',
'access' => [
'permitAllUsers' => false,
'showToAllUsers' => false,
],
'ownerId' => 'currentUser',
'description' => '',
'expires' => 0,
'isAnonymous' => false,
'submitMultiple' => false,
'showExpiration' => false
]
]
];
}
/**
* @dataProvider dataCloneForm()
*/
public function testCloneForm($old, $new) {
$this->configService->expects($this->once())
->method('canCreateForms')
->willReturn(true);
$oldForm = Form::fromParams($old);
$this->formMapper->expects($this->once())
->method('findById')
->with(7)
->willReturn($oldForm);
$this->formsService->expects($this->once())
->method('generateFormHash')
->willReturn('new hash');
$read = $oldForm->read();
unset($read['id']);
$this->formMapper->expects($this->once())
->method('insert')
->with(self::callback(function ($form) {
self::assertInstanceOf(Form::class, $form);
self::assertNull($form->getId());
self::assertEquals($form->getHash(), 'new hash');
$form->setId(14);
return true;
}));
$this->questionMapper->expects($this->once())
->method('findByForm')
->with(7)
->willReturn([]);
/** @var ApiController|MockObject */
$apiController = $this->getMockBuilder(ApiController::class)
->onlyMethods(['getForm'])
->setConstructorArgs(['forms',
$this->activityManager,
$this->answerMapper,
$this->formMapper,
$this->optionMapper,
$this->questionMapper,
$this->shareMapper,
$this->submissionMapper,
$this->configService,
$this->formsService,
$this->submissionService,
$this->l10n,
$this->logger,
$this->request,
$this->userManager,
$this->createUserSession()
])
->getMock();
$apiController->expects($this->once())
->method('getForm')
->with(14)
->willReturn(new DataResponse('success'));
$this->assertEquals(new DataResponse('success'), $apiController->cloneForm(7));
}
}

Просмотреть файл

@ -0,0 +1,11 @@
<?php
namespace OCA\Forms\Tests\Unit;
use OCP\AppFramework\Db\IMapperException;
/**
* Simple wrapper over IMapperException to implement Throwable, needed for throwing a mocked exception
*/
interface MockedMapperException extends IMapperException, \Throwable {
}