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() {
  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 exists.',
  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('userExists')
  204. ->willReturn($user);
  205. }
  206. $command = $this->getCommand();
  207. try {
  208. $this->invokePrivate($command, 'checkInput', [$this->consoleInput]);
  209. $this->assertFalse($expectedException);
  210. } catch (\InvalidArgumentException $e) {
  211. $this->assertEquals($expectedException, $e->getMessage());
  212. }
  213. }
  214. public function testCheckInputExceptionCatch() {
  215. $command = $this->getCommand(['checkInput']);
  216. $command->expects($this->once())
  217. ->method('checkInput')
  218. ->willThrowException(new \InvalidArgumentException('test'));
  219. $this->consoleOutput->expects($this->once())
  220. ->method('writeln')
  221. ->with('<error>test</error>');
  222. $this->assertEquals(1, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  223. }
  224. public function dataExecuteDelete() {
  225. return [
  226. ['config', false, null, 0],
  227. ['config', true, null, 0],
  228. [null, false, null, 0],
  229. [null, true, '<error>The setting does not exist for user "username".</error>', 1],
  230. ];
  231. }
  232. /**
  233. * @dataProvider dataExecuteDelete
  234. *
  235. * @param string|null $value
  236. * @param bool $errorIfNotExists
  237. * @param string $expectedLine
  238. * @param int $expectedReturn
  239. */
  240. public function testExecuteDelete($value, $errorIfNotExists, $expectedLine, $expectedReturn) {
  241. $command = $this->getCommand([
  242. 'writeArrayInOutputFormat',
  243. 'checkInput',
  244. 'getUserSettings',
  245. ]);
  246. $this->consoleInput->expects($this->any())
  247. ->method('getArgument')
  248. ->willReturnMap([
  249. ['uid', 'username'],
  250. ['app', 'appname'],
  251. ['key', 'configkey'],
  252. ]);
  253. $command->expects($this->once())
  254. ->method('checkInput');
  255. $this->config->expects($this->once())
  256. ->method('getUserValue')
  257. ->with('username', 'appname', 'configkey', null)
  258. ->willReturn($value);
  259. $this->consoleInput->expects($this->atLeastOnce())
  260. ->method('hasParameterOption')
  261. ->willReturnMap([
  262. ['--delete', false, true],
  263. ['--error-if-not-exists', false, $errorIfNotExists],
  264. ]);
  265. if ($expectedLine === null) {
  266. $this->consoleOutput->expects($this->never())
  267. ->method('writeln');
  268. $this->config->expects($this->once())
  269. ->method('deleteUserValue')
  270. ->with('username', 'appname', 'configkey');
  271. } else {
  272. $this->consoleOutput->expects($this->once())
  273. ->method('writeln')
  274. ->with($expectedLine);
  275. $this->config->expects($this->never())
  276. ->method('deleteUserValue');
  277. }
  278. $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  279. }
  280. public function dataExecuteSet() {
  281. return [
  282. ['config', false, null, 0],
  283. ['config', true, null, 0],
  284. [null, false, null, 0],
  285. [null, true, '<error>The setting does not exist for user "username".</error>', 1],
  286. ];
  287. }
  288. /**
  289. * @dataProvider dataExecuteSet
  290. *
  291. * @param string|null $value
  292. * @param bool $updateOnly
  293. * @param string $expectedLine
  294. * @param int $expectedReturn
  295. */
  296. public function testExecuteSet($value, $updateOnly, $expectedLine, $expectedReturn) {
  297. $command = $this->getCommand([
  298. 'writeArrayInOutputFormat',
  299. 'checkInput',
  300. 'getUserSettings',
  301. ]);
  302. $this->consoleInput->expects($this->atLeast(4))
  303. ->method('getArgument')
  304. ->willReturnMap([
  305. ['uid', 'username'],
  306. ['app', 'appname'],
  307. ['key', 'configkey'],
  308. ['value', 'setValue'],
  309. ]);
  310. $command->expects($this->once())
  311. ->method('checkInput');
  312. $this->config->expects($this->once())
  313. ->method('getUserValue')
  314. ->with('username', 'appname', 'configkey', null)
  315. ->willReturn($value);
  316. $this->consoleInput->expects($this->atLeastOnce())
  317. ->method('hasParameterOption')
  318. ->willReturnMap([
  319. ['--update-only', false, $updateOnly],
  320. ]);
  321. if ($expectedLine === null) {
  322. $this->consoleOutput->expects($this->never())
  323. ->method('writeln');
  324. $this->consoleInput->expects($this->never())
  325. ->method('getOption');
  326. $this->config->expects($this->once())
  327. ->method('setUserValue')
  328. ->with('username', 'appname', 'configkey', 'setValue');
  329. } else {
  330. $this->consoleOutput->expects($this->once())
  331. ->method('writeln')
  332. ->with($expectedLine);
  333. $this->config->expects($this->never())
  334. ->method('setUserValue');
  335. }
  336. $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  337. }
  338. public function dataExecuteGet() {
  339. return [
  340. ['config', null, 'config', 0],
  341. [null, 'config', 'config', 0],
  342. [null, null, '<error>The setting does not exist for user "username".</error>', 1],
  343. ];
  344. }
  345. /**
  346. * @dataProvider dataExecuteGet
  347. *
  348. * @param string|null $value
  349. * @param string|null $defaultValue
  350. * @param string $expectedLine
  351. * @param int $expectedReturn
  352. */
  353. public function testExecuteGet($value, $defaultValue, $expectedLine, $expectedReturn) {
  354. $command = $this->getCommand([
  355. 'writeArrayInOutputFormat',
  356. 'checkInput',
  357. 'getUserSettings',
  358. ]);
  359. $this->consoleInput->expects($this->any())
  360. ->method('getArgument')
  361. ->willReturnMap([
  362. ['uid', 'username'],
  363. ['app', 'appname'],
  364. ['key', 'configkey'],
  365. ]);
  366. $command->expects($this->once())
  367. ->method('checkInput');
  368. $this->config->expects($this->once())
  369. ->method('getUserValue')
  370. ->with('username', 'appname', 'configkey', null)
  371. ->willReturn($value);
  372. if ($value === null) {
  373. if ($defaultValue === null) {
  374. $this->consoleInput->expects($this->atLeastOnce())
  375. ->method('hasParameterOption')
  376. ->willReturnMap([
  377. ['--default-value', false],
  378. ]);
  379. } else {
  380. $this->consoleInput->expects($this->atLeastOnce())
  381. ->method('hasParameterOption')
  382. ->willReturnMap([
  383. ['--default-value', false, true],
  384. ]);
  385. $this->consoleInput->expects($this->once())
  386. ->method('getOption')
  387. ->with('default-value')
  388. ->willReturn($defaultValue);
  389. }
  390. }
  391. $this->consoleOutput->expects($this->once())
  392. ->method('writeln')
  393. ->with($expectedLine);
  394. $this->assertEquals($expectedReturn, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  395. }
  396. public function testExecuteList() {
  397. $command = $this->getCommand([
  398. 'writeArrayInOutputFormat',
  399. 'checkInput',
  400. 'getUserSettings',
  401. ]);
  402. $this->consoleInput->expects($this->any())
  403. ->method('getArgument')
  404. ->willReturnMap([
  405. ['uid', 'username'],
  406. ['app', 'appname'],
  407. ['key', ''],
  408. ]);
  409. $command->expects($this->once())
  410. ->method('checkInput');
  411. $command->expects($this->once())
  412. ->method('getUserSettings')
  413. ->willReturn(['settings']);
  414. $command->expects($this->once())
  415. ->method('writeArrayInOutputFormat')
  416. ->with($this->consoleInput, $this->consoleOutput, ['settings']);
  417. $this->assertEquals(0, $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  418. }
  419. }