aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/NavigationManagerTest.php
blob: 39f326ef0da0982d51a7608354c65095c928d40e (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
 * ownCloud
 *
 * @author Joas Schilling
 * @copyright 2015 Joas Schilling nickvergessen@owncloud.com
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test;

use OC\App\AppManager;
use OC\Group\Manager;
use OC\NavigationManager;
use OC\SubAdmin;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;

class NavigationManagerTest extends TestCase {
	/** @var AppManager|\PHPUnit\Framework\MockObject\MockObject */
	protected $appManager;
	/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
	protected $urlGenerator;
	/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
	protected $l10nFac;
	/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
	protected $userSession;
	/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
	protected $groupManager;
	/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
	protected $config;

	/** @var \OC\NavigationManager */
	protected $navigationManager;

	protected function setUp(): void {
		parent::setUp();

		$this->appManager = $this->createMock(AppManager::class);
		$this->urlGenerator = $this->createMock(IURLGenerator::class);
		$this->l10nFac = $this->createMock(IFactory::class);
		$this->userSession = $this->createMock(IUserSession::class);
		$this->groupManager = $this->createMock(Manager::class);
		$this->config = $this->createMock(IConfig::class);
		$this->navigationManager = new NavigationManager(
			$this->appManager,
			$this->urlGenerator,
			$this->l10nFac,
			$this->userSession,
			$this->groupManager,
			$this->config
		);

		$this->navigationManager->clear(false);
	}

	public function addArrayData() {
		return [
			[
				'entry id' => [
					'id' => 'entry id',
					'name' => 'link text',
					'order' => 1,
					'icon' => 'optional',
					'href' => 'url',
					'type' => 'settings',
					'classes' => '',
					'unread' => 0
				],
				'entry id2' => [
					'id' => 'entry id',
					'name' => 'link text',
					'order' => 1,
					'icon' => 'optional',
					'href' => 'url',
					'active' => false,
					'type' => 'settings',
					'classes' => '',
					'unread' => 0
				]
			],
			[
				'entry id' => [
					'id' => 'entry id',
					'name' => 'link text',
					'order' => 1,
					//'icon'	=> 'optional',
					'href' => 'url',
					'active' => true,
					'unread' => 0
				],
				'entry id2' => [
					'id' => 'entry id',
					'name' => 'link text',
					'order' => 1,
					'icon' => '',
					'href' => 'url',
					'active' => false,
					'type' => 'link',
					'classes' => '',
					'unread' => 0
				]
			]
		];
	}

	/**
	 * @dataProvider addArrayData
	 *
	 * @param array $entry
	 * @param array $expectedEntry
	 */
	public function testAddArray(array $entry, array $expectedEntry) {
		$this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists');
		$this->navigationManager->add($entry);

		$navigationEntries = $this->navigationManager->getAll('all');
		$this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
		$this->assertEquals($expectedEntry, $navigationEntries['entry id']);

		$this->navigationManager->clear(false);
		$this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
	}

	/**
	 * @dataProvider addArrayData
	 *
	 * @param array $entry
	 * @param array $expectedEntry
	 */
	public function testAddClosure(array $entry, array $expectedEntry) {
		global $testAddClosureNumberOfCalls;
		$testAddClosureNumberOfCalls = 0;

		$this->navigationManager->add(function () use ($entry) {
			global $testAddClosureNumberOfCalls;
			$testAddClosureNumberOfCalls++;

			return $entry;
		});

		$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');

		$navigationEntries = $this->navigationManager->getAll('all');
		$this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is called by getAll()');
		$this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
		$this->assertEquals($expectedEntry, $navigationEntries['entry id']);

		$navigationEntries = $this->navigationManager->getAll('all');
		$this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is only called once for getAll()');
		$this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
		$this->assertEquals($expectedEntry, $navigationEntries['entry id']);

		$this->navigationManager->clear(false);
		$this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
	}

	public function testAddArrayClearGetAll() {
		$entry = [
			'id' => 'entry id',
			'name' => 'link text',
			'order' => 1,
			'icon' => 'optional',
			'href' => 'url'
		];

		$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
		$this->navigationManager->add($entry);
		$this->navigationManager->clear(false);
		$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
	}

	public function testAddClosureClearGetAll() {
		$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');

		$entry = [
			'id' => 'entry id',
			'name' => 'link text',
			'order' => 1,
			'icon' => 'optional',
			'href' => 'url'
		];

		global $testAddClosureNumberOfCalls;
		$testAddClosureNumberOfCalls = 0;

		$this->navigationManager->add(function () use ($entry) {
			global $testAddClosureNumberOfCalls;
			$testAddClosureNumberOfCalls++;

			return $entry;
		});

		$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
		$this->navigationManager->clear(false);
		$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by clear()');
		$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
		$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()');
	}

	/**
	 * @dataProvider providesNavigationConfig
	 */
	public function testWithAppManager($expected, $navigation, $isAdmin = false) {
		$l = $this->createMock(IL10N::class);
		$l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) {
			return vsprintf($text, $parameters);
		});

		$this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
		$this->l10nFac->expects($this->any())->method('get')->willReturn($l);
		$this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
			return "/apps/$appName/img/$file";
		});
		$this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function ($route) {
			if ($route === 'core.login.logout') {
				return 'https://example.com/logout';
			}
			return '/apps/test/';
		});
		$user = $this->createMock(IUser::class);
		$user->expects($this->any())->method('getUID')->willReturn('user001');
		$this->userSession->expects($this->any())->method('getUser')->willReturn($user);
		$this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
		$this->appManager->expects($this->once())
			 ->method('getEnabledAppsForUser')
			 ->with($user)
			 ->willReturn(['test']);
		$this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
		$subadmin = $this->createMock(SubAdmin::class);
		$subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
		$this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);

		$this->navigationManager->clear();
		$entries = $this->navigationManager->getAll('all');
		$this->assertEquals($expected, $entries);
	}

	public function providesNavigationConfig() {
		$apps = [
			'core_apps' => [
				'id' => 'core_apps',
				'order' => 4,
				'href' => '/apps/test/',
				'icon' => '/apps/settings/img/apps.svg',
				'name' => 'Apps',
				'active' => false,
				'type' => 'settings',
				'classes' => '',
				'unread' => 0
			]
		];
		$defaults = [
			'settings' => [
				'id' => 'settings',
				'order' => 2,
				'href' => '/apps/test/',
				'icon' => '/apps/settings/img/admin.svg',
				'name' => 'Settings',
				'active' => false,
				'type' => 'settings',
				'classes' => '',
				'unread' => 0
			],
			'logout' => [
				'id' => 'logout',
				'order' => 99999,
				'href' => 'https://example.com/logout?requesttoken='. urlencode(\OCP\Util::callRegister()),
				'icon' => '/apps/core/img/actions/logout.svg',
				'name' => 'Log out',
				'active' => false,
				'type' => 'settings',
				'classes' => '',
				'unread' => 0
			]
		];

		return [
			'minimalistic' => [
				array_merge(
					['settings' => $defaults['settings']],
					['test' => [
						'id' => 'test',
						'order' => 100,
						'href' => '/apps/test/',
						'icon' => '/apps/test/img/app.svg',
						'name' => 'Test',
						'active' => false,
						'type' => 'link',
						'classes' => '',
						'unread' => 0
					]],
					['logout' => $defaults['logout']]
				),
				['navigations' => [
					'navigation' => [
						['route' => 'test.page.index', 'name' => 'Test']
					]
				]]
			],
			'minimalistic-settings' => [
				array_merge(
					['settings' => $defaults['settings']],
					['test' => [
						'id' => 'test',
						'order' => 100,
						'href' => '/apps/test/',
						'icon' => '/apps/test/img/app.svg',
						'name' => 'Test',
						'active' => false,
						'type' => 'settings',
						'classes' => '',
						'unread' => 0
					]],
					['logout' => $defaults['logout']]
				),
				['navigations' => [
					'navigation' => [
						['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']
					],
				]]
			],
			'admin' => [
				array_merge(
					['settings' => $defaults['settings']],
					$apps,
					['test' => [
						'id' => 'test',
						'order' => 100,
						'href' => '/apps/test/',
						'icon' => '/apps/test/img/app.svg',
						'name' => 'Test',
						'active' => false,
						'type' => 'link',
						'classes' => '',
						'unread' => 0
					]],
					['logout' => $defaults['logout']]
				),
				['navigations' => [
					'navigation' => [
						['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
					],
				]],
				true
			],
			'no name' => [
				array_merge(
					['settings' => $defaults['settings']],
					$apps,
					['logout' => $defaults['logout']]
				),
				['navigations' => [
					'navigation' => [
						['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']
					],
				]],
				true
			],
			'no admin' => [
				$defaults,
				['navigations' => [[
					'@attributes' => ['role' => 'admin'],
					'route' => 'test.page.index',
					'name' => 'Test'
				]]]
			]
		];
	}
}