|
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 OC\AppFramework\Http; |
|
22
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
23
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
24
|
|
|
use OCA\Mail\Controller\FoldersController; |
|
25
|
|
|
|
|
26
|
|
|
class FoldersControllerTest extends PHPUnit_Framework_TestCase { |
|
27
|
|
|
|
|
28
|
|
|
private $controller; |
|
29
|
|
|
private $appName = 'mail'; |
|
30
|
|
|
private $request; |
|
31
|
|
|
private $accountService; |
|
32
|
|
|
private $userId = 'john'; |
|
33
|
|
|
|
|
34
|
|
|
public function setUp() { |
|
35
|
|
|
$this->request = $this->getMockBuilder('OCP\IRequest') |
|
36
|
|
|
->getMock(); |
|
37
|
|
|
$this->accountService = $this->getMockBuilder('OCA\Mail\Service\AccountService') |
|
38
|
|
|
->disableOriginalConstructor() |
|
39
|
|
|
->getMock(); |
|
40
|
|
|
$this->controller = new FoldersController($this->appName, $this->request, $this->accountService, $this->userId); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function folderDataProvider() { |
|
44
|
|
|
$files = [ |
|
45
|
|
|
'folders_german', |
|
46
|
|
|
]; |
|
47
|
|
|
// Add directory prefix to tests/data |
|
48
|
|
|
$data = array_map(function ($file) { |
|
49
|
|
|
$path = dirname(__FILE__) . '/../data/' . $file . '.json'; |
|
50
|
|
|
return [json_decode(file_get_contents($path), true)]; |
|
51
|
|
|
}, $files); |
|
52
|
|
|
|
|
53
|
|
|
// Add empty account = no folders |
|
54
|
|
|
array_push($data, [ |
|
55
|
|
|
[ |
|
56
|
|
|
'folders' => [ |
|
57
|
|
|
], |
|
58
|
|
|
], |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
return $data; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @dataProvider folderDataProvider |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testIndex($data) { |
|
68
|
|
|
$account = $this->getMockBuilder('OCA\Mail\Account') |
|
69
|
|
|
->disableOriginalConstructor() |
|
70
|
|
|
->getMock(); |
|
71
|
|
|
$accountId = 28; |
|
72
|
|
|
$this->accountService->expects($this->once()) |
|
73
|
|
|
->method('find') |
|
74
|
|
|
->with($this->userId, $accountId) |
|
75
|
|
|
->will($this->returnValue($account)); |
|
76
|
|
|
$account->expects($this->once()) |
|
77
|
|
|
->method('getListArray') |
|
78
|
|
|
->will($this->returnValue($data)); |
|
79
|
|
|
|
|
80
|
|
|
$this->controller->index($accountId); |
|
81
|
|
|
|
|
82
|
|
|
//TODO: check result |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testShow() { |
|
86
|
|
|
$result = $this->controller->show(); |
|
87
|
|
|
$this->assertEquals(Http::STATUS_NOT_IMPLEMENTED, $result->getStatus()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testUpdate() { |
|
91
|
|
|
$result = $this->controller->update(); |
|
92
|
|
|
$this->assertEquals(Http::STATUS_NOT_IMPLEMENTED, $result->getStatus()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testDestroy() { |
|
96
|
|
|
$accountId = 28; |
|
97
|
|
|
$folderId = 'my folder'; |
|
98
|
|
|
$account = $this->getMockBuilder('OCA\Mail\Account') |
|
99
|
|
|
->disableOriginalConstructor() |
|
100
|
|
|
->getMock(); |
|
101
|
|
|
$this->accountService->expects($this->once()) |
|
102
|
|
|
->method('find') |
|
103
|
|
|
->with($this->userId, $accountId) |
|
104
|
|
|
->will($this->returnValue($account)); |
|
105
|
|
|
$imapConnection = $this->getMockBuilder('Horde_Imap_Client_Socket') |
|
106
|
|
|
->disableOriginalConstructor() |
|
107
|
|
|
->getMock(); |
|
108
|
|
|
$account->expects($this->once()) |
|
109
|
|
|
->method('getImapConnection') |
|
110
|
|
|
->will($this->returnValue($imapConnection)); |
|
111
|
|
|
$imapConnection->expects($this->once()) |
|
112
|
|
|
->method('deleteMailbox') |
|
113
|
|
|
->with($folderId); |
|
114
|
|
|
|
|
115
|
|
|
$this->controller->destroy($accountId, $folderId); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testDestroyAccountNotFound() { |
|
119
|
|
|
$accountId = 28; |
|
120
|
|
|
$folderId = 'my folder'; |
|
121
|
|
|
$this->accountService->expects($this->once()) |
|
122
|
|
|
->method('find') |
|
123
|
|
|
->with($this->userId, $accountId) |
|
124
|
|
|
->will($this->throwException(new DoesNotExistException('folder not found'))); |
|
125
|
|
|
|
|
126
|
|
|
$response = $this->controller->destroy($accountId, $folderId); |
|
127
|
|
|
$expected = new JSONResponse(null, 404); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals($expected, $response); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testDestroyFolderNotFound() { |
|
133
|
|
|
// TODO: write test |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testCreate() { |
|
137
|
|
|
$accountId = 13; |
|
138
|
|
|
$folderId = 'new folder'; |
|
139
|
|
|
$account = $this->getMockBuilder('OCA\Mail\Account') |
|
140
|
|
|
->disableOriginalConstructor() |
|
141
|
|
|
->getMock(); |
|
142
|
|
|
$this->accountService->expects($this->once()) |
|
143
|
|
|
->method('find') |
|
144
|
|
|
->with($this->userId, $accountId) |
|
145
|
|
|
->will($this->returnValue($account)); |
|
146
|
|
|
$imapConnection = $this->getMockBuilder('Horde_Imap_Client_Socket') |
|
147
|
|
|
->disableOriginalConstructor() |
|
148
|
|
|
->getMock(); |
|
149
|
|
|
$account->expects($this->once()) |
|
150
|
|
|
->method('getImapConnection') |
|
151
|
|
|
->will($this->returnValue($imapConnection)); |
|
152
|
|
|
$imapConnection->expects($this->once()) |
|
153
|
|
|
->method('createMailbox') |
|
154
|
|
|
->with($folderId); |
|
155
|
|
|
|
|
156
|
|
|
$response = $this->controller->create($accountId, $folderId); |
|
157
|
|
|
|
|
158
|
|
|
$expected = new JSONResponse([ |
|
159
|
|
|
'data' => [ |
|
160
|
|
|
'id' => $folderId |
|
161
|
|
|
] |
|
162
|
|
|
], Http::STATUS_CREATED); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertEquals($expected, $response); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testCreateWithError() { |
|
168
|
|
|
$accountId = 13; |
|
169
|
|
|
$folderId = 'new folder'; |
|
170
|
|
|
$account = $this->getMockBuilder('OCA\Mail\Account') |
|
171
|
|
|
->disableOriginalConstructor() |
|
172
|
|
|
->getMock(); |
|
173
|
|
|
$this->accountService->expects($this->once()) |
|
174
|
|
|
->method('find') |
|
175
|
|
|
->with($this->userId, $accountId) |
|
176
|
|
|
->will($this->returnValue($account)); |
|
177
|
|
|
$imapConnection = $this->getMockBuilder('Horde_Imap_Client_Socket') |
|
178
|
|
|
->disableOriginalConstructor() |
|
179
|
|
|
->getMock(); |
|
180
|
|
|
$account->expects($this->once()) |
|
181
|
|
|
->method('getImapConnection') |
|
182
|
|
|
->will($this->returnValue($imapConnection)); |
|
183
|
|
|
$imapConnection->expects($this->once()) |
|
184
|
|
|
->method('createMailbox') |
|
185
|
|
|
->with($folderId) |
|
186
|
|
|
->will($this->throwException(new \Horde_Imap_Client_Exception())); |
|
187
|
|
|
|
|
188
|
|
|
$response = $this->controller->create($accountId, $folderId); |
|
189
|
|
|
|
|
190
|
|
|
$expected = new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertEquals($expected, $response); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testCreateSubFolder() { |
|
196
|
|
|
// TODO: write test |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testDetectChanges() { |
|
200
|
|
|
// TODO: write test |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
|