Completed
Push — master ( edcff3...04863a )
by Jan-Christoph
07:35
created

PageControllerTest::testIndex()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 85
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 85
rs 8.6875
cc 2
eloc 65
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
use OCA\Mail\Controller\PageController;
22
use OCP\AppFramework\Http\ContentSecurityPolicy;
23
use OCP\AppFramework\Http\RedirectResponse;
24
use OCP\AppFramework\Http\TemplateResponse;
25
use Test\TestCase;
26
27
class PageControllerTest extends TestCase {
28
29
	private $appName;
30
	private $request;
31
	private $userId;
32
	private $mailAccountMapper;
33
	private $urlGenerator;
34
	private $config;
35
	private $controller;
36
	private $accountService;
37
	private $aliasesService;
38
	private $userSession;
39
40
	protected function setUp() {
41
		parent::setUp();
42
43
		$this->appName = 'mail';
44
		$this->userId = 'george';
45
		$this->request = $this->getMockBuilder('\OCP\IRequest')->getMock();
46
		$this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')->getMock();
47
		$this->config = $this->getMockBuilder('OCP\IConfig')->getMock();
48
		$this->accountService = $this->getMockBuilder('OCA\Mail\Service\AccountService')
49
			->disableOriginalConstructor()
50
			->getMock();
51
		$this->aliasesService = $this->getMockBuilder('\OCA\Mail\Service\AliasesService')
52
			->disableOriginalConstructor()
53
			->getMock();
54
		$this->userSession = $this->getMockBuilder('OCP\IUserSession')->getMock();
55
		$this->controller = new PageController($this->appName, $this->request,
56
			$this->urlGenerator, $this->config, $this->accountService,
57
			$this->aliasesService, $this->userId, $this->userSession);
58
	}
59
60
	public function testIndex() {
61
		$account1 = $this->getMock('OCA\Mail\Service\IAccount');
62
		$account2 = $this->getMock('OCA\Mail\Service\IAccount');
63
64
		$this->accountService->expects($this->once())
65
			->method('findByUserId')
66
			->with($this->userId)
67
			->will($this->returnValue([
68
					$account1,
69
					$account2,
70
		]));
71
		$account1->expects($this->once())
72
			->method('getConfiguration')
73
			->will($this->returnValue([
74
					'accountId' => 1,
75
		]));
76
		$account2->expects($this->once())
77
			->method('getConfiguration')
78
			->will($this->returnValue([
79
					'accountId' => 2,
80
		]));
81
		$this->aliasesService->expects($this->exactly(2))
82
			->method('findAll')
83
			->will($this->returnValueMap([
84
					[1, $this->userId, ['a11', 'a12']],
85
					[2, $this->userId, ['a21', 'a22']],
86
		]));
87
		$accountsJson = [
88
			[
89
				'accountId' => 1,
90
				'aliases' => [
91
					'a11',
92
					'a12',
93
				]
94
			],
95
			[
96
				'accountId' => 2,
97
				'aliases' => [
98
					'a21',
99
					'a22',
100
				]
101
			],
102
		];
103
104
		$user = $this->getMockBuilder('\OCP\IUser')->getMock();
105
		$this->userSession->expects($this->once())
106
			->method('getUser')
107
			->will($this->returnValue($user));
108
		$this->config->expects($this->once())
109
			->method('getSystemValue')
110
			->with('debug', false)
111
			->will($this->returnValue(true));
112
		$this->config->expects($this->once())
113
			->method('getAppValue')
114
			->with('mail', 'installed_version')
115
			->will($this->returnValue('1.2.3'));
116
		$user->expects($this->once())
117
			->method('getDisplayName')
118
			->will($this->returnValue('Jane Doe'));
119
		$user->expects($this->once())
120
			->method('getUID')
121
			->will($this->returnValue('jane'));
122
		$this->config->expects($this->once())
123
			->method('getUserValue')
124
			->with($this->equalTo('jane'), $this->equalTo('settings'), $this->equalTo('email'), $this->equalTo(''))
125
			->will($this->returnValue('[email protected]'));
126
127
		$expected = new TemplateResponse($this->appName, 'index', [
128
			'debug' => true,
129
			'app-version' => '1.2.3',
130
			'accounts' => base64_encode(json_encode($accountsJson)),
131
			'prefill_displayName' => 'Jane Doe',
132
			'prefill_email' => '[email protected]',
133
		]);
134
		// set csp rules for ownCloud 8.1
135
		if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
136
			$csp = new ContentSecurityPolicy();
137
			$csp->addAllowedFrameDomain('\'self\'');
138
			$expected->setContentSecurityPolicy($csp);
139
		}
140
141
		$response = $this->controller->index();
142
143
		$this->assertEquals($expected, $response);
144
	}
145
146
	public function testComposeSimple() {
147
		$address = '[email protected]';
148
		$uri = "mailto:$address";
149
150
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address));
151
152
		$response = $this->controller->compose($uri);
153
154
		$this->assertEquals($expected, $response);
155
	}
156
157 View Code Duplication
	public function testComposeWithSubject() {
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...
158
		$address = '[email protected]';
159
		$subject = 'hello there';
160
		$uri = "mailto:$address?subject=$subject";
161
162
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
163
			. '&subject=' . urlencode($subject));
164
165
		$response = $this->controller->compose($uri);
166
167
		$this->assertEquals($expected, $response);
168
	}
169
170 View Code Duplication
	public function testComposeWithCc() {
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...
171
		$address = '[email protected]';
172
		$cc = '[email protected]';
173
		$uri = "mailto:$address?cc=$cc";
174
175
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
176
			. '&cc=' . urlencode($cc));
177
178
		$response = $this->controller->compose($uri);
179
180
		$this->assertEquals($expected, $response);
181
	}
182
183 View Code Duplication
	public function testComposeWithBcc() {
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...
184
		$address = '[email protected]';
185
		$bcc = '[email protected]';
186
		$uri = "mailto:$address?bcc=$bcc";
187
188
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
189
			. '&bcc=' . urlencode($bcc));
190
191
		$response = $this->controller->compose($uri);
192
193
		$this->assertEquals($expected, $response);
194
	}
195
196 View Code Duplication
	public function testComposeWithMultilineBody() {
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...
197
		$address = '[email protected]';
198
		$body = 'Hi!\nWhat\'s up?\nAnother line';
199
		$uri = "mailto:$address?body=$body";
200
201
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
202
			. '&body=' . urlencode($body));
203
204
		$response = $this->controller->compose($uri);
205
206
		$this->assertEquals($expected, $response);
207
	}
208
209
}
210