PageRenderTime 24ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/include/utils/SugarFileUtilsTest.php

https://github.com/mikmagic/sugarcrm_dev
PHP | 196 lines | 129 code | 33 blank | 34 comment | 4 complexity | 64fe64b31a6fc376cf81f656b7441437 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. /*********************************************************************************
  3. * SugarCRM Community Edition is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  24. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  25. *
  26. * The interactive user interfaces in modified source and object code versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the "Powered by
  32. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  33. * technical reasons, the Appropriate Legal Notices must display the words
  34. * "Powered by SugarCRM".
  35. ********************************************************************************/
  36. require_once 'include/utils/file_utils.php';
  37. class SugarFileUtilsTest extends Sugar_PHPUnit_Framework_TestCase
  38. {
  39. private $_filename;
  40. private $_old_default_permissions;
  41. public function setUp()
  42. {
  43. if (is_windows())
  44. $this->markTestSkipped('Skipping on Windows');
  45. $this->_filename = realpath(dirname(__FILE__).'/../../../cache/').'file_utils_override'.mt_rand().'.txt';
  46. touch($this->_filename);
  47. $this->_old_default_permissions = $GLOBALS['sugar_config']['default_permissions'];
  48. $GLOBALS['sugar_config']['default_permissions'] =
  49. array (
  50. 'dir_mode' => 0777,
  51. 'file_mode' => 0660,
  52. 'user' => $this->_getCurrentUser(),
  53. 'group' => $this->_getCurrentGroup(),
  54. );
  55. }
  56. public function tearDown()
  57. {
  58. if(file_exists($this->_filename)) {
  59. unlink($this->_filename);
  60. }
  61. $GLOBALS['sugar_config']['default_permissions'] = $this->_old_default_permissions;
  62. SugarConfig::getInstance()->clearCache();
  63. }
  64. private function _getCurrentUser()
  65. {
  66. if ( function_exists('posix_getuid') ) {
  67. return posix_getuid();
  68. }
  69. return '';
  70. }
  71. private function _getCurrentGroup()
  72. {
  73. if ( function_exists('posix_getgid') ) {
  74. return posix_getgid();
  75. }
  76. return '';
  77. }
  78. private function _getTestFilePermissions()
  79. {
  80. return substr(sprintf('%o', fileperms($this->_filename)), -4);
  81. }
  82. public function testSugarTouch()
  83. {
  84. $this->assertTrue(sugar_touch($this->_filename));
  85. }
  86. public function testSugarTouchWithTime()
  87. {
  88. $time = filemtime($this->_filename);
  89. $this->assertTrue(sugar_touch($this->_filename, $time));
  90. $this->assertEquals($time,filemtime($this->_filename));
  91. }
  92. public function testSugarTouchWithAccessTime()
  93. {
  94. $time = filemtime($this->_filename);
  95. $atime = gmmktime();
  96. $this->assertTrue(sugar_touch($this->_filename, $time, $atime));
  97. $this->assertEquals($time,filemtime($this->_filename));
  98. $this->assertEquals($atime,fileatime($this->_filename));
  99. }
  100. public function testSugarChmod()
  101. {
  102. return true;
  103. $this->assertTrue(sugar_chmod($this->_filename));
  104. $this->assertEquals($this->_getTestFilePermissions(),decoct(get_mode()));
  105. }
  106. public function testSugarChmodWithMode()
  107. {
  108. $mode = 0411;
  109. $this->assertTrue(sugar_chmod($this->_filename, $mode));
  110. $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
  111. }
  112. public function testSugarChmodNoDefaultMode()
  113. {
  114. $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
  115. $this->assertFalse(sugar_chmod($this->_filename));
  116. }
  117. public function testSugarChmodDefaultModeNotAnInteger()
  118. {
  119. $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
  120. $this->assertFalse(sugar_chmod($this->_filename));
  121. }
  122. public function testSugarChmodDefaultModeIsZero()
  123. {
  124. $GLOBALS['sugar_config']['default_permissions']['file_mode'] = 0;
  125. $this->assertFalse(sugar_chmod($this->_filename));
  126. }
  127. public function testSugarChmodWithModeNoDefaultMode()
  128. {
  129. $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
  130. $mode = 0411;
  131. $this->assertTrue(sugar_chmod($this->_filename, $mode));
  132. $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
  133. }
  134. public function testSugarChmodWithModeDefaultModeNotAnInteger()
  135. {
  136. $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
  137. $mode = 0411;
  138. $this->assertTrue(sugar_chmod($this->_filename, $mode));
  139. $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
  140. }
  141. public function testSugarChown()
  142. {
  143. $this->assertTrue(sugar_chown($this->_filename));
  144. $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
  145. }
  146. public function testSugarChownWithUser()
  147. {
  148. $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
  149. $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
  150. }
  151. public function testSugarChownNoDefaultUser()
  152. {
  153. $GLOBALS['sugar_config']['default_permissions']['user'] = '';
  154. $this->assertFalse(sugar_chown($this->_filename));
  155. }
  156. public function testSugarChownWithUserNoDefaultUser()
  157. {
  158. $GLOBALS['sugar_config']['default_permissions']['user'] = '';
  159. $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
  160. $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
  161. }
  162. }
  163. ?>