ImportTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  8. use OC\Core\Command\Config\Import;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. class ImportTest extends TestCase {
  14. /** @var \PHPUnit\Framework\MockObject\MockObject */
  15. protected $config;
  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. $config = $this->config = $this->getMockBuilder(IConfig::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  28. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  29. /** @var \OCP\IConfig $config */
  30. $this->command = new Import($config);
  31. }
  32. public function validateAppsArrayData() {
  33. return [
  34. [0],
  35. [1],
  36. [null],
  37. ['new \Exception()'],
  38. [json_encode([])],
  39. ];
  40. }
  41. /**
  42. * @dataProvider validateAppsArrayData
  43. *
  44. * @param mixed $configValue
  45. */
  46. public function testValidateAppsArray($configValue): void {
  47. $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]);
  48. $this->assertTrue(true, 'Asserting that no exception is thrown');
  49. }
  50. public function validateAppsArrayThrowsData() {
  51. return [
  52. [false],
  53. [true],
  54. [[]],
  55. [new \Exception()],
  56. ];
  57. }
  58. /**
  59. * @dataProvider validateAppsArrayThrowsData
  60. *
  61. * @param mixed $configValue
  62. */
  63. public function testValidateAppsArrayThrows($configValue): void {
  64. try {
  65. $this->invokePrivate($this->command, 'validateAppsArray', [['app' => ['name' => $configValue]]]);
  66. $this->fail('Did not throw expected UnexpectedValueException');
  67. } catch (\UnexpectedValueException $e) {
  68. $this->assertStringStartsWith('Invalid app config value for "app":"name".', $e->getMessage());
  69. }
  70. }
  71. public function checkTypeRecursivelyData() {
  72. return [
  73. [0],
  74. [1],
  75. [null],
  76. ['new \Exception()'],
  77. [json_encode([])],
  78. [false],
  79. [true],
  80. [[]],
  81. [['string']],
  82. [['test' => 'string']],
  83. [['test' => ['sub' => 'string']]],
  84. ];
  85. }
  86. /**
  87. * @dataProvider checkTypeRecursivelyData
  88. *
  89. * @param mixed $configValue
  90. */
  91. public function testCheckTypeRecursively($configValue): void {
  92. $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']);
  93. $this->assertTrue(true, 'Asserting that no exception is thrown');
  94. }
  95. public function checkTypeRecursivelyThrowsData() {
  96. return [
  97. [new \Exception()],
  98. [[new \Exception()]],
  99. [['test' => new \Exception()]],
  100. [['test' => ['sub' => new \Exception()]]],
  101. ];
  102. }
  103. /**
  104. * @dataProvider checkTypeRecursivelyThrowsData
  105. *
  106. * @param mixed $configValue
  107. */
  108. public function testCheckTypeRecursivelyThrows($configValue): void {
  109. try {
  110. $this->invokePrivate($this->command, 'checkTypeRecursively', [$configValue, 'name']);
  111. $this->fail('Did not throw expected UnexpectedValueException');
  112. } catch (\UnexpectedValueException $e) {
  113. $this->assertStringStartsWith('Invalid system config value for "name"', $e->getMessage());
  114. }
  115. }
  116. public function validateArrayData() {
  117. return [
  118. [['system' => []]],
  119. [['apps' => []]],
  120. [['system' => [], 'apps' => []]],
  121. ];
  122. }
  123. /**
  124. * @dataProvider validateArrayData
  125. *
  126. * @param array $configArray
  127. */
  128. public function testValidateArray($configArray): void {
  129. $this->invokePrivate($this->command, 'validateArray', [$configArray]);
  130. $this->assertTrue(true, 'Asserting that no exception is thrown');
  131. }
  132. public function validateArrayThrowsData() {
  133. return [
  134. [[], 'At least one key of the following is expected:'],
  135. [[0 => []], 'Found invalid entries in root'],
  136. [['string' => []], 'Found invalid entries in root'],
  137. ];
  138. }
  139. /**
  140. * @dataProvider validateArrayThrowsData
  141. *
  142. * @param mixed $configArray
  143. * @param string $expectedException
  144. */
  145. public function testValidateArrayThrows($configArray, $expectedException): void {
  146. try {
  147. $this->invokePrivate($this->command, 'validateArray', [$configArray]);
  148. $this->fail('Did not throw expected UnexpectedValueException');
  149. } catch (\UnexpectedValueException $e) {
  150. $this->assertStringStartsWith($expectedException, $e->getMessage());
  151. }
  152. }
  153. }