Completed
Push — master ( 86ef77...7b60e3 )
by
unknown
19:14
created
apps/files_external/lib/Controller/AjaxController.php 1 patch
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -21,122 +21,122 @@
 block discarded – undo
21 21
 use OCP\IUserSession;
22 22
 
23 23
 class AjaxController extends Controller {
24
-	/**
25
-	 * @param string $appName
26
-	 * @param IRequest $request
27
-	 * @param RSA $rsaMechanism
28
-	 * @param GlobalAuth $globalAuth
29
-	 * @param IUserSession $userSession
30
-	 * @param IGroupManager $groupManager
31
-	 */
32
-	public function __construct(
33
-		$appName,
34
-		IRequest $request,
35
-		private RSA $rsaMechanism,
36
-		private GlobalAuth $globalAuth,
37
-		private IUserSession $userSession,
38
-		private IGroupManager $groupManager,
39
-		private IUserManager $userManager,
40
-		private IL10N $l10n,
41
-	) {
42
-		parent::__construct($appName, $request);
43
-	}
24
+    /**
25
+     * @param string $appName
26
+     * @param IRequest $request
27
+     * @param RSA $rsaMechanism
28
+     * @param GlobalAuth $globalAuth
29
+     * @param IUserSession $userSession
30
+     * @param IGroupManager $groupManager
31
+     */
32
+    public function __construct(
33
+        $appName,
34
+        IRequest $request,
35
+        private RSA $rsaMechanism,
36
+        private GlobalAuth $globalAuth,
37
+        private IUserSession $userSession,
38
+        private IGroupManager $groupManager,
39
+        private IUserManager $userManager,
40
+        private IL10N $l10n,
41
+    ) {
42
+        parent::__construct($appName, $request);
43
+    }
44 44
 
45 45
 
46
-	/**
47
-	 * Legacy endpoint for oauth2 callback
48
-	 */
49
-	#[NoAdminRequired()]
50
-	public function oauth2Callback(): JSONResponse {
51
-		return new JSONResponse(['status' => 'success']);
52
-	}
46
+    /**
47
+     * Legacy endpoint for oauth2 callback
48
+     */
49
+    #[NoAdminRequired()]
50
+    public function oauth2Callback(): JSONResponse {
51
+        return new JSONResponse(['status' => 'success']);
52
+    }
53 53
 
54
-	/**
55
-	 * Returns a list of users and groups that match the given pattern.
56
-	 * Used for user and group picker in the admin settings.
57
-	 *
58
-	 * @param string $pattern The search pattern
59
-	 * @param int|null $limit The maximum number of results to return
60
-	 * @param int|null $offset The offset from which to start returning results
61
-	 * @return JSONResponse
62
-	 */
63
-	public function getApplicableEntities(string $pattern = '', ?int $limit = null, ?int $offset = null): JSONResponse {
64
-		$groups = [];
65
-		foreach ($this->groupManager->search($pattern, $limit, $offset) as $group) {
66
-			$groups[$group->getGID()] = $group->getDisplayName();
67
-		}
54
+    /**
55
+     * Returns a list of users and groups that match the given pattern.
56
+     * Used for user and group picker in the admin settings.
57
+     *
58
+     * @param string $pattern The search pattern
59
+     * @param int|null $limit The maximum number of results to return
60
+     * @param int|null $offset The offset from which to start returning results
61
+     * @return JSONResponse
62
+     */
63
+    public function getApplicableEntities(string $pattern = '', ?int $limit = null, ?int $offset = null): JSONResponse {
64
+        $groups = [];
65
+        foreach ($this->groupManager->search($pattern, $limit, $offset) as $group) {
66
+            $groups[$group->getGID()] = $group->getDisplayName();
67
+        }
68 68
 
69
-		$users = [];
70
-		foreach ($this->userManager->searchDisplayName($pattern, $limit, $offset) as $user) {
71
-			$users[$user->getUID()] = $user->getDisplayName();
72
-		}
69
+        $users = [];
70
+        foreach ($this->userManager->searchDisplayName($pattern, $limit, $offset) as $user) {
71
+            $users[$user->getUID()] = $user->getDisplayName();
72
+        }
73 73
 
74
-		$results = ['groups' => $groups, 'users' => $users];
75
-		return new JSONResponse($results);
76
-	}
74
+        $results = ['groups' => $groups, 'users' => $users];
75
+        return new JSONResponse($results);
76
+    }
77 77
 
78
-	/**
79
-	 * @param int $keyLength
80
-	 * @return array
81
-	 */
82
-	private function generateSshKeys($keyLength) {
83
-		$key = $this->rsaMechanism->createKey($keyLength);
84
-		// Replace the placeholder label with a more meaningful one
85
-		$key['publickey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']);
78
+    /**
79
+     * @param int $keyLength
80
+     * @return array
81
+     */
82
+    private function generateSshKeys($keyLength) {
83
+        $key = $this->rsaMechanism->createKey($keyLength);
84
+        // Replace the placeholder label with a more meaningful one
85
+        $key['publickey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']);
86 86
 
87
-		return $key;
88
-	}
87
+        return $key;
88
+    }
89 89
 
90
-	/**
91
-	 * Generates an SSH public/private key pair.
92
-	 *
93
-	 * @param int $keyLength
94
-	 */
95
-	#[NoAdminRequired]
96
-	public function getSshKeys($keyLength = 1024) {
97
-		$key = $this->generateSshKeys($keyLength);
98
-		return new JSONResponse([
99
-			'data' => [
100
-				'private_key' => $key['privatekey'],
101
-				'public_key' => $key['publickey']
102
-			],
103
-			'status' => 'success',
104
-		]);
105
-	}
90
+    /**
91
+     * Generates an SSH public/private key pair.
92
+     *
93
+     * @param int $keyLength
94
+     */
95
+    #[NoAdminRequired]
96
+    public function getSshKeys($keyLength = 1024) {
97
+        $key = $this->generateSshKeys($keyLength);
98
+        return new JSONResponse([
99
+            'data' => [
100
+                'private_key' => $key['privatekey'],
101
+                'public_key' => $key['publickey']
102
+            ],
103
+            'status' => 'success',
104
+        ]);
105
+    }
106 106
 
107
-	/**
108
-	 * @param string $uid
109
-	 * @param string $user
110
-	 * @param string $password
111
-	 * @return JSONResponse
112
-	 */
113
-	#[NoAdminRequired]
114
-	#[PasswordConfirmationRequired(strict: true)]
115
-	public function saveGlobalCredentials($uid, $user, $password): JSONResponse {
116
-		$currentUser = $this->userSession->getUser();
117
-		if ($currentUser === null) {
118
-			return new JSONResponse([
119
-				'status' => 'error',
120
-				'message' => $this->l10n->t('You are not logged in'),
121
-			], Http::STATUS_UNAUTHORIZED);
122
-		}
107
+    /**
108
+     * @param string $uid
109
+     * @param string $user
110
+     * @param string $password
111
+     * @return JSONResponse
112
+     */
113
+    #[NoAdminRequired]
114
+    #[PasswordConfirmationRequired(strict: true)]
115
+    public function saveGlobalCredentials($uid, $user, $password): JSONResponse {
116
+        $currentUser = $this->userSession->getUser();
117
+        if ($currentUser === null) {
118
+            return new JSONResponse([
119
+                'status' => 'error',
120
+                'message' => $this->l10n->t('You are not logged in'),
121
+            ], Http::STATUS_UNAUTHORIZED);
122
+        }
123 123
 
124
-		// Non-admins can only edit their own credentials
125
-		// Admin can edit global credentials
126
-		$allowedToEdit = $uid === ''
127
-			? $this->groupManager->isAdmin($currentUser->getUID())
128
-			: $currentUser->getUID() === $uid;
124
+        // Non-admins can only edit their own credentials
125
+        // Admin can edit global credentials
126
+        $allowedToEdit = $uid === ''
127
+            ? $this->groupManager->isAdmin($currentUser->getUID())
128
+            : $currentUser->getUID() === $uid;
129 129
 
130
-		if ($allowedToEdit) {
131
-			$this->globalAuth->saveAuth($uid, $user, $password);
132
-			return new JSONResponse([
133
-				'status' => 'success',
134
-			]);
135
-		}
130
+        if ($allowedToEdit) {
131
+            $this->globalAuth->saveAuth($uid, $user, $password);
132
+            return new JSONResponse([
133
+                'status' => 'success',
134
+            ]);
135
+        }
136 136
 
137
-		return new JSONResponse([
138
-			'status' => 'success',
139
-			'message' => $this->l10n->t('Permission denied'),
140
-		], Http::STATUS_FORBIDDEN);
141
-	}
137
+        return new JSONResponse([
138
+            'status' => 'success',
139
+            'message' => $this->l10n->t('Permission denied'),
140
+        ], Http::STATUS_FORBIDDEN);
141
+    }
142 142
 }
Please login to merge, or discard this patch.
apps/files_external/tests/Controller/AjaxControllerTest.php 1 patch
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -21,136 +21,136 @@
 block discarded – undo
21 21
 use Test\TestCase;
22 22
 
23 23
 class AjaxControllerTest extends TestCase {
24
-	private IRequest&MockObject $request;
25
-	private RSA&MockObject $rsa;
26
-	private GlobalAuth&MockObject $globalAuth;
27
-	private IUserSession&MockObject $userSession;
28
-	private IGroupManager&MockObject $groupManager;
29
-	private IUserManager&MockObject $userManager;
30
-	private IL10N&MockObject $l10n;
31
-	private AjaxController $ajaxController;
32
-
33
-	protected function setUp(): void {
34
-		$this->request = $this->createMock(IRequest::class);
35
-		$this->rsa = $this->createMock(RSA::class);
36
-		$this->globalAuth = $this->createMock(GlobalAuth::class);
37
-		$this->userSession = $this->createMock(IUserSession::class);
38
-		$this->groupManager = $this->createMock(IGroupManager::class);
39
-		$this->userManager = $this->createMock(IUserManager::class);
40
-		$this->l10n = $this->createMock(IL10N::class);
41
-
42
-		$this->ajaxController = new AjaxController(
43
-			'files_external',
44
-			$this->request,
45
-			$this->rsa,
46
-			$this->globalAuth,
47
-			$this->userSession,
48
-			$this->groupManager,
49
-			$this->userManager,
50
-			$this->l10n,
51
-		);
52
-
53
-		$this->l10n->expects($this->any())
54
-			->method('t')
55
-			->willReturnCallback(function ($string, $args) {
56
-				if (!is_array($args)) {
57
-					$args = [$args];
58
-				}
59
-				return vsprintf($string, $args);
60
-			});
61
-
62
-		parent::setUp();
63
-	}
64
-
65
-	public function testGetSshKeys(): void {
66
-		$this->rsa
67
-			->expects($this->once())
68
-			->method('createKey')
69
-			->willReturn([
70
-				'privatekey' => 'MyPrivateKey',
71
-				'publickey' => 'MyPublicKey',
72
-			]);
73
-
74
-		$expected = new JSONResponse(
75
-			[
76
-				'data' => [
77
-					'private_key' => 'MyPrivateKey',
78
-					'public_key' => 'MyPublicKey',
79
-				],
80
-				'status' => 'success',
81
-			]
82
-		);
83
-		$this->assertEquals($expected, $this->ajaxController->getSshKeys());
84
-	}
85
-
86
-	public function testSaveGlobalCredentialsAsAdminForAnotherUser(): void {
87
-		$user = $this->createMock(IUser::class);
88
-		$user
89
-			->expects($this->once())
90
-			->method('getUID')
91
-			->willReturn('MyAdminUid');
92
-		$this->userSession
93
-			->expects($this->once())
94
-			->method('getUser')
95
-			->willReturn($user);
96
-		$this->globalAuth
97
-			->expects($this->never())
98
-			->method('saveAuth');
99
-
100
-		$response = $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password');
101
-		$this->assertSame($response->getStatus(), 403);
102
-		$this->assertSame('Permission denied', $response->getData()['message']);
103
-	}
104
-
105
-	public function testSaveGlobalCredentialsAsAdminForSelf(): void {
106
-		$user = $this->createMock(IUser::class);
107
-		$user
108
-			->expects($this->once())
109
-			->method('getUID')
110
-			->willReturn('MyAdminUid');
111
-		$this->userSession
112
-			->expects($this->once())
113
-			->method('getUser')
114
-			->willReturn($user);
115
-		$this->globalAuth
116
-			->expects($this->once())
117
-			->method('saveAuth')
118
-			->with('MyAdminUid', 'test', 'password');
119
-
120
-		$response = $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password');
121
-		$this->assertSame($response->getStatus(), 200);
122
-	}
123
-
124
-	public function testSaveGlobalCredentialsAsNormalUserForSelf(): void {
125
-		$user = $this->createMock(IUser::class);
126
-		$user
127
-			->method('getUID')
128
-			->willReturn('MyUserUid');
129
-		$this->userSession
130
-			->method('getUser')
131
-			->willReturn($user);
132
-		$this->globalAuth
133
-			->method('saveAuth')
134
-			->with('MyUserUid', 'test', 'password');
135
-
136
-		$response = $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password');
137
-		$this->assertSame($response->getStatus(), 200);
138
-	}
139
-
140
-	public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void {
141
-		$user = $this->createMock(IUser::class);
142
-		$user
143
-			->method('getUID')
144
-			->willReturn('MyUserUid');
145
-		$this->userSession
146
-			->method('getUser')
147
-			->willReturn($user);
148
-		$this->globalAuth
149
-			->expects($this->never())
150
-			->method('saveAuth');
151
-
152
-		$response = $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password');
153
-		$this->assertSame($response->getStatus(), 403);
154
-		$this->assertSame('Permission denied', $response->getData()['message']);
155
-	}
24
+    private IRequest&MockObject $request;
25
+    private RSA&MockObject $rsa;
26
+    private GlobalAuth&MockObject $globalAuth;
27
+    private IUserSession&MockObject $userSession;
28
+    private IGroupManager&MockObject $groupManager;
29
+    private IUserManager&MockObject $userManager;
30
+    private IL10N&MockObject $l10n;
31
+    private AjaxController $ajaxController;
32
+
33
+    protected function setUp(): void {
34
+        $this->request = $this->createMock(IRequest::class);
35
+        $this->rsa = $this->createMock(RSA::class);
36
+        $this->globalAuth = $this->createMock(GlobalAuth::class);
37
+        $this->userSession = $this->createMock(IUserSession::class);
38
+        $this->groupManager = $this->createMock(IGroupManager::class);
39
+        $this->userManager = $this->createMock(IUserManager::class);
40
+        $this->l10n = $this->createMock(IL10N::class);
41
+
42
+        $this->ajaxController = new AjaxController(
43
+            'files_external',
44
+            $this->request,
45
+            $this->rsa,
46
+            $this->globalAuth,
47
+            $this->userSession,
48
+            $this->groupManager,
49
+            $this->userManager,
50
+            $this->l10n,
51
+        );
52
+
53
+        $this->l10n->expects($this->any())
54
+            ->method('t')
55
+            ->willReturnCallback(function ($string, $args) {
56
+                if (!is_array($args)) {
57
+                    $args = [$args];
58
+                }
59
+                return vsprintf($string, $args);
60
+            });
61
+
62
+        parent::setUp();
63
+    }
64
+
65
+    public function testGetSshKeys(): void {
66
+        $this->rsa
67
+            ->expects($this->once())
68
+            ->method('createKey')
69
+            ->willReturn([
70
+                'privatekey' => 'MyPrivateKey',
71
+                'publickey' => 'MyPublicKey',
72
+            ]);
73
+
74
+        $expected = new JSONResponse(
75
+            [
76
+                'data' => [
77
+                    'private_key' => 'MyPrivateKey',
78
+                    'public_key' => 'MyPublicKey',
79
+                ],
80
+                'status' => 'success',
81
+            ]
82
+        );
83
+        $this->assertEquals($expected, $this->ajaxController->getSshKeys());
84
+    }
85
+
86
+    public function testSaveGlobalCredentialsAsAdminForAnotherUser(): void {
87
+        $user = $this->createMock(IUser::class);
88
+        $user
89
+            ->expects($this->once())
90
+            ->method('getUID')
91
+            ->willReturn('MyAdminUid');
92
+        $this->userSession
93
+            ->expects($this->once())
94
+            ->method('getUser')
95
+            ->willReturn($user);
96
+        $this->globalAuth
97
+            ->expects($this->never())
98
+            ->method('saveAuth');
99
+
100
+        $response = $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password');
101
+        $this->assertSame($response->getStatus(), 403);
102
+        $this->assertSame('Permission denied', $response->getData()['message']);
103
+    }
104
+
105
+    public function testSaveGlobalCredentialsAsAdminForSelf(): void {
106
+        $user = $this->createMock(IUser::class);
107
+        $user
108
+            ->expects($this->once())
109
+            ->method('getUID')
110
+            ->willReturn('MyAdminUid');
111
+        $this->userSession
112
+            ->expects($this->once())
113
+            ->method('getUser')
114
+            ->willReturn($user);
115
+        $this->globalAuth
116
+            ->expects($this->once())
117
+            ->method('saveAuth')
118
+            ->with('MyAdminUid', 'test', 'password');
119
+
120
+        $response = $this->ajaxController->saveGlobalCredentials('MyAdminUid', 'test', 'password');
121
+        $this->assertSame($response->getStatus(), 200);
122
+    }
123
+
124
+    public function testSaveGlobalCredentialsAsNormalUserForSelf(): void {
125
+        $user = $this->createMock(IUser::class);
126
+        $user
127
+            ->method('getUID')
128
+            ->willReturn('MyUserUid');
129
+        $this->userSession
130
+            ->method('getUser')
131
+            ->willReturn($user);
132
+        $this->globalAuth
133
+            ->method('saveAuth')
134
+            ->with('MyUserUid', 'test', 'password');
135
+
136
+        $response = $this->ajaxController->saveGlobalCredentials('MyUserUid', 'test', 'password');
137
+        $this->assertSame($response->getStatus(), 200);
138
+    }
139
+
140
+    public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void {
141
+        $user = $this->createMock(IUser::class);
142
+        $user
143
+            ->method('getUID')
144
+            ->willReturn('MyUserUid');
145
+        $this->userSession
146
+            ->method('getUser')
147
+            ->willReturn($user);
148
+        $this->globalAuth
149
+            ->expects($this->never())
150
+            ->method('saveAuth');
151
+
152
+        $response = $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password');
153
+        $this->assertSame($response->getStatus(), 403);
154
+        $this->assertSame('Permission denied', $response->getData()['message']);
155
+    }
156 156
 }
Please login to merge, or discard this patch.
apps/files_external/appinfo/routes.php 1 patch
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -7,38 +7,38 @@
 block discarded – undo
7 7
  */
8 8
 
9 9
 return [
10
-	'resources' => [
11
-		'global_storages' => ['url' => '/globalstorages'],
12
-		'user_storages' => ['url' => '/userstorages'],
13
-		'user_global_storages' => ['url' => '/userglobalstorages'],
14
-	],
15
-	'routes' => [
16
-		[
17
-			'name' => 'Ajax#getApplicableEntities',
18
-			'url' => '/ajax/applicable',
19
-			'verb' => 'GET',
20
-		],
21
-		[
22
-			'name' => 'Ajax#oauth2Callback',
23
-			'url' => '/ajax/oauth2.php',
24
-			'verb' => 'GET',
25
-		],
26
-		[
27
-			'name' => 'Ajax#getSshKeys',
28
-			'url' => '/ajax/public_key.php',
29
-			'verb' => 'POST',
30
-		],
31
-		[
32
-			'name' => 'Ajax#saveGlobalCredentials',
33
-			'url' => '/globalcredentials',
34
-			'verb' => 'POST',
35
-		],
36
-	],
37
-	'ocs' => [
38
-		[
39
-			'name' => 'Api#getUserMounts',
40
-			'url' => '/api/v1/mounts',
41
-			'verb' => 'GET',
42
-		],
43
-	],
10
+    'resources' => [
11
+        'global_storages' => ['url' => '/globalstorages'],
12
+        'user_storages' => ['url' => '/userstorages'],
13
+        'user_global_storages' => ['url' => '/userglobalstorages'],
14
+    ],
15
+    'routes' => [
16
+        [
17
+            'name' => 'Ajax#getApplicableEntities',
18
+            'url' => '/ajax/applicable',
19
+            'verb' => 'GET',
20
+        ],
21
+        [
22
+            'name' => 'Ajax#oauth2Callback',
23
+            'url' => '/ajax/oauth2.php',
24
+            'verb' => 'GET',
25
+        ],
26
+        [
27
+            'name' => 'Ajax#getSshKeys',
28
+            'url' => '/ajax/public_key.php',
29
+            'verb' => 'POST',
30
+        ],
31
+        [
32
+            'name' => 'Ajax#saveGlobalCredentials',
33
+            'url' => '/globalcredentials',
34
+            'verb' => 'POST',
35
+        ],
36
+    ],
37
+    'ocs' => [
38
+        [
39
+            'name' => 'Api#getUserMounts',
40
+            'url' => '/api/v1/mounts',
41
+            'verb' => 'GET',
42
+        ],
43
+    ],
44 44
 ];
Please login to merge, or discard this patch.
tests/lib/UrlGeneratorTest.php 1 patch
Indentation   +255 added lines, -255 removed lines patch added patch discarded remove patch
@@ -22,259 +22,259 @@
 block discarded – undo
22 22
  * @package Test
23 23
  */
24 24
 class UrlGeneratorTest extends \Test\TestCase {
25
-	/** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
26
-	private $config;
27
-	/** @var \PHPUnit\Framework\MockObject\MockObject|IUserSession */
28
-	private $userSession;
29
-	/** @var \PHPUnit\Framework\MockObject\MockObject|ICacheFactory */
30
-	private $cacheFactory;
31
-	/** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
32
-	private $request;
33
-	/** @var \PHPUnit\Framework\MockObject\MockObject|Router */
34
-	private $router;
35
-	/** @var IURLGenerator */
36
-	private $urlGenerator;
37
-	/** @var string */
38
-	private $originalWebRoot;
39
-
40
-	protected function setUp(): void {
41
-		parent::setUp();
42
-		$this->config = $this->createMock(IConfig::class);
43
-		$this->userSession = $this->createMock(IUserSession::class);
44
-		$this->cacheFactory = $this->createMock(ICacheFactory::class);
45
-		$this->request = $this->createMock(IRequest::class);
46
-		$this->router = $this->createMock(Router::class);
47
-		$this->urlGenerator = new URLGenerator(
48
-			$this->config,
49
-			$this->userSession,
50
-			$this->cacheFactory,
51
-			$this->request,
52
-			$this->router
53
-		);
54
-		$this->originalWebRoot = \OC::$WEBROOT;
55
-	}
56
-
57
-	protected function tearDown(): void {
58
-		// Reset webRoot
59
-		\OC::$WEBROOT = $this->originalWebRoot;
60
-	}
61
-
62
-	private function mockBaseUrl() {
63
-		$this->request->expects($this->once())
64
-			->method('getServerProtocol')
65
-			->willReturn('http');
66
-		$this->request->expects($this->once())
67
-			->method('getServerHost')
68
-			->willReturn('localhost');
69
-	}
70
-
71
-	/**
72
-	 * @small
73
-	 * test linkTo URL construction
74
-	 */
75
-	#[\PHPUnit\Framework\Attributes\DataProvider('provideDocRootAppUrlParts')]
76
-	public function testLinkToDocRoot($app, $file, $args, $expectedResult): void {
77
-		\OC::$WEBROOT = '';
78
-		$result = $this->urlGenerator->linkTo($app, $file, $args);
79
-		$this->assertEquals($expectedResult, $result);
80
-	}
81
-
82
-	/**
83
-	 * @small
84
-	 * test linkTo URL construction in sub directory
85
-	 */
86
-	#[\PHPUnit\Framework\Attributes\DataProvider('provideSubDirAppUrlParts')]
87
-	public function testLinkToSubDir($app, $file, $args, $expectedResult): void {
88
-		\OC::$WEBROOT = '/nextcloud';
89
-		$result = $this->urlGenerator->linkTo($app, $file, $args);
90
-		$this->assertEquals($expectedResult, $result);
91
-	}
92
-
93
-	#[\PHPUnit\Framework\Attributes\DataProvider('provideRoutes')]
94
-	public function testLinkToRouteAbsolute($route, $expected): void {
95
-		$this->mockBaseUrl();
96
-		\OC::$WEBROOT = '/nextcloud';
97
-		$this->router->expects($this->once())
98
-			->method('generate')
99
-			->willReturnCallback(function ($routeName, $parameters) {
100
-				if ($routeName === 'core.Preview.getPreview') {
101
-					return '/index.php/core/preview.png';
102
-				} elseif ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') {
103
-					return '/index.php/ocm/shares';
104
-				}
105
-			});
106
-		$result = $this->urlGenerator->linkToRouteAbsolute($route);
107
-		$this->assertEquals($expected, $result);
108
-	}
109
-
110
-	public static function provideRoutes(): array {
111
-		return [
112
-			['core.Preview.getPreview', 'http://localhost/nextcloud/index.php/core/preview.png'],
113
-			['cloud_federation_api.requesthandlercontroller.addShare', 'http://localhost/nextcloud/index.php/ocm/shares'],
114
-		];
115
-	}
116
-
117
-	public static function provideDocRootAppUrlParts(): array {
118
-		return [
119
-			['user_ldap', 'ajax/wizard.php', [], '/index.php/apps/user_ldap/ajax/wizard.php'],
120
-			['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
121
-			['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
122
-		];
123
-	}
124
-
125
-	public static function provideSubDirAppUrlParts(): array {
126
-		return [
127
-			['user_ldap', 'ajax/wizard.php', [], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php'],
128
-			['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
129
-			['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
130
-		];
131
-	}
132
-
133
-	/**
134
-	 * @small
135
-	 * test absolute URL construction
136
-	 */
137
-	#[\PHPUnit\Framework\Attributes\DataProvider('provideDocRootURLs')]
138
-	public function testGetAbsoluteURLDocRoot($url, $expectedResult): void {
139
-		$this->mockBaseUrl();
140
-		\OC::$WEBROOT = '';
141
-		$result = $this->urlGenerator->getAbsoluteURL($url);
142
-		$this->assertEquals($expectedResult, $result);
143
-	}
144
-
145
-	/**
146
-	 * @small
147
-	 * test absolute URL construction
148
-	 */
149
-	#[\PHPUnit\Framework\Attributes\DataProvider('provideSubDirURLs')]
150
-	public function testGetAbsoluteURLSubDir($url, $expectedResult): void {
151
-		$this->mockBaseUrl();
152
-		\OC::$WEBROOT = '/nextcloud';
153
-		$result = $this->urlGenerator->getAbsoluteURL($url);
154
-		$this->assertEquals($expectedResult, $result);
155
-	}
156
-
157
-	public static function provideDocRootURLs(): array {
158
-		return [
159
-			['index.php', 'http://localhost/index.php'],
160
-			['/index.php', 'http://localhost/index.php'],
161
-			['/apps/index.php', 'http://localhost/apps/index.php'],
162
-			['apps/index.php', 'http://localhost/apps/index.php'],
163
-		];
164
-	}
165
-
166
-	public static function provideSubDirURLs(): array {
167
-		return [
168
-			['', 'http://localhost/nextcloud/'],
169
-			['/', 'http://localhost/nextcloud/'],
170
-			['index.php', 'http://localhost/nextcloud/index.php'],
171
-			['/index.php', 'http://localhost/nextcloud/index.php'],
172
-			['/apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
173
-			['apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
174
-		];
175
-	}
176
-
177
-	public function testGetBaseUrl(): void {
178
-		$this->mockBaseUrl();
179
-		\OC::$WEBROOT = '/nextcloud';
180
-		$actual = $this->urlGenerator->getBaseUrl();
181
-		$expected = 'http://localhost/nextcloud';
182
-		$this->assertEquals($expected, $actual);
183
-	}
184
-
185
-	public function testGetWebroot(): void {
186
-		\OC::$WEBROOT = '/nextcloud';
187
-		$actual = $this->urlGenerator->getWebroot();
188
-		$this->assertEquals(\OC::$WEBROOT, $actual);
189
-	}
190
-
191
-	#[\PHPUnit\Framework\Attributes\DataProvider('provideOCSRoutes')]
192
-	public function testLinkToOCSRouteAbsolute(string $route, bool $ignoreFrontController, string $expected): void {
193
-		$this->mockBaseUrl();
194
-		\OC::$WEBROOT = '/nextcloud';
195
-		$this->router->expects($this->once())
196
-			->method('generate')
197
-			->willReturnCallback(function (string $routeName, array $parameters) use ($ignoreFrontController) {
198
-				if ($routeName === 'ocs.core.OCS.getCapabilities') {
199
-					if (!$ignoreFrontController) {
200
-						return '/nextcloud/index.php/ocsapp/cloud/capabilities';
201
-					}
202
-					return '/nextcloud/ocsapp/cloud/capabilities';
203
-				} elseif ($routeName === 'ocs.core.WhatsNew.dismiss') {
204
-					if (!$ignoreFrontController) {
205
-						return '/nextcloud/index.php/ocsapp/core/whatsnew';
206
-					}
207
-					return '/nextcloud/ocsapp/core/whatsnew';
208
-				}
209
-			});
210
-		$result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
211
-		$this->assertEquals($expected, $result);
212
-	}
213
-
214
-	public static function provideOCSRoutes(): array {
215
-		return [
216
-			['core.OCS.getCapabilities', false, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
217
-			['core.OCS.getCapabilities', true, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
218
-			['core.WhatsNew.dismiss', false, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
219
-			['core.WhatsNew.dismiss', true, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
220
-		];
221
-	}
222
-
223
-	private function mockLinkToDefaultPageUrl(bool $ignoreFrontControllerConfig = false) {
224
-		$this->config->expects($this->once())
225
-			->method('getAppValue')
226
-			->with('core', 'defaultpage')
227
-			->willReturn('');
228
-
229
-		$this->config->expects($this->once())
230
-			->method('getSystemValueBool')
231
-			->with('htaccess.IgnoreFrontController', $this->anything())
232
-			->willReturn($ignoreFrontControllerConfig);
233
-	}
234
-
235
-	public function testLinkToDefaultPageUrlWithRedirectUrlWithoutFrontController(): void {
236
-		$this->mockBaseUrl();
237
-
238
-		$_REQUEST['redirect_url'] = 'myRedirectUrl.com';
239
-		$this->assertSame('http://localhost' . \OC::$WEBROOT . '/myRedirectUrl.com', $this->urlGenerator->linkToDefaultPageUrl());
240
-	}
241
-
242
-	public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController(): void {
243
-		$this->mockBaseUrl();
244
-		$this->mockLinkToDefaultPageUrl();
245
-		putenv('front_controller_active=false');
246
-
247
-		$_REQUEST['redirect_url'] = '[email protected]:a';
248
-		$this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
249
-	}
250
-
251
-	public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController(): void {
252
-		$this->mockBaseUrl();
253
-		$this->mockLinkToDefaultPageUrl();
254
-		putenv('front_controller_active=true');
255
-
256
-		$_REQUEST['redirect_url'] = '[email protected]:a';
257
-		$this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
258
-	}
259
-
260
-	public function testLinkToDefaultPageUrlWithRedirectUrlWithIgnoreFrontController(): void {
261
-		$this->mockBaseUrl();
262
-		$this->mockLinkToDefaultPageUrl(true);
263
-		putenv('front_controller_active=false');
264
-
265
-		$_REQUEST['redirect_url'] = '[email protected]:a';
266
-		$this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
267
-	}
268
-
269
-	public static function imagePathProvider(): array {
270
-		return [
271
-			['core', 'favicon-mask.svg', \OC::$WEBROOT . '/core/img/favicon-mask.svg'],
272
-			['files', 'folder.svg', \OC::$WEBROOT . '/apps/files/img/folder.svg'],
273
-		];
274
-	}
275
-
276
-	#[\PHPUnit\Framework\Attributes\DataProvider('imagePathProvider')]
277
-	public function testImagePath(string $appName, string $file, string $result): void {
278
-		$this->assertSame($result, $this->urlGenerator->imagePath($appName, $file));
279
-	}
25
+    /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
26
+    private $config;
27
+    /** @var \PHPUnit\Framework\MockObject\MockObject|IUserSession */
28
+    private $userSession;
29
+    /** @var \PHPUnit\Framework\MockObject\MockObject|ICacheFactory */
30
+    private $cacheFactory;
31
+    /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
32
+    private $request;
33
+    /** @var \PHPUnit\Framework\MockObject\MockObject|Router */
34
+    private $router;
35
+    /** @var IURLGenerator */
36
+    private $urlGenerator;
37
+    /** @var string */
38
+    private $originalWebRoot;
39
+
40
+    protected function setUp(): void {
41
+        parent::setUp();
42
+        $this->config = $this->createMock(IConfig::class);
43
+        $this->userSession = $this->createMock(IUserSession::class);
44
+        $this->cacheFactory = $this->createMock(ICacheFactory::class);
45
+        $this->request = $this->createMock(IRequest::class);
46
+        $this->router = $this->createMock(Router::class);
47
+        $this->urlGenerator = new URLGenerator(
48
+            $this->config,
49
+            $this->userSession,
50
+            $this->cacheFactory,
51
+            $this->request,
52
+            $this->router
53
+        );
54
+        $this->originalWebRoot = \OC::$WEBROOT;
55
+    }
56
+
57
+    protected function tearDown(): void {
58
+        // Reset webRoot
59
+        \OC::$WEBROOT = $this->originalWebRoot;
60
+    }
61
+
62
+    private function mockBaseUrl() {
63
+        $this->request->expects($this->once())
64
+            ->method('getServerProtocol')
65
+            ->willReturn('http');
66
+        $this->request->expects($this->once())
67
+            ->method('getServerHost')
68
+            ->willReturn('localhost');
69
+    }
70
+
71
+    /**
72
+     * @small
73
+     * test linkTo URL construction
74
+     */
75
+    #[\PHPUnit\Framework\Attributes\DataProvider('provideDocRootAppUrlParts')]
76
+    public function testLinkToDocRoot($app, $file, $args, $expectedResult): void {
77
+        \OC::$WEBROOT = '';
78
+        $result = $this->urlGenerator->linkTo($app, $file, $args);
79
+        $this->assertEquals($expectedResult, $result);
80
+    }
81
+
82
+    /**
83
+     * @small
84
+     * test linkTo URL construction in sub directory
85
+     */
86
+    #[\PHPUnit\Framework\Attributes\DataProvider('provideSubDirAppUrlParts')]
87
+    public function testLinkToSubDir($app, $file, $args, $expectedResult): void {
88
+        \OC::$WEBROOT = '/nextcloud';
89
+        $result = $this->urlGenerator->linkTo($app, $file, $args);
90
+        $this->assertEquals($expectedResult, $result);
91
+    }
92
+
93
+    #[\PHPUnit\Framework\Attributes\DataProvider('provideRoutes')]
94
+    public function testLinkToRouteAbsolute($route, $expected): void {
95
+        $this->mockBaseUrl();
96
+        \OC::$WEBROOT = '/nextcloud';
97
+        $this->router->expects($this->once())
98
+            ->method('generate')
99
+            ->willReturnCallback(function ($routeName, $parameters) {
100
+                if ($routeName === 'core.Preview.getPreview') {
101
+                    return '/index.php/core/preview.png';
102
+                } elseif ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') {
103
+                    return '/index.php/ocm/shares';
104
+                }
105
+            });
106
+        $result = $this->urlGenerator->linkToRouteAbsolute($route);
107
+        $this->assertEquals($expected, $result);
108
+    }
109
+
110
+    public static function provideRoutes(): array {
111
+        return [
112
+            ['core.Preview.getPreview', 'http://localhost/nextcloud/index.php/core/preview.png'],
113
+            ['cloud_federation_api.requesthandlercontroller.addShare', 'http://localhost/nextcloud/index.php/ocm/shares'],
114
+        ];
115
+    }
116
+
117
+    public static function provideDocRootAppUrlParts(): array {
118
+        return [
119
+            ['user_ldap', 'ajax/wizard.php', [], '/index.php/apps/user_ldap/ajax/wizard.php'],
120
+            ['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
121
+            ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
122
+        ];
123
+    }
124
+
125
+    public static function provideSubDirAppUrlParts(): array {
126
+        return [
127
+            ['user_ldap', 'ajax/wizard.php', [], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php'],
128
+            ['user_ldap', 'ajax/wizard.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/user_ldap/ajax/wizard.php?trut=trat&dut=dat'],
129
+            ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
130
+        ];
131
+    }
132
+
133
+    /**
134
+     * @small
135
+     * test absolute URL construction
136
+     */
137
+    #[\PHPUnit\Framework\Attributes\DataProvider('provideDocRootURLs')]
138
+    public function testGetAbsoluteURLDocRoot($url, $expectedResult): void {
139
+        $this->mockBaseUrl();
140
+        \OC::$WEBROOT = '';
141
+        $result = $this->urlGenerator->getAbsoluteURL($url);
142
+        $this->assertEquals($expectedResult, $result);
143
+    }
144
+
145
+    /**
146
+     * @small
147
+     * test absolute URL construction
148
+     */
149
+    #[\PHPUnit\Framework\Attributes\DataProvider('provideSubDirURLs')]
150
+    public function testGetAbsoluteURLSubDir($url, $expectedResult): void {
151
+        $this->mockBaseUrl();
152
+        \OC::$WEBROOT = '/nextcloud';
153
+        $result = $this->urlGenerator->getAbsoluteURL($url);
154
+        $this->assertEquals($expectedResult, $result);
155
+    }
156
+
157
+    public static function provideDocRootURLs(): array {
158
+        return [
159
+            ['index.php', 'http://localhost/index.php'],
160
+            ['/index.php', 'http://localhost/index.php'],
161
+            ['/apps/index.php', 'http://localhost/apps/index.php'],
162
+            ['apps/index.php', 'http://localhost/apps/index.php'],
163
+        ];
164
+    }
165
+
166
+    public static function provideSubDirURLs(): array {
167
+        return [
168
+            ['', 'http://localhost/nextcloud/'],
169
+            ['/', 'http://localhost/nextcloud/'],
170
+            ['index.php', 'http://localhost/nextcloud/index.php'],
171
+            ['/index.php', 'http://localhost/nextcloud/index.php'],
172
+            ['/apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
173
+            ['apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
174
+        ];
175
+    }
176
+
177
+    public function testGetBaseUrl(): void {
178
+        $this->mockBaseUrl();
179
+        \OC::$WEBROOT = '/nextcloud';
180
+        $actual = $this->urlGenerator->getBaseUrl();
181
+        $expected = 'http://localhost/nextcloud';
182
+        $this->assertEquals($expected, $actual);
183
+    }
184
+
185
+    public function testGetWebroot(): void {
186
+        \OC::$WEBROOT = '/nextcloud';
187
+        $actual = $this->urlGenerator->getWebroot();
188
+        $this->assertEquals(\OC::$WEBROOT, $actual);
189
+    }
190
+
191
+    #[\PHPUnit\Framework\Attributes\DataProvider('provideOCSRoutes')]
192
+    public function testLinkToOCSRouteAbsolute(string $route, bool $ignoreFrontController, string $expected): void {
193
+        $this->mockBaseUrl();
194
+        \OC::$WEBROOT = '/nextcloud';
195
+        $this->router->expects($this->once())
196
+            ->method('generate')
197
+            ->willReturnCallback(function (string $routeName, array $parameters) use ($ignoreFrontController) {
198
+                if ($routeName === 'ocs.core.OCS.getCapabilities') {
199
+                    if (!$ignoreFrontController) {
200
+                        return '/nextcloud/index.php/ocsapp/cloud/capabilities';
201
+                    }
202
+                    return '/nextcloud/ocsapp/cloud/capabilities';
203
+                } elseif ($routeName === 'ocs.core.WhatsNew.dismiss') {
204
+                    if (!$ignoreFrontController) {
205
+                        return '/nextcloud/index.php/ocsapp/core/whatsnew';
206
+                    }
207
+                    return '/nextcloud/ocsapp/core/whatsnew';
208
+                }
209
+            });
210
+        $result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
211
+        $this->assertEquals($expected, $result);
212
+    }
213
+
214
+    public static function provideOCSRoutes(): array {
215
+        return [
216
+            ['core.OCS.getCapabilities', false, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
217
+            ['core.OCS.getCapabilities', true, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
218
+            ['core.WhatsNew.dismiss', false, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
219
+            ['core.WhatsNew.dismiss', true, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
220
+        ];
221
+    }
222
+
223
+    private function mockLinkToDefaultPageUrl(bool $ignoreFrontControllerConfig = false) {
224
+        $this->config->expects($this->once())
225
+            ->method('getAppValue')
226
+            ->with('core', 'defaultpage')
227
+            ->willReturn('');
228
+
229
+        $this->config->expects($this->once())
230
+            ->method('getSystemValueBool')
231
+            ->with('htaccess.IgnoreFrontController', $this->anything())
232
+            ->willReturn($ignoreFrontControllerConfig);
233
+    }
234
+
235
+    public function testLinkToDefaultPageUrlWithRedirectUrlWithoutFrontController(): void {
236
+        $this->mockBaseUrl();
237
+
238
+        $_REQUEST['redirect_url'] = 'myRedirectUrl.com';
239
+        $this->assertSame('http://localhost' . \OC::$WEBROOT . '/myRedirectUrl.com', $this->urlGenerator->linkToDefaultPageUrl());
240
+    }
241
+
242
+    public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController(): void {
243
+        $this->mockBaseUrl();
244
+        $this->mockLinkToDefaultPageUrl();
245
+        putenv('front_controller_active=false');
246
+
247
+        $_REQUEST['redirect_url'] = '[email protected]:a';
248
+        $this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
249
+    }
250
+
251
+    public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController(): void {
252
+        $this->mockBaseUrl();
253
+        $this->mockLinkToDefaultPageUrl();
254
+        putenv('front_controller_active=true');
255
+
256
+        $_REQUEST['redirect_url'] = '[email protected]:a';
257
+        $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
258
+    }
259
+
260
+    public function testLinkToDefaultPageUrlWithRedirectUrlWithIgnoreFrontController(): void {
261
+        $this->mockBaseUrl();
262
+        $this->mockLinkToDefaultPageUrl(true);
263
+        putenv('front_controller_active=false');
264
+
265
+        $_REQUEST['redirect_url'] = '[email protected]:a';
266
+        $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
267
+    }
268
+
269
+    public static function imagePathProvider(): array {
270
+        return [
271
+            ['core', 'favicon-mask.svg', \OC::$WEBROOT . '/core/img/favicon-mask.svg'],
272
+            ['files', 'folder.svg', \OC::$WEBROOT . '/apps/files/img/folder.svg'],
273
+        ];
274
+    }
275
+
276
+    #[\PHPUnit\Framework\Attributes\DataProvider('imagePathProvider')]
277
+    public function testImagePath(string $appName, string $file, string $result): void {
278
+        $this->assertSame($result, $this->urlGenerator->imagePath($appName, $file));
279
+    }
280 280
 }
Please login to merge, or discard this patch.