SetupTest.php 5.2 KB

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