diff options
Diffstat (limited to 'tests/Core/Controller')
17 files changed, 301 insertions, 183 deletions
diff --git a/tests/Core/Controller/AppPasswordControllerTest.php b/tests/Core/Controller/AppPasswordControllerTest.php index b33033edac3..eb1566eca8b 100644 --- a/tests/Core/Controller/AppPasswordControllerTest.php +++ b/tests/Core/Controller/AppPasswordControllerTest.php @@ -123,7 +123,7 @@ class AppPasswordControllerTest extends TestCase { $credentials->method('getLoginName') ->willReturn('myLoginName'); $this->request->method('getHeader') - ->with('USER_AGENT') + ->with('user-agent') ->willReturn('myUA'); $this->random->method('generate') ->with( @@ -164,7 +164,7 @@ class AppPasswordControllerTest extends TestCase { $credentials->method('getLoginName') ->willReturn('myLoginName'); $this->request->method('getHeader') - ->with('USER_AGENT') + ->with('user-agent') ->willReturn('myUA'); $this->random->method('generate') ->with( diff --git a/tests/Core/Controller/AutoCompleteControllerTest.php b/tests/Core/Controller/AutoCompleteControllerTest.php index 23bd03be7af..c5574f78fc1 100644 --- a/tests/Core/Controller/AutoCompleteControllerTest.php +++ b/tests/Core/Controller/AutoCompleteControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -42,7 +43,7 @@ class AutoCompleteControllerTest extends TestCase { ); } - public function searchDataProvider() { + public static function searchDataProvider(): array { return [ [ #0 – regular search // searchResults @@ -153,9 +154,7 @@ class AutoCompleteControllerTest extends TestCase { ]; } - /** - * @dataProvider searchDataProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('searchDataProvider')] public function testGet(array $searchResults, array $expected, string $searchTerm, ?string $itemType, ?string $itemId, ?string $sorter): void { $this->collaboratorSearch->expects($this->once()) ->method('search') diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php index 0dca611b020..a78e2c1bb5c 100644 --- a/tests/Core/Controller/AvatarControllerTest.php +++ b/tests/Core/Controller/AvatarControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -130,7 +131,7 @@ class AvatarControllerTest extends \Test\TestCase { */ public function testGetAvatarNoAvatar(): void { $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); - $this->avatarMock->method('getFile')->will($this->throwException(new NotFoundException())); + $this->avatarMock->method('getFile')->willThrowException(new NotFoundException()); $response = $this->avatarController->getAvatar('userId', 32); //Comment out until JS is fixed @@ -183,7 +184,7 @@ class AvatarControllerTest extends \Test\TestCase { $this->avatarManager ->method('getAvatar') ->with('userDoesNotExist') - ->will($this->throwException(new \Exception('user does not exist'))); + ->willThrowException(new \Exception('user does not exist')); $response = $this->avatarController->getAvatar('userDoesNotExist', 32); @@ -287,7 +288,7 @@ class AvatarControllerTest extends \Test\TestCase { * Test what happens if the removing of the avatar fails */ public function testDeleteAvatarException(): void { - $this->avatarMock->method('remove')->will($this->throwException(new \Exception('foo'))); + $this->avatarMock->method('remove')->willThrowException(new \Exception('foo')); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); $this->logger->expects($this->once()) @@ -469,7 +470,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostAvatarException(): void { $this->cache->expects($this->once()) ->method('set') - ->will($this->throwException(new \Exception('foo'))); + ->willThrowException(new \Exception('foo')); $file = $this->getMockBuilder('OCP\Files\File') ->disableOriginalConstructor()->getMock(); $file->expects($this->once()) @@ -514,7 +515,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostCroppedAvatarNoSquareCrop(): void { $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); - $this->avatarMock->method('set')->will($this->throwException(new \OC\NotSquareException)); + $this->avatarMock->method('set')->willThrowException(new \OC\NotSquareException); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); $response = $this->avatarController->postCroppedAvatar(['x' => 0, 'y' => 0, 'w' => 10, 'h' => 11]); @@ -539,7 +540,7 @@ class AvatarControllerTest extends \Test\TestCase { public function testPostCroppedAvatarException(): void { $this->cache->method('get')->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg')); - $this->avatarMock->method('set')->will($this->throwException(new \Exception('foo'))); + $this->avatarMock->method('set')->willThrowException(new \Exception('foo')); $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock); $this->logger->expects($this->once()) diff --git a/tests/Core/Controller/ChangePasswordControllerTest.php b/tests/Core/Controller/ChangePasswordControllerTest.php index a806b091477..aae36fb52b8 100644 --- a/tests/Core/Controller/ChangePasswordControllerTest.php +++ b/tests/Core/Controller/ChangePasswordControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -96,7 +97,7 @@ class ChangePasswordControllerTest extends \Test\TestCase { $user->expects($this->once()) ->method('setPassword') ->with('new') - ->will($this->throwException(new HintException('Common password'))); + ->willThrowException(new HintException('Common password')); $expects = new JSONResponse([ 'status' => 'error', diff --git a/tests/Core/Controller/ClientFlowLoginControllerTest.php b/tests/Core/Controller/ClientFlowLoginControllerTest.php index 1f4575208b8..b182bb1bb39 100644 --- a/tests/Core/Controller/ClientFlowLoginControllerTest.php +++ b/tests/Core/Controller/ClientFlowLoginControllerTest.php @@ -18,10 +18,14 @@ use OCA\OAuth2\Db\AccessTokenMapper; use OCA\OAuth2\Db\Client; use OCA\OAuth2\Db\ClientMapper; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\StandaloneTemplateResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Defaults; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; use OCP\ISession; @@ -48,6 +52,7 @@ class ClientFlowLoginControllerTest extends TestCase { private ICrypto&MockObject $crypto; private IEventDispatcher&MockObject $eventDispatcher; private ITimeFactory&MockObject $timeFactory; + private IConfig&MockObject $config; private ClientFlowLoginController $clientFlowLoginController; @@ -73,6 +78,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->crypto = $this->createMock(ICrypto::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->config = $this->createMock(IConfig::class); $this->clientFlowLoginController = new ClientFlowLoginController( 'core', @@ -89,6 +95,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->crypto, $this->eventDispatcher, $this->timeFactory, + $this->config, ); } @@ -97,8 +104,8 @@ class ClientFlowLoginControllerTest extends TestCase { 'core', 'error', [ - 'errors' => - [ + 'errors' + => [ [ 'error' => 'Access Forbidden', 'hint' => 'Invalid request', @@ -114,12 +121,8 @@ class ClientFlowLoginControllerTest extends TestCase { public function testShowAuthPickerPageWithOcsHeader(): void { $this->request ->method('getHeader') - ->withConsecutive( - ['USER_AGENT'], - ['OCS-APIREQUEST'] - ) ->willReturnMap([ - ['USER_AGENT', 'Mac OS X Sync Client'], + ['user-agent', 'Mac OS X Sync Client'], ['OCS-APIREQUEST', 'true'], ]); $this->random @@ -163,11 +166,12 @@ class ClientFlowLoginControllerTest extends TestCase { 'serverHost' => 'https://example.com', 'oauthState' => 'OauthStateToken', 'user' => '', - 'direct' => 0 + 'direct' => 0, + 'providedRedirectUri' => '', ], 'guest' ); - $csp = new Http\ContentSecurityPolicy(); + $csp = new ContentSecurityPolicy(); $csp->addAllowedFormActionDomain('nc://*'); $expected->setContentSecurityPolicy($csp); $this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage()); @@ -176,12 +180,8 @@ class ClientFlowLoginControllerTest extends TestCase { public function testShowAuthPickerPageWithOauth(): void { $this->request ->method('getHeader') - ->withConsecutive( - ['USER_AGENT'], - ['OCS-APIREQUEST'] - ) ->willReturnMap([ - ['USER_AGENT', 'Mac OS X Sync Client'], + ['user-agent', 'Mac OS X Sync Client'], ['OCS-APIREQUEST', 'false'], ]); $client = new Client(); @@ -233,11 +233,12 @@ class ClientFlowLoginControllerTest extends TestCase { 'serverHost' => 'https://example.com', 'oauthState' => 'OauthStateToken', 'user' => '', - 'direct' => 0 + 'direct' => 0, + 'providedRedirectUri' => '', ], 'guest' ); - $csp = new Http\ContentSecurityPolicy(); + $csp = new ContentSecurityPolicy(); $csp->addAllowedFormActionDomain('https://example.com/redirect.php'); $expected->setContentSecurityPolicy($csp); $this->assertEquals($expected, $this->clientFlowLoginController->showAuthPickerPage('MyClientIdentifier')); @@ -281,7 +282,7 @@ class ClientFlowLoginControllerTest extends TestCase { ->method('getId') ->willThrowException(new SessionNotAvailableException()); - $expected = new Http\Response(); + $expected = new Response(); $expected->setStatus(Http::STATUS_FORBIDDEN); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } @@ -306,7 +307,7 @@ class ClientFlowLoginControllerTest extends TestCase { ->with('SessionId') ->willThrowException(new InvalidTokenException()); - $expected = new Http\Response(); + $expected = new Response(); $expected->setStatus(Http::STATUS_FORBIDDEN); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } @@ -382,7 +383,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); + $expected = new RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } @@ -398,20 +399,20 @@ class ClientFlowLoginControllerTest extends TestCase { public function testGeneratePasswordWithPasswordForOauthClient($redirectUri, $redirectUrl): void { $this->session ->method('get') - ->withConsecutive( - ['client.flow.state.token'], - ['oauth.state'] - ) ->willReturnMap([ ['client.flow.state.token', 'MyStateToken'], ['oauth.state', 'MyOauthState'], ]); + $calls = [ + 'client.flow.state.token', + 'oauth.state', + ]; $this->session ->method('remove') - ->withConsecutive( - ['client.flow.state.token'], - ['oauth.state'] - ); + ->willReturnCallback(function ($key) use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, $key); + }); $this->session ->expects($this->once()) ->method('getId') @@ -433,10 +434,6 @@ class ClientFlowLoginControllerTest extends TestCase { ->willReturn('MyPassword'); $this->random ->method('generate') - ->withConsecutive( - [72], - [128] - ) ->willReturnMap([ [72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS, 'MyGeneratedToken'], [128, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS, 'MyAccessCode'], @@ -476,7 +473,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse($redirectUrl); + $expected = new RedirectResponse($redirectUrl); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken', 'MyClientIdentifier')); } @@ -551,17 +548,17 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); + $expected = new RedirectResponse('nc://login/server:http://example.com&user:MyLoginName&password:MyGeneratedToken'); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } - public function dataGeneratePasswordWithHttpsProxy() { + public static function dataGeneratePasswordWithHttpsProxy(): array { return [ [ [ ['X-Forwarded-Proto', 'http'], ['X-Forwarded-Ssl', 'off'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'http', @@ -570,7 +567,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'http'], ['X-Forwarded-Ssl', 'off'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'https', 'https', @@ -579,7 +576,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'https'], ['X-Forwarded-Ssl', 'off'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'https', @@ -588,7 +585,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'https'], ['X-Forwarded-Ssl', 'on'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'https', @@ -597,7 +594,7 @@ class ClientFlowLoginControllerTest extends TestCase { [ ['X-Forwarded-Proto', 'http'], ['X-Forwarded-Ssl', 'on'], - ['USER_AGENT', ''], + ['user-agent', ''], ], 'http', 'https', @@ -606,11 +603,11 @@ class ClientFlowLoginControllerTest extends TestCase { } /** - * @dataProvider dataGeneratePasswordWithHttpsProxy * @param array $headers * @param string $protocol * @param string $expected */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGeneratePasswordWithHttpsProxy')] public function testGeneratePasswordWithHttpsProxy(array $headers, $protocol, $expected): void { $this->session ->expects($this->once()) @@ -682,7 +679,7 @@ class ClientFlowLoginControllerTest extends TestCase { $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped'); - $expected = new Http\RedirectResponse('nc://login/server:' . $expected . '://example.com&user:MyLoginName&password:MyGeneratedToken'); + $expected = new RedirectResponse('nc://login/server:' . $expected . '://example.com&user:MyLoginName&password:MyGeneratedToken'); $this->assertEquals($expected, $this->clientFlowLoginController->generateAppPassword('MyStateToken')); } } diff --git a/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php b/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php index 98c7821791d..d130eb75c1a 100644 --- a/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php +++ b/tests/Core/Controller/ClientFlowLoginV2ControllerTest.php @@ -11,9 +11,12 @@ namespace Test\Core\Controller; use OC\Core\Controller\ClientFlowLoginV2Controller; use OC\Core\Data\LoginFlowV2Credentials; use OC\Core\Db\LoginFlowV2; +use OC\Core\Exception\LoginFlowV2ClientForbiddenException; use OC\Core\Exception\LoginFlowV2NotFoundException; use OC\Core\Service\LoginFlowV2Service; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\StandaloneTemplateResponse; use OCP\Defaults; use OCP\IL10N; use OCP\IRequest; @@ -56,6 +59,12 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->random = $this->createMock(ISecureRandom::class); $this->defaults = $this->createMock(Defaults::class); $this->l = $this->createMock(IL10N::class); + $this->l + ->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($text, $parameters = []) { + return vsprintf($text, $parameters); + }); $this->controller = new ClientFlowLoginV2Controller( 'core', $this->request, @@ -104,7 +113,7 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $result = $this->controller->landing('token'); $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); - $this->assertInstanceOf(Http\StandaloneTemplateResponse::class, $result); + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); } public function testLandingValid(): void { @@ -122,7 +131,7 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $result = $this->controller->landing('token'); - $this->assertInstanceOf(Http\RedirectResponse::class, $result); + $this->assertInstanceOf(RedirectResponse::class, $result); $this->assertSame(Http::STATUS_SEE_OTHER, $result->getStatus()); $this->assertSame('https://server/path', $result->getRedirectURL()); } @@ -150,6 +159,22 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); } + public function testShowAuthPickerForbiddenUserClient() { + $this->session->method('get') + ->with('client.flow.v2.login.token') + ->willReturn('loginToken'); + + $this->loginFlowV2Service->method('getByLoginToken') + ->with('loginToken') + ->willThrowException(new LoginFlowV2ClientForbiddenException()); + + $result = $this->controller->showAuthPickerPage(); + + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); + $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); + $this->assertSame('Please use original client', $result->getParams()['message']); + } + public function testShowAuthPickerValidLoginToken(): void { $this->session->method('get') ->with('client.flow.v2.login.token') @@ -206,6 +231,29 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); } + public function testGrantPageForbiddenUserClient() { + $this->session->method('get') + ->willReturnCallback(function ($name) { + if ($name === 'client.flow.v2.state.token') { + return 'stateToken'; + } + if ($name === 'client.flow.v2.login.token') { + return 'loginToken'; + } + return null; + }); + + $this->loginFlowV2Service->method('getByLoginToken') + ->with('loginToken') + ->willThrowException(new LoginFlowV2ClientForbiddenException()); + + $result = $this->controller->grantPage('stateToken'); + + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); + $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); + $this->assertSame('Please use original client', $result->getParams()['message']); + } + public function testGrantPageValid(): void { $this->session->method('get') ->willReturnCallback(function ($name) { @@ -266,6 +314,29 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); } + public function testGenerateAppPasswordForbiddenUserClient() { + $this->session->method('get') + ->willReturnCallback(function ($name) { + if ($name === 'client.flow.v2.state.token') { + return 'stateToken'; + } + if ($name === 'client.flow.v2.login.token') { + return 'loginToken'; + } + return null; + }); + + $this->loginFlowV2Service->method('getByLoginToken') + ->with('loginToken') + ->willThrowException(new LoginFlowV2ClientForbiddenException()); + + $result = $this->controller->generateAppPassword('stateToken'); + + $this->assertInstanceOf(StandaloneTemplateResponse::class, $result); + $this->assertSame(Http::STATUS_FORBIDDEN, $result->getStatus()); + $this->assertSame('Please use original client', $result->getParams()['message']); + } + public function testGenerateAppPassworValid(): void { $this->session->method('get') ->willReturnCallback(function ($name) { @@ -286,7 +357,7 @@ class ClientFlowLoginV2ControllerTest extends TestCase { $clearedState = false; $clearedLogin = false; $this->session->method('remove') - ->willReturnCallback(function ($name) use (&$clearedLogin, &$clearedState) { + ->willReturnCallback(function ($name) use (&$clearedLogin, &$clearedState): void { if ($name === 'client.flow.v2.state.token') { $clearedState = true; } diff --git a/tests/Core/Controller/CssControllerTest.php b/tests/Core/Controller/CssControllerTest.php index cae6f7989c4..b4764d6ea3a 100644 --- a/tests/Core/Controller/CssControllerTest.php +++ b/tests/Core/Controller/CssControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Controller/GuestAvatarControllerTest.php b/tests/Core/Controller/GuestAvatarControllerTest.php index 1ad9e49f858..66a83098130 100644 --- a/tests/Core/Controller/GuestAvatarControllerTest.php +++ b/tests/Core/Controller/GuestAvatarControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -7,6 +8,7 @@ namespace Core\Controller; use OC\Core\Controller\GuestAvatarController; use OCP\AppFramework\Http\FileDisplayResponse; +use OCP\Files\File; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IAvatar; use OCP\IAvatarManager; @@ -38,7 +40,7 @@ class GuestAvatarControllerTest extends \Test\TestCase { private $avatar; /** - * @var \OCP\Files\File|\PHPUnit\Framework\MockObject\MockObject + * @var File|\PHPUnit\Framework\MockObject\MockObject */ private $file; diff --git a/tests/Core/Controller/JsControllerTest.php b/tests/Core/Controller/JsControllerTest.php index 1500ed6eacf..30bc02e8625 100644 --- a/tests/Core/Controller/JsControllerTest.php +++ b/tests/Core/Controller/JsControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php index a6438a35af0..18baaf5b08c 100644 --- a/tests/Core/Controller/LoginControllerTest.php +++ b/tests/Core/Controller/LoginControllerTest.php @@ -255,9 +255,9 @@ class LoginControllerTest extends TestCase { ], ] ); - $this->initialState->expects($this->exactly(13)) - ->method('provideInitialState') - ->withConsecutive([ + + $calls = [ + [ 'loginMessages', [ 'MessageArray1', @@ -265,17 +265,26 @@ class LoginControllerTest extends TestCase { 'This community release of Nextcloud is unsupported and push notifications are limited.', ], ], + [ + 'loginErrors', [ - 'loginErrors', - [ - 'ErrorArray1', - 'ErrorArray2', - ], + 'ErrorArray1', + 'ErrorArray2', ], - [ - 'loginUsername', - '', - ]); + ], + [ + 'loginUsername', + '', + ] + ]; + $this->initialState->expects($this->exactly(13)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', @@ -294,15 +303,25 @@ class LoginControllerTest extends TestCase { ->expects($this->once()) ->method('isLoggedIn') ->willReturn(false); - $this->initialState->expects($this->exactly(14)) - ->method('provideInitialState') - ->withConsecutive([], [], [], [ + $calls = [ + [], [], [], + [ 'loginAutocomplete', false - ], [ + ], + [ 'loginRedirectUrl', 'login/flow' - ]); + ], + ]; + $this->initialState->expects($this->exactly(14)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', @@ -319,7 +338,7 @@ class LoginControllerTest extends TestCase { /** * @return array */ - public function passwordResetDataProvider(): array { + public static function passwordResetDataProvider(): array { return [ [ true, @@ -332,9 +351,7 @@ class LoginControllerTest extends TestCase { ]; } - /** - * @dataProvider passwordResetDataProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('passwordResetDataProvider')] public function testShowLoginFormWithPasswordResetOption($canChangePassword, $expectedResult): void { $this->userSession @@ -363,15 +380,26 @@ class LoginControllerTest extends TestCase { ->method('get') ->with('LdapUser') ->willReturn($user); - $this->initialState->expects($this->exactly(13)) - ->method('provideInitialState') - ->withConsecutive([], [], [ + $calls = [ + [], [], + [ 'loginUsername', 'LdapUser' - ], [], [], [], [ + ], + [], [], [], + [ 'loginCanResetPassword', $expectedResult - ]); + ], + ]; + $this->initialState->expects($this->exactly(13)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', @@ -411,18 +439,30 @@ class LoginControllerTest extends TestCase { ->method('get') ->with('0') ->willReturn($user); - $this->initialState->expects($this->exactly(13)) - ->method('provideInitialState') - ->withConsecutive([], [], [], [ + $calls = [ + [], [], [], + [ 'loginAutocomplete', true - ], [], [ + ], + [], + [ 'loginResetPasswordLink', false - ], [ + ], + [ 'loginCanResetPassword', false - ]); + ], + ]; + $this->initialState->expects($this->exactly(13)) + ->method('provideInitialState') + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + if (!empty($expected)) { + $this->assertEquals($expected, func_get_args()); + } + }); $expectedResponse = new TemplateResponse( 'core', diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index 2a99c9f9d16..bbb5f2c2e54 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only @@ -172,13 +173,18 @@ class LostControllerTest extends TestCase { ->method('linkToRouteAbsolute') ->with('core.lost.setPassword', ['userId' => 'ValidTokenUser', 'token' => 'MySecretToken']) ->willReturn('https://example.tld/index.php/lostpassword/set/sometoken/someuser'); + + $calls = [ + ['resetPasswordUser', 'ValidTokenUser'], + ['resetPasswordTarget', 'https://example.tld/index.php/lostpassword/set/sometoken/someuser'], + ]; $this->initialState ->expects($this->exactly(2)) ->method('provideInitialState') - ->withConsecutive( - ['resetPasswordUser', 'ValidTokenUser'], - ['resetPasswordTarget', 'https://example.tld/index.php/lostpassword/set/sometoken/someuser'] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $response = $this->lostController->resetform('MySecretToken', 'ValidTokenUser'); $expectedResponse = new TemplateResponse('core', @@ -398,7 +404,7 @@ class LostControllerTest extends TestCase { ->expects($this->once()) ->method('send') ->with($message) - ->will($this->throwException(new \Exception())); + ->willThrowException(new \Exception()); $this->logger->expects($this->exactly(1)) ->method('error'); @@ -448,12 +454,19 @@ class LostControllerTest extends TestCase { $this->userManager->method('get') ->with('ValidTokenUser') ->willReturn($this->existingUser); - $beforePasswordResetEvent = new BeforePasswordResetEvent($this->existingUser, 'NewPassword'); - $passwordResetEvent = new PasswordResetEvent($this->existingUser, 'NewPassword'); + + $calls = [ + [new BeforePasswordResetEvent($this->existingUser, 'NewPassword')], + [new PasswordResetEvent($this->existingUser, 'NewPassword')], + ]; $this->eventDispatcher ->expects($this->exactly(2)) ->method('dispatchTyped') - ->withConsecutive([$beforePasswordResetEvent], [$passwordResetEvent]); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); + $this->config->expects($this->once()) ->method('deleteUserValue') ->with('ValidTokenUser', 'core', 'lostpassword'); @@ -666,18 +679,18 @@ class LostControllerTest extends TestCase { /** * @return array */ - public function dataTwoUserswithSameEmailOneDisabled(): array { + public static function dataTwoUsersWithSameEmailOneDisabled(): array { return [ - ['user1' => true, 'user2' => false], - ['user1' => false, 'user2' => true] + ['userEnabled1' => true, 'userEnabled2' => false], + ['userEnabled1' => false, 'userEnabled2' => true] ]; } /** - * @dataProvider dataTwoUserswithSameEmailOneDisabled * @param bool $userEnabled1 * @param bool $userEnabled2 */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTwoUsersWithSameEmailOneDisabled')] public function testTwoUsersWithSameEmailOneDisabled(bool $userEnabled1, bool $userEnabled2): void { $user1 = $this->createMock(IUser::class); $user1->method('getEMailAddress') diff --git a/tests/Core/Controller/NavigationControllerTest.php b/tests/Core/Controller/NavigationControllerTest.php index 4995bd2fed0..d00976f18ec 100644 --- a/tests/Core/Controller/NavigationControllerTest.php +++ b/tests/Core/Controller/NavigationControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -7,7 +8,6 @@ namespace Tests\Core\Controller; use OC\Core\Controller\NavigationController; -use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\INavigationManager; use OCP\IRequest; @@ -42,13 +42,14 @@ class NavigationControllerTest extends TestCase { ); } - public function dataGetNavigation() { + public static function dataGetNavigation(): array { return [ - [false], [true] + [false], + [true], ]; } - /** @dataProvider dataGetNavigation */ - public function testGetAppNavigation($absolute): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetNavigation')] + public function testGetAppNavigation(bool $absolute): void { $this->navigationManager->expects($this->once()) ->method('getAll') ->with('link') @@ -59,11 +60,10 @@ class NavigationControllerTest extends TestCase { ->willReturn('http://localhost/'); $this->urlGenerator->expects($this->exactly(2)) ->method('getAbsoluteURL') - ->withConsecutive(['/index.php/apps/files'], ['icon']) - ->willReturnOnConsecutiveCalls( - 'http://localhost/index.php/apps/files', - 'http://localhost/icon' - ); + ->willReturnMap([ + ['/index.php/apps/files', 'http://localhost/index.php/apps/files'], + ['icon', 'http://localhost/icon'], + ]); $actual = $this->controller->getAppsNavigation($absolute); $this->assertInstanceOf(DataResponse::class, $actual); $this->assertEquals('http://localhost/index.php/apps/files', $actual->getData()[0]['href']); @@ -76,8 +76,8 @@ class NavigationControllerTest extends TestCase { } } - /** @dataProvider dataGetNavigation */ - public function testGetSettingsNavigation($absolute): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetNavigation')] + public function testGetSettingsNavigation(bool $absolute): void { $this->navigationManager->expects($this->once()) ->method('getAll') ->with('settings') @@ -88,14 +88,10 @@ class NavigationControllerTest extends TestCase { ->willReturn('http://localhost/'); $this->urlGenerator->expects($this->exactly(2)) ->method('getAbsoluteURL') - ->withConsecutive( - ['/index.php/settings/user'], - ['/core/img/settings.svg'] - ) - ->willReturnOnConsecutiveCalls( - 'http://localhost/index.php/settings/user', - 'http://localhost/core/img/settings.svg' - ); + ->willReturnMap([ + ['/index.php/settings/user', 'http://localhost/index.php/settings/user'], + ['/core/img/settings.svg', 'http://localhost/core/img/settings.svg'] + ]); $actual = $this->controller->getSettingsNavigation($absolute); $this->assertInstanceOf(DataResponse::class, $actual); $this->assertEquals('http://localhost/index.php/settings/user', $actual->getData()[0]['href']); @@ -108,33 +104,35 @@ class NavigationControllerTest extends TestCase { } } - public function testGetAppNavigationEtagMatch(): void { - $navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ]; - $this->request->expects($this->once()) - ->method('getHeader') - ->with('If-None-Match') - ->willReturn(md5(json_encode($navigation))); - $this->navigationManager->expects($this->once()) + public function testEtagIgnoresLogout(): void { + $navigation1 = [ + ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ], + ['id' => 'logout', 'href' => '/index.php/logout?requesttoken=abcd', 'icon' => 'icon' ], + ]; + $navigation2 = [ + ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ], + ['id' => 'logout', 'href' => '/index.php/logout?requesttoken=1234', 'icon' => 'icon' ], + ]; + $navigation3 = [ + ['id' => 'files', 'href' => '/index.php/apps/files/test', 'icon' => 'icon' ], + ['id' => 'logout', 'href' => '/index.php/logout?requesttoken=1234', 'icon' => 'icon' ], + ]; + $this->navigationManager->expects($this->exactly(3)) ->method('getAll') ->with('link') - ->willReturn($navigation); - $actual = $this->controller->getAppsNavigation(); - $this->assertInstanceOf(DataResponse::class, $actual); - $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus()); - } + ->willReturnOnConsecutiveCalls( + $navigation1, + $navigation2, + $navigation3, + ); - public function testGetSettingsNavigationEtagMatch(): void { - $navigation = [ ['id' => 'logout', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ]; - $this->request->expects($this->once()) - ->method('getHeader') - ->with('If-None-Match') - ->willReturn(md5(json_encode([ ['id' => 'logout', 'href' => 'logout', 'icon' => 'icon' ] ]))); - $this->navigationManager->expects($this->once()) - ->method('getAll') - ->with('settings') - ->willReturn($navigation); - $actual = $this->controller->getSettingsNavigation(); - $this->assertInstanceOf(DataResponse::class, $actual); - $this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus()); + // Changes in the logout url should not change the ETag + $request1 = $this->controller->getAppsNavigation(); + $request2 = $this->controller->getAppsNavigation(); + $this->assertEquals($request1->getETag(), $request2->getETag()); + + // Changes in non-logout urls should result in a different ETag + $request3 = $this->controller->getAppsNavigation(); + $this->assertNotEquals($request2->getETag(), $request3->getETag()); } } diff --git a/tests/Core/Controller/OCSControllerTest.php b/tests/Core/Controller/OCSControllerTest.php index 142a15e2308..bd7e26d5e8f 100644 --- a/tests/Core/Controller/OCSControllerTest.php +++ b/tests/Core/Controller/OCSControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -15,6 +16,7 @@ use OCP\IRequest; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use OCP\Server; use OCP\ServerVersion; use Test\TestCase; @@ -42,7 +44,7 @@ class OCSControllerTest extends TestCase { $this->userSession = $this->createMock(IUserSession::class); $this->userManager = $this->createMock(IUserManager::class); $this->keyManager = $this->createMock(Manager::class); - $serverVersion = \OCP\Server::get(ServerVersion::class); + $serverVersion = Server::get(ServerVersion::class); $this->controller = new OCSController( 'core', @@ -78,7 +80,7 @@ class OCSControllerTest extends TestCase { ->method('isLoggedIn') ->willReturn(true); - $serverVersion = \OCP\Server::get(ServerVersion::class); + $serverVersion = Server::get(ServerVersion::class); $result = []; $result['version'] = [ @@ -111,7 +113,7 @@ class OCSControllerTest extends TestCase { $this->userSession->expects($this->once()) ->method('isLoggedIn') ->willReturn(false); - $serverVersion = \OCP\Server::get(ServerVersion::class); + $serverVersion = Server::get(ServerVersion::class); $result = []; $result['version'] = [ diff --git a/tests/Core/Controller/PreviewControllerTest.php b/tests/Core/Controller/PreviewControllerTest.php index e7ecba27064..5a6cd1fba0a 100644 --- a/tests/Core/Controller/PreviewControllerTest.php +++ b/tests/Core/Controller/PreviewControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -19,7 +20,6 @@ use OCP\Files\Storage\IStorage; use OCP\IPreview; use OCP\IRequest; use OCP\Preview\IMimeIconProvider; -use OCP\Share\IAttributes; use OCP\Share\IShare; use PHPUnit\Framework\MockObject\MockObject; @@ -196,15 +196,9 @@ class PreviewControllerTest extends \Test\TestCase { ->with($this->equalTo($file)) ->willReturn(true); - $shareAttributes = $this->createMock(IAttributes::class); - $shareAttributes->expects(self::atLeastOnce()) - ->method('getAttribute') - ->with('permissions', 'download') - ->willReturn(false); - $share = $this->createMock(IShare::class); - $share->method('getAttributes') - ->willReturn($shareAttributes); + $share->method('canSeeContent') + ->willReturn(false); $storage = $this->createMock(ISharedStorage::class); $storage->method('instanceOfStorage') @@ -242,14 +236,9 @@ class PreviewControllerTest extends \Test\TestCase { ->with($this->equalTo($file)) ->willReturn(true); - $shareAttributes = $this->createMock(IAttributes::class); - $shareAttributes->method('getAttribute') - ->with('permissions', 'download') - ->willReturn(false); - $share = $this->createMock(IShare::class); - $share->method('getAttributes') - ->willReturn($shareAttributes); + $share->method('canSeeContent') + ->willReturn(false); $storage = $this->createMock(ISharedStorage::class); $storage->method('instanceOfStorage') @@ -265,7 +254,7 @@ class PreviewControllerTest extends \Test\TestCase { $this->request ->method('getHeader') - ->with('X-NC-Preview') + ->with('x-nc-preview') ->willReturn('true'); $preview = $this->createMock(ISimpleFile::class); @@ -341,8 +330,8 @@ class PreviewControllerTest extends \Test\TestCase { // No attributes set -> download permitted $share = $this->createMock(IShare::class); - $share->method('getAttributes') - ->willReturn(null); + $share->method('canSeeContent') + ->willReturn(true); $storage = $this->createMock(ISharedStorage::class); $storage->method('instanceOfStorage') diff --git a/tests/Core/Controller/TwoFactorChallengeControllerTest.php b/tests/Core/Controller/TwoFactorChallengeControllerTest.php index 11e18bd622a..d9ea1ca263f 100644 --- a/tests/Core/Controller/TwoFactorChallengeControllerTest.php +++ b/tests/Core/Controller/TwoFactorChallengeControllerTest.php @@ -22,7 +22,7 @@ use OCP\ISession; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; -use OCP\Template; +use OCP\Template\ITemplate; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -68,7 +68,7 @@ class TwoFactorChallengeControllerTest extends TestCase { $this->urlGenerator, $this->logger, ]) - ->setMethods(['getLogoutUrl']) + ->onlyMethods(['getLogoutUrl']) ->getMock(); $this->controller->expects($this->any()) ->method('getLogoutUrl') @@ -115,7 +115,7 @@ class TwoFactorChallengeControllerTest extends TestCase { $provider->method('getId')->willReturn('myprovider'); $backupProvider = $this->createMock(IProvider::class); $backupProvider->method('getId')->willReturn('backup_codes'); - $tmpl = $this->createMock(Template::class); + $tmpl = $this->createMock(ITemplate::class); $providerSet = new ProviderSet([$provider, $backupProvider], true); $this->userSession->expects($this->once()) @@ -301,13 +301,17 @@ class TwoFactorChallengeControllerTest extends TestCase { $this->twoFactorManager->expects($this->once()) ->method('verifyChallenge') ->with('myprovider', $user, 'token') - ->will($this->throwException($exception)); + ->willThrowException($exception); + $calls = [ + ['two_factor_auth_error_message', '2FA failed'], + ['two_factor_auth_error', true], + ]; $this->session->expects($this->exactly(2)) ->method('set') - ->withConsecutive( - ['two_factor_auth_error_message', '2FA failed'], - ['two_factor_auth_error', true] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('core.TwoFactorChallenge.showChallenge', [ @@ -399,7 +403,7 @@ class TwoFactorChallengeControllerTest extends TestCase { ->method('getLoginSetup') ->with($user) ->willReturn($loginSetup); - $tmpl = $this->createMock(Template::class); + $tmpl = $this->createMock(ITemplate::class); $loginSetup->expects($this->once()) ->method('getBody') ->willReturn($tmpl); diff --git a/tests/Core/Controller/UserControllerTest.php b/tests/Core/Controller/UserControllerTest.php index 979c723dd85..2473f280580 100644 --- a/tests/Core/Controller/UserControllerTest.php +++ b/tests/Core/Controller/UserControllerTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/Core/Controller/WipeControllerTest.php b/tests/Core/Controller/WipeControllerTest.php index 2cd315db5bf..5330eb599e6 100644 --- a/tests/Core/Controller/WipeControllerTest.php +++ b/tests/Core/Controller/WipeControllerTest.php @@ -33,7 +33,7 @@ class WipeControllerTest extends TestCase { $this->remoteWipe); } - public function dataTest() { + public static function dataTest(): array { return [ // valid token, could perform operation, valid result [ true, true, true], @@ -47,9 +47,8 @@ class WipeControllerTest extends TestCase { * @param bool $valid * @param bool $couldPerform * @param bool $result - * - * @dataProvider dataTest */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTest')] public function testCheckWipe(bool $valid, bool $couldPerform, bool $result): void { if (!$valid) { $this->remoteWipe->method('start') @@ -76,9 +75,8 @@ class WipeControllerTest extends TestCase { * @param bool $valid * @param bool $couldPerform * @param bool $result - * - * @dataProvider dataTest */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataTest')] public function testWipeDone(bool $valid, bool $couldPerform, bool $result): void { if (!$valid) { $this->remoteWipe->method('finish') |