utilcheckserver.php 4.7 KB

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