utilcheckserver.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  9. * Tests for server check functions
  10. *
  11. * @group DB
  12. */
  13. class Test_Util_CheckServer extends \Test\TestCase {
  14. private $datadir;
  15. /**
  16. * @param array $systemOptions
  17. * @return \OCP\IConfig | 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('\OCP\IConfig')
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $config->expects($this->any())
  26. ->method('getSystemValue')
  27. ->will($this->returnCallback(function ($key, $default) use ($systemOptions) {
  28. return isset($systemOptions[$key]) ? $systemOptions[$key] : $default;
  29. }));
  30. return $config;
  31. }
  32. protected function setUp() {
  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() {
  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(array(
  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(array(
  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', array(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(array(
  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(array(
  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(array(
  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. if (\OC_Util::runningOnWindows()) {
  126. $this->markTestSkipped('[Windows] chmod() does not work as intended on Windows.');
  127. }
  128. $this->markTestSkipped('TODO: Disable because fails on drone');
  129. chmod($this->datadir, 0300);
  130. $result = \OC_Util::checkServer($this->getConfig(array(
  131. 'installed' => true,
  132. 'version' => implode('.', \OCP\Util::getVersion())
  133. )));
  134. $this->assertCount(1, $result);
  135. }
  136. /**
  137. * Tests no error is given when the datadir is not writable during setup
  138. */
  139. public function testDataDirNotWritableSetup() {
  140. chmod($this->datadir, 0300);
  141. $result = \OC_Util::checkServer($this->getConfig(array(
  142. 'installed' => false,
  143. 'version' => implode('.', \OCP\Util::getVersion())
  144. )));
  145. chmod($this->datadir, 0700); //needed for cleanup
  146. $this->assertEmpty($result);
  147. }
  148. }