Skip to content

Commit d078d53

Browse files
committed
Fix tests
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent cc6653e commit d078d53

10 files changed

Lines changed: 121 additions & 136 deletions

File tree

apps/dav/tests/unit/Connector/Sabre/FileTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
use OC\Files\Storage\Temporary;
3636
use OC\Files\Storage\Wrapper\PermissionsMask;
3737
use OC\Files\View;
38-
use OC\Security\SecureRandom;
3938
use OCA\DAV\Connector\Sabre\File;
4039
use OCP\Constants;
4140
use OCP\Files\ForbiddenException;
4241
use OCP\Files\Storage;
4342
use OCP\IConfig;
43+
use OCP\IRequestId;
4444
use OCP\Lock\ILockingProvider;
45-
use OCP\Security\ISecureRandom;
45+
use PHPUnit\Framework\MockObject\MockObject;
4646
use Test\HookHelper;
4747
use Test\TestCase;
4848
use Test\Traits\MountProviderTrait;
@@ -64,11 +64,11 @@ class FileTest extends TestCase {
6464
*/
6565
private $user;
6666

67-
/** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
67+
/** @var IConfig|MockObject */
6868
protected $config;
6969

70-
/** @var ISecureRandom */
71-
protected $secureRandom;
70+
/** @var IRequestId|MockObject */
71+
protected $requestId;
7272

7373
protected function setUp(): void {
7474
parent::setUp();
@@ -83,8 +83,8 @@ protected function setUp(): void {
8383

8484
$this->loginAsUser($this->user);
8585

86-
$this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
87-
$this->secureRandom = new SecureRandom();
86+
$this->config = $this->createMock(IConfig::class);
87+
$this->requestId = $this->createMock(IRequestId::class);
8888
}
8989

9090
protected function tearDown(): void {
@@ -96,7 +96,7 @@ protected function tearDown(): void {
9696
}
9797

9898
/**
99-
* @return \PHPUnit\Framework\MockObject\MockObject|Storage
99+
* @return MockObject|Storage
100100
*/
101101
private function getMockStorage() {
102102
$storage = $this->getMockBuilder(Storage::class)
@@ -184,7 +184,7 @@ public function testSimplePutFails($thrownException, $expectedException, $checkP
184184
->setConstructorArgs([['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]])
185185
->getMock();
186186
\OC\Files\Filesystem::mount($storage, [], $this->user . '/');
187-
/** @var View | \PHPUnit\Framework\MockObject\MockObject $view */
187+
/** @var View | MockObject $view */
188188
$view = $this->getMockBuilder(View::class)
189189
->setMethods(['getRelativePath', 'resolvePath'])
190190
->getMock();
@@ -330,7 +330,7 @@ private function doPut($path, $viewRoot = null, Request $request = null) {
330330
null
331331
);
332332

333-
/** @var \OCA\DAV\Connector\Sabre\File | \PHPUnit\Framework\MockObject\MockObject $file */
333+
/** @var \OCA\DAV\Connector\Sabre\File | MockObject $file */
334334
$file = $this->getMockBuilder(\OCA\DAV\Connector\Sabre\File::class)
335335
->setConstructorArgs([$view, $info, null, $request])
336336
->setMethods(['header'])
@@ -416,7 +416,7 @@ public function testPutSingleFileLegalMtime($requestMtime, $resultMtime) {
416416
'server' => [
417417
'HTTP_X_OC_MTIME' => $requestMtime,
418418
]
419-
], $this->secureRandom, $this->config, null);
419+
], $this->requestId, $this->config, null);
420420
$file = 'foo.txt';
421421

422422
if ($resultMtime === null) {
@@ -439,7 +439,7 @@ public function testChunkedPutLegalMtime($requestMtime, $resultMtime) {
439439
'server' => [
440440
'HTTP_X_OC_MTIME' => $requestMtime,
441441
]
442-
], $this->secureRandom, $this->config, null);
442+
], $this->requestId, $this->config, null);
443443

444444
$_SERVER['HTTP_OC_CHUNKED'] = true;
445445
$file = 'foo.txt';

tests/Core/Middleware/TwoFactorMiddlewareTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
use OC\Core\Middleware\TwoFactorMiddleware;
3232
use OC\User\Session;
3333
use OCP\AppFramework\Controller;
34+
use OCP\AppFramework\Http\RedirectResponse;
3435
use OCP\AppFramework\Utility\IControllerMethodReflector;
3536
use OCP\Authentication\TwoFactorAuth\ALoginSetupController;
3637
use OCP\Authentication\TwoFactorAuth\IProvider;
3738
use OCP\IConfig;
3839
use OCP\IRequest;
40+
use OCP\IRequestId;
3941
use OCP\ISession;
4042
use OCP\IURLGenerator;
4143
use OCP\IUser;
4244
use OCP\IUserSession;
43-
use OCP\Security\ISecureRandom;
4445
use PHPUnit\Framework\MockObject\MockObject;
4546
use Test\TestCase;
4647

@@ -88,7 +89,7 @@ protected function setUp(): void {
8889
'REQUEST_URI' => 'test/url'
8990
]
9091
],
91-
$this->createMock(ISecureRandom::class),
92+
$this->createMock(IRequestId::class),
9293
$this->createMock(IConfig::class)
9394
);
9495

@@ -139,9 +140,9 @@ public function testBeforeControllerNoTwoFactorCheckNeeded() {
139140
$this->middleware->beforeController($this->controller, 'index');
140141
}
141142

142-
143+
143144
public function testBeforeControllerTwoFactorAuthRequired() {
144-
$this->expectException(\OC\Authentication\Exceptions\TwoFactorAuthRequiredException::class);
145+
$this->expectException(TwoFactorAuthRequiredException::class);
145146

146147
$user = $this->createMock(IUser::class);
147148

@@ -163,9 +164,9 @@ public function testBeforeControllerTwoFactorAuthRequired() {
163164
$this->middleware->beforeController($this->controller, 'index');
164165
}
165166

166-
167+
167168
public function testBeforeControllerUserAlreadyLoggedIn() {
168-
$this->expectException(\OC\Authentication\Exceptions\UserAlreadyLoggedInException::class);
169+
$this->expectException(UserAlreadyLoggedInException::class);
169170

170171
$user = $this->createMock(IUser::class);
171172

@@ -187,32 +188,32 @@ public function testBeforeControllerUserAlreadyLoggedIn() {
187188
->with($user)
188189
->willReturn(false);
189190

190-
$twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController')
191+
$twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class)
191192
->disableOriginalConstructor()
192193
->getMock();
193194
$this->middleware->beforeController($twoFactorChallengeController, 'index');
194195
}
195196

196197
public function testAfterExceptionTwoFactorAuthRequired() {
197-
$ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException();
198+
$ex = new TwoFactorAuthRequiredException();
198199

199200
$this->urlGenerator->expects($this->once())
200201
->method('linkToRoute')
201202
->with('core.TwoFactorChallenge.selectChallenge')
202203
->willReturn('test/url');
203-
$expected = new \OCP\AppFramework\Http\RedirectResponse('test/url');
204+
$expected = new RedirectResponse('test/url');
204205

205206
$this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
206207
}
207208

208209
public function testAfterException() {
209-
$ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException();
210+
$ex = new UserAlreadyLoggedInException();
210211

211212
$this->urlGenerator->expects($this->once())
212213
->method('linkToRoute')
213214
->with('files.view.index')
214215
->willReturn('redirect/url');
215-
$expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url');
216+
$expected = new RedirectResponse('redirect/url');
216217

217218
$this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
218219
}

tests/lib/AppFramework/Controller/ApiControllerTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OC\AppFramework\Http\Request;
2727
use OCP\AppFramework\ApiController;
2828
use OCP\IConfig;
29+
use OCP\IRequestId;
2930

3031
class ChildApiController extends ApiController {
3132
};
@@ -38,12 +39,8 @@ class ApiControllerTest extends \Test\TestCase {
3839
public function testCors() {
3940
$request = new Request(
4041
['server' => ['HTTP_ORIGIN' => 'test']],
41-
$this->getMockBuilder('\OCP\Security\ISecureRandom')
42-
->disableOriginalConstructor()
43-
->getMock(),
44-
$this->getMockBuilder(IConfig::class)
45-
->disableOriginalConstructor()
46-
->getMock()
42+
$this->createMock(IRequestId::class),
43+
$this->createMock(IConfig::class)
4744
);
4845
$this->controller = new ChildApiController('app', $request, 'verbs',
4946
'headers', 100);

tests/lib/AppFramework/Controller/ControllerTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
use OCP\AppFramework\Http\JSONResponse;
3030
use OCP\IConfig;
3131
use OCP\IRequest;
32+
use OCP\IRequestId;
33+
use OC\AppFramework\DependencyInjection\DIContainer;
3234

3335
class ChildController extends Controller {
3436
public function __construct($appName, $request) {
@@ -75,15 +77,11 @@ protected function setUp(): void {
7577
'session' => ['sezession' => 'kein'],
7678
'method' => 'hi',
7779
],
78-
$this->getMockBuilder('\OCP\Security\ISecureRandom')
79-
->disableOriginalConstructor()
80-
->getMock(),
81-
$this->getMockBuilder(IConfig::class)
82-
->disableOriginalConstructor()
83-
->getMock()
80+
$this->createMock(IRequestId::class),
81+
$this->createMock(IConfig::class)
8482
);
8583

86-
$this->app = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer')
84+
$this->app = $this->getMockBuilder(DIContainer::class)
8785
->setMethods(['getAppName'])
8886
->setConstructorArgs(['test'])
8987
->getMock();

tests/lib/AppFramework/Controller/OCSControllerTest.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
2929
use OCP\AppFramework\OCSController;
3030
use OCP\IConfig;
31-
use OCP\Security\ISecureRandom;
31+
use OCP\IRequestId;
3232

3333
class ChildOCSController extends OCSController {
3434
}
@@ -42,12 +42,8 @@ public function testCors() {
4242
'HTTP_ORIGIN' => 'test',
4343
],
4444
],
45-
$this->getMockBuilder(ISecureRandom::class)
46-
->disableOriginalConstructor()
47-
->getMock(),
48-
$this->getMockBuilder(IConfig::class)
49-
->disableOriginalConstructor()
50-
->getMock()
45+
$this->createMock(IRequestId::class),
46+
$this->createMock(IConfig::class)
5147
);
5248
$controller = new ChildOCSController('app', $request, 'verbs',
5349
'headers', 100);
@@ -67,12 +63,8 @@ public function testCors() {
6763
public function testXML() {
6864
$controller = new ChildOCSController('app', new Request(
6965
[],
70-
$this->getMockBuilder(ISecureRandom::class)
71-
->disableOriginalConstructor()
72-
->getMock(),
73-
$this->getMockBuilder(IConfig::class)
74-
->disableOriginalConstructor()
75-
->getMock()
66+
$this->createMock(IRequestId::class),
67+
$this->createMock(IConfig::class)
7668
));
7769
$controller->setOCSVersion(1);
7870

@@ -100,12 +92,8 @@ public function testXML() {
10092
public function testJSON() {
10193
$controller = new ChildOCSController('app', new Request(
10294
[],
103-
$this->getMockBuilder(ISecureRandom::class)
104-
->disableOriginalConstructor()
105-
->getMock(),
106-
$this->getMockBuilder(IConfig::class)
107-
->disableOriginalConstructor()
108-
->getMock()
95+
$this->createMock(IRequestId::class),
96+
$this->createMock(IConfig::class)
10997
));
11098
$controller->setOCSVersion(1);
11199
$expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' .
@@ -121,12 +109,8 @@ public function testJSON() {
121109
public function testXMLV2() {
122110
$controller = new ChildOCSController('app', new Request(
123111
[],
124-
$this->getMockBuilder(ISecureRandom::class)
125-
->disableOriginalConstructor()
126-
->getMock(),
127-
$this->getMockBuilder(IConfig::class)
128-
->disableOriginalConstructor()
129-
->getMock()
112+
$this->createMock(IRequestId::class),
113+
$this->createMock(IConfig::class)
130114
));
131115
$controller->setOCSVersion(2);
132116

@@ -152,12 +136,8 @@ public function testXMLV2() {
152136
public function testJSONV2() {
153137
$controller = new ChildOCSController('app', new Request(
154138
[],
155-
$this->getMockBuilder(ISecureRandom::class)
156-
->disableOriginalConstructor()
157-
->getMock(),
158-
$this->getMockBuilder(IConfig::class)
159-
->disableOriginalConstructor()
160-
->getMock()
139+
$this->createMock(IRequestId::class),
140+
$this->createMock(IConfig::class)
161141
));
162142
$controller->setOCSVersion(2);
163143
$expected = '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},"data":{"test":"hi"}}}';

tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCP\AppFramework\Http\Response;
2929
use OCP\AppFramework\Middleware;
3030
use OCP\IConfig;
31+
use OCP\IRequestId;
3132

3233
// needed to test ordering
3334
class TestMiddleware extends Middleware {
@@ -129,8 +130,8 @@ private function getControllerMock() {
129130
->setConstructorArgs(['app',
130131
new Request(
131132
['method' => 'GET'],
132-
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
133-
$this->getMockBuilder(IConfig::class)->getMock()
133+
$this->createMock(IRequestId::class),
134+
$this->createMock(IConfig::class)
134135
)
135136
])->getMock();
136137
}
@@ -272,7 +273,7 @@ public function testBeforeOutputOrder() {
272273
public function testExceptionShouldRunAfterExceptionOfOnlyPreviouslyExecutedMiddlewares() {
273274
$m1 = $this->getMiddleware();
274275
$m2 = $this->getMiddleware(true);
275-
$m3 = $this->getMockBuilder('\OCP\AppFramework\Middleware')->getMock();
276+
$m3 = $this->createMock(Middleware::class);
276277
$m3->expects($this->never())
277278
->method('afterException');
278279
$m3->expects($this->never())

tests/lib/AppFramework/Middleware/MiddlewareTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
namespace Test\AppFramework\Middleware;
2525

2626
use OC\AppFramework\Http\Request;
27+
use OCP\AppFramework\Controller;
2728
use OCP\AppFramework\Http\Response;
2829
use OCP\AppFramework\Middleware;
2930
use OCP\IConfig;
31+
use OCP\IRequestId;
32+
use OC\AppFramework\DependencyInjection\DIContainer;
3033

3134
class ChildMiddleware extends Middleware {
3235
};
@@ -49,22 +52,22 @@ protected function setUp(): void {
4952

5053
$this->middleware = new ChildMiddleware();
5154

52-
$this->api = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer')
55+
$this->api = $this->getMockBuilder(DIContainer::class)
5356
->disableOriginalConstructor()
5457
->getMock();
5558

56-
$this->controller = $this->getMockBuilder('OCP\AppFramework\Controller')
59+
$this->controller = $this->getMockBuilder(Controller::class)
5760
->setMethods([])
5861
->setConstructorArgs([
5962
$this->api,
6063
new Request(
6164
[],
62-
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
63-
$this->getMockBuilder(IConfig::class)->getMock()
65+
$this->createMock(IRequestId::class),
66+
$this->createMock(IConfig::class)
6467
)
6568
])->getMock();
6669
$this->exception = new \Exception();
67-
$this->response = $this->getMockBuilder('OCP\AppFramework\Http\Response')->getMock();
70+
$this->response = $this->getMockBuilder(Response::class)->getMock();
6871
}
6972

7073

0 commit comments

Comments
 (0)