1
0

SetupTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use bantu\IniGetWrapper\IniGetWrapper;
  10. use OC\Installer;
  11. use OC\Setup;
  12. use OC\SystemConfig;
  13. use OCP\Defaults;
  14. use OCP\IL10N;
  15. use OCP\ILogger;
  16. use OCP\Security\ISecureRandom;
  17. class SetupTest extends \Test\TestCase {
  18. /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $config;
  20. /** @var \bantu\IniGetWrapper\IniGetWrapper|\PHPUnit_Framework_MockObject_MockObject */
  21. private $iniWrapper;
  22. /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject */
  23. private $l10n;
  24. /** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
  25. private $defaults;
  26. /** @var \OC\Setup|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $setupClass;
  28. /** @var \OCP\ILogger|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $logger;
  30. /** @var \OCP\Security\ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $random;
  32. /** @var Installer|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $installer;
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->config = $this->createMock(SystemConfig::class);
  37. $this->iniWrapper = $this->createMock(IniGetWrapper::class);
  38. $this->l10n = $this->createMock(IL10N::class);
  39. $this->defaults = $this->createMock(Defaults::class);
  40. $this->logger = $this->createMock(ILogger::class);
  41. $this->random = $this->createMock(ISecureRandom::class);
  42. $this->installer = $this->createMock(Installer::class);
  43. $this->setupClass = $this->getMockBuilder(Setup::class)
  44. ->setMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo'])
  45. ->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10n, $this->defaults, $this->logger, $this->random, $this->installer])
  46. ->getMock();
  47. }
  48. public function testGetSupportedDatabasesWithOneWorking() {
  49. $this->config
  50. ->expects($this->once())
  51. ->method('getValue')
  52. ->will($this->returnValue(
  53. ['sqlite', 'mysql', 'oci']
  54. ));
  55. $this->setupClass
  56. ->expects($this->once())
  57. ->method('is_callable')
  58. ->will($this->returnValue(false));
  59. $this->setupClass
  60. ->expects($this->any())
  61. ->method('getAvailableDbDriversForPdo')
  62. ->will($this->returnValue(['sqlite']));
  63. $result = $this->setupClass->getSupportedDatabases();
  64. $expectedResult = [
  65. 'sqlite' => 'SQLite'
  66. ];
  67. $this->assertSame($expectedResult, $result);
  68. }
  69. public function testGetSupportedDatabasesWithNoWorking() {
  70. $this->config
  71. ->expects($this->once())
  72. ->method('getValue')
  73. ->will($this->returnValue(
  74. ['sqlite', 'mysql', 'oci', 'pgsql']
  75. ));
  76. $this->setupClass
  77. ->expects($this->any())
  78. ->method('is_callable')
  79. ->will($this->returnValue(false));
  80. $this->setupClass
  81. ->expects($this->any())
  82. ->method('getAvailableDbDriversForPdo')
  83. ->will($this->returnValue([]));
  84. $result = $this->setupClass->getSupportedDatabases();
  85. $this->assertSame([], $result);
  86. }
  87. public function testGetSupportedDatabasesWithAllWorking() {
  88. $this->config
  89. ->expects($this->once())
  90. ->method('getValue')
  91. ->will($this->returnValue(
  92. ['sqlite', 'mysql', 'pgsql', 'oci']
  93. ));
  94. $this->setupClass
  95. ->expects($this->any())
  96. ->method('is_callable')
  97. ->will($this->returnValue(true));
  98. $this->setupClass
  99. ->expects($this->any())
  100. ->method('getAvailableDbDriversForPdo')
  101. ->will($this->returnValue(['sqlite', 'mysql', 'pgsql']));
  102. $result = $this->setupClass->getSupportedDatabases();
  103. $expectedResult = [
  104. 'sqlite' => 'SQLite',
  105. 'mysql' => 'MySQL/MariaDB',
  106. 'pgsql' => 'PostgreSQL',
  107. 'oci' => 'Oracle'
  108. ];
  109. $this->assertSame($expectedResult, $result);
  110. }
  111. /**
  112. * @expectedException \Exception
  113. * @expectedExceptionMessage Supported databases are not properly configured.
  114. */
  115. public function testGetSupportedDatabaseException() {
  116. $this->config
  117. ->expects($this->once())
  118. ->method('getValue')
  119. ->will($this->returnValue('NotAnArray'));
  120. $this->setupClass->getSupportedDatabases();
  121. }
  122. /**
  123. * @dataProvider findWebRootProvider
  124. * @param $url
  125. * @param $expected
  126. */
  127. public function testFindWebRootCli($url, $expected) {
  128. $cliState = \OC::$CLI;
  129. $this->config
  130. ->expects($this->once())
  131. ->method('getValue')
  132. ->will($this->returnValue($url));
  133. \OC::$CLI = true;
  134. try {
  135. $webRoot = self::invokePrivate($this->setupClass, 'findWebRoot', [$this->config]);
  136. } catch (\InvalidArgumentException $e) {
  137. $webRoot = false;
  138. }
  139. \OC::$CLI = $cliState;
  140. $this->assertSame($webRoot, $expected);
  141. }
  142. public function findWebRootProvider(): array {
  143. return [
  144. 'https://www.example.com/nextcloud/' => ['https://www.example.com/nextcloud/', '/nextcloud'],
  145. 'https://www.example.com/nextcloud' => ['https://www.example.com/nextcloud', '/nextcloud'],
  146. 'https://www.example.com/' => ['https://www.example.com/', ''],
  147. 'https://www.example.com' => ['https://www.example.com', ''],
  148. 'https://nctest13pgsql.lan/test123/' => ['https://nctest13pgsql.lan/test123/', '/test123'],
  149. 'https://nctest13pgsql.lan/test123' => ['https://nctest13pgsql.lan/test123', '/test123'],
  150. 'https://nctest13pgsql.lan/' => ['https://nctest13pgsql.lan/', ''],
  151. 'https://nctest13pgsql.lan' => ['https://nctest13pgsql.lan', ''],
  152. 'https://192.168.10.10/nc/' => ['https://192.168.10.10/nc/', '/nc'],
  153. 'https://192.168.10.10/nc' => ['https://192.168.10.10/nc', '/nc'],
  154. 'https://192.168.10.10/' => ['https://192.168.10.10/', ''],
  155. 'https://192.168.10.10' => ['https://192.168.10.10', ''],
  156. 'invalid' => ['invalid', false],
  157. 'empty' => ['', false],
  158. ];
  159. }
  160. }