Completed
Push — master ( b61757...6f0255 )
by John
19:53 queued 16s
created
tests/Core/Controller/AvatarControllerTest.php 1 patch
Indentation   +406 added lines, -406 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
  * proper unit testing of the postAvatar call.
14 14
  */
15 15
 function is_uploaded_file($filename) {
16
-	return file_exists($filename);
16
+    return file_exists($filename);
17 17
 }
18 18
 
19 19
 namespace Tests\Core\Controller;
@@ -41,409 +41,409 @@  discard block
 block discarded – undo
41 41
  * @package OC\Core\Controller
42 42
  */
43 43
 class AvatarControllerTest extends \Test\TestCase {
44
-	/** @var AvatarController */
45
-	private $avatarController;
46
-	/** @var GuestAvatarController */
47
-	private $guestAvatarController;
48
-
49
-	/** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */
50
-	private $avatarMock;
51
-	/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
52
-	private $userMock;
53
-	/** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */
54
-	private $avatarFile;
55
-	/** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
56
-	private $avatarManager;
57
-	/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
58
-	private $l;
59
-	/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
60
-	private $userManager;
61
-	/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
62
-	private $rootFolder;
63
-	/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
64
-	private $logger;
65
-	/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
66
-	private $request;
67
-	/** @var TimeFactory|\PHPUnit\Framework\MockObject\MockObject */
68
-	private $timeFactory;
69
-
70
-	protected function setUp(): void {
71
-		parent::setUp();
72
-
73
-		$this->avatarManager = $this->getMockBuilder('OCP\IAvatarManager')->getMock();
74
-		$this->l = $this->getMockBuilder(IL10N::class)->getMock();
75
-		$this->l->method('t')->willReturnArgument(0);
76
-		$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
77
-		$this->request = $this->getMockBuilder(IRequest::class)->getMock();
78
-		$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
79
-		$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
80
-		$this->timeFactory = $this->getMockBuilder('OC\AppFramework\Utility\TimeFactory')->getMock();
81
-
82
-		$this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock();
83
-		$this->userMock = $this->getMockBuilder(IUser::class)->getMock();
84
-
85
-		$this->guestAvatarController = new GuestAvatarController(
86
-			'core',
87
-			$this->request,
88
-			$this->avatarManager,
89
-			$this->logger
90
-		);
91
-
92
-		$this->avatarController = new AvatarController(
93
-			'core',
94
-			$this->request,
95
-			$this->avatarManager,
96
-			$this->l,
97
-			$this->userManager,
98
-			$this->rootFolder,
99
-			$this->logger,
100
-			'userid',
101
-			$this->timeFactory,
102
-			$this->guestAvatarController,
103
-		);
104
-
105
-		// Configure userMock
106
-		$this->userMock->method('getDisplayName')->willReturn('displayName');
107
-		$this->userMock->method('getUID')->willReturn('userId');
108
-		$this->userManager->method('get')
109
-			->willReturnMap([['userId', $this->userMock]]);
110
-
111
-		$this->avatarFile = $this->getMockBuilder(ISimpleFile::class)->getMock();
112
-		$this->avatarFile->method('getContent')->willReturn('image data');
113
-		$this->avatarFile->method('getMimeType')->willReturn('image type');
114
-		$this->avatarFile->method('getEtag')->willReturn('my etag');
115
-		$this->avatarFile->method('getName')->willReturn('my name');
116
-		$this->avatarFile->method('getMTime')->willReturn(42);
117
-	}
118
-
119
-	protected function tearDown(): void {
120
-		parent::tearDown();
121
-	}
122
-
123
-	/**
124
-	 * Fetch an avatar if a user has no avatar
125
-	 */
126
-	public function testGetAvatarNoAvatar(): void {
127
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
128
-		$this->avatarMock->method('getFile')->willThrowException(new NotFoundException());
129
-		$response = $this->avatarController->getAvatar('userId', 32);
130
-
131
-		//Comment out until JS is fixed
132
-		$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
133
-	}
134
-
135
-	/**
136
-	 * Fetch the user's avatar
137
-	 */
138
-	public function testGetAvatar(): void {
139
-		$this->avatarMock->method('getFile')->willReturn($this->avatarFile);
140
-		$this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
141
-		$this->avatarMock->expects($this->once())
142
-			->method('isCustomAvatar')
143
-			->willReturn(true);
144
-
145
-		$response = $this->avatarController->getAvatar('userId', 32);
146
-
147
-		$this->assertEquals(Http::STATUS_OK, $response->getStatus());
148
-		$this->assertArrayHasKey('Content-Type', $response->getHeaders());
149
-		$this->assertEquals('image type', $response->getHeaders()['Content-Type']);
150
-		$this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders());
151
-		$this->assertEquals('1', $response->getHeaders()['X-NC-IsCustomAvatar']);
152
-
153
-		$this->assertEquals('my etag', $response->getETag());
154
-	}
155
-
156
-	/**
157
-	 * Fetch the user's avatar
158
-	 */
159
-	public function testGetGeneratedAvatar(): void {
160
-		$this->avatarMock->method('getFile')->willReturn($this->avatarFile);
161
-		$this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
162
-
163
-		$response = $this->avatarController->getAvatar('userId', 32);
164
-
165
-		$this->assertEquals(Http::STATUS_OK, $response->getStatus());
166
-		$this->assertArrayHasKey('Content-Type', $response->getHeaders());
167
-		$this->assertEquals('image type', $response->getHeaders()['Content-Type']);
168
-		$this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders());
169
-		$this->assertEquals('0', $response->getHeaders()['X-NC-IsCustomAvatar']);
170
-
171
-		$this->assertEquals('my etag', $response->getETag());
172
-	}
173
-
174
-	/**
175
-	 * Fetch the avatar of a non-existing user
176
-	 */
177
-	public function testGetAvatarNoUser(): void {
178
-		$this->avatarManager
179
-			->method('getAvatar')
180
-			->with('userDoesNotExist')
181
-			->willThrowException(new \Exception('user does not exist'));
182
-
183
-		$response = $this->avatarController->getAvatar('userDoesNotExist', 32);
184
-
185
-		//Comment out until JS is fixed
186
-		$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
187
-	}
188
-
189
-	public function testGetAvatarSize64(): void {
190
-		$this->avatarMock->expects($this->once())
191
-			->method('getFile')
192
-			->with($this->equalTo(64))
193
-			->willReturn($this->avatarFile);
194
-
195
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
196
-
197
-		$this->logger->expects($this->never())
198
-			->method('debug');
199
-
200
-		$this->avatarController->getAvatar('userId', 64);
201
-	}
202
-
203
-	public function testGetAvatarSize512(): void {
204
-		$this->avatarMock->expects($this->once())
205
-			->method('getFile')
206
-			->with($this->equalTo(512))
207
-			->willReturn($this->avatarFile);
208
-
209
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
210
-
211
-		$this->logger->expects($this->never())
212
-			->method('debug');
213
-
214
-		$this->avatarController->getAvatar('userId', 512);
215
-	}
216
-
217
-	/**
218
-	 * Small sizes return 64 and generate a log
219
-	 */
220
-	public function testGetAvatarSizeTooSmall(): void {
221
-		$this->avatarMock->expects($this->once())
222
-			->method('getFile')
223
-			->with($this->equalTo(64))
224
-			->willReturn($this->avatarFile);
225
-
226
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
227
-
228
-		$this->logger->expects($this->once())
229
-			->method('debug')
230
-			->with('Avatar requested in deprecated size 32');
231
-
232
-		$this->avatarController->getAvatar('userId', 32);
233
-	}
234
-
235
-	/**
236
-	 * Avatars between 64 and 512 are upgraded to 512
237
-	 */
238
-	public function testGetAvatarSizeBetween(): void {
239
-		$this->avatarMock->expects($this->once())
240
-			->method('getFile')
241
-			->with($this->equalTo(512))
242
-			->willReturn($this->avatarFile);
243
-
244
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
245
-
246
-		$this->logger->expects($this->once())
247
-			->method('debug')
248
-			->with('Avatar requested in deprecated size 65');
249
-
250
-		$this->avatarController->getAvatar('userId', 65);
251
-	}
252
-
253
-	/**
254
-	 * We do not support avatars larger than 512
255
-	 */
256
-	public function testGetAvatarSizeTooBig(): void {
257
-		$this->avatarMock->expects($this->once())
258
-			->method('getFile')
259
-			->with($this->equalTo(512))
260
-			->willReturn($this->avatarFile);
261
-
262
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
263
-
264
-		$this->logger->expects($this->once())
265
-			->method('debug')
266
-			->with('Avatar requested in deprecated size 513');
267
-
268
-		$this->avatarController->getAvatar('userId', 513);
269
-	}
270
-
271
-	/**
272
-	 * Remove an avatar
273
-	 */
274
-	public function testDeleteAvatar(): void {
275
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
276
-
277
-		$response = $this->avatarController->deleteAvatar();
278
-		$this->assertEquals(Http::STATUS_OK, $response->getStatus());
279
-	}
280
-
281
-	/**
282
-	 * Test what happens if the removing of the avatar fails
283
-	 */
284
-	public function testDeleteAvatarException(): void {
285
-		$this->avatarMock->method('remove')->willThrowException(new \Exception('foo'));
286
-		$this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
287
-
288
-		$this->logger->expects($this->once())
289
-			->method('error')
290
-			->with('foo', ['exception' => new \Exception('foo'), 'app' => 'core']);
291
-		$expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
292
-		$this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar());
293
-	}
294
-
295
-	/**
296
-	 * When trying to post a new avatar a path or image should be posted.
297
-	 */
298
-	public function testPostAvatarNoPathOrImage(): void {
299
-		$response = $this->avatarController->postAvatar(null);
300
-
301
-		$this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
302
-	}
303
-
304
-	/**
305
-	 * Test a correct post of an avatar using POST
306
-	 */
307
-	public function testPostAvatarFile(): void {
308
-		//Create temp file
309
-		$fileName = tempnam('', 'avatarTest');
310
-		$copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.jpg', $fileName);
311
-		$this->assertTrue($copyRes);
312
-
313
-		//Create request return
314
-		$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.jpg')]];
315
-		$this->request->method('getUploadedFile')->willReturn($reqRet);
316
-
317
-		$response = $this->avatarController->postAvatar(null);
318
-
319
-		//On correct upload always respond with the notsquare message
320
-		$this->assertEquals('notsquare', $response->getData()['data']);
321
-
322
-		//File should be deleted
323
-		$this->assertFalse(file_exists($fileName));
324
-	}
325
-
326
-	/**
327
-	 * Test invalid post os an avatar using POST
328
-	 */
329
-	public function testPostAvatarInvalidFile(): void {
330
-		//Create request return
331
-		$reqRet = ['error' => [1], 'tmp_name' => ['foo']];
332
-		$this->request->method('getUploadedFile')->willReturn($reqRet);
333
-
334
-		$response = $this->avatarController->postAvatar(null);
335
-
336
-		$this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
337
-	}
338
-
339
-	/**
340
-	 * Check what happens when we upload a GIF
341
-	 */
342
-	public function testPostAvatarFileGif(): void {
343
-		//Create temp file
344
-		$fileName = tempnam('', 'avatarTest');
345
-		$copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.gif', $fileName);
346
-		$this->assertTrue($copyRes);
347
-
348
-		//Create request return
349
-		$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.gif')]];
350
-		$this->request->method('getUploadedFile')->willReturn($reqRet);
351
-
352
-		$response = $this->avatarController->postAvatar(null);
353
-
354
-		$this->assertEquals('Unknown filetype', $response->getData()['data']['message']);
355
-
356
-		//File should be deleted
357
-		$this->assertFalse(file_exists($fileName));
358
-	}
359
-
360
-	/**
361
-	 * Test posting avatar from existing file
362
-	 */
363
-	public function testPostAvatarFromFile(): void {
364
-		//Mock node API call
365
-		$file = $this->getMockBuilder('OCP\Files\File')
366
-			->disableOriginalConstructor()->getMock();
367
-		$file->expects($this->once())
368
-			->method('getContent')
369
-			->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg'));
370
-		$file->expects($this->once())
371
-			->method('getMimeType')
372
-			->willReturn('image/jpeg');
373
-		$userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
374
-		$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
375
-		$userFolder->method('get')->willReturn($file);
376
-
377
-		//Create request return
378
-		$response = $this->avatarController->postAvatar('avatar.jpg');
379
-
380
-		//On correct upload always respond with the notsquare message
381
-		$this->assertEquals('notsquare', $response->getData()['data']);
382
-	}
383
-
384
-	/**
385
-	 * Test posting avatar from existing folder
386
-	 */
387
-	public function testPostAvatarFromNoFile(): void {
388
-		$file = $this->getMockBuilder('OCP\Files\Node')->getMock();
389
-		$userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
390
-		$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
391
-		$userFolder
392
-			->method('get')
393
-			->with('folder')
394
-			->willReturn($file);
395
-
396
-		//Create request return
397
-		$response = $this->avatarController->postAvatar('folder');
398
-
399
-		//On correct upload always respond with the notsquare message
400
-		$this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData());
401
-	}
402
-
403
-	public function testPostAvatarInvalidType(): void {
404
-		$file = $this->getMockBuilder('OCP\Files\File')
405
-			->disableOriginalConstructor()->getMock();
406
-		$file->expects($this->never())
407
-			->method('getContent');
408
-		$file->expects($this->exactly(2))
409
-			->method('getMimeType')
410
-			->willReturn('text/plain');
411
-		$userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
412
-		$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
413
-		$userFolder->method('get')->willReturn($file);
414
-
415
-		$expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST);
416
-		$this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
417
-	}
418
-
419
-	public function testPostAvatarNotPermittedException(): void {
420
-		$file = $this->getMockBuilder('OCP\Files\File')
421
-			->disableOriginalConstructor()->getMock();
422
-		$file->expects($this->once())
423
-			->method('getContent')
424
-			->willThrowException(new NotPermittedException());
425
-		$file->expects($this->once())
426
-			->method('getMimeType')
427
-			->willReturn('image/jpeg');
428
-		$userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
429
-		$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
430
-		$userFolder->method('get')->willReturn($file);
431
-
432
-		$expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST);
433
-		$this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
434
-	}
435
-
436
-	/**
437
-	 * Check for proper reply on proper crop argument
438
-	 */
439
-	public function testFileTooBig(): void {
440
-		$fileName = \OC::$SERVERROOT . '/tests/data/testimage.jpg';
441
-		//Create request return
442
-		$reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21 * 1024 * 1024]];
443
-		$this->request->method('getUploadedFile')->willReturn($reqRet);
444
-
445
-		$response = $this->avatarController->postAvatar(null);
446
-
447
-		$this->assertEquals('File is too big', $response->getData()['data']['message']);
448
-	}
44
+    /** @var AvatarController */
45
+    private $avatarController;
46
+    /** @var GuestAvatarController */
47
+    private $guestAvatarController;
48
+
49
+    /** @var IAvatar|\PHPUnit\Framework\MockObject\MockObject */
50
+    private $avatarMock;
51
+    /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
52
+    private $userMock;
53
+    /** @var ISimpleFile|\PHPUnit\Framework\MockObject\MockObject */
54
+    private $avatarFile;
55
+    /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */
56
+    private $avatarManager;
57
+    /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
58
+    private $l;
59
+    /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
60
+    private $userManager;
61
+    /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
62
+    private $rootFolder;
63
+    /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
64
+    private $logger;
65
+    /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
66
+    private $request;
67
+    /** @var TimeFactory|\PHPUnit\Framework\MockObject\MockObject */
68
+    private $timeFactory;
69
+
70
+    protected function setUp(): void {
71
+        parent::setUp();
72
+
73
+        $this->avatarManager = $this->getMockBuilder('OCP\IAvatarManager')->getMock();
74
+        $this->l = $this->getMockBuilder(IL10N::class)->getMock();
75
+        $this->l->method('t')->willReturnArgument(0);
76
+        $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
77
+        $this->request = $this->getMockBuilder(IRequest::class)->getMock();
78
+        $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
79
+        $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
80
+        $this->timeFactory = $this->getMockBuilder('OC\AppFramework\Utility\TimeFactory')->getMock();
81
+
82
+        $this->avatarMock = $this->getMockBuilder('OCP\IAvatar')->getMock();
83
+        $this->userMock = $this->getMockBuilder(IUser::class)->getMock();
84
+
85
+        $this->guestAvatarController = new GuestAvatarController(
86
+            'core',
87
+            $this->request,
88
+            $this->avatarManager,
89
+            $this->logger
90
+        );
91
+
92
+        $this->avatarController = new AvatarController(
93
+            'core',
94
+            $this->request,
95
+            $this->avatarManager,
96
+            $this->l,
97
+            $this->userManager,
98
+            $this->rootFolder,
99
+            $this->logger,
100
+            'userid',
101
+            $this->timeFactory,
102
+            $this->guestAvatarController,
103
+        );
104
+
105
+        // Configure userMock
106
+        $this->userMock->method('getDisplayName')->willReturn('displayName');
107
+        $this->userMock->method('getUID')->willReturn('userId');
108
+        $this->userManager->method('get')
109
+            ->willReturnMap([['userId', $this->userMock]]);
110
+
111
+        $this->avatarFile = $this->getMockBuilder(ISimpleFile::class)->getMock();
112
+        $this->avatarFile->method('getContent')->willReturn('image data');
113
+        $this->avatarFile->method('getMimeType')->willReturn('image type');
114
+        $this->avatarFile->method('getEtag')->willReturn('my etag');
115
+        $this->avatarFile->method('getName')->willReturn('my name');
116
+        $this->avatarFile->method('getMTime')->willReturn(42);
117
+    }
118
+
119
+    protected function tearDown(): void {
120
+        parent::tearDown();
121
+    }
122
+
123
+    /**
124
+     * Fetch an avatar if a user has no avatar
125
+     */
126
+    public function testGetAvatarNoAvatar(): void {
127
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
128
+        $this->avatarMock->method('getFile')->willThrowException(new NotFoundException());
129
+        $response = $this->avatarController->getAvatar('userId', 32);
130
+
131
+        //Comment out until JS is fixed
132
+        $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
133
+    }
134
+
135
+    /**
136
+     * Fetch the user's avatar
137
+     */
138
+    public function testGetAvatar(): void {
139
+        $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
140
+        $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
141
+        $this->avatarMock->expects($this->once())
142
+            ->method('isCustomAvatar')
143
+            ->willReturn(true);
144
+
145
+        $response = $this->avatarController->getAvatar('userId', 32);
146
+
147
+        $this->assertEquals(Http::STATUS_OK, $response->getStatus());
148
+        $this->assertArrayHasKey('Content-Type', $response->getHeaders());
149
+        $this->assertEquals('image type', $response->getHeaders()['Content-Type']);
150
+        $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders());
151
+        $this->assertEquals('1', $response->getHeaders()['X-NC-IsCustomAvatar']);
152
+
153
+        $this->assertEquals('my etag', $response->getETag());
154
+    }
155
+
156
+    /**
157
+     * Fetch the user's avatar
158
+     */
159
+    public function testGetGeneratedAvatar(): void {
160
+        $this->avatarMock->method('getFile')->willReturn($this->avatarFile);
161
+        $this->avatarManager->method('getAvatar')->with('userId')->willReturn($this->avatarMock);
162
+
163
+        $response = $this->avatarController->getAvatar('userId', 32);
164
+
165
+        $this->assertEquals(Http::STATUS_OK, $response->getStatus());
166
+        $this->assertArrayHasKey('Content-Type', $response->getHeaders());
167
+        $this->assertEquals('image type', $response->getHeaders()['Content-Type']);
168
+        $this->assertArrayHasKey('X-NC-IsCustomAvatar', $response->getHeaders());
169
+        $this->assertEquals('0', $response->getHeaders()['X-NC-IsCustomAvatar']);
170
+
171
+        $this->assertEquals('my etag', $response->getETag());
172
+    }
173
+
174
+    /**
175
+     * Fetch the avatar of a non-existing user
176
+     */
177
+    public function testGetAvatarNoUser(): void {
178
+        $this->avatarManager
179
+            ->method('getAvatar')
180
+            ->with('userDoesNotExist')
181
+            ->willThrowException(new \Exception('user does not exist'));
182
+
183
+        $response = $this->avatarController->getAvatar('userDoesNotExist', 32);
184
+
185
+        //Comment out until JS is fixed
186
+        $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
187
+    }
188
+
189
+    public function testGetAvatarSize64(): void {
190
+        $this->avatarMock->expects($this->once())
191
+            ->method('getFile')
192
+            ->with($this->equalTo(64))
193
+            ->willReturn($this->avatarFile);
194
+
195
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
196
+
197
+        $this->logger->expects($this->never())
198
+            ->method('debug');
199
+
200
+        $this->avatarController->getAvatar('userId', 64);
201
+    }
202
+
203
+    public function testGetAvatarSize512(): void {
204
+        $this->avatarMock->expects($this->once())
205
+            ->method('getFile')
206
+            ->with($this->equalTo(512))
207
+            ->willReturn($this->avatarFile);
208
+
209
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
210
+
211
+        $this->logger->expects($this->never())
212
+            ->method('debug');
213
+
214
+        $this->avatarController->getAvatar('userId', 512);
215
+    }
216
+
217
+    /**
218
+     * Small sizes return 64 and generate a log
219
+     */
220
+    public function testGetAvatarSizeTooSmall(): void {
221
+        $this->avatarMock->expects($this->once())
222
+            ->method('getFile')
223
+            ->with($this->equalTo(64))
224
+            ->willReturn($this->avatarFile);
225
+
226
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
227
+
228
+        $this->logger->expects($this->once())
229
+            ->method('debug')
230
+            ->with('Avatar requested in deprecated size 32');
231
+
232
+        $this->avatarController->getAvatar('userId', 32);
233
+    }
234
+
235
+    /**
236
+     * Avatars between 64 and 512 are upgraded to 512
237
+     */
238
+    public function testGetAvatarSizeBetween(): void {
239
+        $this->avatarMock->expects($this->once())
240
+            ->method('getFile')
241
+            ->with($this->equalTo(512))
242
+            ->willReturn($this->avatarFile);
243
+
244
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
245
+
246
+        $this->logger->expects($this->once())
247
+            ->method('debug')
248
+            ->with('Avatar requested in deprecated size 65');
249
+
250
+        $this->avatarController->getAvatar('userId', 65);
251
+    }
252
+
253
+    /**
254
+     * We do not support avatars larger than 512
255
+     */
256
+    public function testGetAvatarSizeTooBig(): void {
257
+        $this->avatarMock->expects($this->once())
258
+            ->method('getFile')
259
+            ->with($this->equalTo(512))
260
+            ->willReturn($this->avatarFile);
261
+
262
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
263
+
264
+        $this->logger->expects($this->once())
265
+            ->method('debug')
266
+            ->with('Avatar requested in deprecated size 513');
267
+
268
+        $this->avatarController->getAvatar('userId', 513);
269
+    }
270
+
271
+    /**
272
+     * Remove an avatar
273
+     */
274
+    public function testDeleteAvatar(): void {
275
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
276
+
277
+        $response = $this->avatarController->deleteAvatar();
278
+        $this->assertEquals(Http::STATUS_OK, $response->getStatus());
279
+    }
280
+
281
+    /**
282
+     * Test what happens if the removing of the avatar fails
283
+     */
284
+    public function testDeleteAvatarException(): void {
285
+        $this->avatarMock->method('remove')->willThrowException(new \Exception('foo'));
286
+        $this->avatarManager->method('getAvatar')->willReturn($this->avatarMock);
287
+
288
+        $this->logger->expects($this->once())
289
+            ->method('error')
290
+            ->with('foo', ['exception' => new \Exception('foo'), 'app' => 'core']);
291
+        $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'An error occurred. Please contact your admin.']], Http::STATUS_BAD_REQUEST);
292
+        $this->assertEquals($expectedResponse, $this->avatarController->deleteAvatar());
293
+    }
294
+
295
+    /**
296
+     * When trying to post a new avatar a path or image should be posted.
297
+     */
298
+    public function testPostAvatarNoPathOrImage(): void {
299
+        $response = $this->avatarController->postAvatar(null);
300
+
301
+        $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
302
+    }
303
+
304
+    /**
305
+     * Test a correct post of an avatar using POST
306
+     */
307
+    public function testPostAvatarFile(): void {
308
+        //Create temp file
309
+        $fileName = tempnam('', 'avatarTest');
310
+        $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.jpg', $fileName);
311
+        $this->assertTrue($copyRes);
312
+
313
+        //Create request return
314
+        $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.jpg')]];
315
+        $this->request->method('getUploadedFile')->willReturn($reqRet);
316
+
317
+        $response = $this->avatarController->postAvatar(null);
318
+
319
+        //On correct upload always respond with the notsquare message
320
+        $this->assertEquals('notsquare', $response->getData()['data']);
321
+
322
+        //File should be deleted
323
+        $this->assertFalse(file_exists($fileName));
324
+    }
325
+
326
+    /**
327
+     * Test invalid post os an avatar using POST
328
+     */
329
+    public function testPostAvatarInvalidFile(): void {
330
+        //Create request return
331
+        $reqRet = ['error' => [1], 'tmp_name' => ['foo']];
332
+        $this->request->method('getUploadedFile')->willReturn($reqRet);
333
+
334
+        $response = $this->avatarController->postAvatar(null);
335
+
336
+        $this->assertEquals(Http::STATUS_BAD_REQUEST, $response->getStatus());
337
+    }
338
+
339
+    /**
340
+     * Check what happens when we upload a GIF
341
+     */
342
+    public function testPostAvatarFileGif(): void {
343
+        //Create temp file
344
+        $fileName = tempnam('', 'avatarTest');
345
+        $copyRes = copy(\OC::$SERVERROOT . '/tests/data/testimage.gif', $fileName);
346
+        $this->assertTrue($copyRes);
347
+
348
+        //Create request return
349
+        $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [filesize(\OC::$SERVERROOT . '/tests/data/testimage.gif')]];
350
+        $this->request->method('getUploadedFile')->willReturn($reqRet);
351
+
352
+        $response = $this->avatarController->postAvatar(null);
353
+
354
+        $this->assertEquals('Unknown filetype', $response->getData()['data']['message']);
355
+
356
+        //File should be deleted
357
+        $this->assertFalse(file_exists($fileName));
358
+    }
359
+
360
+    /**
361
+     * Test posting avatar from existing file
362
+     */
363
+    public function testPostAvatarFromFile(): void {
364
+        //Mock node API call
365
+        $file = $this->getMockBuilder('OCP\Files\File')
366
+            ->disableOriginalConstructor()->getMock();
367
+        $file->expects($this->once())
368
+            ->method('getContent')
369
+            ->willReturn(file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg'));
370
+        $file->expects($this->once())
371
+            ->method('getMimeType')
372
+            ->willReturn('image/jpeg');
373
+        $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
374
+        $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
375
+        $userFolder->method('get')->willReturn($file);
376
+
377
+        //Create request return
378
+        $response = $this->avatarController->postAvatar('avatar.jpg');
379
+
380
+        //On correct upload always respond with the notsquare message
381
+        $this->assertEquals('notsquare', $response->getData()['data']);
382
+    }
383
+
384
+    /**
385
+     * Test posting avatar from existing folder
386
+     */
387
+    public function testPostAvatarFromNoFile(): void {
388
+        $file = $this->getMockBuilder('OCP\Files\Node')->getMock();
389
+        $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
390
+        $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
391
+        $userFolder
392
+            ->method('get')
393
+            ->with('folder')
394
+            ->willReturn($file);
395
+
396
+        //Create request return
397
+        $response = $this->avatarController->postAvatar('folder');
398
+
399
+        //On correct upload always respond with the notsquare message
400
+        $this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData());
401
+    }
402
+
403
+    public function testPostAvatarInvalidType(): void {
404
+        $file = $this->getMockBuilder('OCP\Files\File')
405
+            ->disableOriginalConstructor()->getMock();
406
+        $file->expects($this->never())
407
+            ->method('getContent');
408
+        $file->expects($this->exactly(2))
409
+            ->method('getMimeType')
410
+            ->willReturn('text/plain');
411
+        $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
412
+        $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
413
+        $userFolder->method('get')->willReturn($file);
414
+
415
+        $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST);
416
+        $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
417
+    }
418
+
419
+    public function testPostAvatarNotPermittedException(): void {
420
+        $file = $this->getMockBuilder('OCP\Files\File')
421
+            ->disableOriginalConstructor()->getMock();
422
+        $file->expects($this->once())
423
+            ->method('getContent')
424
+            ->willThrowException(new NotPermittedException());
425
+        $file->expects($this->once())
426
+            ->method('getMimeType')
427
+            ->willReturn('image/jpeg');
428
+        $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
429
+        $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
430
+        $userFolder->method('get')->willReturn($file);
431
+
432
+        $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST);
433
+        $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
434
+    }
435
+
436
+    /**
437
+     * Check for proper reply on proper crop argument
438
+     */
439
+    public function testFileTooBig(): void {
440
+        $fileName = \OC::$SERVERROOT . '/tests/data/testimage.jpg';
441
+        //Create request return
442
+        $reqRet = ['error' => [0], 'tmp_name' => [$fileName], 'size' => [21 * 1024 * 1024]];
443
+        $this->request->method('getUploadedFile')->willReturn($reqRet);
444
+
445
+        $response = $this->avatarController->postAvatar(null);
446
+
447
+        $this->assertEquals('File is too big', $response->getData()['data']['message']);
448
+    }
449 449
 }
Please login to merge, or discard this patch.