DeleteConfigTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Config\System;
  8. use OC\Core\Command\Config\System\DeleteConfig;
  9. use OC\SystemConfig;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. class DeleteConfigTest extends TestCase {
  14. /** @var \PHPUnit\Framework\MockObject\MockObject */
  15. protected $systemConfig;
  16. /** @var \PHPUnit\Framework\MockObject\MockObject */
  17. protected $consoleInput;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject */
  19. protected $consoleOutput;
  20. /** @var \Symfony\Component\Console\Command\Command */
  21. protected $command;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $systemConfig = $this->systemConfig = $this->getMockBuilder(SystemConfig::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  28. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  29. /** @var \OC\SystemConfig $systemConfig */
  30. $this->command = new DeleteConfig($systemConfig);
  31. }
  32. public function deleteData() {
  33. return [
  34. [
  35. 'name1',
  36. true,
  37. true,
  38. 0,
  39. 'info',
  40. ],
  41. [
  42. 'name2',
  43. true,
  44. false,
  45. 0,
  46. 'info',
  47. ],
  48. [
  49. 'name3',
  50. false,
  51. false,
  52. 0,
  53. 'info',
  54. ],
  55. [
  56. 'name4',
  57. false,
  58. true,
  59. 1,
  60. 'error',
  61. ],
  62. ];
  63. }
  64. /**
  65. * @dataProvider deleteData
  66. *
  67. * @param string $configName
  68. * @param bool $configExists
  69. * @param bool $checkIfExists
  70. * @param int $expectedReturn
  71. * @param string $expectedMessage
  72. */
  73. public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage): void {
  74. $this->systemConfig->expects(($checkIfExists) ? $this->once() : $this->never())
  75. ->method('getKeys')
  76. ->willReturn($configExists ? [$configName] : []);
  77. $this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never())
  78. ->method('deleteValue')
  79. ->with($configName);
  80. $this->consoleInput->expects($this->once())
  81. ->method('getArgument')
  82. ->with('name')
  83. ->willReturn([$configName]);
  84. $this->consoleInput->expects($this->any())
  85. ->method('hasParameterOption')
  86. ->with('--error-if-not-exists')
  87. ->willReturn($checkIfExists);
  88. $this->consoleOutput->expects($this->any())
  89. ->method('writeln')
  90. ->with($this->stringContains($expectedMessage));
  91. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  92. }
  93. public function deleteArrayData() {
  94. return [
  95. [
  96. ['name', 'sub'],
  97. true,
  98. false,
  99. true,
  100. true,
  101. 0,
  102. 'info',
  103. ],
  104. [
  105. ['name', 'sub', '2sub'],
  106. true,
  107. false,
  108. ['sub' => ['2sub' => 1], 'sub2' => false],
  109. ['sub' => [], 'sub2' => false],
  110. 0,
  111. 'info',
  112. ],
  113. [
  114. ['name', 'sub3'],
  115. true,
  116. false,
  117. ['sub' => ['2sub' => 1], 'sub2' => false],
  118. ['sub' => ['2sub' => 1], 'sub2' => false],
  119. 0,
  120. 'info',
  121. ],
  122. [
  123. ['name', 'sub'],
  124. false,
  125. true,
  126. true,
  127. true,
  128. 1,
  129. 'error',
  130. ],
  131. [
  132. ['name', 'sub'],
  133. true,
  134. true,
  135. true,
  136. true,
  137. 1,
  138. 'error',
  139. ],
  140. [
  141. ['name', 'sub3'],
  142. true,
  143. true,
  144. ['sub' => ['2sub' => 1], 'sub2' => false],
  145. ['sub' => ['2sub' => 1], 'sub2' => false],
  146. 1,
  147. 'error',
  148. ],
  149. ];
  150. }
  151. /**
  152. * @dataProvider deleteArrayData
  153. *
  154. * @param string[] $configNames
  155. * @param bool $configKeyExists
  156. * @param bool $checkIfKeyExists
  157. * @param mixed $configValue
  158. * @param mixed $updateValue
  159. * @param int $expectedReturn
  160. * @param string $expectedMessage
  161. */
  162. public function testArrayDelete(array $configNames, $configKeyExists, $checkIfKeyExists, $configValue, $updateValue, $expectedReturn, $expectedMessage): void {
  163. $this->systemConfig->expects(($checkIfKeyExists) ? $this->once() : $this->never())
  164. ->method('getKeys')
  165. ->willReturn($configKeyExists ? [$configNames[0]] : []);
  166. $this->systemConfig->expects(($configKeyExists) ? $this->once() : $this->never())
  167. ->method('getValue')
  168. ->willReturn($configValue);
  169. $this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never())
  170. ->method('setValue')
  171. ->with($configNames[0], $updateValue);
  172. $this->consoleInput->expects($this->once())
  173. ->method('getArgument')
  174. ->with('name')
  175. ->willReturn($configNames);
  176. $this->consoleInput->expects($this->any())
  177. ->method('hasParameterOption')
  178. ->with('--error-if-not-exists')
  179. ->willReturn($checkIfKeyExists);
  180. $this->consoleOutput->expects($this->any())
  181. ->method('writeln')
  182. ->with($this->stringContains($expectedMessage));
  183. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  184. }
  185. }