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