-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathSettingsControllerTest.php
100 lines (88 loc) · 2.8 KB
/
SettingsControllerTest.php
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
<?php
namespace OCA\Bookmarks\Tests;
use OCA\Bookmarks\Controller\SettingsController;
use OCA\Bookmarks\Service\UserSettingsService;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\L10N\IFactory;
/**
* Class Test_SettingsController
*
* @group Controller
*/
class SettingsControllerTest extends TestCase {
/**
* @var string
*/
private $userId;
/**
* @var string
*/
private $appName;
/**
* @var IRequest
*/
private $request;
/** @var IConfig */
private $config;
/** @var SettingsController */
private $controller;
/**
* @var \OC\User\Manager
*/
private $userManager;
/**
* @var string
*/
private $user;
protected function setUp(): void {
parent::setUp();
$this->cleanUp();
$this->userManager = \OC::$server->get(IUserManager::class);
$this->user = 'test';
if (!$this->userManager->userExists($this->user)) {
$this->userManager->createUser($this->user, 'password');
}
$this->userId = $this->userManager->get($this->user)->getUID();
$this->appName = 'bookmarks';
$this->request = \OC::$server->get(IRequest::class);
/** @var IFactory $l10nFactory */
$l10nFactory = \OC::$server->get(IFactory::class);
$l = $l10nFactory->get('bookmarks');
$this->config = \OC::$server->get(IConfig::class);
$userSettings = new UserSettingsService($this->userId, $this->appName, $this->config, $l);
if (!$this->userManager->userExists($this->userId)) {
$this->userManager->createUser($this->userId, 'password');
}
$this->controller = new SettingsController('bookmarks', $this->request, $userSettings);
}
/**
* @throws \OCP\PreConditionNotMetException
*/
public function testGetSorting(): void {
$this->config->setUserValue($this->userId, $this->appName, 'sorting', 'clickcount'); //case: user has a normal sorting option
$output = $this->controller->getSetting('sorting');
$data = $output->getData();
$this->assertEquals('clickcount', $data['sorting']);
$this->config->deleteUserValue($this->userId, $this->appName, 'sorting'); //case: user has no sorting option
$output = $this->controller->getSetting('sorting');
$data = $output->getData();
$this->assertEquals('lastmodified', $data['sorting']); //returns default
}
/**
*
*/
public function testSetSorting(): void {
$output = $this->controller->setSetting('sorting', 'added'); //case: set a normal sorting option
$data = $output->getData();
$this->assertEquals('success', $data['status']);
$this->assertEquals('added', $this->config->getUserValue($this->userId, $this->appName, 'sorting', ''));
$output = $this->controller->setSetting('sorting', 'foo'); //case: set an invalid sorting option
$data = $output->getData();
$this->assertEquals('error', $data['status']);
}
protected function tearDown(): void {
$this->config->deleteUserValue($this->userId, $this->appName, 'sorting');
}
}