utilcheckserver.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 PHPUnit_Framework_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. public function setUp() {
  31. $this->datadir = \OC_Helper::tmpFolder();
  32. file_put_contents($this->datadir . '/.ocdata', '');
  33. \OC::$server->getSession()->set('checkServer_succeeded', false);
  34. }
  35. public function tearDown() {
  36. // clean up
  37. @unlink($this->datadir . '/.ocdata');
  38. }
  39. /**
  40. * Test that checkServer() returns no errors in the regular case.
  41. */
  42. public function testCheckServer() {
  43. $result = \OC_Util::checkServer($this->getConfig(array(
  44. 'installed' => true
  45. )));
  46. $this->assertEmpty($result);
  47. }
  48. /**
  49. * Test that checkServer() does not check the data dir validity
  50. * when the server is not installed yet (else the setup cannot
  51. * be run...)
  52. */
  53. public function testCheckServerSkipDataDirValidityOnSetup() {
  54. // simulate old version that didn't have it
  55. unlink($this->datadir . '/.ocdata');
  56. // even though ".ocdata" is missing, the error isn't
  57. // triggered to allow setup to run
  58. $result = \OC_Util::checkServer($this->getConfig(array(
  59. 'installed' => false
  60. )));
  61. $this->assertEmpty($result);
  62. }
  63. /**
  64. * Test that checkServer() does not check the data dir validity
  65. * when an upgrade is required (else the upgrade cannot be
  66. * performed...)
  67. */
  68. public function testCheckServerSkipDataDirValidityOnUpgrade() {
  69. // simulate old version that didn't have it
  70. unlink($this->datadir . '/.ocdata');
  71. $session = \OC::$server->getSession();
  72. $oldCurrentVersion = $session->get('OC_Version');
  73. // upgrade condition to simulate needUpgrade() === true
  74. $session->set('OC_Version', array(6, 0, 0, 2));
  75. // even though ".ocdata" is missing, the error isn't
  76. // triggered to allow for upgrade
  77. $result = \OC_Util::checkServer($this->getConfig(array(
  78. 'installed' => true,
  79. 'version' => '6.0.0.1'
  80. )));
  81. $this->assertEmpty($result);
  82. // restore versions
  83. $session->set('OC_Version', $oldCurrentVersion);
  84. }
  85. /**
  86. * Test that checkDataDirectoryValidity returns no error
  87. * when ".ocdata" is present.
  88. */
  89. public function testCheckDataDirValidity() {
  90. $result = \OC_Util::checkDataDirectoryValidity($this->datadir);
  91. $this->assertEmpty($result);
  92. }
  93. /**
  94. * Test that checkDataDirectoryValidity and checkServer
  95. * both return an error when ".ocdata" is missing.
  96. */
  97. public function testCheckDataDirValidityWhenFileMissing() {
  98. unlink($this->datadir . '/.ocdata');
  99. $result = \OC_Util::checkDataDirectoryValidity($this->datadir);
  100. $this->assertEquals(1, count($result));
  101. $result = \OC_Util::checkServer($this->getConfig(array(
  102. 'installed' => true,
  103. 'version' => implode('.', OC_Util::getVersion())
  104. )));
  105. $this->assertCount(1, $result);
  106. }
  107. /**
  108. * Tests that no error is given when the datadir is writable
  109. */
  110. public function testDataDirWritable() {
  111. $result = \OC_Util::checkServer($this->getConfig(array(
  112. 'installed' => true,
  113. 'version' => implode('.', OC_Util::getVersion())
  114. )));
  115. $this->assertEmpty($result);
  116. }
  117. /**
  118. * Tests an error is given when the datadir is not writable
  119. */
  120. public function testDataDirNotWritable() {
  121. chmod($this->datadir, 0300);
  122. $result = \OC_Util::checkServer($this->getConfig(array(
  123. 'installed' => true,
  124. 'version' => implode('.', OC_Util::getVersion())
  125. )));
  126. $this->assertCount(1, $result);
  127. }
  128. /**
  129. * Tests no error is given when the datadir is not writable during setup
  130. */
  131. public function testDataDirNotWritableSetup() {
  132. chmod($this->datadir, 0300);
  133. $result = \OC_Util::checkServer($this->getConfig(array(
  134. 'installed' => false,
  135. 'version' => implode('.', OC_Util::getVersion())
  136. )));
  137. chmod($this->datadir, 0700); //needed for cleanup
  138. $this->assertEmpty($result);
  139. }
  140. }