SetupTest.php 5.4 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(): void {
  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. ->willReturn(
  53. ['sqlite', 'mysql', 'oci']
  54. );
  55. $this->setupClass
  56. ->expects($this->once())
  57. ->method('is_callable')
  58. ->willReturn(false);
  59. $this->setupClass
  60. ->expects($this->any())
  61. ->method('getAvailableDbDriversForPdo')
  62. ->willReturn(['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. ->willReturn(
  74. ['sqlite', 'mysql', 'oci', 'pgsql']
  75. );
  76. $this->setupClass
  77. ->expects($this->any())
  78. ->method('is_callable')
  79. ->willReturn(false);
  80. $this->setupClass
  81. ->expects($this->any())
  82. ->method('getAvailableDbDriversForPdo')
  83. ->willReturn([]);
  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. ->willReturn(
  92. ['sqlite', 'mysql', 'pgsql', 'oci']
  93. );
  94. $this->setupClass
  95. ->expects($this->any())
  96. ->method('is_callable')
  97. ->willReturn(true);
  98. $this->setupClass
  99. ->expects($this->any())
  100. ->method('getAvailableDbDriversForPdo')
  101. ->willReturn(['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. public function testGetSupportedDatabaseException() {
  112. $this->expectException(\Exception::class);
  113. $this->expectExceptionMessage('Supported databases are not properly configured.');
  114. $this->config
  115. ->expects($this->once())
  116. ->method('getValue')
  117. ->willReturn('NotAnArray');
  118. $this->setupClass->getSupportedDatabases();
  119. }
  120. /**
  121. * @dataProvider findWebRootProvider
  122. * @param $url
  123. * @param $expected
  124. */
  125. public function testFindWebRootCli($url, $expected) {
  126. $cliState = \OC::$CLI;
  127. $this->config
  128. ->expects($this->once())
  129. ->method('getValue')
  130. ->willReturn($url);
  131. \OC::$CLI = true;
  132. try {
  133. $webRoot = self::invokePrivate($this->setupClass, 'findWebRoot', [$this->config]);
  134. } catch (\InvalidArgumentException $e) {
  135. $webRoot = false;
  136. }
  137. \OC::$CLI = $cliState;
  138. $this->assertSame($webRoot, $expected);
  139. }
  140. public function findWebRootProvider(): array {
  141. return [
  142. 'https://www.example.com/nextcloud/' => ['https://www.example.com/nextcloud/', '/nextcloud'],
  143. 'https://www.example.com/nextcloud' => ['https://www.example.com/nextcloud', '/nextcloud'],
  144. 'https://www.example.com/' => ['https://www.example.com/', ''],
  145. 'https://www.example.com' => ['https://www.example.com', ''],
  146. 'https://nctest13pgsql.lan/test123/' => ['https://nctest13pgsql.lan/test123/', '/test123'],
  147. 'https://nctest13pgsql.lan/test123' => ['https://nctest13pgsql.lan/test123', '/test123'],
  148. 'https://nctest13pgsql.lan/' => ['https://nctest13pgsql.lan/', ''],
  149. 'https://nctest13pgsql.lan' => ['https://nctest13pgsql.lan', ''],
  150. 'https://192.168.10.10/nc/' => ['https://192.168.10.10/nc/', '/nc'],
  151. 'https://192.168.10.10/nc' => ['https://192.168.10.10/nc', '/nc'],
  152. 'https://192.168.10.10/' => ['https://192.168.10.10/', ''],
  153. 'https://192.168.10.10' => ['https://192.168.10.10', ''],
  154. 'invalid' => ['invalid', false],
  155. 'empty' => ['', false],
  156. ];
  157. }
  158. }