1
0

UtilCheckServerTest.php 4.6 KB

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