Completed
Push — master ( c57b95...1d3e11 )
by Maxence
30s queued 14s
created

testCreateCircleWithDuplicateFromOthers()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 7.8682
c 0
b 0
f 0
cc 8
nc 17
nop 0
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Tests\Api;
28
29
use Exception;
30
use OCA\Circles\Api\v1\Circles;
31
use OCA\Circles\Exceptions\CircleAlreadyExistsException;
32
use OCA\Circles\Exceptions\CircleDoesNotExistException;
33
use OCA\Circles\Exceptions\CircleTypeNotValidException;
34
use OCA\Circles\Exceptions\MemberDoesNotExistException;
35
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
36
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
37
use OCA\Circles\Exceptions\ModeratorIsNotHighEnoughException;
38
use OCA\Circles\Model\DeprecatedCircle;
39
use OCA\Circles\Model\DeprecatedMember;
40
use OCA\Circles\Tests\Env;
41
use OCP\AppFramework\QueryException;
42
43
44
class CirclesTest extends \PHPUnit_Framework_TestCase {
45
46
	const NAME_PUBLIC_CIRCLE1 = '_circleNamePublic1';
47
	const NAME_SECRET_CIRCLE1 = '_circleNameSecret1';
48
	const NAME_CLOSED_CIRCLE1 = '_circleNameClosed1';
49
	const NAME_PERSONAL_CIRCLE1 = '_circleNamePersonal1';
50
51
	const NAME_PUBLIC_CIRCLE2 = '_circleNamePublic2';
52
	const NAME_SECRET_CIRCLE2 = '_circleNameSecret2';
53
	const NAME_CLOSED_CIRCLE2 = '_circleNameClosed2';
54
	const NAME_PERSONAL_CIRCLE2 = '_circleNamePersonal2';
55
56
57
	/** @var DeprecatedCircle[] */
58
	private $circles;
59
60
	/**
61
	 * setUp() is initiated before each test.
62
	 *
63
	 * Function will create 4 differents circles under user ENV_TEST_OWNER1
64
	 *
65
	 * @throws Exception
66
	 */
67
	protected function setUp() {
68
		Env::setUser(Env::ENV_TEST_OWNER1);
69
70
		$this->circles = array();
71
		try {
72
			$this->circles = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Public' => \OCA\C...NAME_PERSONAL_CIRCLE1)) of type array<string,object<OCA\...l\\DeprecatedCircle>"}> is incompatible with the declared type array<integer,object<OCA...odel\DeprecatedCircle>> of property $circles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
				'Public'   =>
74
					Circles::createCircle(DeprecatedCircle::CIRCLES_PUBLIC, self::NAME_PUBLIC_CIRCLE1),
75
				'Secret'   =>
76
					Circles::createCircle(DeprecatedCircle::CIRCLES_SECRET, self::NAME_SECRET_CIRCLE1),
77
				'Closed'   =>
78
					Circles::createCircle(DeprecatedCircle::CIRCLES_CLOSED, self::NAME_CLOSED_CIRCLE1),
79
				'Personal' =>
80
					Circles::createCircle(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE1)
81
			];
82
83
		} catch (Exception $e) {
84
			throw $e;
85
		}
86
87
		Env::logout();
88
	}
89
90
91
	/**
92
	 * tearDown() is initiated after each test.
93
	 *
94
	 * Function will destroy the circles created in setUp()
95
	 *
96
	 * @throws Exception
97
	 */
98
	protected function tearDown() {
99
		Env::setUser(Env::ENV_TEST_OWNER1);
100
		try {
101
			foreach ($this->circles as $circle) {
102
				Circles::destroyCircle($circle->getId());
103
			}
104
		} catch (Exception $e) {
105
			throw $e;
106
		}
107
108
		Env::logout();
109
	}
110
111
112
	/**
113
	 * Testing Circles::version()
114
	 */
115
	public function testVersion() {
116
		$this->assertSame(Circles::version(), Circles::API_VERSION);
117
	}
118
119
120
	/**
121
	 * Testing the tools to switch users
122
	 */
123
	public function testUserSession() {
124
		Env::setUser(Env::ENV_TEST_ADMIN1);
125
		$this->assertEquals(Env::currentUser(), Env::ENV_TEST_ADMIN1);
126
		Env::setUser(Env::ENV_TEST_OWNER3);
127
		try {
128
			$this->assertEquals(Env::currentUser(), Env::ENV_TEST_ADMIN1);
129
			$this->assertSame(true, false, 'should return an exception');
130
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
131
		}
132
		Env::setUser(Env::ENV_TEST_OWNER1);
133
		$this->assertEquals(Env::currentUser(), Env::ENV_TEST_OWNER1);
134
	}
135
136
	/**
137
	 * Testing Leveling Members. (not in Personal Circle)
138
	 *
139
	 * @throws Exception
140
	 */
141
	public function testLevelMemberInCircles() {
142
		Env::setUser(Env::ENV_TEST_OWNER1);
143
144
		$circles = [$this->circles['Public'], $this->circles['Closed'], $this->circles['Secret']];
145
146
		// OWNER1 Should be able to add/level anyone to Admin Level at least
147
		try {
148
			foreach ($circles as $circle) {
149
				$this->generateSimpleCircleWithAllLevel(
150
					$circle->getId(), ($circle->getType() === DeprecatedCircle::CIRCLES_CLOSED)
151
				);
152
			}
153
		} catch (Exception $e) {
154
			throw $e;
155
		}
156
157
		Env::logout();
158
159
160
		// ADMIN1 should be able to add/level anyone to Moderator level
161
		Env::setUser(Env::ENV_TEST_ADMIN1);
162
163
		try {
164
			foreach ($circles as $circle) {
165
				Circles::addMember($circle->getId(), Env::ENV_TEST_ADMIN2, DeprecatedMember::TYPE_USER);
166
167 View Code Duplication
				if ($circle->getType() === DeprecatedCircle::CIRCLES_CLOSED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
168
					// In closed circle, we need to confirm the invitation
169
					Env::setUser(Env::ENV_TEST_ADMIN2);
170
					Circles::joinCircle($circle->getId());
171
					Env::setUser(Env::ENV_TEST_ADMIN1);
172
				}
173
174
				Circles::levelMember(
175
					$circle->getId(), Env::ENV_TEST_ADMIN2, DeprecatedMember::TYPE_USER,
176
					DeprecatedMember::LEVEL_MODERATOR
177
				);
178
			}
179
		} catch (Exception $e) {
180
			throw $e;
181
		}
182
		Env::logout();
183
184
185
		// ADMIN1 should not be able to level anyone to Admin Level
186
		Env::setUser(Env::ENV_TEST_ADMIN1);
187
188 View Code Duplication
		foreach ($circles as $circle) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
189
190
			try {
191
				Circles::levelMember(
192
					$circle->getId(), Env::ENV_TEST_ADMIN3, DeprecatedMember::TYPE_USER,
193
					DeprecatedMember::LEVEL_MODERATOR
194
				);
195
				$this->assertSame(true, false, 'should return an exception');
196
			} catch (MemberDoesNotExistException $e) {
197
			} catch (Exception $e) {
198
				$this->assertSame(
199
					true, false, 'should have returned a MemberDoesNotExistException'
200
				);
201
			}
202
203
			try {
204
				Circles::levelMember(
205
					$circle->getId(), Env::ENV_TEST_ADMIN2, DeprecatedMember::TYPE_USER, DeprecatedMember::LEVEL_ADMIN
206
				);
207
				$this->assertSame(true, false, 'should return an exception');
208
			} catch (ModeratorIsNotHighEnoughException $e) {
209
			} catch (Exception $e) {
210
				$this->assertSame(
211
					true, false, 'should have returned a ModeratorIsNotHighEnoughException'
212
				);
213
			}
214
215
			try {
216
				Circles::levelMember(
217
					$circle->getId(), Env::ENV_TEST_ADMIN2, DeprecatedMember::TYPE_USER, DeprecatedMember::LEVEL_OWNER
218
				);
219
				$this->assertSame(true, false, 'should return an exception');
220
			} catch (MemberIsNotOwnerException $e) {
221
			} catch (Exception $e) {
222
				$this->assertSame(
223
					true, false, 'should have returned a MemberIsNotOwnerException'
224
				);
225
			}
226
		}
227
228
		Env::logout();
229
230
231
		// MODERATOR1 should be able to add anyone
232
		Env::setUser(Env::ENV_TEST_MODERATOR1);
233
234
		try {
235
			foreach ($circles as $circle) {
236
				Circles::addMember($circle->getId(), Env::ENV_TEST_MODERATOR2, DeprecatedMember::TYPE_USER);
237 View Code Duplication
				if ($circle->getType() === DeprecatedCircle::CIRCLES_CLOSED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
238
					// In closed circle, we need to confirm the invitation
239
					Env::setUser(Env::ENV_TEST_MODERATOR2);
240
					Circles::joinCircle($circle->getId());
241
					Env::setUser(Env::ENV_TEST_MODERATOR1);
242
				}
243
			}
244
		} catch (Exception $e) {
245
			throw $e;
246
		}
247
248
		Env::logout();
249
250
251
		// MODERATOR1 should not be able to add/level anyone to Moderator/Admin Level
252
		Env::setUser(Env::ENV_TEST_MODERATOR1);
253
254 View Code Duplication
		foreach ($circles as $circle) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
255
			try {
256
				Circles::levelMember(
257
					$circle->getId(), Env::ENV_TEST_MODERATOR2, DeprecatedMember::TYPE_USER,
258
					DeprecatedMember::LEVEL_MODERATOR
259
				);
260
				$this->assertSame(true, false, 'should return an exception');
261
			} catch (ModeratorIsNotHighEnoughException $e) {
262
			} catch (Exception $e) {
263
				$this->assertSame(
264
					true, false, 'should have returned a ModeratorIsNotHighEnoughException'
265
				);
266
			}
267
			try {
268
				Circles::levelMember(
269
					$circle->getId(), Env::ENV_TEST_MODERATOR2, DeprecatedMember::TYPE_USER,
270
					DeprecatedMember::LEVEL_ADMIN
271
				);
272
				$this->assertSame(true, false, 'should return an exception');
273
			} catch (ModeratorIsNotHighEnoughException $e) {
274
			} catch (Exception $e) {
275
				$this->assertSame(
276
					true, false, 'should have returned a ModeratorIsNotHighEnoughException'
277
				);
278
			}
279
			try {
280
				Circles::levelMember(
281
					$circle->getId(), Env::ENV_TEST_MODERATOR2, DeprecatedMember::TYPE_USER,
282
					DeprecatedMember::LEVEL_OWNER
283
				);
284
				$this->assertSame(true, false, 'should return an exception');
285
			} catch (MemberIsNotOwnerException $e) {
286
			} catch (Exception $e) {
287
				$this->assertSame(
288
					true, false, 'should have returned a MemberIsNotOwnerException'
289
				);
290
			}
291
		}
292
293
		Env::logout();
294
295
296
		// MEMBER1 should not be able to add/level anyone to any level
297
		Env::setUser(Env::ENV_TEST_MEMBER1);
298
299
		foreach ($circles as $circle) {
300
			try {
301
				Circles::addMember(
302
					$circle->getId(), Env::ENV_TEST_MEMBER2, DeprecatedMember::TYPE_USER
303
				);
304
				$this->assertSame(true, false, 'should return an exception');
305
			} catch (MemberIsNotModeratorException $e) {
306
			} catch (Exception $e) {
307
				$this->assertSame(
308
					true, false, 'should have returned a MemberIsNotModeratorException'
309
				);
310
			}
311
312
			try {
313
				Circles::levelMember(
314
					$circle->getId(), Env::ENV_TEST_USER1, DeprecatedMember::TYPE_USER, DeprecatedMember::LEVEL_MEMBER
315
				);
316
				$this->assertSame(true, false, 'should return an exception');
317
			} catch (MemberDoesNotExistException $e) {
318
			} catch (Exception $e) {
319
				$this->assertSame(
320
					true, false, 'should have returned a MemberDoesNotExistException'
321
				);
322
			}
323
324
			try {
325
				Circles::levelMember(
326
					$circle->getId(), Env::ENV_TEST_MEMBER1, DeprecatedMember::TYPE_USER,
327
					DeprecatedMember::LEVEL_MODERATOR
328
				);
329
				$this->assertSame(true, false, 'should return an exception');
330
			} catch (MemberIsNotModeratorException $e) {
331
			} catch (Exception $e) {
332
				$this->assertSame(
333
					true, false,
334
					'should have returned a MemberIsNotModeratorException - ' . $e->getMessage()
335
				);
336
			}
337
338
339
			try {
340
				Circles::levelMember(
341
					$circle->getId(), Env::ENV_TEST_OWNER1, DeprecatedMember::TYPE_USER, DeprecatedMember::LEVEL_MEMBER
342
				);
343
				$this->assertSame(true, false, 'should return an exception');
344
			} catch (MemberIsNotModeratorException $e) {
345
			} catch (Exception $e) {
346
				$this->assertSame(
347
					true, false,
348
					'should have returned a MemberIsNotModeratorException - ' . $e->getMessage()
349
				);
350
			}
351
352
		}
353
354
		Env::logout();
355
	}
356
357
358
	/**
359
	 * Testing Leveling Members in Personal Circle.
360
	 *
361
	 * @throws Exception
362
	 */
363
	public function testLevelMemberInPersonalCircle() {
364
		Env::setUser(Env::ENV_TEST_OWNER1);
365
366
		try {
367
			$this->generateSimpleCircleWithAllLevel($this->circles['Personal']->getId());
368
			$this->assertSame(true, false, 'should return an exception');
369
		} catch (CircleTypeNotValidException $e) {
370
		} catch (Exception $e) {
371
			$this->assertSame(true, false, 'should have returned a CircleTypeNotValid');
372
		}
373
374
		Env::logout();
375
	}
376
377
378
	/**
379
	 * Testing creation of a circle with duplicate name as the owner.
380
	 */
381
	public function testCreateCircleWithDuplicate() {
382
		Env::setUser(Env::ENV_TEST_OWNER1);
383
384
		$circleNames = [
385
			self::NAME_PUBLIC_CIRCLE1,
386
			self::NAME_SECRET_CIRCLE1,
387
			self::NAME_CLOSED_CIRCLE1
388
		];
389
390
		for ($i = 0; $i < sizeof(Env::listCircleTypes()); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
391
			if (Env::listCircleTypes()[$i] === DeprecatedCircle::CIRCLES_PERSONAL) {
392
				try {
393
					Circles::createCircle(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE1);
394
					$this->assertSame(true, false, 'should return an exception');
395
				} catch (CircleAlreadyExistsException $e) {
396
				} catch (Exception $e) {
397
					$this->assertSame(
398
						true, false, 'should have returned a CircleAlreadyExistsException'
399
					);
400
				}
401
402
			} else {
403
				for ($j = 0; $j < sizeof($circleNames); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
404
					try {
405
						Circles::createCircle(Env::listCircleTypes()[$i], $circleNames[$j]);
406
						$this->assertSame(true, false, 'should return an exception');
407
					} catch (CircleAlreadyExistsException $e) {
408
					} catch (Exception $e) {
409
						$this->assertSame(
410
							true, false, 'should have returned a CircleAlreadyExistsException'
411
						);
412
					}
413
				}
414
			}
415
		}
416
417
		Env::logout();
418
	}
419
420
421
	/**
422
	 * Testing creation of a circle with duplicate name as a new owner.
423
	 */
424
	public function testCreateCircleWithDuplicateFromOthers() {
425
		Env::setUser(Env::ENV_TEST_OWNER2);
426
427
		$circleNames = [
428
			self::NAME_PUBLIC_CIRCLE1,
429
			self::NAME_SECRET_CIRCLE1,
430
			self::NAME_CLOSED_CIRCLE1,
431
		];
432
433
		$circles = [];
434
		array_push(
435
			$circles, Circles::createCircle(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE1)
436
		);
437
438
		for ($i = 0; $i < sizeof(Env::listCircleTypes()); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
439
			for ($j = 0; $j < sizeof($circleNames); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
440
				if (Env::listCircleTypes()[$i] === DeprecatedCircle::CIRCLES_PERSONAL) {
441
					try {
442
						array_push(
443
							$circles, Circles::createCircle(
444
							Env::listCircleTypes()[$i], $circleNames[$j]
445
						)
446
						);
447
					} catch (Exception $e) {
448
						throw $e;
449
					}
450
				} else {
451
					try {
452
						Circles::createCircle(Env::listCircleTypes()[$i], $circleNames[$j]);
453
						$this->assertSame(
454
							true, false, 'should return an exception'
455
						);
456
					} catch (CircleAlreadyExistsException $e) {
457
					} catch (Exception $e) {
458
						$this->assertSame(
459
							true, false,
460
							'should have returned a CircleAlreadyExistsException'
461
						);
462
					}
463
				}
464
			}
465
		}
466
467
		foreach ($circles as $circle) {
468
			Circles::destroyCircle($circle->getId());
469
		}
470
471
		Env::logout();
472
	}
473
474
475
	/**
476
	 * In this test, we will add user to circle, check their level and rights and remove them
477
	 * before checking their rights again.
478
	 */
479
	public function testAddAndRemoveUser() {
480
		Env::setUser(Env::ENV_TEST_OWNER1);
481
482
		for ($i = 0; $i < 3; $i++) {
483
			foreach ($this->circles as $circle) {
484
485
				try {
486
					$member = Circles::getMember(
487
						$circle->getId(), Env::ENV_TEST_MEMBER2, DeprecatedMember::TYPE_USER
488
					);
489
					$this->assertEquals(
490
						[
491
							Env::ENV_TEST_MEMBER2, DeprecatedMember::LEVEL_NONE, DeprecatedMember::STATUS_NONMEMBER,
492
							$circle->getId()
493
						]
494
						, [
495
							$member->getUserId(), $member->getLevel(), $member->getStatus(),
496
							$member->getCircleId()
497
						]
498
					);
499
				} catch (MemberDoesNotExistException $e) {
500
				} catch (Exception $e) {
501
					throw $e;
502
				}
503
504
505
				try {
506
					Circles::addMember($circle->getId(), Env::ENV_TEST_MEMBER2, DeprecatedMember::TYPE_USER);
507
508
					// If Closed, we check that the user is not a member before confirming
509
					// the invitation using member account
510 View Code Duplication
					if ($circle->getType() === DeprecatedCircle::CIRCLES_CLOSED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
511
						$member = Circles::getMember(
512
							$circle->getId(), Env::ENV_TEST_MEMBER2, DeprecatedMember::TYPE_USER
513
						);
514
						$this->assertEquals(
515
							[
516
								Env::ENV_TEST_MEMBER2, DeprecatedMember::LEVEL_NONE, DeprecatedMember::STATUS_INVITED,
517
								$circle->getId()
518
							]
519
							, [
520
								$member->getUserId(), $member->getLevel(), $member->getStatus(),
521
								$member->getCircleId()
522
							]
523
						);
524
525
						Env::setUser(Env::ENV_TEST_MEMBER2);
526
						Circles::joinCircle($circle->getId());
527
						Env::setUser(Env::ENV_TEST_OWNER1);
528
					}
529
530
					$member = Circles::getMember(
531
						$circle->getId(), Env::ENV_TEST_MEMBER2, DeprecatedMember::TYPE_USER
532
					);
533
					$this->assertEquals(
534
						[
535
							Env::ENV_TEST_MEMBER2, DeprecatedMember::LEVEL_MEMBER, DeprecatedMember::STATUS_MEMBER,
536
							$circle->getId()
537
						]
538
						, [
539
							$member->getUserId(), $member->getLevel(), $member->getStatus(),
540
							$member->getCircleId()
541
						]
542
					);
543
544
545
					Circles::removeMember(
546
						$circle->getId(), Env::ENV_TEST_MEMBER2, DeprecatedMember::TYPE_USER
547
					);
548
549
					try {
550
						$member = Circles::getMember(
551
							$circle->getId(), Env::ENV_TEST_MEMBER2, DeprecatedMember::TYPE_USER
552
						);
553
						$this->assertEquals(
554
							[
555
								Env::ENV_TEST_MEMBER2, DeprecatedMember::LEVEL_NONE, DeprecatedMember::STATUS_NONMEMBER,
556
								$circle->getId()
557
							]
558
							, [
559
								$member->getUserId(), $member->getLevel(), $member->getStatus(),
560
								$member->getCircleId()
561
							]
562
						);
563
					} catch (MemberDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
564
					}
565
566
				} catch (Exception $e) {
567
					throw $e;
568
				}
569
570
			}
571
		}
572
573
		Env::logout();
574
	}
575
576
577
	/**
578
	 * We check the join/leave and the rights of a member during the process.
579
	 *
580
	 * @throws Exception
581
	 */
582
	public function testJoinCircleAndLeave() {
583
		Env::setUser(Env::ENV_TEST_MEMBER3);
584
585
		for ($i = 0; $i < 3; $i++) {
586
			foreach ($this->circles as $circle) {
587
588
				try {
589
					$member = Circles::getMember(
590
						$circle->getId(), Env::ENV_TEST_MEMBER3, DeprecatedMember::TYPE_USER
591
					);
592
					$this->assertEquals(
593
						[
594
							Env::ENV_TEST_MEMBER3, DeprecatedMember::LEVEL_NONE, DeprecatedMember::STATUS_NONMEMBER,
595
							$circle->getId()
596
						]
597
						, [
598
							$member->getUserId(), $member->getLevel(), $member->getStatus(),
599
							$member->getCircleId()
600
						]
601
					);
602
				} catch (MemberDoesNotExistException $e) {
603
					if ($circle->getType() === DeprecatedCircle::CIRCLES_PERSONAL) {
604
						throw $e;
605
					}
606
				} catch (CircleDoesNotExistException $f) {
607
					if ($circle->getType() !== DeprecatedCircle::CIRCLES_PERSONAL) {
608
						throw $f;
609
					}
610
				} catch (Exception $e) {
611
					throw $e;
612
				}
613
614
615
				if ($circle->getType() === DeprecatedCircle::CIRCLES_PERSONAL) {
616
					try {
617
						Circles::joinCircle($circle->getId());
618
						$this->assertSame(
619
							true, false, 'should return an exception'
620
						);
621
					} catch (CircleDoesNotExistException $e) {
622
					} catch (Exception $e) {
623
						$this->assertSame(
624
							true, false, 'should have returned a CircleDoesNotExistException'
625
						);
626
					}
627
				} else {
628
					Circles::joinCircle($circle->getId());
629
630
631
					try {
632
633
						// If Closed, we check that the user is not a member before accepting
634
						// the request using a moderator account
635 View Code Duplication
						if ($circle->getType() === DeprecatedCircle::CIRCLES_CLOSED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
636
							Env::setUser(Env::ENV_TEST_OWNER1);
637
							$member = Circles::getMember(
638
								$circle->getId(), Env::ENV_TEST_MEMBER3, DeprecatedMember::TYPE_USER
639
							);
640
							$this->assertEquals(
641
								[
642
									Env::ENV_TEST_MEMBER3, DeprecatedMember::LEVEL_NONE,
643
									DeprecatedMember::STATUS_REQUEST,
644
									$circle->getId()
645
								]
646
								, [
647
									$member->getUserId(), $member->getLevel(), $member->getStatus(),
648
									$member->getCircleId()
649
								]
650
							);
651
652
							Circles::addMember($circle->getId(), Env::ENV_TEST_MEMBER3, DeprecatedMember::TYPE_USER);
653
							Env::setUser(Env::ENV_TEST_MEMBER3);
654
						}
655
656
						$member = Circles::getMember(
657
							$circle->getId(), Env::ENV_TEST_MEMBER3, DeprecatedMember::TYPE_USER
658
						);
659
						$this->assertEquals(
660
							[
661
								Env::ENV_TEST_MEMBER3, DeprecatedMember::LEVEL_MEMBER, DeprecatedMember::STATUS_MEMBER,
662
								$circle->getId()
663
							]
664
							, [
665
								$member->getUserId(), $member->getLevel(), $member->getStatus(),
666
								$member->getCircleId()
667
							]
668
						);
669
670
					} catch (Exception $e) {
671
						throw $e;
672
					}
673
674
					Circles::leaveCircle($circle->getId());
675
676
					// We check the member have no access to the circle
677
					try {
678
						Circles::getMember(
679
							$circle->getId(), Env::ENV_TEST_MEMBER3, DeprecatedMember::TYPE_USER
680
						);
681
						$this->assertSame(
682
							true, false, 'should return an exception'
683
						);
684
					} catch (MemberDoesNotExistException $e) {
685
					} catch (Exception $e) {
686
						$this->assertSame(
687
							true, false, 'should have returned a MemberDoesNotExistException'
688
						);
689
					}
690
691
					// We check that the user is not a member from the owner PoV
692
					Env::setUser(Env::ENV_TEST_OWNER1);
693
					try {
694
						$member = Circles::getMember(
695
							$circle->getId(), Env::ENV_TEST_MEMBER3, DeprecatedMember::TYPE_USER
696
						);
697
						$this->assertEquals(
698
							[
699
								Env::ENV_TEST_MEMBER3, DeprecatedMember::LEVEL_NONE, DeprecatedMember::STATUS_NONMEMBER,
700
								$circle->getId()
701
							]
702
							, [
703
								$member->getUserId(), $member->getLevel(), $member->getStatus(),
704
								$member->getCircleId()
705
							]
706
						);
707
					} catch (MemberDoesNotExistException $e) {
708
					} catch (Exception $e) {
709
						throw $e;
710
					}
711
					Env::setUser(Env::ENV_TEST_MEMBER3);
712
713
				}
714
			}
715
		}
716
717
		Env::logout();
718
	}
719
720
721
	/**
722
	 * Listing Circles, as a non-member and as a member
723
	 */
724
	public function testListCircles() {
725
726
		// First, we check from an outside PoV, user is not in any circles right now.
727
		Env::setUser(Env::ENV_TEST_MEMBER1);
728
729
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_ALL);
730
		$this->assertCount(2, $listing);
731
732
		$result = [];
733
		foreach ($listing as $circle) {
734
			array_push($result, $circle->getName());
735
		}
736
737
		$this->assertEquals($result, [self::NAME_PUBLIC_CIRCLE1, self::NAME_CLOSED_CIRCLE1]);
738
739
740
		// Let's add user to all circle
741
		Env::setUser(Env::ENV_TEST_OWNER1);
742
		$circles = [$this->circles['Public'], $this->circles['Closed'], $this->circles['Secret']];
743
		foreach ($circles as $circle) {
744
			$this->generateSimpleCircleWithAllLevel(
745
				$circle->getId(), ($circle->getType() === DeprecatedCircle::CIRCLES_CLOSED)
746
			);
747
		}
748
749
750
		// Let's check from an owner PoV
751
		Env::setUser(Env::ENV_TEST_OWNER1);
752
753
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_ALL);
754
		$this->assertCount(4, $listing);
755
756
		$result = [];
757
		foreach ($listing as $circle) {
758
			array_push($result, $circle->getName());
759
		}
760
761
		$this->assertEquals(
762
			$result, [
763
					   self::NAME_PUBLIC_CIRCLE1,
764
					   self::NAME_SECRET_CIRCLE1,
765
					   self::NAME_CLOSED_CIRCLE1,
766
					   self::NAME_PERSONAL_CIRCLE1
767
				   ]
768
		);
769
770
771
		// check from a member PoV
772
		Env::setUser(Env::ENV_TEST_MEMBER1);
773
774
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_ALL);
775
		$this->assertCount(3, $listing);
776
777
		$result = [];
778
		foreach ($listing as $circle) {
779
			array_push($result, $circle->getName());
780
		}
781
782
		$this->assertEquals(
783
			$result, [
784
					   self::NAME_PUBLIC_CIRCLE1,
785
					   self::NAME_SECRET_CIRCLE1,
786
					   self::NAME_CLOSED_CIRCLE1
787
				   ]
788
		);
789
790
791
		// member with a dedicated search on secret
792
		Env::setUser(Env::ENV_TEST_MEMBER1);
793
794
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_SECRET, self::NAME_SECRET_CIRCLE1);
795
		$this->assertCount(1, $listing);
796
797
		// member with a search on secret
798
		Env::setUser(Env::ENV_TEST_MEMBER1);
799
800
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_SECRET, '');
801
		$this->assertCount(1, $listing);
802
803
		// removing member from Circle
804
		Env::setUser(Env::ENV_TEST_OWNER1);
805
		Circles::removeMember(
806
			$this->circles['Secret']->getId(), Env::ENV_TEST_MEMBER1, DeprecatedMember::TYPE_USER
807
		);
808
809
		// member with a search on secret
810
		Env::setUser(Env::ENV_TEST_MEMBER1);
811
812
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_SECRET, '');
813
		$this->assertCount(0, $listing);
814
815
		// non-member with a dedicated search on secret
816
		Env::setUser(Env::ENV_TEST_MEMBER2);
817
818
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_SECRET, self::NAME_SECRET_CIRCLE1);
819
		$this->assertCount(1, $listing);
820
821
		// member with a dedicated search on personal
822
		Env::setUser(Env::ENV_TEST_MEMBER1);
823
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE1);
824
		$this->assertCount(0, $listing);
825
826
		// non-member with a dedicated search on personal
827
		Env::setUser(Env::ENV_TEST_MEMBER2);
828
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE1);
829
		$this->assertCount(0, $listing);
830
831
		// few request as another Owner on secret
832
		Env::SetUser(Env::ENV_TEST_OWNER2);
833
		$circle = Circles::createCircle(DeprecatedCircle::CIRCLES_SECRET, self::NAME_SECRET_CIRCLE2);
834
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_SECRET, '');
835
		$this->assertCount(1, $listing);
836
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_SECRET, self::NAME_SECRET_CIRCLE1);
837
		$this->assertCount(1, $listing);
838
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_SECRET, self::NAME_SECRET_CIRCLE2);
839
		$this->assertCount(1, $listing);
840
		Circles::destroyCircle($circle->getId());
841
842
		// few request as another Owner on personal
843
		Env::SetUser(Env::ENV_TEST_OWNER2);
844
		$circle = Circles::createCircle(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE2);
845
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_PERSONAL, '');
846
		$this->assertCount(1, $listing);
847
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE1);
848
		$this->assertCount(0, $listing);
849
		$listing = Circles::listCircles(DeprecatedCircle::CIRCLES_PERSONAL, self::NAME_PERSONAL_CIRCLE2);
850
		$this->assertCount(1, $listing);
851
		Circles::destroyCircle($circle->getId());
852
853
		Env::logout();
854
	}
855
856
857
	public function testDetailsCircle() {
858
	}
859
860
861
	/**
862
	 * function to generate admin/moderator/member and assigning them their level.
863
	 *
864
	 * @param $circleId
865
	 * @param bool $isClosed
866
	 *
867
	 * @throws QueryException
868
	 */
869
	protected function generateSimpleCircleWithAllLevel($circleId, $isClosed = false) {
870
871
		$curr = Env::currentUser();
872
873
		Circles::addMember($circleId, Env::ENV_TEST_ADMIN1, DeprecatedMember::TYPE_USER);
874
		if ($isClosed) {
875
			Env::setUser(Env::ENV_TEST_ADMIN1);
876
			Circles::joinCircle($circleId);
877
			Env::setUser($curr);
878
		}
879
		Circles::levelMember(
880
			$circleId, Env::ENV_TEST_ADMIN1, DeprecatedMember::TYPE_USER, DeprecatedMember::LEVEL_ADMIN
881
		);
882
883
884
		Circles::addMember($circleId, Env::ENV_TEST_MODERATOR1, DeprecatedMember::TYPE_USER);
885
		if ($isClosed) {
886
			Env::setUser(Env::ENV_TEST_MODERATOR1);
887
			Circles::joinCircle($circleId);
888
			Env::setUser($curr);
889
		}
890
		Circles::levelMember(
891
			$circleId, Env::ENV_TEST_MODERATOR1, DeprecatedMember::TYPE_USER, DeprecatedMember::LEVEL_MODERATOR
892
		);
893
894
		Circles::addMember($circleId, Env::ENV_TEST_MEMBER1, DeprecatedMember::TYPE_USER);
895
		if ($isClosed) {
896
			Env::setUser(Env::ENV_TEST_MEMBER1);
897
			Circles::joinCircle($circleId);
898
			Env::setUser($curr);
899
		}
900
		Circles::levelMember(
901
			$circleId, Env::ENV_TEST_MEMBER1, DeprecatedMember::TYPE_USER, DeprecatedMember::LEVEL_MEMBER
902
		);
903
	}
904
905
}
906