Completed
Pull Request — master (#1276)
by Thomas
04:23
created

AccountsControllerTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 381
Duplicated Lines 20.47 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 19
lcom 1
cbo 2
dl 78
loc 381
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testIndex() 0 14 1
A testShow() 14 14 1
A testShowDoesNotExist() 15 15 1
A testDestroy() 10 10 1
A testDestroyDoesNotExist() 11 11 1
B testCreateAutoDetectSuccess() 0 27 1
A testCreateAutoDetectFailure() 0 22 1
A newMessageDataProvider() 8 8 1
B setUp() 0 42 1
B testSend() 6 112 5
A draftDataProvider() 8 8 1
B testDraft() 6 63 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Christoph Wurst <[email protected]>
4
 *
5
 * ownCloud - Mail
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 *
19
 */
20
use OCA\Mail\Controller\AccountsController;
21
use OCA\Mail\Model\Message;
22
use OC\AppFramework\Http;
23
use OCP\AppFramework\Http\JSONResponse;
24
25
class AccountsControllerTest extends \Test\TestCase {
26
27
	private $appName;
28
	private $request;
29
	private $accountService;
30
	private $userId;
31
	private $userFolder;
32
	private $autoConfig;
33
	private $logger;
34
	private $l10n;
35
	private $crypto;
36
	private $controller;
37
	private $accountId;
38
	private $account;
39
	private $unifiedAccount;
40
41
	protected function setUp() {
42
		parent::setUp();
43
44
		$this->appName = 'mail';
45
		$this->request = $this->getMockBuilder('\OCP\IRequest')
46
			->disableOriginalConstructor()
47
			->getMock();
48
		$this->accountService = $this->getMockBuilder('\OCA\Mail\Service\AccountService')
49
			->disableOriginalConstructor()
50
			->getMock();
51
		$this->userId = 'manfred';
52
		$this->userFolder = $this->getMockBuilder('\OCP\Files\Folder')
53
			->disableOriginalConstructor()
54
			->getMock();
55
		$this->autoConfig = $this->getMockBuilder('\OCA\Mail\Service\AutoConfig\AutoConfig')
56
			->disableOriginalConstructor()
57
			->getMock();
58
		$this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger')
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$this->l10n = $this->getMockBuilder('\OCP\IL10N')
62
			->disableOriginalConstructor()
63
			->getMock();
64
		$this->crypto = $this->getMockBuilder('\OCP\Security\ICrypto')
65
			->disableOriginalConstructor()
66
			->getMock();
67
		$this->collector = $this->getMockBuilder('\OCA\Mail\Service\AutoCompletion\AddressCollector')
68
			->disableOriginalConstructor()
69
			->getMock();
70
71
		$this->controller = new AccountsController($this->appName, $this->request,
72
			$this->accountService, $this->userId, $this->userFolder, $this->autoConfig,
73
			$this->logger, $this->l10n, $this->crypto, $this->collector);
74
75
		$this->account = $this->getMockBuilder('\OCA\Mail\Account')
76
			->disableOriginalConstructor()
77
			->getMock();
78
		$this->unifiedAccount = $this->getMockBuilder('\OCA\Mail\Service\UnifiedAccount')
79
			->disableOriginalConstructor()
80
			->getMock();
81
		$this->accountId = 123;
82
	}
83
84
	public function testIndex() {
85
		$this->account->expects($this->once())
86
			->method('getConfiguration')
87
			->will($this->returnValue('conf'));
88
		$this->accountService->expects($this->once())
89
			->method('findByUserId')
90
			->with($this->equalTo($this->userId))
91
			->will($this->returnValue([$this->account]));
92
93
		$response = $this->controller->index();
94
95
		$expectedResponse = new JSONResponse(['conf']);
96
		$this->assertEquals($expectedResponse, $response);
97
	}
98
99 View Code Duplication
	public function testShow() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
		$this->accountService->expects($this->once())
101
			->method('find')
102
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId))
103
			->will($this->returnValue($this->account));
104
		$this->account->expects($this->once())
105
			->method('getConfiguration')
106
			->will($this->returnValue('conf'));
107
108
		$response = $this->controller->show($this->accountId);
109
110
		$expectedResponse = new JSONResponse('conf');
111
		$this->assertEquals($expectedResponse, $response);
112
	}
113
114 View Code Duplication
	public function testShowDoesNotExist() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
		$this->accountService->expects($this->once())
116
			->method('find')
117
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId))
118
			->will($this->returnValue($this->account));
119
		$this->account->expects($this->once())
120
			->method('getConfiguration')
121
			->will($this->throwException(new OCP\AppFramework\Db\DoesNotExistException('test123')));
122
123
		$response = $this->controller->show($this->accountId);
124
125
		$expectedResponse = new JSONResponse([]);
126
		$expectedResponse->setStatus(404);
127
		$this->assertEquals($expectedResponse, $response);
128
	}
129
130 View Code Duplication
	public function testDestroy() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
		$this->accountService->expects($this->once())
132
			->method('delete')
133
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId));
134
135
		$response = $this->controller->destroy($this->accountId);
136
137
		$expectedResponse = new JSONResponse();
138
		$this->assertEquals($expectedResponse, $response);
139
	}
140
141 View Code Duplication
	public function testDestroyDoesNotExist() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
		$this->accountService->expects($this->once())
143
			->method('delete')
144
			->with($this->equalTo($this->userId), $this->equalTo($this->accountId))
145
			->will($this->throwException(new \OCP\AppFramework\Db\DoesNotExistException('test')));
146
147
		$response = $this->controller->destroy($this->accountId);
148
149
		$expectedResponse = new JSONResponse();
150
		$this->assertEquals($expectedResponse, $response);
151
	}
152
153
	public function testCreateAutoDetectSuccess() {
154
		$email = '[email protected]';
155
		$password = '123456';
156
		$accountName = 'John Doe';
157
158
		$this->account->expects($this->exactly(2))
159
			->method('getId')
160
			->will($this->returnValue(135));
161
		$this->autoConfig->expects($this->once())
162
			->method('createAutoDetected')
163
			->with($this->equalTo($email), $this->equalTo($password),
164
				$this->equalTo($accountName))
165
			->will($this->returnValue($this->account));
166
		$this->accountService->expects($this->once())
167
			->method('save')
168
			->with($this->equalTo($this->account));
169
170
		$response = $this->controller->create($accountName, $email, $password, null,
171
			null, null, null, null, null, null, null, null, null, true);
172
173
		$expectedResponse = new JSONResponse([
174
			'data' => [
175
				'id' => 135,
176
			],
177
			], Http::STATUS_CREATED);
178
		$this->assertEquals($expectedResponse, $response);
179
	}
180
181
	public function testCreateAutoDetectFailure() {
182
		$email = '[email protected]';
183
		$password = '123456';
184
		$accountName = 'John Doe';
185
186
		$this->autoConfig->expects($this->once())
187
			->method('createAutoDetected')
188
			->with($this->equalTo($email), $this->equalTo($password),
189
				$this->equalTo($accountName))
190
			->will($this->returnValue(null));
191
		$this->l10n->expects($this->once())
192
			->method('t')
193
			->will($this->returnValue('fail'));
194
195
		$response = $this->controller->create($accountName, $email, $password, null,
196
			null, null, null, null, null, null, null, null, null, true);
197
198
		$expectedResponse = new JSONResponse([
199
			'message' => 'fail',
200
			], Http::STATUS_BAD_REQUEST);
201
		$this->assertEquals($expectedResponse, $response);
202
	}
203
204 View Code Duplication
	public function newMessageDataProvider() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
		return [
206
			[false, false],
207
			[true, false],
208
			[false, true],
209
			[true, true],
210
		];
211
	}
212
213
	/**
214
	 * @dataProvider newMessageDataProvider
215
	 */
216
	public function testSend($isUnifiedInbox, $isReply) {
217
		$account = $isUnifiedInbox ? $this->unifiedAccount : $this->account;
218
		$folderId = base64_encode('My folder');
219
		$subject = 'Hello';
220
		$body = 'Hi!';
221
		$from = '[email protected]';
222
		$to = '[email protected]';
223
		$cc = '"user2" <[email protected]>, [email protected]';
224
		$bcc = '[email protected]';
225
		$draftUID = 45;
226
		$messageId = $isReply ? 123 : null;
227
		$attachmentName = 'kitten.png';
228
		$attachments = [
229
			[
230
				'fileName' => $attachmentName
231
			],
232
		];
233
234
		$this->accountService->expects($this->once())
235
			->method('find')
236
			->with($this->userId, $this->accountId)
237
			->will($this->returnValue($account));
238 View Code Duplication
		if ($isUnifiedInbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239
			$this->unifiedAccount->expects($this->once())
240
				->method('resolve')
241
				->with($messageId)
242
				->will($this->returnValue([$this->account, $folderId, $messageId]));
243
		}
244
245
		if ($isReply) {
246
			$message = $this->getMockBuilder('OCA\Mail\Model\ReplyMessage')
247
				->disableOriginalConstructor()
248
				->getMock();
249
			$this->account->expects($this->once())
250
				->method('newReplyMessage')
251
				->will($this->returnValue($message));
252
			$mailbox = $this->getMockBuilder('OCA\Mail\Service\IMailBox')
253
				->disableOriginalConstructor()
254
				->getMock();
255
			$this->account->expects($this->once())
256
				->method('getMailbox')
257
				->with(base64_decode($folderId))
258
				->will($this->returnValue($mailbox));
259
			$reply = new Message();
260
			$mailbox->expects($this->once())
261
				->method('getMessage')
262
				->with($messageId)
263
				->will($this->returnValue($reply));
264
			$message->expects($this->once())
265
				->method('setRepliedMessage')
266
				->with($reply);
267
		} else {
268
			$message = $this->getMockBuilder('OCA\Mail\Model\Message')
269
				->disableOriginalConstructor()
270
				->getMock();
271
			$this->account->expects($this->once())
272
				->method('newMessage')
273
				->will($this->returnValue($message));
274
		}
275
276
		$message->expects($this->once())
277
			->method('setTo')
278
			->with(Message::parseAddressList($to));
279
		$message->expects($this->once())
280
			->method('setSubject')
281
			->with($subject);
282
		$message->expects($this->once())
283
			->method('setFrom')
284
			->with($from);
285
		$message->expects($this->once())
286
			->method('setCC')
287
			->with(Message::parseAddressList($cc));
288
		$message->expects($this->once())
289
			->method('setBcc')
290
			->with(Message::parseAddressList($bcc));
291
		$message->expects($this->once())
292
			->method('setContent')
293
			->with($body);
294
		$message->expects($this->once())
295
			->method('getToList')
296
			->willReturn([]);
297
		$message->expects($this->once())
298
			->method('getCCList')
299
			->willReturn([]);
300
		$message->expects($this->once())
301
			->method('getBCCList')
302
			->willReturn([]);
303
304
		$this->account->expects($this->once())
305
			->method('getEMailAddress')
306
			->will($this->returnValue($from));
307
		$this->account->expects($this->once())
308
			->method('sendMessage')
309
			->with($message, $draftUID);
310
		$this->userFolder->expects($this->at(0))
311
			->method('nodeExists')
312
			->with($attachmentName)
313
			->will($this->returnValue(true));
314
		$file = $this->getMockBuilder('OCP\Files\File')
315
			->disableOriginalConstructor()
316
			->getMock();
317
		$this->userFolder->expects($this->once())
318
			->method('get')
319
			->with($attachmentName)
320
			->will($this->returnValue($file));
321
322
		$expected = new JSONResponse();
323
		$actual = $this->controller->send($this->accountId, $folderId, $subject,
324
			$body, $to, $cc, $bcc, $draftUID, $messageId, $attachments);
325
326
		$this->assertEquals($expected, $actual);
327
	}
328
329 View Code Duplication
	public function draftDataProvider() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
330
		return [
331
			[false, false],
332
			[true, true],
333
			[true, false],
334
			[true, true],
335
		];
336
	}
337
338
	/**
339
	 * @dataProvider newMessageDataProvider
340
	 */
341
	public function testDraft($isUnifiedInbox, $withPreviousDraft) {
342
		$account = $isUnifiedInbox ? $this->unifiedAccount : $this->account;
343
		$subject = 'Hello';
344
		$body = 'Hi!';
345
		$from = '[email protected]';
346
		$to = '[email protected]';
347
		$cc = '"user2" <[email protected]>, [email protected]';
348
		$bcc = '[email protected]';
349
		$messageId = 123;
350
		$uid = $withPreviousDraft ? 123 : null;
351
		$newUID = 124;
352
353
		$this->accountService->expects($this->once())
354
			->method('find')
355
			->with($this->userId, $this->accountId)
356
			->will($this->returnValue($account));
357 View Code Duplication
		if ($isUnifiedInbox) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
358
			$this->unifiedAccount->expects($this->once())
359
				->method('resolve')
360
				->with($messageId)
361
				->will($this->returnValue([$this->account]));
362
		}
363
364
		$message = $this->getMockBuilder('OCA\Mail\Model\Message')
365
			->disableOriginalConstructor()
366
			->getMock();
367
		$this->account->expects($this->once())
368
			->method('newMessage')
369
			->will($this->returnValue($message));
370
		$message->expects($this->once())
371
			->method('setTo')
372
			->with(Message::parseAddressList($to));
373
		$message->expects($this->once())
374
			->method('setSubject')
375
			->with($subject);
376
		$message->expects($this->once())
377
			->method('setFrom')
378
			->with($from);
379
		$message->expects($this->once())
380
			->method('setCC')
381
			->with(Message::parseAddressList($cc));
382
		$message->expects($this->once())
383
			->method('setBcc')
384
			->with(Message::parseAddressList($bcc));
385
		$message->expects($this->once())
386
			->method('setContent')
387
			->with($body);
388
		$this->account->expects($this->once())
389
			->method('getEMailAddress')
390
			->will($this->returnValue($from));
391
		$this->account->expects($this->once())
392
			->method('saveDraft')
393
			->with($message, $uid)
394
			->will($this->returnValue($newUID));
395
396
		$expected = new JSONResponse([
397
			'uid' => $newUID,
398
		]);
399
		$actual = $this->controller->draft($this->accountId, $subject, $body, $to,
400
			$cc, $bcc, $uid, $messageId);
401
402
		$this->assertEquals($expected, $actual);
403
	}
404
405
}
406