EmailControllerTest::testEmailPublicLink()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 131

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 8
c 0
b 0
f 0
cc 3
nc 4
nop 1

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
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use PHPUnit\Framework\TestCase;
25
26
class EmailControllerTest extends TestCase {
27
28
	private $appName;
29
	private $request;
30
	private $userSession;
31
	private $dummyUser;
32
	private $config;
33
	private $mailer;
34
	private $emailTemplate;
35
	private $message;
36
	private $l10n;
37
	private $defaults;
38
	private $controller;
39
40
	public function setUp() {
41
		$this->appName = 'calendar';
42
		$this->request = $this->getMockBuilder('\OCP\IRequest')
43
			->disableOriginalConstructor()
44
			->getMock();
45
46
		$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
47
			->disableOriginalConstructor()
48
			->getMock();
49
		$this->dummyUser = $this->getMockBuilder('\OCP\IUser')
50
			->disableOriginalConstructor()
51
			->getMock();
52
53
		$this->config = $this->getMockBuilder('\OCP\IConfig')
54
			->disableOriginalConstructor()
55
			->getMock();
56
57
		$this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
58
			->disableOriginalConstructor()
59
			->getMock();
60
		$this->emailTemplate = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
61
			->disableOriginalConstructor()
62
			->getMock();
63
		$this->message = $this->getMockBuilder('\OC\Mail\Message')
64
			->disableOriginalConstructor()
65
			->getMock();
66
67
		$this->l10n = $this->getMockBuilder('OC\L10N\L10N')
68
			->disableOriginalConstructor()
69
			->getMock();
70
71
		$this->defaults = $this->getMockBuilder('OCP\Defaults')
72
			->disableOriginalConstructor()
73
			->getMock();
74
75
		$this->controller = new EmailController($this->appName, $this->request,
76
			$this->userSession, $this->config, $this->mailer, $this->l10n, $this->defaults);
77
	}
78
79
	/**
80
	 * @dataProvider emailPublicLinkProvider
81
	 */
82
	public function testEmailPublicLink($validEmailAddress) {
83
		$this->userSession->expects($this->once())
84
			->method('getUser')
85
			->will($this->returnValue($this->dummyUser));
86
		$this->dummyUser->expects($this->once())
87
			->method('getDisplayName')
88
			->will($this->returnValue('Fancy displayname 42'));
89
90
		$this->l10n->expects($this->at(0))
91
			->method('t')
92
			->with('%s has published the calendar »%s«', ['Fancy displayname 42', 'Calendarname 1337'])
93
			->will($this->returnValue('localized_1'));
94
95
		$this->config->expects($this->at(0))
96
			->method('getSystemValue')
97
			->with('version')
98
			->will($this->returnValue('12.0.0'));
99
100
		$this->mailer->expects($this->at(0))
101
			->method('createEMailTemplate')
102
			->will($this->returnValue($this->emailTemplate));
103
104
		$this->emailTemplate->expects($this->at(1))
105
			->method('addHeader');
106
107
		$this->l10n->expects($this->at(1))
108
			->method('t')
109
			->with('%s has published the calendar »%s«', ['Fancy displayname 42', 'Calendarname 1337'])
110
			->will($this->returnValue('localized_2'));
111
		$this->emailTemplate->expects($this->at(1))
112
			->method('addHeading')
113
			->with('localized_2');
114
115
		$this->l10n->expects($this->at(2))
116
			->method('t')
117
			->with('Hello,')
118
			->will($this->returnValue('localized_3'));
119
		$this->emailTemplate->expects($this->at(2))
120
			->method('addBodyText')
121
			->with('localized_3');
122
123
		$this->l10n->expects($this->at(3))
124
			->method('t')
125
			->with('We wanted to inform you that %s has published the calendar »%s«.', ['Fancy displayname 42', 'Calendarname 1337'])
126
			->will($this->returnValue('localized_4'));
127
		$this->emailTemplate->expects($this->at(3))
128
			->method('addBodyText')
129
			->with('localized_4');
130
131
		$this->l10n->expects($this->at(4))
132
			->method('t')
133
			->with('Open »%s«', ['Calendarname 1337'])
134
			->will($this->returnValue('localized_5'));
135
		$this->emailTemplate->expects($this->at(4))
136
			->method('addBodyButton')
137
			->with('localized_5', 'https://url-to-public-calendar');
138
139
		$this->l10n->expects($this->at(5))
140
			->method('t')
141
			->with('Cheers!')
142
			->will($this->returnValue('localized_6'));
143
		$this->emailTemplate->expects($this->at(5))
144
			->method('addBodyText')
145
			->with('localized_6');
146
147
		$this->emailTemplate->expects($this->at(6))
148
			->method('addFooter');
149
		$this->emailTemplate->expects($this->at(7))
150
			->method('renderHtml')
151
			->will($this->returnValue('html_body'));
152
		$this->emailTemplate->expects($this->at(8))
153
			->method('renderText')
154
			->will($this->returnValue('text_body'));
155
156
		$this->mailer->expects($this->at(1))
157
			->method('validateMailAddress')
158
			->with('[email protected]')
159
			->will($this->returnValue($validEmailAddress));
160
161
		if ($validEmailAddress) {
162
			$this->config->expects($this->at(1))
163
				->method('getSystemValue')
164
				->with('mail_domain', 'domain.org')
165
				->will($this->returnValue('domain_123'));
166
			$this->config->expects($this->at(2))
167
				->method('getSystemValue')
168
				->with('mail_from_address', 'nextcloud')
169
				->will($this->returnValue('from_456'));
170
171
			$this->mailer->expects($this->at(2))
172
				->method('createMessage')
173
				->will($this->returnValue($this->message));
174
175
			$this->message->expects($this->at(0))
176
				->method('setSubject')
177
				->with('localized_1');
178
			$this->defaults->expects($this->at(0))
179
				->method('getName')
180
				->will($this->returnValue('default_instance_name'));
181
			$this->message->expects($this->at(1))
182
				->method('setFrom')
183
				->with(['from_456@domain_123' => 'default_instance_name']);
184
			$this->l10n->expects($this->at(6))
185
				->method('t')
186
				->with('Recipient')
187
				->will($this->returnValue('localized_7'));
188
			$this->message->expects($this->at(2))
189
				->method('setTo')
190
				->with(['[email protected]' => 'localized_7']);
191
			$this->message->expects($this->at(3))
192
				->method('setPlainBody')
193
				->with('text_body');
194
			$this->message->expects($this->at(4))
195
				->method('setHtmlBody')
196
				->with('html_body');
197
198
			$this->mailer->expects($this->at(3))
199
				->method('send')
200
				->with($this->message);
201
		}
202
203
		$response = $this->controller->sendEmailPublicLink('[email protected]', 'https://url-to-public-calendar', 'Calendarname 1337');
204
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
205
		$this->assertEquals([], $response->getData());
206
207
		if ($validEmailAddress) {
208
			$this->assertEquals(200, $response->getStatus());
209
		} else {
210
			$this->assertEquals(400, $response->getStatus());
211
		}
212
	}
213
214
	public function emailPublicLinkProvider() {
215
		return [
216
			[true],
217
			[false],
218
		];
219
	}
220
}
221