Skip to content

Commit

Permalink
Revert "MDL-54110 repositories: Whitespace, Typo + unit test fixes"
Browse files Browse the repository at this point in the history
This reverts commit 7e4d43e.
  • Loading branch information
Frederic Massart committed Jun 23, 2016
1 parent 49619ce commit 796f86f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 77 deletions.
10 changes: 6 additions & 4 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -6259,17 +6259,20 @@ function valid_uploaded_file($newfile) {
* The php.ini settings are only used if $usespost is true. This allows repositories that do not use post requests, such as
* repository_filesystem, to copy in files that are larger than post_max_size if the user has permission.
*
* @todo Finish documenting this function
*
* @param int $sitebytes Set maximum size
* @param int $coursebytes Current course $course->maxbytes (in bytes)
* @param int $modulebytes Current module ->maxbytes (in bytes)
* @param bool $usespost Does the upload we're getting the max size for use a post request?
* @return int The maximum size for uploading files.
*/
function get_max_upload_file_size($sitebytes = 0, $coursebytes = 0, $modulebytes = 0, $usespost = true) {
function get_max_upload_file_size($sitebytes=0, $coursebytes=0, $modulebytes=0, $usespost = true) {

$sizes = array();

if ($usespost) {
if (!$filesize = ini_get('upload_max_filesize')) {
if (! $filesize = ini_get('upload_max_filesize')) {
$filesize = '5M';
}
$sizes[] = get_real_size($filesize);
Expand All @@ -6282,8 +6285,7 @@ function get_max_upload_file_size($sitebytes = 0, $coursebytes = 0, $modulebytes
$sizes[] = $sitebytes;
}
} else {
if ($sitebytes != 0) {
// It's for possible that $sitebytes == USER_CAN_IGNORE_FILE_SIZE_LIMITS (-1).
if ($sitebytes != 0) { // It's for possible that $sitebytes == USER_CAN_IGNORE_FILE_SIZE_LIMITS (-1).
$sizes[] = $sitebytes;
}
}
Expand Down
109 changes: 39 additions & 70 deletions lib/tests/moodlelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2216,82 +2216,51 @@ public function test_get_max_upload_sizes() {
$this->assertArrayHasKey(get_max_upload_file_size(), $result);
}

/**
* Provider for get_max_upload_file_size.
*
* @return array
*/
public function get_max_upload_file_size_provider() {
public function test_get_max_upload_file_size() {
// Get the smallest upload limit from ini settings.
$inisize = min(array(get_real_size(ini_get('post_max_size')), get_real_size(ini_get('upload_max_filesize'))));
return [
'POST: inisize smallest' => [
$inisize + 10,
$inisize + 20,
$inisize + 30,
true,
$inisize,
],
'POST: sitebytes smallest' => [
$inisize - 30,
$inisize - 20,
$inisize - 10,
true,
$inisize - 30,
],
'POST: coursebytes smallest' => [
$inisize - 20,
$inisize - 30,
$inisize - 10,
true,
$inisize - 30,
],
'POST: modulebytes smallest' => [
$inisize - 20,
$inisize - 10,
$inisize - 30,
true,
$inisize - 30,
],
'POST: User can ignore site limit (respect ini)' => [
USER_CAN_IGNORE_FILE_SIZE_LIMITS,
0,
0,
true,
$inisize,
],
'NOPOST: inisize smallest' => [
$inisize + 10,
$inisize + 20,
$inisize + 30,
false,
$inisize + 10,
],
'NOPOST: User can ignore site limit (no limit)' => [
USER_CAN_IGNORE_FILE_SIZE_LIMITS,
0,
0,
false,
USER_CAN_IGNORE_FILE_SIZE_LIMITS,
],
];
}

/**
* Test get_max_upload_file_size with various combinations.
*
* @dataProvider get_max_upload_file_size_provider
*/
public function test_get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes, $ispost, $expectation) {
$this->assertEquals($expectation, get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes, $ispost));
}
// The inisize is the smallest.
$sitebytes = $inisize + 10;
$coursebytes = $inisize + 20;
$modulebytes = $inisize + 30;
$this->assertEquals($inisize, get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes));

// Site limit is the smallest.
$sitebytes = $inisize - 30;
$coursebytes = $inisize - 20;
$modulebytes = $inisize - 10;
$this->assertEquals($sitebytes, get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes));

// Course limit is the smallest.
$sitebytes = $inisize - 20;
$coursebytes = $inisize - 30;
$modulebytes = $inisize - 10;
$this->assertEquals($coursebytes, get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes));

// Module limit is the smallest.
$sitebytes = $inisize - 20;
$coursebytes = $inisize - 10;
$modulebytes = $inisize - 30;
$this->assertEquals($modulebytes, get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes));

// The inisize is the smallest, the upload does not use post.
$sitebytes = $inisize + 10;
$coursebytes = $inisize + 20;
$modulebytes = $inisize + 30;
$this->assertEquals($sitebytes, get_max_upload_file_size($sitebytes, $coursebytes, $modulebytes, false));

// The user can ignore file size limits, the upload does use post.
$this->assertEquals($inisize, get_max_upload_file_size(USER_CAN_IGNORE_FILE_SIZE_LIMITS, 0, 0));

// The user can ignore file size limits, the upload not does use post.
$this->assertEquals(USER_CAN_IGNORE_FILE_SIZE_LIMITS,
get_max_upload_file_size(USER_CAN_IGNORE_FILE_SIZE_LIMITS, 0, 0, false));

/**
* Test that when get_max_upload_file_size is called with no sizes, and no post, an exception is thrown.
*/
public function test_get_max_upload_file_size_no_sizes() {
// If not using post we have to provide at least one other limit.
$this->setExpectedException('coding_exception', 'You must specify at least one filesize limit.');
get_max_upload_file_size(0, 0, 0, false);

}

/**
Expand Down
2 changes: 1 addition & 1 deletion repository/filesystem/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public function supports_relative_file() {
}

/**
* Helper function to indicate if this repository uses post requests for uploading files.
* Helper funtion to indicate if this repository uses post requests for uploading files.
*
* Files are copied from the filesystem so don't rely on POST requests.
*
Expand Down
4 changes: 2 additions & 2 deletions repository/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2780,9 +2780,9 @@ public function supports_relative_file() {
}

/**
* Helper function to indicate if this repository uses post requests for uploading files.
* Helper funtion to indicate if this repository uses post requests for uploading files.
*
* If the respository doesn't rely on uploading via POST requests, this can be overridden to return false,
* If the respository doesn't rely on uploading via POST requests, this can be overridden to return true,
* allowing users with the right permissions to upload files of any size from this repository.
*
* @return bool
Expand Down

0 comments on commit 796f86f

Please sign in to comment.