InviteTest::testUserToInviteIsOCUser()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
rs 8.9411
cc 1
eloc 36
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be
4
 * This file is licensed under the AGPL version 3 or later.
5
 * See the COPYING file.
6
 */
7
8
namespace OCA\Chat\OCH\Commands;
9
10
include_once(__DIR__ . '/../../../autoloader.php');
11
include_once(__DIR__ . '/../../../vendor/Pimple/Pimple.php');
12
13
14
use OCA\Chat\App\Chat;
15
use OCA\Chat\OCH\Db\PushMessage;
16
use OCA\Chat\OCH\Db\User;
17
18
// Refer to GreetTest::testDBFailure for a DBFailure test
19
// This almost the same for every unit test
20
class InviteTest extends \PHPUnit_Framework_TestCase {
21
22
	public static $pushMessage;
23
24
	public function setUp(){
25
		$app =  new Chat();
0 ignored issues
show
Bug introduced by
The call to Chat::__construct() misses some required arguments starting with $backendManager.
Loading history...
26
		$this->container = $app->getContainer();
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The method getContainer() does not seem to exist on object<OCA\Chat\App\Chat>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
		$this->container['API'] = $this->getMockBuilder('\OCA\Chat\Core\API')
28
			->disableOriginalConstructor()
29
			->getMock();
30
		$this->container['API']->expects($this->any())
31
			->method('log')
32
			->will($this->returnValue(null));
33
34
	}
35
36
	public function testOmmittedConvId(){
37
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'CONV-ID-MUST-BE-PROVIDED');
38
39
		$this->container['API']->expects($this->any())
40
			->method('prepareQuery')
41
			->will($this->returnValue(true));
42
43
		// logic
44
		$invite = new Invite($this->container);
0 ignored issues
show
Bug introduced by
The call to Invite::__construct() misses some required arguments starting with $join.
Loading history...
45
		$invite->setRequestData(array(
46
			'user' => array (
47
				'id' => 'admin',
48
				'online' => false,
49
				'displayname' => 'admin',
50
				'backends' => array (
51
					'och' => array (
52
						'id' => NULL,
53
						'displayname' => 'ownCloud Handle',
54
						'protocol' => 'x-owncloud-handle',
55
						'namespace' => 'och',
56
						'value' => 'admin',
57
					),
58
				),
59
				'address_book_id' => 'admin',
60
				'address_book_backend' => 'localusers',
61
			),
62
			'session_id' => 'c08809598b01894c468873fab54291aa',
63
			'timestamp' => 1397328934.658,
64
			'user_to_invite' => array (
65
				'id' => 'derp',
66
				'online' => false,
67
				'displayname' => 'derp',
68
				'backends' => array (
69
					'och' => array (
70
						'id' => NULL,
71
						'displayname' => 'ownCloud Handle',
72
						'protocol' => 'x-owncloud-handle',
73
						'namespace' => 'och',
74
						'value' => 'derp',
75
					),
76
				),
77
				'address_book_id' => 'admin',
78
				'address_book_backend' => 'localusers',
79
			),
80
			//'conv_id' => 'addeimnpr',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
		));
82
	}
83
84
	public function testOmmittedUserToInvite(){
85
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'USER-TO-INVITE-MUST-BE-PROVIDED');
86
87
		// logic
88
		$invite= new Invite($this->container);
0 ignored issues
show
Bug introduced by
The call to Invite::__construct() misses some required arguments starting with $join.
Loading history...
89
		$invite->setRequestData(array(
90
			'user' => array (
91
				'id' => 'admin',
92
				'online' => false,
93
				'displayname' => 'admin',
94
				'backends' => array (
95
					'och' => array (
96
						'id' => NULL,
97
						'displayname' => 'ownCloud Handle',
98
						'protocol' => 'x-owncloud-handle',
99
						'namespace' => 'och',
100
						'value' => 'admin',
101
					),
102
				),
103
				'address_book_id' => 'admin',
104
				'address_book_backend' => 'localusers',
105
			),
106
			'session_id' => 'c08809598b01894c468873fab54291aa',
107
			'timestamp' => 1397328934.658,
108
			'conv_id' => 'addeimnpr',
109
		));
110
	}
111
112
113
	public function testEmptyUserToInvite(){
114
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'USER-TO-INVITE-MUST-BE-PROVIDED');
115
116
		// logic
117
		$invite= new Invite($this->container);
0 ignored issues
show
Bug introduced by
The call to Invite::__construct() misses some required arguments starting with $join.
Loading history...
118
		$invite->setRequestData(array(
119
				'user' => array (
120
						'id' => 'admin',
121
						'online' => false,
122
						'displayname' => 'admin',
123
						'backends' => array (
124
								'och' => array (
125
										'id' => NULL,
126
										'displayname' => 'ownCloud Handle',
127
										'protocol' => 'x-owncloud-handle',
128
										'namespace' => 'och',
129
										'value' => 'admin',
130
								),
131
						),
132
						'address_book_id' => 'admin',
133
						'address_book_backend' => 'localusers',
134
				),
135
				'session_id' => 'c08809598b01894c468873fab54291aa',
136
				'timestamp' => 1397328934.658,
137
				'conv_id' => 'addeimnpr',
138
				'user_to_invite' => array()
139
		));
140
	}
141
142
	public function testUserEqualsUserToInvite(){
143
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'USER-EQAUL-TO-USER-TO-INVITE');
144
145
		// logic
146
		$invite= new Invite($this->container);
0 ignored issues
show
Bug introduced by
The call to Invite::__construct() misses some required arguments starting with $join.
Loading history...
147
		$invite->setRequestData(array(
148
				'user' => array (
149
					'id' => 'admin',
150
					'online' => false,
151
					'displayname' => 'admin',
152
					'backends' => array (
153
						'och' => array (
154
							'id' => NULL,
155
							'displayname' => 'ownCloud Handle',
156
							'protocol' => 'x-owncloud-handle',
157
							'namespace' => 'och',
158
							'value' => 'admin',
159
						),
160
					),
161
					'address_book_id' => 'admin',
162
					'address_book_backend' => 'localusers',
163
				),
164
				'user_to_invite' => array (
165
					'id' => 'admin',
166
					'online' => false,
167
					'displayname' => 'admin',
168
					'backends' => array (
169
						'och' => array (
170
							'id' => NULL,
171
							'displayname' => 'ownCloud Handle',
172
							'protocol' => 'x-owncloud-handle',
173
							'namespace' => 'och',
174
							'value' => 'admin',
175
						),
176
					),
177
					'address_book_id' => 'admin',
178
					'address_book_backend' => 'localusers',
179
				),
180
				'session_id' => 'c08809598b01894c468873fab54291aa',
181
				'timestamp' => 1397328934.658,
182
				'conv_id' => 'addeimnpr',
183
		));
184
	}
185
186
	public function testUserToInviteIsOCUser(){
187
		$this->container['API']->expects($this->once())
188
			->method('getUsers')
189
			->will($this->returnValue(array("admin", "herp"))); // Simulation of the OC users, derp is omitted
190
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'USER-TO-INVITE-NOT-OC-USER');
191
192
		// logic
193
		$invite= new Invite($this->container);
0 ignored issues
show
Bug introduced by
The call to Invite::__construct() misses some required arguments starting with $join.
Loading history...
194
		$invite->setRequestData(array(
195
			'user' => array (
196
				'id' => 'admin',
197
				'online' => false,
198
				'displayname' => 'admin',
199
				'backends' => array (
200
					'och' => array (
201
						'id' => NULL,
202
						'displayname' => 'ownCloud Handle',
203
						'protocol' => 'x-owncloud-handle',
204
						'namespace' => 'och',
205
						'value' => 'admin',
206
					),
207
				),
208
				'address_book_id' => 'admin',
209
				'address_book_backend' => 'localusers',
210
			),
211
			'user_to_invite' => array (
212
				'id' => 'derp',
213
				'online' => false,
214
				'displayname' => 'derp',
215
				'backends' => array (
216
					'och' => array (
217
						'id' => NULL,
218
						'displayname' => 'ownCloud Handle',
219
						'protocol' => 'x-owncloud-handle',
220
						'namespace' => 'och',
221
						'value' => 'derp',
222
					),
223
				),
224
				'address_book_id' => 'admin',
225
				'address_book_backend' => 'localusers',
226
			),
227
			'session_id' => 'c08809598b01894c468873fab54291aa',
228
			'timestamp' => 1397328934.658,
229
			'conv_id' => 'addeimnpr',
230
		));
231
	}
232
233
	public function testExecute(){
234
		$this->container['API']->expects($this->once())
235
			->method('getUsers')
236
			->will($this->returnValue(array("admin", "herp", "derp"))); // Simulation of the OC users
237
238
        $this->container['UserOnlineMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserOnlineMapper')
239
			->disableOriginalConstructor()
240
			->getMock();
241
242
        $this->container['PushMessageMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\PushMessageMapper')
243
			->disableOriginalConstructor()
244
			->getMock();
245
246
		$this->container['PushMessageMapper']->expects($this->any())
247
			->method('insert')
248
			->will($this->returnCallback(function($pushMessage){
249
				InviteTest::$pushMessage = $pushMessage;
250
			}));
251
252
        $this->container['UserMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserMapper')
253
            ->disableOriginalConstructor()
254
            ->getMock();
255
256
        $this->container['UserMapper']->expects($this->any())
257
            ->method('insertUnique')
258
            ->will($this->returnValue(true));
259
260
        $this->container['UserOnlineMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserOnlineMapper')
261
            ->disableOriginalConstructor()
262
            ->getMock();
263
264
        $userToInviteSession = new User();
265
        $userToInviteSession->setUser('foo');
266
        $userToInviteSession->setSessionId(md5(time()));
267
268
        $this->container['UserOnlineMapper']->expects($this->any())
269
            ->method('findByUser')
270
            ->will($this->returnValue(array(
271
                    $userToInviteSession
272
                )
273
            ));
274
275
		$expectedPushMessage = new PushMessage();
276
		$expectedPushMessage->setSender('admin');
277
		$expectedPushMessage->setReceiver('foo');
278
		$expectedPushMessage->setReceiverSessionId(md5(time()));
279
		$expectedPushMessage->setCommand(json_encode(array(
280
			'type' => 'invite',
281
			'data' => array(
282
				'user' => 	array (
283
					'id' => 'admin',
284
					'online' => false,
285
					'displayname' => 'admin',
286
					'backends' => array (
287
						'och' => array (
288
							'id' => NULL,
289
							'displayname' => 'ownCloud Handle',
290
							'protocol' => 'x-owncloud-handle',
291
							'namespace' => 'och',
292
							'value' => 'admin',
293
						),
294
					),
295
					'address_book_id' => 'admin',
296
					'address_book_backend' => 'localusers',
297
				),
298
				'conv_id' => 'addeimnpr',
299
				'user_to_invite' => array (
300
						'id' => 'derp',
301
						'online' => false,
302
						'displayname' => 'derp',
303
						'backends' => array (
304
						'och' => array (
305
							'id' => NULL,
306
							'displayname' => 'ownCloud Handle',
307
							'protocol' => 'x-owncloud-handle',
308
							'namespace' => 'och',
309
							'value' => 'derp',
310
						),
311
					),
312
					'address_book_id' => 'admin',
313
					'address_book_backend' => 'localusers',
314
				),
315
			)
316
		)));
317
318
        // logic
319
		$invite= new Invite($this->container);
0 ignored issues
show
Bug introduced by
The call to Invite::__construct() misses some required arguments starting with $join.
Loading history...
Documentation introduced by
$this->container is of type array<string,?,{"UserOnlineMapper":"?"}>, but the function expects a object<OCA\Chat\OCH\Db\PushMessageMapper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
320
		$invite->setRequestData(array(
321
			'user' => array (
322
				'id' => 'admin',
323
				'online' => false,
324
				'displayname' => 'admin',
325
				'backends' => array (
326
					'och' => array (
327
						'id' => NULL,
328
						'displayname' => 'ownCloud Handle',
329
						'protocol' => 'x-owncloud-handle',
330
						'namespace' => 'och',
331
						'value' => 'admin',
332
					),
333
				),
334
				'address_book_id' => 'admin',
335
				'address_book_backend' => 'localusers',
336
			),
337
			'user_to_invite' => array (
338
				'id' => 'derp',
339
				'online' => false,
340
				'displayname' => 'derp',
341
				'backends' => array (
342
					'och' => array (
343
						'id' => NULL,
344
						'displayname' => 'ownCloud Handle',
345
						'protocol' => 'x-owncloud-handle',
346
						'namespace' => 'och',
347
						'value' => 'derp',
348
					),
349
				),
350
				'address_book_id' => 'admin',
351
				'address_book_backend' => 'localusers',
352
			),
353
			'session_id' => 'c08809598b01894c468873fab54291aa',
354
			'timestamp' => 1397328934.658,
355
			'conv_id' => 'addeimnpr',
356
		));
357
		$invite->execute();
358
359
		$this->assertEquals($expectedPushMessage, InviteTest::$pushMessage);
360
	}
361
362
}
363