зеркало из https://github.com/nextcloud/forms.git
feat(tests): Add test for method `newForm` of `ApiController`
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
This commit is contained in:
Родитель
742c3473b8
Коммит
51ba94e24c
|
@ -79,6 +79,8 @@ class ApiControllerTest extends TestCase {
|
|||
private $request;
|
||||
/** @var IUserManager|MockObject */
|
||||
private $userManager;
|
||||
/** @var IL10N|MockObject */
|
||||
private $l10n;
|
||||
|
||||
public function setUp(): void {
|
||||
$this->activityManager = $this->createMock(ActivityManager::class);
|
||||
|
@ -94,23 +96,13 @@ class ApiControllerTest extends TestCase {
|
|||
$this->logger = $this->createMock(LoggerInterface::class);
|
||||
$this->request = $this->createMock(IRequest::class);
|
||||
$this->userManager = $this->createMock(IUserManager::class);
|
||||
$userSession = $this->createMock(IUserSession::class);
|
||||
/** @var IL10N|MockObject */
|
||||
$l10n = $this->createMock(IL10N::class);
|
||||
$l10n->expects($this->any())
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
$this->l10n->expects($this->any())
|
||||
->method('t')
|
||||
->willReturnCallback(function ($v) {
|
||||
return $v;
|
||||
});
|
||||
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user->expects($this->any())
|
||||
->method('getUID')
|
||||
->willReturn('currentUser');
|
||||
$userSession->expects($this->once())
|
||||
->method('getUser')
|
||||
->willReturn($user);
|
||||
|
||||
$this->apiController = new ApiController(
|
||||
'forms',
|
||||
$this->activityManager,
|
||||
|
@ -123,14 +115,43 @@ class ApiControllerTest extends TestCase {
|
|||
$this->configService,
|
||||
$this->formsService,
|
||||
$this->submissionService,
|
||||
$l10n,
|
||||
$this->l10n,
|
||||
$this->logger,
|
||||
$this->request,
|
||||
$this->userManager,
|
||||
$userSession
|
||||
$this->createUserSession()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper factory to prevent duplicated code
|
||||
*/
|
||||
protected function createUserSession() {
|
||||
$userSession = $this->createMock(IUserSession::class);
|
||||
$user = $this->createMock(IUser::class);
|
||||
$user->expects($this->any())
|
||||
->method('getUID')
|
||||
->willReturn('currentUser');
|
||||
$userSession->expects($this->once())
|
||||
->method('getUser')
|
||||
->willReturn($user);
|
||||
return $userSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory to create a validator used to compare forms passed as parameters
|
||||
* Required as the timestamps might differ
|
||||
*/
|
||||
public static function createFormValidator(array $expected) {
|
||||
return function ($form) use ($expected): bool {
|
||||
self::assertInstanceOf(Form::class, $form);
|
||||
$read = $form->read();
|
||||
unset($read['created']);
|
||||
self::assertEquals($expected, $read);
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
public function testGetSubmissions_invalidForm() {
|
||||
$exception = $this->createMock(MapperException::class);
|
||||
$this->formMapper->expects($this->once())
|
||||
|
@ -282,4 +303,80 @@ class ApiControllerTest extends TestCase {
|
|||
|
||||
$this->assertEquals(new DataDownloadResponse($csv['data'], $csv['fileName'], 'text/csv'), $this->apiController->exportSubmissions('hash'));
|
||||
}
|
||||
|
||||
public function testCreateNewForm_notAllowed() {
|
||||
$this->configService->expects($this->once())
|
||||
->method('canCreateForms')
|
||||
->willReturn(false);
|
||||
|
||||
$this->expectException(OCSForbiddenException::class);
|
||||
$this->apiController->newForm();
|
||||
}
|
||||
|
||||
public function dataTestCreateNewForm() {
|
||||
return [
|
||||
"forms" => ['expectedForm' => [
|
||||
'id' => 7,
|
||||
'hash' => 'formHash',
|
||||
'title' => '',
|
||||
'description' => '',
|
||||
'ownerId' => 'currentUser',
|
||||
'access' => [
|
||||
'permitAllUsers' => false,
|
||||
'showToAllUsers' => false,
|
||||
],
|
||||
'expires' => 0,
|
||||
'isAnonymous' => false,
|
||||
'submitMultiple' => false,
|
||||
'showExpiration' => false
|
||||
]]
|
||||
];
|
||||
}
|
||||
/**
|
||||
* @dataProvider dataTestCreateNewForm()
|
||||
*/
|
||||
public function testCreateNewForm($expectedForm) {
|
||||
// Create a partial mock, as we only test newForm and not getForm
|
||||
/** @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();
|
||||
|
||||
$this->configService->expects($this->once())
|
||||
->method('canCreateForms')
|
||||
->willReturn(true);
|
||||
$this->formsService->expects($this->once())
|
||||
->method('generateFormHash')
|
||||
->willReturn('formHash');
|
||||
$expected = $expectedForm;
|
||||
$expected['id'] = null;
|
||||
$this->formMapper->expects($this->once())
|
||||
->method('insert')
|
||||
->with(self::callback(self::createFormValidator($expected)))
|
||||
->willReturnCallback(function ($form) {
|
||||
$form->setId(7);
|
||||
return $form;
|
||||
});
|
||||
$apiController->expects($this->once())
|
||||
->method('getForm')
|
||||
->with(7)
|
||||
->willReturn(new DataResponse('succeeded'));
|
||||
$this->assertEquals(new DataResponse('succeeded'), $apiController->newForm());
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче