Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def runBehatUITest():
"update-ca-certificates",
"cd /var/www/html/moodle",
'vendor/bin/behat --config /var/www/behatdata/behatrun/behat/behat.yml --tags="@ocis"',
'vendor/bin/behat --config /var/www/behatdata/behatrun/behat/behat.yml --tags="@max_uploas"',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'vendor/bin/behat --config /var/www/behatdata/behatrun/behat/behat.yml --tags="@max_uploas"',
'vendor/bin/behat --config /var/www/behatdata/behatrun/behat/behat.yml --tags="@max_uploads"',

],
"volumes": [
{
Expand Down
1 change: 1 addition & 0 deletions lang/en/repository_ocis.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@
$string['webfinger_error'] = 'Could not get user-information from webfinger service.';
$string['personal_drive'] = 'Personal';
$string['shares_drive'] = 'Shares';
$string['exceed_maxbytes_error'] = 'File exceeds maximum uploaded file size limit';
13 changes: 12 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,21 @@ public static function get_instance_option_names() {
* @throws moodle_exception
*/
public function get_file($fileid, $filename = ''): array {
global $CFG;

$localpath = $this->prepare_file($fileid);
try {
$file = $this->getocismanager()->get_ocis_client()->getResourceById($fileid);
file_put_contents($localpath, $file->getContentStream());
if ($file->getSize() > (int)$CFG->maxbytes && (int)$CFG->maxbytes !== 0) {
throw new moodle_exception(
'exceed_maxbytes_error',
'repository_ocis',
'',
null,
);
} else {
file_put_contents($localpath, $file->getContentStream());
}
} catch (HttpException $e) {
throw new moodle_exception(
'could_not_connect_error',
Expand Down
42 changes: 42 additions & 0 deletions tests/behat/behat_repository_ocis.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,46 @@ public function user_enables_sync_of_share(string $user, string $share, string $
throw new Exception("Error enabling sync of share");
}
}

/**
* Step definition for upload a file from disk
* @Given user :user has uploaded file :source to :destination
*
* @param string $user
* @param string $source
* @param string $destination
*
* @return void
*/
public function user_has_uploaded_a_file_to(string $user, string $source, string $destination): void {
$response = $this->graphhelper->uploadFile($user, $source, $destination);
if (!in_array($response['statusCode'], [201, 204])) {
$error = json_decode($response['body'], true);
$message = $error['error']['message'] ?? 'Unknown error';
throw new Exception("Error creating resource in personal space: $message");
}
}

/**
* Step definition for checking if error message is present on screen
* @When I should see error :errorMessage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @When I should see error :errorMessage
* @Then I should see error :errorMessage

This looks like a Then step.

*
* @param string $errormessage The content of error message
*
* @return void
*/
public function i_should_see_error(string $errormessage): void {
$actualmessage = '';
try {
$this->execute('behat_general::assert_page_contains_text', [$errormessage]);
} catch (Exception $e) {
$actualmessage = $e->getMessage();
}

if (str_contains($actualmessage, $errormessage)) {
die();
} else {
throw new Exception("Expected message: $errormessage\nBut got: $actualmessage", 1);
}
}
}
27 changes: 27 additions & 0 deletions tests/behat/graph_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,31 @@ public function enable_share_sync(string $user, string $share, string $offeredby
json_encode($body)
);
}

/**
* upload a file from disk
* @param string $user
* @param string $source
* @param string $destination
*
* @return array
*/
public function uploadfile(
string $user,
string $source,
string $destination,
): array {
$filepath = __DIR__ . '/../' . $source;

if (!file_exists($filepath)) {
throw new Exception("Source file not found: $filepath");
}
$filecontents = file_get_contents($filepath);
$client = $this->get_client($user);
return $client->request(
'PUT',
"/dav/files/$user/$destination",
$filecontents,
);
}
}
26 changes: 26 additions & 0 deletions tests/behat/maxUpload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@max_uploas @javascript @repository_ocis @repository
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@max_uploas @javascript @repository_ocis @repository
@max_uploads @javascript @repository_ocis @repository


Feature: try to upload the resource in moodle from oCIS with maxupload setting
As a user who manages moodle content
I want to limit file upload size
So that the storage won't be full

Scenario: upload file that exceeds moodle upload limit
Given user "Admin" has uploaded file "fileForUpload/testavatar.jpg" to "testavatar.jpg"
And I log in as "admin"
And I am on site homepage
And I navigate to "General > Security > Site security settings" in site administration
And I click on "//*[@id='id_s__maxbytes']/option[10]" "xpath_element"
And I press "Save changes"
And I follow "Profile" in the user menu
And I click on "Blog entries" "link"
And I click on "Add a new entry" "link"
And I click on "Add.." "button"
And I click on "oCIS" "link"
And I click on "Log in to your account" "button"
And I switch to a second window
And I log in to ocis as "admin"
When I click on "//*[@class='fp-filename-field']/p[text()='Personal']" "xpath_element"
And I click on "//*[@class='fp-filename-field']/p[text()='testavatar.jpg']" "xpath_element"
And I click on "Select this file" "button"
Then I should see error "File exceeds maximum uploaded file size limit"
Binary file added tests/fileForUpload/testavatar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.