SettingTest.php 13 KB

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