importtest.php 4.9 KB

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