Completed
Pull Request — master (#1613)
by Christoph
27:51
created

PageControllerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 166
Duplicated Lines 28.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 1
dl 48
loc 166
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 1
A testIndex() 0 70 2
A testComposeSimple() 0 10 1
A testComposeWithSubject() 12 12 1
A testComposeWithCc() 12 12 1
A testComposeWithBcc() 12 12 1
A testComposeWithMultilineBody() 12 12 1

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
/**
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 OCP\AppFramework\Http\RedirectResponse;
22
use OCP\AppFramework\Http\TemplateResponse;
23
use Test\TestCase;
24
use OCA\Mail\Controller\PageController;
25
26
class PageControllerTest extends TestCase {
27
28
	private $appName;
29
	private $request;
30
	private $userId;
31
	private $mailAccountMapper;
32
	private $urlGenerator;
33
	private $config;
34
	private $controller;
35
	private $accountService;
36
	private $aliasesService;
37
38
	protected function setUp() {
39
		parent::setUp();
40
41
		$this->appName = 'mail';
42
		$this->userId = 'george';
43
		$this->request = $this->getMock('\OCP\IRequest');
44
		$this->urlGenerator = $this->getMock('OCP\IURLGenerator');
45
		$this->config = $this->getMock('OCP\IConfig');
46
		$this->accountService = $this->getMockBuilder('OCA\Mail\Service\AccountService')
47
			->disableOriginalConstructor()
48
			->getMock();
49
		$this->aliasesService = $this->getMockBuilder('\OCA\Mail\Service\AliasesService')
50
			->disableOriginalConstructor()
51
			->getMock();
52
		$this->controller = new PageController($this->appName, $this->request,
53
			$this->urlGenerator, $this->config, $this->accountService,
54
			$this->aliasesService, $this->userId);
55
	}
56
57
	public function testIndex() {
58
		$account1 = $this->getMock('OCA\Mail\Service\IAccount');
59
		$account2 = $this->getMock('OCA\Mail\Service\IAccount');
60
61
		$this->accountService->expects($this->once())
62
			->method('findByUserId')
63
			->with($this->userId)
64
			->will($this->returnValue([
65
				$account1,
66
				$account2,
67
			]));
68
		$account1->expects($this->once())
69
			->method('getConfiguration')
70
			->will($this->returnValue([
71
				'accountId' => 1,
72
			]));
73
		$account2->expects($this->once())
74
			->method('getConfiguration')
75
			->will($this->returnValue([
76
				'accountId' => 2,
77
			]));
78
		$this->aliasesService->expects($this->exactly(2))
79
			->method('findAll')
80
			->will($this->returnValueMap([
81
				[1, $this->userId, ['a11', 'a12']],
82
				[2, $this->userId, ['a21', 'a22']],
83
			]));
84
		$accountsJson = [
85
			[
86
				'accountId' => 1,
87
				'aliases' => [
88
					'a11',
89
					'a12',
90
				]
91
			],
92
			[
93
				'accountId' => 2,
94
				'aliases' => [
95
					'a21',
96
					'a22',
97
				]
98
			],
99
		];
100
101
		$this->config->expects($this->once())
102
			->method('getSystemValue')
103
			->with('debug', false)
104
			->will($this->returnValue(true));
105
		$this->config->expects($this->once())
106
			->method('getAppValue')
107
			->with('mail', 'installed_version')
108
			->will($this->returnValue('1.2.3'));
109
110
		$expected = new TemplateResponse($this->appName, 'index',
111
			[
112
			'debug' => true,
113
			'app-version' => '1.2.3',
114
			'accounts' => base64_encode(json_encode($accountsJson)),
115
		]);
116
		// set csp rules for ownCloud 8.1
117
		if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
118
			$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
119
			$csp->addAllowedFrameDomain('\'self\'');
120
			$expected->setContentSecurityPolicy($csp);
121
		}
122
123
		$response = $this->controller->index();
124
125
		$this->assertEquals($expected, $response);
126
	}
127
128
	public function testComposeSimple() {
129
		$address = '[email protected]';
130
		$uri = "mailto:$address";
131
132
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address));
133
134
		$response = $this->controller->compose($uri);
135
136
		$this->assertEquals($expected, $response);
137
	}
138
139 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...
140
		$address = '[email protected]';
141
		$subject = 'hello there';
142
		$uri = "mailto:$address?subject=$subject";
143
144
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
145
			. '&subject=' . urlencode($subject));
146
147
		$response = $this->controller->compose($uri);
148
149
		$this->assertEquals($expected, $response);
150
	}
151
152 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...
153
		$address = '[email protected]';
154
		$cc = '[email protected]';
155
		$uri = "mailto:$address?cc=$cc";
156
157
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
158
			. '&cc=' . urlencode($cc));
159
160
		$response = $this->controller->compose($uri);
161
162
		$this->assertEquals($expected, $response);
163
	}
164
165 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...
166
		$address = '[email protected]';
167
		$bcc = '[email protected]';
168
		$uri = "mailto:$address?bcc=$bcc";
169
170
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
171
			. '&bcc=' . urlencode($bcc));
172
173
		$response = $this->controller->compose($uri);
174
175
		$this->assertEquals($expected, $response);
176
	}
177
178 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...
179
		$address = '[email protected]';
180
		$body = 'Hi!\nWhat\'s up?\nAnother line';
181
		$uri = "mailto:$address?body=$body";
182
183
		$expected = new RedirectResponse('#mailto?to=' . urlencode($address)
184
			. '&body=' . urlencode($body));
185
186
		$response = $this->controller->compose($uri);
187
188
		$this->assertEquals($expected, $response);
189
	}
190
191
}
192