Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RosterPushTest.php « integration « tests - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b46b112cbee279489278c7c2f34682855a2627c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

namespace OCA\OJSXC;

use OCA\OJSXC\AppInfo\Application;
use OCA\OJSXC\Db\IQRosterPushMapper;
use OCP\Contacts\ContactsMenu\IContactsStore;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IUserSession;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class RosterPushTest extends TestCase
{

	/**
	 * @var RosterPush
	 */
	private $rosterPush;

	/**
	 * @var IUserManager
	 */
	private $userManager;

	/**
	 * @var IGroupManager
	 */
	private $groupManager;

	/**
	 * @var IUserSession
	 */
	private $userSession;

	/**
	 * @var MockObject | IQRosterPushMapper
	 */
	private $iqRosterPushMapper;

	/**
	 * @var IDBConnection
	 */
	private $dbConnection;

	/**
	 * @var IUserProvider
	 */
	private $userProvider;

	/**
	 * @var IConfig
	 */
	private $config;

	/**
	 * @var MockObject | IContactsStore
	 */
	private $contactsStore;

	public function setUp()
	{
		if (!Application::contactsStoreApiSupported()) {
			$this->markTestSkipped();
			return;
		}

		$this->userManager = \OC::$server->getUserManager();
		$this->groupManager = \OC::$server->getGroupManager();
		$this->userSession = \OC::$server->getUserSession();
		$this->iqRosterPushMapper = $this->getMockBuilder(IQRosterPushMapper::class)->disableOriginalConstructor()->getMock();
		$this->dbConnection = \OC::$server->getDatabaseConnection();
		$this->config = \OC::$server->getConfig();
		$this->contactsStore = $this->getMockBuilder(IContactsStore::class)->disableOriginalConstructor()->getMock();

		$this->userProvider = new ContactsStoreUserProvider(
			$this->contactsStore,
			$this->userSession,
			$this->userManager,
			$this->groupManager,
			$this->config
		);

		$this->rosterPush = new RosterPush(
			$this->userManager,
			$this->userSession,
			'localhost',
			$this->iqRosterPushMapper,
			$this->dbConnection,
			$this->userProvider
		);

		foreach (\OC::$server->getUserManager()->search('') as $user) {
			$user->delete();
		}
		foreach (\OC::$server->getGroupManager()->search('') as $group) {
			$group->delete();
		}
	}

	public function testRemoveRosterItemForUsersInGroup()
	{
		$group1 = $this->groupManager->createGroup('group1');
		$group2 = $this->groupManager->createGroup('group2');
		$group3 = $this->groupManager->createGroup('group3');
		$user1 = $this->userManager->createUser('user1', 'user1');
		$user2 = $this->userManager->createUser('user2', 'user2');
		$user3 = $this->userManager->createUser('user3', 'user3');
		$user4 = $this->userManager->createUser('user4', 'user4');

		$group1->addUser($user1);
		$group1->addUser($user2);
		$group1->addUser($user3);
		$group2->addUser($user1);
		$group2->addUser($user2);
		$group3->addUser($user4);

		// remove $user1 from $group1
		// when no special settings are set this should result in no rosterMessages
		$this->iqRosterPushMapper->expects($this->never())->method('insert');
		$this->contactsStore->expects($this->at(0))
			->method('findOne')
			->with($user2, 0, 'user1')
			->willReturn([$user2]);
		$this->contactsStore->expects($this->at(1))
			->method('findOne')
			->with($user3, 0, 'user1')
			->willReturn([$user3]);
		$this->rosterPush->removeRosterItemForUsersInGroup($group1, 'user1');
	}
	public function testRemoveRosterItemForUsersInGroupOwnGroups()
	{
		$group1 = $this->groupManager->createGroup('group1');
		$group2 = $this->groupManager->createGroup('group2');
		$group3 = $this->groupManager->createGroup('group3');
		$user1 = $this->userManager->createUser('user1', 'user1');
		$user2 = $this->userManager->createUser('user2', 'user2');
		$user3 = $this->userManager->createUser('user3', 'user3');
		$user4 = $this->userManager->createUser('user4', 'user4');

		$group1->addUser($user1);
		$group1->addUser($user2);
		$group1->addUser($user3);
		$group2->addUser($user1);
		$group2->addUser($user2);
		$group3->addUser($user4);

		$this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes');

		// remove $user1 from $group1
		// users can only chat with users in their groups
		// $user2 should still be reachable by $group2
		// $user3 should be become unreachable
		// for $user4 nothing changes
		$this->iqRosterPushMapper->expects($this->once())->method('insert');

		$this->contactsStore->expects($this->at(0))
			->method('findOne')
			->with($user2, 0, 'user1')
			->willReturn([$user2]);

		$this->contactsStore->expects($this->at(1))
			->method('findOne')
			->with($user3, 0, 'user1')
			->willReturn(null);

		$this->rosterPush->removeRosterItemForUsersInGroup($group1, 'user1');

		$this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no');
	}
}