UtilCheckServerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /**
  9. * Tests for server check functions
  10. *
  11. * @group DB
  12. */
  13. class UtilCheckServerTest extends \Test\TestCase {
  14. private $datadir;
  15. /**
  16. * @param array $systemOptions
  17. * @return \OC\SystemConfig | \PHPUnit\Framework\MockObject\MockObject
  18. */
  19. protected function getConfig($systemOptions) {
  20. $systemOptions['datadirectory'] = $this->datadir;
  21. $systemOptions['appstoreenabled'] = false; //it's likely that there is no app folder we can write in
  22. $config = $this->getMockBuilder('\OC\SystemConfig')
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $config->expects($this->any())
  26. ->method('getValue')
  27. ->willReturnCallback(function ($key, $default) use ($systemOptions) {
  28. return $systemOptions[$key] ?? $default;
  29. });
  30. return $config;
  31. }
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->datadir = \OC::$server->getTempManager()->getTemporaryFolder();
  35. file_put_contents($this->datadir . '/.ocdata', '');
  36. \OC::$server->getSession()->set('checkServer_succeeded', false);
  37. }
  38. protected function tearDown(): void {
  39. // clean up
  40. @unlink($this->datadir . '/.ocdata');
  41. parent::tearDown();
  42. }
  43. /**
  44. * Test that checkServer() returns no errors in the regular case.
  45. */
  46. public function testCheckServer() {
  47. $result = \OC_Util::checkServer($this->getConfig([
  48. 'installed' => true
  49. ]));
  50. $this->assertEmpty($result);
  51. }
  52. /**
  53. * Test that checkServer() does not check the data dir validity
  54. * when the server is not installed yet (else the setup cannot
  55. * be run...)
  56. */
  57. public function testCheckServerSkipDataDirValidityOnSetup() {
  58. // simulate old version that didn't have it
  59. unlink($this->datadir . '/.ocdata');
  60. // even though ".ocdata" is missing, the error isn't
  61. // triggered to allow setup to run
  62. $result = \OC_Util::checkServer($this->getConfig([
  63. 'installed' => false
  64. ]));
  65. $this->assertEmpty($result);
  66. }
  67. /**
  68. * Test that checkServer() does not check the data dir validity
  69. * when an upgrade is required (else the upgrade cannot be
  70. * performed...)
  71. */
  72. public function testCheckServerSkipDataDirValidityOnUpgrade() {
  73. // simulate old version that didn't have it
  74. unlink($this->datadir . '/.ocdata');
  75. $session = \OC::$server->getSession();
  76. $oldCurrentVersion = $session->get('OC_Version');
  77. // upgrade condition to simulate needUpgrade() === true
  78. $session->set('OC_Version', [6, 0, 0, 2]);
  79. // even though ".ocdata" is missing, the error isn't
  80. // triggered to allow for upgrade
  81. $result = \OC_Util::checkServer($this->getConfig([
  82. 'installed' => true,
  83. 'version' => '6.0.0.1'
  84. ]));
  85. $this->assertEmpty($result);
  86. // restore versions
  87. $session->set('OC_Version', $oldCurrentVersion);
  88. }
  89. /**
  90. * Test that checkDataDirectoryValidity returns no error
  91. * when ".ocdata" is present.
  92. */
  93. public function testCheckDataDirValidity() {
  94. $result = \OC_Util::checkDataDirectoryValidity($this->datadir);
  95. $this->assertEmpty($result);
  96. }
  97. /**
  98. * Test that checkDataDirectoryValidity and checkServer
  99. * both return an error when ".ocdata" is missing.
  100. */
  101. public function testCheckDataDirValidityWhenFileMissing() {
  102. unlink($this->datadir . '/.ocdata');
  103. $result = \OC_Util::checkDataDirectoryValidity($this->datadir);
  104. $this->assertEquals(1, count($result));
  105. $result = \OC_Util::checkServer($this->getConfig([
  106. 'installed' => true,
  107. 'version' => implode('.', \OCP\Util::getVersion())
  108. ]));
  109. $this->assertCount(1, $result);
  110. }
  111. /**
  112. * Tests that no error is given when the datadir is writable
  113. */
  114. public function testDataDirWritable() {
  115. $result = \OC_Util::checkServer($this->getConfig([
  116. 'installed' => true,
  117. 'version' => implode('.', \OCP\Util::getVersion())
  118. ]));
  119. $this->assertEmpty($result);
  120. }
  121. /**
  122. * Tests an error is given when the datadir is not writable
  123. */
  124. public function testDataDirNotWritable() {
  125. $this->markTestSkipped('TODO: Disable because fails on drone');
  126. chmod($this->datadir, 0300);
  127. $result = \OC_Util::checkServer($this->getConfig([
  128. 'installed' => true,
  129. 'version' => implode('.', \OCP\Util::getVersion())
  130. ]));
  131. $this->assertCount(1, $result);
  132. }
  133. /**
  134. * Tests no error is given when the datadir is not writable during setup
  135. */
  136. public function testDataDirNotWritableSetup() {
  137. chmod($this->datadir, 0300);
  138. $result = \OC_Util::checkServer($this->getConfig([
  139. 'installed' => false,
  140. 'version' => implode('.', \OCP\Util::getVersion())
  141. ]));
  142. chmod($this->datadir, 0700); //needed for cleanup
  143. $this->assertEmpty($result);
  144. }
  145. }