SettingTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\User;
  8. use OC\Core\Command\User\Setting;
  9. use OCP\IConfig;
  10. use OCP\IDBConnection;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Test\TestCase;
  15. class SettingTest extends TestCase {
  16. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  17. protected $userManager;
  18. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  19. protected $config;
  20. /** @var \OCP\IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  21. protected $connection;
  22. /** @var \Symfony\Component\Console\Input\InputInterface|\PHPUnit\Framework\MockObject\MockObject */
  23. protected $consoleInput;
  24. /** @var \Symfony\Component\Console\Output\OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
  25. protected $consoleOutput;
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->userManager = $this->getMockBuilder(IUserManager::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->config = $this->getMockBuilder(IConfig::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->connection = $this->getMockBuilder(IDBConnection::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->consoleInput = $this->getMockBuilder(InputInterface::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. }
  44. public function getCommand(array $methods = []) {
  45. if (empty($methods)) {
  46. return new Setting($this->userManager, $this->config, $this->connection);
  47. } else {
  48. $mock = $this->getMockBuilder('OC\Core\Command\User\Setting')
  49. ->setConstructorArgs([
  50. $this->userManager,
  51. $this->config,
  52. $this->connection,
  53. ])
  54. ->setMethods($methods)
  55. ->getMock();
  56. return $mock;
  57. }
  58. }
  59. public function dataCheckInput() {
  60. return [
  61. [
  62. [['uid', 'username']],
  63. [['ignore-missing-user', true]],
  64. [],
  65. false,
  66. false,
  67. ],
  68. [
  69. [['uid', 'username']],
  70. [['ignore-missing-user', false]],
  71. [],
  72. null,
  73. 'The user "username" does not exist.',
  74. ],
  75. [
  76. [['uid', 'username'], ['key', 'configkey']],
  77. [['ignore-missing-user', true]],
  78. [['--default-value', false, true]],
  79. false,
  80. false,
  81. ],
  82. [
  83. [['uid', 'username'], ['key', '']],
  84. [['ignore-missing-user', true]],
  85. [['--default-value', false, true]],
  86. false,
  87. 'The "default-value" option can only be used when specifying a key.',
  88. ],
  89. [
  90. [['uid', 'username'], ['key', 'configkey'], ['value', '']],
  91. [['ignore-missing-user', true]],
  92. [],
  93. false,
  94. false,
  95. ],
  96. [
  97. [['uid', 'username'], ['key', ''], ['value', '']],
  98. [['ignore-missing-user', true]],
  99. [],
  100. false,
  101. 'The value argument can only be used when specifying a key.',
  102. ],
  103. [
  104. [['uid', 'username'], ['key', 'configkey'], ['value', '']],
  105. [['ignore-missing-user', true]],
  106. [['--default-value', false, true]],
  107. false,
  108. 'The value argument can not be used together with "default-value".',
  109. ],
  110. [
  111. [['uid', 'username'], ['key', 'configkey'], ['value', '']],
  112. [['ignore-missing-user', true], ['update-only', true]],
  113. [],
  114. false,
  115. false,
  116. ],
  117. [
  118. [['uid', 'username'], ['key', 'configkey'], ['value', null]],
  119. [['ignore-missing-user', true], ['update-only', true]],
  120. [],
  121. false,
  122. 'The "update-only" option can only be used together with "value".',
  123. ],
  124. [
  125. [['uid', 'username'], ['key', 'configkey']],
  126. [['ignore-missing-user', true], ['delete', true]],
  127. [],
  128. false,
  129. false,
  130. ],
  131. [
  132. [['uid', 'username'], ['key', '']],
  133. [['ignore-missing-user', true], ['delete', true]],
  134. [],
  135. false,
  136. 'The "delete" option can only be used when specifying a key.',
  137. ],
  138. [
  139. [['uid', 'username'], ['key', 'configkey']],
  140. [['ignore-missing-user', true], ['delete', true]],
  141. [['--default-value', false, true]],
  142. false,
  143. 'The "delete" option can not be used together with "default-value".',
  144. ],
  145. [
  146. [['uid', 'username'], ['key', 'configkey'], ['value', '']],
  147. [['ignore-missing-user', true], ['delete', true]],
  148. [],
  149. false,
  150. 'The "delete" option can not be used together with "value".',
  151. ],
  152. [
  153. [['uid', 'username'], ['key', 'configkey']],
  154. [['ignore-missing-user', true], ['delete', true], ['error-if-not-exists', true]],
  155. [],
  156. false,
  157. false,
  158. ],
  159. [
  160. [['uid', 'username'], ['key', 'configkey']],
  161. [['ignore-missing-user', true], ['delete', false], ['error-if-not-exists', true]],
  162. [],
  163. false,
  164. 'The "error-if-not-exists" option can only be used together with "delete".',
  165. ],
  166. ];
  167. }
  168. /**
  169. * @dataProvider dataCheckInput
  170. *
  171. * @param array $arguments
  172. * @param array $options
  173. * @param array $parameterOptions
  174. * @param mixed $user
  175. * @param string $expectedException
  176. */
  177. public function testCheckInput($arguments, $options, $parameterOptions, $user, $expectedException) {
  178. $this->consoleInput->expects($this->any())
  179. ->method('getArgument')
  180. ->willReturnMap($arguments);
  181. $this->consoleInput->expects($this->any())
  182. ->method('getOption')
  183. ->willReturnMap($options);
  184. $this->consoleInput->expects($this->any())
  185. ->method('hasParameterOption')
  186. ->willReturnMap($parameterOptions);
  187. if ($user !== false) {
  188. $this->userManager->expects($this->once())
  189. ->method('get')
  190. ->willReturn($user);
  191. } else {
  192. $this->userManager->expects($this->never())
  193. ->method('get');
  194. }
  195. $command = $this->getCommand();
  196. try {
  197. $this->invokePrivate($command, 'checkInput', [$this->consoleInput]);
  198. $this->assertFalse($expectedException);
  199. } catch (\InvalidArgumentException $e) {
  200. $this->assertEquals($expectedException, $e->getMessage());
  201. }
  202. }
  203. public function testCheckInputExceptionCatch() {
  204. $command = $this->getCommand(['checkInput']);
  205. $command->expects($this->once())
  206. ->method('checkInput')
  207. ->willThrowException(new \InvalidArgumentException('test'));
  208. $this->consoleOutput->expects($this->once())
  209. ->method('writeln')
  210. ->with('<error>test</error>');
  211. $this->assertEquals(1, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  212. }
  213. public function dataExecuteDelete() {
  214. return [
  215. ['config', false, null, 0],
  216. ['config', true, null, 0],
  217. [null, false, null, 0],
  218. [null, true, '<error>The setting does not exist for user "username".</error>', 1],
  219. ];
  220. }
  221. /**
  222. * @dataProvider dataExecuteDelete
  223. *
  224. * @param string|null $value
  225. * @param bool $errorIfNotExists
  226. * @param string $expectedLine
  227. * @param int $expectedReturn
  228. */
  229. public function testExecuteDelete($value, $errorIfNotExists, $expectedLine, $expectedReturn) {
  230. $command = $this->getCommand([
  231. 'writeArrayInOutputFormat',
  232. 'checkInput',
  233. 'getUserSettings',
  234. ]);
  235. $this->consoleInput->expects($this->any())
  236. ->method('getArgument')
  237. ->willReturnMap([
  238. ['uid', 'username'],
  239. ['app', 'appname'],
  240. ['key', 'configkey'],
  241. ]);
  242. $command->expects($this->once())
  243. ->method('checkInput');
  244. $this->config->expects($this->once())
  245. ->method('getUserValue')
  246. ->with('username', 'appname', 'configkey', null)
  247. ->willReturn($value);
  248. $this->consoleInput->expects($this->atLeastOnce())
  249. ->method('hasParameterOption')
  250. ->willReturnMap([
  251. ['--delete', false, true],
  252. ['--error-if-not-exists', false, $errorIfNotExists],
  253. ]);
  254. if ($expectedLine === null) {
  255. $this->consoleOutput->expects($this->never())
  256. ->method('writeln');
  257. $this->config->expects($this->once())
  258. ->method('deleteUserValue')
  259. ->with('username', 'appname', 'configkey');
  260. } else {
  261. $this->consoleOutput->expects($this->once())
  262. ->method('writeln')
  263. ->with($expectedLine);
  264. $this->config->expects($this->never())
  265. ->method('deleteUserValue');
  266. }
  267. $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  268. }
  269. public function dataExecuteSet() {
  270. return [
  271. ['config', false, null, 0],
  272. ['config', true, null, 0],
  273. [null, false, null, 0],
  274. [null, true, '<error>The setting does not exist for user "username".</error>', 1],
  275. ];
  276. }
  277. /**
  278. * @dataProvider dataExecuteSet
  279. *
  280. * @param string|null $value
  281. * @param bool $updateOnly
  282. * @param string $expectedLine
  283. * @param int $expectedReturn
  284. */
  285. public function testExecuteSet($value, $updateOnly, $expectedLine, $expectedReturn) {
  286. $command = $this->getCommand([
  287. 'writeArrayInOutputFormat',
  288. 'checkInput',
  289. 'getUserSettings',
  290. ]);
  291. $this->consoleInput->expects($this->atLeast(4))
  292. ->method('getArgument')
  293. ->willReturnMap([
  294. ['uid', 'username'],
  295. ['app', 'appname'],
  296. ['key', 'configkey'],
  297. ['value', 'setValue'],
  298. ]);
  299. $command->expects($this->once())
  300. ->method('checkInput');
  301. $this->config->expects($this->once())
  302. ->method('getUserValue')
  303. ->with('username', 'appname', 'configkey', null)
  304. ->willReturn($value);
  305. $this->consoleInput->expects($this->atLeastOnce())
  306. ->method('hasParameterOption')
  307. ->willReturnMap([
  308. ['--update-only', false, $updateOnly],
  309. ]);
  310. if ($expectedLine === null) {
  311. $this->consoleOutput->expects($this->never())
  312. ->method('writeln');
  313. $this->consoleInput->expects($this->never())
  314. ->method('getOption');
  315. $this->config->expects($this->once())
  316. ->method('setUserValue')
  317. ->with('username', 'appname', 'configkey', 'setValue');
  318. } else {
  319. $this->consoleOutput->expects($this->once())
  320. ->method('writeln')
  321. ->with($expectedLine);
  322. $this->config->expects($this->never())
  323. ->method('setUserValue');
  324. }
  325. $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  326. }
  327. public function dataExecuteGet() {
  328. return [
  329. ['config', null, 'config', 0],
  330. [null, 'config', 'config', 0],
  331. [null, null, '<error>The setting does not exist for user "username".</error>', 1],
  332. ];
  333. }
  334. /**
  335. * @dataProvider dataExecuteGet
  336. *
  337. * @param string|null $value
  338. * @param string|null $defaultValue
  339. * @param string $expectedLine
  340. * @param int $expectedReturn
  341. */
  342. public function testExecuteGet($value, $defaultValue, $expectedLine, $expectedReturn) {
  343. $command = $this->getCommand([
  344. 'writeArrayInOutputFormat',
  345. 'checkInput',
  346. 'getUserSettings',
  347. ]);
  348. $this->consoleInput->expects($this->any())
  349. ->method('getArgument')
  350. ->willReturnMap([
  351. ['uid', 'username'],
  352. ['app', 'appname'],
  353. ['key', 'configkey'],
  354. ]);
  355. $command->expects($this->once())
  356. ->method('checkInput');
  357. $this->config->expects($this->once())
  358. ->method('getUserValue')
  359. ->with('username', 'appname', 'configkey', null)
  360. ->willReturn($value);
  361. if ($value === null) {
  362. if ($defaultValue === null) {
  363. $this->consoleInput->expects($this->atLeastOnce())
  364. ->method('hasParameterOption')
  365. ->willReturnMap([
  366. ['--default-value', false],
  367. ]);
  368. } else {
  369. $this->consoleInput->expects($this->atLeastOnce())
  370. ->method('hasParameterOption')
  371. ->willReturnMap([
  372. ['--default-value', false, true],
  373. ]);
  374. $this->consoleInput->expects($this->once())
  375. ->method('getOption')
  376. ->with('default-value')
  377. ->willReturn($defaultValue);
  378. }
  379. }
  380. $this->consoleOutput->expects($this->once())
  381. ->method('writeln')
  382. ->with($expectedLine);
  383. $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  384. }
  385. public function testExecuteList() {
  386. $command = $this->getCommand([
  387. 'writeArrayInOutputFormat',
  388. 'checkInput',
  389. 'getUserSettings',
  390. ]);
  391. $this->consoleInput->expects($this->any())
  392. ->method('getArgument')
  393. ->willReturnMap([
  394. ['uid', 'username'],
  395. ['app', 'appname'],
  396. ['key', ''],
  397. ]);
  398. $command->expects($this->once())
  399. ->method('checkInput');
  400. $command->expects($this->once())
  401. ->method('getUserSettings')
  402. ->willReturn(['settings']);
  403. $command->expects($this->once())
  404. ->method('writeArrayInOutputFormat')
  405. ->with($this->consoleInput, $this->consoleOutput, ['settings']);
  406. $this->assertEquals(0, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  407. }
  408. }