SetupTest.php 5.1 KB

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