Passed
Push — master ( fa7896...dc98af )
by
unknown
19:33
created

EmailControllerTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
rs 8.8571
cc 1
eloc 31
nc 1
nop 0
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
class EmailControllerTest extends \PHPUnit_Framework_TestCase {
25
26
	private $appName;
27
	private $request;
28
	private $userSession;
29
	private $dummyUser;
30
	private $config;
31
	private $mailer;
32
	private $emailTemplate;
33
	private $message;
34
	private $l10n;
35
	private $defaults;
36
	private $controller;
37
38
	public function setUp() {
39
		$this->appName = 'calendar';
40
		$this->request = $this->getMockBuilder('\OCP\IRequest')
41
			->disableOriginalConstructor()
42
			->getMock();
43
44
		$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
45
			->disableOriginalConstructor()
46
			->getMock();
47
		$this->dummyUser = $this->getMockBuilder('\OCP\IUser')
48
			->disableOriginalConstructor()
49
			->getMock();
50
51
		$this->config = $this->getMockBuilder('\OCP\IConfig')
52
			->disableOriginalConstructor()
53
			->getMock();
54
55
		$this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
56
			->disableOriginalConstructor()
57
			->getMock();
58
		$this->emailTemplate = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$this->message = $this->getMockBuilder('\OC\Mail\Message')
62
			->disableOriginalConstructor()
63
			->getMock();
64
65
		$this->l10n = $this->getMockBuilder('OC\L10N\L10N')
66
			->disableOriginalConstructor()
67
			->getMock();
68
69
		$this->defaults = $this->getMockBuilder('OCP\Defaults')
70
			->disableOriginalConstructor()
71
			->getMock();
72
73
		$this->controller = new EmailController($this->appName, $this->request,
74
			$this->userSession, $this->config, $this->mailer, $this->l10n, $this->defaults);
75
	}
76
77
	/**
78
	 * @dataProvider emailPublicLinkProvider
79
	 */
80
	public function testEmailPublicLink($validEmailAddress) {
81
		$this->userSession->expects($this->once())
82
			->method('getUser')
83
			->will($this->returnValue($this->dummyUser));
84
		$this->dummyUser->expects($this->once())
85
			->method('getDisplayName')
86
			->will($this->returnValue('Fancy displayname 42'));
87
88
		$this->l10n->expects($this->at(0))
89
			->method('t')
90
			->with('%s has published the calendar »%s«', ['Fancy displayname 42', 'Calendarname 1337'])
91
			->will($this->returnValue('localized_1'));
92
93
		$this->config->expects($this->at(0))
94
			->method('getSystemValue')
95
			->with('version')
96
			->will($this->returnValue('12.0.0'));
97
98
		$this->mailer->expects($this->at(0))
99
			->method('createEMailTemplate')
100
			->will($this->returnValue($this->emailTemplate));
101
102
		$this->emailTemplate->expects($this->at(1))
103
			->method('addHeader');
104
105
		$this->l10n->expects($this->at(1))
106
			->method('t')
107
			->with('%s has published the calendar »%s«', ['Fancy displayname 42', 'Calendarname 1337'])
108
			->will($this->returnValue('localized_2'));
109
		$this->emailTemplate->expects($this->at(1))
110
			->method('addHeading')
111
			->with('localized_2');
112
113
		$this->l10n->expects($this->at(2))
114
			->method('t')
115
			->with('Hello,')
116
			->will($this->returnValue('localized_3'));
117
		$this->emailTemplate->expects($this->at(2))
118
			->method('addBodyText')
119
			->with('localized_3');
120
121
		$this->l10n->expects($this->at(3))
122
			->method('t')
123
			->with('We wanted to inform you that %s has published the calendar »%s«.', ['Fancy displayname 42', 'Calendarname 1337'])
124
			->will($this->returnValue('localized_4'));
125
		$this->emailTemplate->expects($this->at(3))
126
			->method('addBodyText')
127
			->with('localized_4');
128
129
		$this->l10n->expects($this->at(4))
130
			->method('t')
131
			->with('Open »%s«', ['Calendarname 1337'])
132
			->will($this->returnValue('localized_5'));
133
		$this->emailTemplate->expects($this->at(4))
134
			->method('addBodyButton')
135
			->with('localized_5', 'https://url-to-public-calendar');
136
137
		$this->l10n->expects($this->at(5))
138
			->method('t')
139
			->with('Cheers!')
140
			->will($this->returnValue('localized_6'));
141
		$this->emailTemplate->expects($this->at(5))
142
			->method('addBodyText')
143
			->with('localized_6');
144
145
		$this->emailTemplate->expects($this->at(6))
146
			->method('addFooter');
147
		$this->emailTemplate->expects($this->at(7))
148
			->method('renderHtml')
149
			->will($this->returnValue('html_body'));
150
		$this->emailTemplate->expects($this->at(8))
151
			->method('renderText')
152
			->will($this->returnValue('text_body'));
153
154
		$this->mailer->expects($this->at(1))
155
			->method('validateMailAddress')
156
			->with('[email protected]')
157
			->will($this->returnValue($validEmailAddress));
158
159
		if ($validEmailAddress) {
160
			$this->config->expects($this->at(1))
161
				->method('getSystemValue')
162
				->with('mail_domain', 'domain.org')
163
				->will($this->returnValue('domain_123'));
164
			$this->config->expects($this->at(2))
165
				->method('getSystemValue')
166
				->with('mail_from_address', 'nextcloud')
167
				->will($this->returnValue('from_456'));
168
169
			$this->mailer->expects($this->at(2))
170
				->method('createMessage')
171
				->will($this->returnValue($this->message));
172
173
			$this->message->expects($this->at(0))
174
				->method('setSubject')
175
				->with('localized_1');
176
			$this->defaults->expects($this->at(0))
177
				->method('getName')
178
				->will($this->returnValue('default_instance_name'));
179
			$this->message->expects($this->at(1))
180
				->method('setFrom')
181
				->with(['from_456@domain_123' => 'default_instance_name']);
182
			$this->l10n->expects($this->at(6))
183
				->method('t')
184
				->with('Recipient')
185
				->will($this->returnValue('localized_7'));
186
			$this->message->expects($this->at(2))
187
				->method('setTo')
188
				->with(['[email protected]' => 'localized_7']);
189
			$this->message->expects($this->at(3))
190
				->method('setPlainBody')
191
				->with('text_body');
192
			$this->message->expects($this->at(4))
193
				->method('setHtmlBody')
194
				->with('html_body');
195
196
			$this->mailer->expects($this->at(3))
197
				->method('send')
198
				->with($this->message);
199
		}
200
201
		$response = $this->controller->sendEmailPublicLink('[email protected]', 'https://url-to-public-calendar', 'Calendarname 1337');
202
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
203
		$this->assertEquals([], $response->getData());
204
205
		if ($validEmailAddress) {
206
			$this->assertEquals(200, $response->getStatus());
207
		} else {
208
			$this->assertEquals(400, $response->getStatus());
209
		}
210
	}
211
212
	public function emailPublicLinkProvider() {
213
		return [
214
			[true],
215
			[false],
216
		];
217
	}
218
}
219