first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+425
View File
@@ -0,0 +1,425 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Filemanager and filepicker manipulation steps definitions.
*
* @package core_filepicker
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../lib/behat/core_behat_file_helper.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
Behat\Gherkin\Node\TableNode as TableNode;
/**
* Steps definitions to deal with the filemanager and filepicker.
*
* @package core_filepicker
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_filepicker extends behat_base {
use core_behat_file_helper;
/**
* Creates a folder with specified name in the current folder and in the specified filemanager field.
*
* @Given /^I create "(?P<foldername_string>(?:[^"]|\\")*)" folder in "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $foldername
* @param string $filemanagerelement
*/
public function i_create_folder_in_filemanager($foldername, $filemanagerelement) {
$fieldnode = $this->get_filepicker_node($filemanagerelement);
// Looking for the create folder button inside the specified filemanager.
$exception = new ExpectationException('No folders can be created in "'.$filemanagerelement.'" filemanager',
$this->getSession());
$newfolder = $this->find('css', 'div.fp-btn-mkdir a', $exception, $fieldnode);
$newfolder->click();
// Setting the folder name in the modal window.
$exception = new ExpectationException('The dialog to enter the folder name does not appear', $this->getSession());
$dialoginput = $this->find('css', '.fp-mkdir-dlg-text input', $exception);
$dialoginput->setValue($foldername);
$exception = new ExpectationException('The button for the create folder dialog can not be located', $this->getSession());
$dialognode = $this->find('css', '.moodle-dialogue-focused');
$buttonnode = $this->find('css', '.fp-dlg-butcreate', $exception, $dialognode);
$buttonnode->click();
}
/**
* Opens the contents of a filemanager folder. It looks for the folder in the current folder and in the path bar.
*
* @Given /^I open "(?P<foldername_string>(?:[^"]|\\")*)" folder from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $foldername
* @param string $filemanagerelement
*/
public function i_open_folder_from_filemanager($foldername, $filemanagerelement) {
$fieldnode = $this->get_filepicker_node($filemanagerelement);
$exception = new ExpectationException(
'The "'.$foldername.'" folder can not be found in the "'.$filemanagerelement.'" filemanager',
$this->getSession()
);
$folderliteral = behat_context_helper::escape($foldername);
// We look both in the pathbar and in the contents.
try {
// In the current folder workspace.
$folder = $this->find(
'xpath',
"//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
"/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]" .
"[normalize-space(.)=$folderliteral]",
$exception,
$fieldnode
);
} catch (ExpectationException $e) {
// And in the pathbar.
$folder = $this->find(
'xpath',
"//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')]" .
"[normalize-space(.)=$folderliteral]",
$exception,
$fieldnode
);
}
// It should be a NodeElement, otherwise an exception would have been thrown.
$folder->click();
}
/**
* Unzips the specified file from the specified filemanager field. The zip file has to be visible in the current folder.
*
* @Given /^I unzip "(?P<filename_string>(?:[^"]|\\")*)" file from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filename
* @param string $filemanagerelement
*/
public function i_unzip_file_from_filemanager($filename, $filemanagerelement) {
// Open the contextual menu of the filemanager element.
$this->open_element_contextual_menu($filename, $filemanagerelement);
// Execute the action.
$exception = new ExpectationException($filename.' element can not be unzipped', $this->getSession());
$this->perform_on_element('unzip', $exception);
}
/**
* Zips the specified folder from the specified filemanager field. The folder has to be in the current folder.
*
* @Given /^I zip "(?P<filename_string>(?:[^"]|\\")*)" folder from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $foldername
* @param string $filemanagerelement
*/
public function i_zip_folder_from_filemanager($foldername, $filemanagerelement) {
// Open the contextual menu of the filemanager element.
$this->open_element_contextual_menu($foldername, $filemanagerelement);
// Execute the action.
$exception = new ExpectationException($foldername.' element can not be zipped', $this->getSession());
$this->perform_on_element('zip', $exception);
}
/**
* Deletes the specified file or folder from the specified filemanager field.
*
* @Given /^I delete "(?P<file_or_folder_name_string>(?:[^"]|\\")*)" from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $name
* @param string $filemanagerelement
*/
public function i_delete_file_from_filemanager($name, $filemanagerelement) {
// Open the contextual menu of the filemanager element.
$this->open_element_contextual_menu($name, $filemanagerelement);
// Execute the action.
$exception = new ExpectationException($name.' element can not be deleted', $this->getSession());
$this->perform_on_element('delete', $exception);
// Yes, we are sure.
$this->execute('behat_general::i_click_on_in_the', [get_string('yes'), 'button', get_string('confirm'), 'dialogue']);
}
/**
* Makes sure user can see the exact number of elements (files in folders) in the filemanager.
*
* @Then /^I should see "(?P<elementscount_number>\d+)" elements in "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param int $elementscount
* @param string $filemanagerelement
*/
public function i_should_see_elements_in_filemanager($elementscount, $filemanagerelement) {
$filemanagernode = $this->get_filepicker_node($filemanagerelement);
// We count .fp-file elements inside a filemanager not being updated.
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
"[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]";
$elements = $this->find_all('xpath', $xpath, false, $filemanagernode);
if (count($elements) != $elementscount) {
throw new ExpectationException('Found '.count($elements).' elements in filemanager. Expected '.$elementscount,
$this->getSession());
}
}
/**
* Picks the file from repository leaving default values in select file dialogue.
*
* @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $repository
* @param string $filemanagerelement
*/
public function i_add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()), false);
}
/**
* Picks the file from repository leaving default values in select file dialogue and confirming to overwrite an existing file.
*
* @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $repository
* @param string $filemanagerelement
*/
public function i_add_and_overwrite_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()),
get_string('overwrite', 'repository'));
}
/**
* Picks the file from repository filling the form in Select file dialogue.
*
* @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $repository
* @param string $filemanagerelement
* @param TableNode $data Data to fill the form in Select file dialogue
*/
public function i_add_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data, false);
}
/**
* Picks the file from repository confirming to overwrite an existing file
*
* @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $repository
* @param string $filemanagerelement
* @param TableNode $data Data to fill the form in Select file dialogue
*/
public function i_add_and_overwrite_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement,
TableNode $data) {
$this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data,
get_string('overwrite', 'repository'));
}
/**
* Picks the file from private files repository
*
* @throws ExpectationException Thrown by behat_base::find
* @param string $filepath
* @param string $repository
* @param string $filemanagerelement
* @param TableNode $data Data to fill the form in Select file dialogue
* @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
* or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
*/
protected function add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, TableNode $data,
$overwriteaction = false) {
$filemanagernode = $this->get_filepicker_node($filemanagerelement);
// Opening the select repository window and selecting the upload repository.
$this->open_add_file_window($filemanagernode, $repository);
$this->open_element_contextual_menu($filepath);
// Fill the form in Select window.
$datahash = $data->getRowsHash();
// The action depends on the field type.
foreach ($datahash as $locator => $value) {
$field = behat_field_manager::get_form_field_from_label($locator, $this);
// Delegates to the field class.
$field->set_value($value);
}
$selectfilebutton = $this->find_button(get_string('getfile', 'repository'));
$selectfilebutton->click();
// We wait for all the JS to finish as it is performing an action.
$this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS);
if ($overwriteaction !== false) {
$overwritebutton = $this->find_button($overwriteaction);
$overwritebutton->click();
// We wait for all the JS to finish.
$this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS);
}
}
/**
* Selects a repository from the repository list in the file picker.
*
* @Then /^I select "(?P<repository_name_string>(?:[^"]|\\")*)" repository in file picker$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $repositoryname
*/
public function i_select_filepicker_repository($repositoryname) {
$exception = new ExpectationException(
"The '{$repositoryname}' repository can not be found in the file picker", $this->getSession());
// We look for a repository that matches a certain name in the file picker repository list.
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' filepicker ')]" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-area ')]" .
"//span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]" .
"[normalize-space(.)='{$repositoryname}']";
$repository = $this->find('xpath', $xpath, $exception);
// If the node exists, click on the node.
$repository->click();
}
/**
* Makes sure user can see the exact number of elements (files and folders) in the repository content area in
* the file picker.
*
* @Then /^I should see "(?P<elements_number>\d+)" elements in repository content area$/
* @throws ExpectationException Thrown by behat_base::find_all
* @param int $expectedcount
*/
public function i_should_see_elements_in_filepicker_repository($expectedcount) {
// We look for all .fp-file elements inside the content area of the file picker repository.
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
"//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]";
try {
$elements = $this->find_all('xpath', $xpath);
} catch (ElementNotFoundException $e) {
$elements = [];
}
// Make sure the expected number is equal to the actual number of .fp-file elements.
if (count($elements) != $expectedcount) {
throw new ExpectationException("Found " . count($elements) .
" elements in filepicker repository. Expected {$expectedcount}", $this->getSession());
}
}
/**
* Returns a specific element (file or folder) in the repository content area in the file picker.
*
* @throws ExpectationException Thrown by behat_base::find
* @param string $elementname The name of the element
* @param string $elementtype The type of the element ("file" or "folder")
* @return NodeElement
*/
protected function get_element_in_filepicker_repository($elementname, $elementtype) {
// We look for a .fp-{type} element with a certain name inside the content area of the file picker repository.
$exception = new ExpectationException(
"The '{$elementname}' {$elementtype} can not be found in the repository content area",
$this->getSession());
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
"//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-{$elementtype} ')]" .
"[normalize-space(.)='{$elementname}']";
return $this->find('xpath', $xpath, $exception);
}
/**
* Makes sure user can see a specific element (file or folder) in the repository content area in the file picker.
*
* @Then /^I should see "(?P<element_name_string>(?:[^"]|\\")*)" "(?P<element_type_string>(?:[^"]|\\")*)" in repository content area$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $elementname The name of the element
* @param string $elementtype The type of the element ("file" or "folder")
*/
public function i_should_see_element_in_filepicker_repository($elementname, $elementtype) {
$this->get_element_in_filepicker_repository($elementname, $elementtype);
}
/**
* Clicks on a specific element (file or folder) in the repository content area in the file picker.
*
* @Then /^I click on "(?P<element_name_string>(?:[^"]|\\")*)" "(?P<element_type_string>(?:[^"]|\\")*)" in repository content area$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $elementname The name of the element
* @param string $elementtype The type of the element ("file" or "folder")
*/
public function i_click_on_element_in_filepicker_repository($elementname, $elementtype) {
$element = $this->get_element_in_filepicker_repository($elementname, $elementtype);
$element->click();
}
/**
* Makes sure the user can see a specific breadcrumb navigation structure in the file picker repository.
*
* @Then /^I should see "(?P<breadcrumb_navigation_string>(?:[^"]|\\")*)" breadcrumb navigation in repository$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $breadcrumbs The breadcrumb navigation structure (ex. "System > Category > Course")
*/
public function i_should_see_breadcrumb_navigation_in_filepicker_repository($breadcrumbs) {
$breadcrumbs = preg_split('/\s*>\s*/', trim($breadcrumbs));
foreach ($breadcrumbs as $breadcrumb) {
// We look for a .fp-path-folder element with a certain name inside the breadcrumb navigation area
// in the repository.
$exception = new ExpectationException(
"The '{$breadcrumb}' node can not be found in the breadcrumb navigation in the repository",
$this->getSession()
);
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]" .
"//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-pathbar ')]" .
"//span[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder ')]" .
"[normalize-space(.)='{$breadcrumb}']";
$this->find('xpath', $xpath, $exception);
}
}
}
@@ -0,0 +1,33 @@
@core @core_filepicker @_file_upload
Feature: A selected file can be cancelled
In order to refine the file manager contents
As a user
I need to cancel a selected file
@javascript @_bug_phantomjs
Scenario: Cancel a selected recent file from being added to a folder
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| private_files | System | 1 | my-index | side-post |
And the following "activities" exist:
| activity | course | name | intro |
| folder | C1 | Folder name | Folder description |
And I log in as "admin"
And I follow "Manage private files"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I press "Save changes"
And I am on the "Folder name" "folder activity" page
And I press "Edit"
And I upload "lib/tests/fixtures/upload_users.csv" file to "Files" filemanager
And I click on "Add..." "button" in the "Files" "form_row"
And I click on "Recent files" "link" in the ".fp-repo-area" "css_element"
And I click on "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')][normalize-space(.)='empty.txt']" "xpath_element"
And I click on ".moodle-dialogue-focused .fp-select .fp-select-cancel" "css_element"
And I click on "Close" "button" in the "File picker" "dialogue"
And I press "Save changes"
Then I should see "upload_users.csv"
And I should not see "empty.txt"
And I should see "Folder description"
@@ -0,0 +1,24 @@
@core @core_filepicker
Feature: Create folders in the file manager
In order to create a directory structure in a file area
As a user
I need to create folders and subfolders in a file area
@javascript @_bug_phantomjs
Scenario: Create folders and subfolders
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And I log in as "admin"
And I add a folder activity to course "Course 1" section "0"
And I set the following fields to these values:
| Name | Folder resource |
| Description | The description |
And I create "Folder 1" folder in "Files" filemanager
And I open "Folder 1" folder from "Files" filemanager
And I create "SubFolder 1" folder in "Files" filemanager
When I open "Files" folder from "Files" filemanager
Then I should see "Folder 1"
And I open "Folder 1" folder from "Files" filemanager
And I should see "SubFolder 1"
And I press "Save and return to course"
@@ -0,0 +1,67 @@
@core @core_filepicker @repository @repository_user @_file_upload
Feature: Create shortcuts
In order to automatically synchronize copies of the file with the source
As a teacher
I need to be able to pick file as a shortcut
@javascript @_bug_phantomjs
Scenario: Upload a file as a copy and as a shortcut in filemanager
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Terry | Teacher | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| private_files | System | 1 | my-index | side-post |
When I log in as "teacher1"
And I follow "Manage private files"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
Then I should see "empty.txt" in the "div.fp-content" "css_element"
And I press "Save changes"
And I add a folder activity to course "Course 1" section "1"
And I set the following fields to these values:
| Name | Test folder |
| Description | Test folder description |
And I add "empty.txt" file from "Private files" to "Files" filemanager
And I should see "1" elements in "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-file" "css_element"
And ".fp-content .fp-file.fp-isreference" "css_element" should not exist
And I add "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_ref.txt |
| Link to the file | 1 |
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
And I press "Save and display"
And I should see "empty.txt"
And I should see "empty_ref.txt"
And I press "Edit"
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
# ------ Overwriting the reference with a non-reference ---------
And I add and overwrite "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_ref.txt |
And I should see "2" elements in "Files" filemanager
And ".fp-content .fp-file.fp-isreference" "css_element" should not exist
And I press "Save changes"
And I should see "empty.txt"
And I should see "empty_ref.txt"
And I press "Edit"
And I should see "2" elements in "Files" filemanager
And ".fp-content .fp-file.fp-isreference" "css_element" should not exist
# ------ Overwriting non-reference with a reference ---------
And I add and overwrite "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_ref.txt |
| Link to the file | 1 |
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
And I press "Save changes"
And I should see "empty.txt"
And I should see "empty_ref.txt"
And I press "Edit"
And I should see "2" elements in "Files" filemanager
And I should see "empty_ref.txt" in the ".fp-content .fp-file.fp-isreference" "css_element"
+131
View File
@@ -0,0 +1,131 @@
@core @core_filepicker @_file_upload
Feature: Delete files and folders from the file manager
In order to clean the file manager contents
As a user
I need to delete files from file areas
Background:
Given the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| private_files | System | 1 | my-index | side-post |
@javascript @_bug_phantomjs
Scenario: Delete a file and a folder
Given I log in as "admin"
And I follow "Manage private files..."
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I create "Delete me" folder in "Files" filemanager
And I press "Save changes"
And I follow "Manage private files..."
When I delete "empty.txt" from "Files" filemanager
And I press "Save changes"
And I follow "Manage private files..."
Then I should not see "empty.txt" in the "Manage private files" "dialogue"
And I delete "Delete me" from "Files" filemanager
And I press "Save changes"
And I follow "Manage private files..."
And I should not see "Delete me" in the "Manage private files" "dialogue"
@javascript
Scenario: Delete a file and a folder using bulk functionality (individually)
Given I log in as "admin"
And I follow "Manage private files..."
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I create "Delete me later" folder in "Files" filemanager
And I press "Save changes"
And I follow "Manage private files..."
And I click on "Display folder with file details" "link"
And I set the field "Select file 'empty.txt'" to "1"
When I click on "Delete" "link"
Then I should see "Are you sure you want to delete the selected 1 file(s)?"
When I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "empty.txt" in the "Manage private files" "dialogue"
But I should see "Delete me later" in the "Manage private files" "dialogue"
When I press "Save changes"
And I follow "Manage private files..."
Then I should not see "empty.txt" in the "Manage private files" "dialogue"
But I should see "Delete me later" in the "Manage private files" "dialogue"
And I set the field "Select file 'Delete me later'" to "1"
And I click on "Delete" "link"
And I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Delete me later" in the "Manage private files" "dialogue"
When I press "Save changes"
And I follow "Manage private files..."
Then I should not see "Delete me later" in the "Manage private files" "dialogue"
@javascript
Scenario: Delete a file and a folder using bulk functionality (multiple)
Given I log in as "admin"
And I follow "Manage private files..."
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I create "Delete me" folder in "Files" filemanager
And I create "Do not delete me" folder in "Files" filemanager
And I press "Save changes"
And I follow "Manage private files..."
And I click on "Display folder with file details" "link"
And I set the field "Select file 'empty.txt'" to "1"
And I set the field "Select file 'Delete me'" to "1"
When I click on "Delete" "link"
Then I should see "Are you sure you want to delete the selected 2 file(s)?"
When I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Delete me" in the "Manage private files" "dialogue"
And I should not see "empty.txt" in the "Manage private files" "dialogue"
But I should see "Do not delete me" in the "Manage private files" "dialogue"
When I press "Save changes"
And I follow "Manage private files..."
Then I should not see "Delete me" in the "Manage private files" "dialogue"
And I should not see "empty.txt" in the "Manage private files" "dialogue"
And I am on homepage
Then I should not see "Delete me" in the "Private files" "block"
And I should not see "empty.txt" in the "Private files" "block"
But I should see "Do not delete me" in the "Private files" "block"
@javascript
Scenario: Delete files using the select all checkbox
Given I log in as "admin"
And I follow "Manage private files..."
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I create "Delete me" folder in "Files" filemanager
And I create "Delete me too" folder in "Files" filemanager
And I press "Save changes"
And I follow "Manage private files..."
And I click on "Display folder with file details" "link"
When I click on "Select all/none" "checkbox"
Then the following fields match these values:
| Select file 'empty.txt' | 1 |
| Select file 'Delete me' | 1 |
| Select file 'Delete me too' | 1 |
When I click on "Delete" "link"
Then I should see "Are you sure you want to delete the selected 3 file(s)?"
When I click on "Yes" "button" in the "Confirm" "dialogue"
Then I should not see "Delete me" in the "Manage private files" "dialogue"
And I should not see "empty.txt" in the "Manage private files" "dialogue"
And I should not see "Delete me too" in the "Manage private files" "dialogue"
When I press "Save changes"
And I follow "Manage private files..."
Then I should not see "Delete me" in the "Manage private files" "dialogue"
And I should not see "empty.txt" in the "Manage private files" "dialogue"
And I am on homepage
Then I should not see "Delete me" in the "Private files" "block"
And I should not see "empty.txt" in the "Private files" "block"
And I should not see "Delete me too" in the "Private files" "block"
@javascript
Scenario: Verify system logs for deleted draft files
Given I log in as "admin"
And I follow "Manage private files..."
And I upload "lib/tests/fixtures/update_validator/zips/multidir.zip" file to "Files" filemanager
And I follow "multidir.zip"
And I click on "Unzip" "button"
And I click on "Display folder with file details" "link"
And I set the field "Select file 'one'" to "1"
And I click on "Delete" "link"
And I should see "Are you sure you want to delete the selected 1 file(s)?"
And I click on "Yes" "button" in the "Confirm" "dialogue"
And I click on "Save changes" "button"
And I am on the "System logs report" page
And I click on "Get these logs" "button"
And I should see "The user with id '2' has deleted folder '/one/.' from the draft file area with item id" in the "has deleted folder '/one/.'" "table_row"
And I should see "Size: 0 bytes. Content hash:" in the "has deleted folder '/one/.'" "table_row"
Then I should see "The user with id '2' has deleted file '/one/version.php' from the draft file area with item id" in the "has deleted file '/one/version.php'" "table_row"
And I should see "Size: 40 bytes. Content hash:" in the "has deleted file '/one/version.php'" "table_row"
+56
View File
@@ -0,0 +1,56 @@
@core @core_filepicker @_file_upload
Feature: Edit file feature
In order to edit a file
As a user
I need to be able to select the file in filemanager and modify the file information
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| private_files | System | 1 | my-index | side-post |
And I log in as "admin"
@javascript
Scenario: Select file from "Files" filemanager using "icons" view and edit the name
Given I follow "Manage private files"
And I click on "Display folder with file icons" "link" in the ".filemanager" "css_element"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-file" "css_element"
And I click on "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]/descendant::a[normalize-space(.)='empty.txt']" "xpath_element"
And I should see "Edit empty.txt"
And I set the following fields to these values:
| Name | empty_edited.txt |
When I click on "Update" "button"
Then I should see "empty_edited.txt" in the ".fp-content .fp-file" "css_element"
And I should not see "empty.txt" in the ".fp-content .fp-file" "css_element"
@javascript
Scenario: Select file from "Files" filemanager using "list" view and edit the name
Given I follow "Manage private files"
And I click on "Display folder with file details" "link" in the ".filemanager" "css_element"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-filename" "css_element"
And I click on "//span[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename-icon ')]/descendant::a[normalize-space(.)='empty.txt']" "xpath_element"
And I should see "Edit empty.txt"
And I set the following fields to these values:
| Name | empty_edited.txt |
When I click on "Update" "button"
Then I should see "empty_edited.txt" in the ".fp-content .fp-filename" "css_element"
And I should not see "empty.txt" in the ".fp-content .fp-filename" "css_element"
@javascript
Scenario: Select file from "Files" filemanager using "tree" view and edit the name
Given I follow "Manage private files"
And I click on "Display folder as file tree" "link" in the ".filemanager" "css_element"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-hascontextmenu .fp-filename" "css_element"
And I click on "//span[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename-icon ')]/descendant::a[normalize-space(.)='empty.txt']" "xpath_element"
And I should see "Edit empty.txt"
And I set the following fields to these values:
| Name | empty_edited.txt |
When I click on "Update" "button"
Then I should see "empty_edited.txt" in the ".fp-content .fp-hascontextmenu .fp-filename" "css_element"
And I should not see "empty.txt" in the ".fp-content .fp-hascontextmenu .fp-filename" "css_element"
@@ -0,0 +1,51 @@
@core @core_filepicker @_file_upload
Feature: Overwrite file feature
In order to update an existing file
As a user
I need to pick the file with the same name and select to overwrite
@javascript @_bug_phantomjs
Scenario: Upload a file in filemanager and overwrite it
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Terry | Teacher | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| private_files | System | 1 | my-index | side-post |
When I log in as "teacher1"
And I follow "Manage private files"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
Then I should see "1" elements in "Files" filemanager
And I upload and overwrite "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I should see "1" elements in "Files" filemanager
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager as:
| Save as | empty_copy.txt |
And I should see "2" elements in "Files" filemanager
And I upload and overwrite "lib/tests/fixtures/empty.txt" file to "Files" filemanager as:
| Save as | empty_copy.txt |
And I should see "2" elements in "Files" filemanager
And I press "Save changes"
And I am on "Course 1" course homepage with editing mode on
And I add a folder activity to course "Course 1" section "1"
And I set the following fields to these values:
| Name | Test folder |
| Description | Test folder description |
And I add "empty.txt" file from "Private files" to "Files" filemanager
And I should see "1" elements in "Files" filemanager
And I add and overwrite "empty.txt" file from "Private files" to "Files" filemanager
And I should see "1" elements in "Files" filemanager
And I add "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_copy.txt |
And I should see "2" elements in "Files" filemanager
And I add and overwrite "empty.txt" file from "Private files" to "Files" filemanager as:
| Save as | empty_copy.txt |
And I should see "2" elements in "Files" filemanager
And I press "Save and display"
And I should see "empty.txt"
And I should see "empty_copy.txt"
@@ -0,0 +1,59 @@
@core @core_filepicker @_file_upload
Feature: Select file feature
In order to add a file to a filearea
As a user
I need to be able to select the file using the file picker
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| private_files | System | 1 | my-index | side-post |
And the following "activities" exist:
| activity | course | name |
| folder | C1 | Test folder |
And I am on the "Test folder" "folder activity" page logged in as admin
And I press "Edit"
And I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager
And I press "Save changes"
@javascript
Scenario: Select a file from the "Recent files" repository using "icons" view
Given I follow "Dashboard"
And I follow "Manage private files"
And I click on "Add..." "button" in the "Files" "form_row"
And I click on "Recent files" "link" in the ".fp-repo-area" "css_element"
And I click on "Display folder with file icons" "link" in the ".file-picker" "css_element"
And I click on "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')][normalize-space(.)='empty.txt']" "xpath_element"
And I should see "Select empty.txt"
When I click on "Select this file" "button"
Then I should see "1" elements in "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-file" "css_element"
@javascript
Scenario: Select a file from the "Recent files" repository using "list" view
Given I follow "Dashboard"
And I follow "Manage private files"
And I click on "Add..." "button" in the "Files" "form_row"
And I click on "Recent files" "link" in the ".fp-repo-area" "css_element"
And I click on "Display folder with file details" "link" in the ".file-picker" "css_element"
And I click on "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]/descendant::span[normalize-space(.)='empty.txt']/ancestor::a" "xpath_element"
And I should see "Select empty.txt"
When I click on "Select this file" "button"
Then I should see "1" elements in "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-file" "css_element"
@javascript
Scenario: Select a file from the "Recent files" repository using "tree" view
Given I follow "Dashboard"
And I follow "Manage private files"
And I click on "Add..." "button" in the "Files" "form_row"
And I click on "Recent files" "link" in the ".fp-repo-area" "css_element"
And I click on "Display folder as file tree" "link" in the ".file-picker" "css_element"
And I click on "//div[contains(concat(' ', normalize-space(@class), ' '), ' file-picker ')]/descendant::span[normalize-space(.)='empty.txt']/ancestor::a" "xpath_element"
And I should see "Select empty.txt"
When I click on "Select this file" "button"
Then I should see "1" elements in "Files" filemanager
And I should see "empty.txt" in the ".fp-content .fp-file" "css_element"
@@ -0,0 +1,29 @@
@core @core_filepicker
Feature: Zip folders and unzip compressed files
In order to download or add contents to file areas easily
As a user
I need to zip and unzip folders and files
@javascript @_bug_phantomjs
Scenario: Zip and unzip folders and files
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And I log in as "admin"
And I am on "Course 1" course homepage with editing mode on
And I add a folder activity to course "Course 1" section "0"
And I set the following fields to these values:
| Name | Folder resource |
| Description | The description |
And I create "Folder 1" folder in "Files" filemanager
And I open "Folder 1" folder from "Files" filemanager
And I create "SubFolder 1" folder in "Files" filemanager
And I open "Files" folder from "Files" filemanager
And I zip "Folder 1" folder from "Files" filemanager
And I delete "Folder 1" from "Files" filemanager
When I unzip "Folder 1.zip" file from "Files" filemanager
And I delete "Folder 1.zip" from "Files" filemanager
Then I should see "Folder 1"
And I open "Folder 1" folder from "Files" filemanager
And I should see "SubFolder 1"
And I press "Save and return to course"
+205
View File
@@ -0,0 +1,205 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Repository generator tests
*
* @package core_repository
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_repository;
use repository_exception;
/**
* Repository generator tests class
*
* @package repository
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_test extends \advanced_testcase {
/**
* Basic test of creation of repository types.
*
* @return void
*/
public function test_create_type(): void {
global $DB;
$this->resetAfterTest(true);
// All the repository types.
$all = array('coursefiles', 'dropbox', 'equella', 'filesystem', 'flickr',
'flickr_public', 'googledocs', 'local', 'nextcloud', 'merlot', 'recent', 's3', 'upload', 'url',
'user', 'webdav', 'wikimedia', 'youtube');
// The ones enabled during installation.
$alreadyenabled = array('local', 'recent', 'upload', 'url', 'user', 'wikimedia');
// Enable all the repositories which are not enabled yet.
foreach ($all as $type) {
if (in_array($type, $alreadyenabled)) {
continue;
}
$repotype = $this->getDataGenerator()->create_repository_type($type);
$this->assertEquals($repotype->type, $type, 'Unexpected name after creating repository type ' . $type);
$this->assertTrue($DB->record_exists('repository', array('type' => $type, 'visible' => 1)));
}
// Check that all the repositories have been enabled.
foreach ($all as $type) {
$caughtexception = false;
try {
$this->getDataGenerator()->create_repository_type($type);
} catch (repository_exception $e) {
if ($e->getMessage() === 'This repository already exists') {
$caughtexception = true;
}
}
$this->assertTrue($caughtexception, "Repository type '$type' should have already been enabled");
}
}
/**
* Ensure that the type options are properly saved.
*
* @return void
*/
public function test_create_type_custom_options(): void {
global $DB;
$this->resetAfterTest(true);
// Single instances.
// Note: for single instances repositories enablecourseinstances and enableuserinstances are forced set to 0.
$record = new \stdClass();
$record->pluginname = 'Custom Flickr';
$record->api_key = '12345';
$record->secret = '67890';
$flickr = $this->getDataGenerator()->create_repository_type('flickr', $record);
$config = get_config('flickr');
$record->enableuserinstances = '0';
$record->enablecourseinstances = '0';
$this->assertEquals($record, $config);
$this->assertEquals('Custom Flickr',
$DB->get_field('repository_instances', 'name', array('typeid' => $flickr->id), MUST_EXIST));
// Create a dropbox oauth issuer.
$this->setAdminUser();
$params = [
'name' => 'Dropbox',
'clientid' => 'key',
'clientsecret' => 'secret',
'loginparamsoffline' => 'token_access_type=offline',
'image' => '',
'showonloginpage' => 1,
];
$issuer = \core\oauth2\api::create_issuer((object)$params);
$record = new \stdClass();
$record->pluginname = 'Custom Dropbox';
$record->dropbox_issuerid = $issuer->get('id');
$record->dropbox_cachelimit = '123';
$dropbox = $this->getDataGenerator()->create_repository_type('dropbox', $record);
$config = get_config('dropbox');
$record->enableuserinstances = '0';
$record->enablecourseinstances = '0';
$this->assertEquals($record, $config);
$this->assertEquals('Custom Dropbox',
$DB->get_field('repository_instances', 'name', array('typeid' => $dropbox->id), MUST_EXIST));
// Multiple instances.
$record = new \stdClass();
$record->pluginname = 'Custom WebDAV';
$record->enableuserinstances = '0';
$record->enablecourseinstances = '0';
$webdav = $this->getDataGenerator()->create_repository_type('webdav', $record);
$config = get_config('webdav');
$this->assertEquals($record, $config);
$this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $webdav->id)));
$record = new \stdClass();
$record->pluginname = 'Custom Equella';
$record->enableuserinstances = '1';
$record->enablecourseinstances = '0';
$equella = $this->getDataGenerator()->create_repository_type('equella', $record);
$config = get_config('equella');
$this->assertEquals($record, $config);
$this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $equella->id)));
}
/**
* Covers basic testing of instance creation.
*
* @return void
*/
public function test_create_instance(): void {
global $DB;
$this->resetAfterTest(true);
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$block = $this->getDataGenerator()->create_block('online_users');
$type = $this->getDataGenerator()->create_repository_type('webdav');
$record = new \stdClass();
$record->name = 'A WebDAV instance';
$record->webdav_type = '1';
$record->webdav_server = 'localhost';
$record->webdav_port = '12345';
$record->webdav_path = '/nothing';
$record->webdav_user = 'me';
$record->webdav_password = '\o/';
$record->webdav_auth = 'basic';
$instance = $this->getDataGenerator()->create_repository('webdav', $record);
$this->assertEquals(1, $DB->count_records('repository_instances', array('typeid' => $type->id)));
$this->assertEquals($record->name, $DB->get_field('repository_instances', 'name', array('id' => $instance->id)));
$entries = $DB->get_records('repository_instance_config', array('instanceid' => $instance->id));
$config = new \stdClass();
foreach ($entries as $entry) {
$config->{$entry->name} = $entry->value;
}
unset($record->name);
$this->assertEquals($config, $record);
// Course context.
$record = new \stdClass();
$record->contextid = \context_course::instance($course->id)->id;
$instance = $this->getDataGenerator()->create_repository('webdav', $record);
$this->assertEquals(2, $DB->count_records('repository_instances', array('typeid' => $type->id)));
$this->assertEquals($record->contextid, $instance->contextid);
// User context.
$record->contextid = \context_user::instance($user->id)->id;
$instance = $this->getDataGenerator()->create_repository('webdav', $record);
$this->assertEquals(3, $DB->count_records('repository_instances', array('typeid' => $type->id)));
$this->assertEquals($record->contextid, $instance->contextid);
// Invalid context.
$this->expectException('coding_exception');
$record->contextid = \context_block::instance($block->id)->id;
$instance = $this->getDataGenerator()->create_repository('webdav', $record);
}
}
+286
View File
@@ -0,0 +1,286 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the core_repository implementation of the privacy API.
*
* @package core_repository
* @category test
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_repository\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\collection;
use core_privacy\local\request\writer;
use core_privacy\local\request\approved_contextlist;
use core_repository\privacy\provider;
use core_privacy\local\request\approved_userlist;
/**
* Unit tests for the core_repository implementation of the privacy API.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends \core_privacy\tests\provider_testcase {
/**
* Overriding setUp() function to always reset after tests.
*/
public function setUp(): void {
$this->resetAfterTest(true);
}
/**
* Test for provider::get_contexts_for_userid().
*/
public function test_get_contexts_for_userid(): void {
// Test setup.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Test the User's retrieved contextlist is empty because no repository_instances have added for the User yet.
$contextlist = provider::get_contexts_for_userid($user->id);
$contexts = $contextlist->get_contexts();
$this->assertCount(0, $contexts);
// Create 3 repository_instances records for the User.
$this->setup_test_scenario_data($user->id, 3);
// Test the User's retrieved contextlist contains only one context.
$contextlist = provider::get_contexts_for_userid($user->id);
$contexts = $contextlist->get_contexts();
$this->assertCount(1, $contexts);
// Test the User's contexts equal the User's own context.
$context = reset($contexts);
$this->assertEquals(CONTEXT_USER, $context->contextlevel);
$this->assertEquals($user->id, $context->instanceid);
}
/**
* Test for provider::export_user_data().
*/
public function test_export_user_data(): void {
// Test setup.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Create 3 repository_instances records for the User.
$this->setup_test_scenario_data($user->id, 3);
// Test the User's retrieved contextlist contains only one context.
$contextlist = provider::get_contexts_for_userid($user->id);
$contexts = $contextlist->get_contexts();
$this->assertCount(1, $contexts);
// Test the User's contexts equal the User's own context.
$context = reset($contexts);
$this->assertEquals(CONTEXT_USER, $context->contextlevel);
$this->assertEquals($user->id, $context->instanceid);
// Retrieve repository_instances data only for this user.
$approvedcontextlist = new approved_contextlist($user, 'core_repository', $contextlist->get_contextids());
provider::export_user_data($approvedcontextlist);
// Test the repository_instances data is exported at the User context level.
$user = $approvedcontextlist->get_user();
$contextuser = \context_user::instance($user->id);
$writer = writer::with_context($contextuser);
$this->assertTrue($writer->has_any_data());
}
/**
* Test for provider::delete_data_for_all_users_in_context().
*/
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
// Test setup.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Create 3 repository_instances records for the User.
$this->setup_test_scenario_data($user->id, 3);
// Test the User's retrieved contextlist contains only one context.
$contextlist = provider::get_contexts_for_userid($user->id);
$contexts = $contextlist->get_contexts();
$this->assertCount(1, $contexts);
// Test the User's contexts equal the User's own context.
$context = reset($contexts);
$this->assertEquals(CONTEXT_USER, $context->contextlevel);
// Delete all the User's records in mdl_repository_instances table by the specified User context.
provider::delete_data_for_all_users_in_context($context);
// Test the cohort roles records in mdl_repository_instances table is equals zero.
$repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]);
$this->assertCount(0, $repositoryinstances);
}
/**
* Test for provider::delete_data_for_user().
*/
public function test_delete_data_for_user(): void {
global $DB;
// Test setup.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Create 3 repository_instances records for the User.
$this->setup_test_scenario_data($user->id, 3);
// Test the User's retrieved contextlist contains only one context.
$contextlist = provider::get_contexts_for_userid($user->id);
$contexts = $contextlist->get_contexts();
$this->assertCount(1, $contexts);
// Test the User's contexts equal the User's own context.
$context = reset($contexts);
$this->assertEquals(CONTEXT_USER, $context->contextlevel);
// Delete all the User's records in mdl_repository_instances table by the specified User approved context list.
$approvedcontextlist = new approved_contextlist($user, 'repository_instances', $contextlist->get_contextids());
provider::delete_data_for_user($approvedcontextlist);
// Test the cohort roles records in mdl_repository_instances table is equals zero.
$repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]);
$this->assertCount(0, $repositoryinstances);
}
/**
* Test that only users with a user context are fetched.
*/
public function test_get_users_in_context(): void {
$this->resetAfterTest();
$component = 'core_repository';
// Create a user.
$user = $this->getDataGenerator()->create_user();
$usercontext = \context_user::instance($user->id);
$userlist = new \core_privacy\local\request\userlist($usercontext, $component);
// The list of users should not return anything yet (related data still haven't been created).
provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist);
// Create 3 repository_instances records for user.
$this->setup_test_scenario_data($user->id, 3);
// The list of users for user context should return the user.
provider::get_users_in_context($userlist);
$this->assertCount(1, $userlist);
$expected = [$user->id];
$actual = $userlist->get_userids();
$this->assertEquals($expected, $actual);
// The list of users for system context should not return any users.
$systemcontext = \context_system::instance();
$userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist);
}
/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users(): void {
$this->resetAfterTest();
$component = 'core_repository';
// Create user1.
$user1 = $this->getDataGenerator()->create_user();
$this->setUser($user1);
$usercontext1 = \context_user::instance($user1->id);
// Create list of users with a related user data in usercontext1.
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
// Create a user2.
$user2 = $this->getDataGenerator()->create_user();
$this->setUser($user2);
$usercontext2 = \context_user::instance($user2->id);
// Create list of users with a related user data in usercontext2.
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
// Create repository_instances record for user1.
$this->setup_test_scenario_data($user1->id, 1);
// Create repository_instances record for user2.
$this->setup_test_scenario_data($user2->id, 1);
// Ensure the user list for usercontext1 contains user1.
provider::get_users_in_context($userlist1);
$this->assertCount(1, $userlist1);
// Ensure the user list for usercontext2 contains user2.
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
// Convert $userlist1 into an approved_contextlist.
$approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist);
// Re-fetch users in the usercontext1 - The user list should now be empty.
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
// Re-fetch users in the usercontext2 - The user list should not be empty.
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
// User data should be only removed in the user context.
$systemcontext = \context_system::instance();
// Add userlist2 to the approved user list in the system context.
$approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
// Delete user1 data using delete_data_for_user.
provider::delete_data_for_users($approvedlist);
// Re-fetch users in usercontext2 - The user list should not be empty (user2).
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
}
/**
* Helper function to setup repository_instances records for testing a specific user.
*
* @param int $userid The Id of the User used for testing.
* @param int $noscenarios The number of repository_instance records to create for the User.
*/
private function setup_test_scenario_data($userid, $noscenarios) {
global $DB;
for ($i = 0; $i < $noscenarios; $i++) {
$repositoryinstance = (object)[
'typeid' => ($i + 1),
'name' => 'My Test Repo',
'userid' => $userid,
'contextid' => 1,
'username' => 'some username',
'password' => 'some password',
'timecreated' => date('u'),
'timemodified' => date('u'),
'readonly' => 0
];
$DB->insert_record('repository_instances', $repositoryinstance);
}
}
}
+644
View File
@@ -0,0 +1,644 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_repository;
use repository;
use repository_exception;
use repository_type;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("$CFG->dirroot/repository/lib.php");
/**
* Repository API unit tests
*
* @package core_repository
* @category test
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repositorylib_test extends \advanced_testcase {
/**
* Installing repository tests
*
* @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
*/
public function test_install_repository(): void {
global $CFG, $DB;
$this->resetAfterTest(true);
$syscontext = \context_system::instance();
$repositorypluginname = 'dropbox';
// override repository permission
$capability = 'repository/' . $repositorypluginname . ':view';
$guestroleid = $DB->get_field('role', 'id', array('shortname' => 'guest'));
assign_capability($capability, CAP_ALLOW, $guestroleid, $syscontext->id, true);
$plugintype = new repository_type($repositorypluginname);
$pluginid = $plugintype->create(false);
$this->assertIsInt($pluginid);
$args = array();
$args['type'] = $repositorypluginname;
$repos = repository::get_instances($args);
$repository = reset($repos);
$this->assertInstanceOf('repository', $repository);
$info = $repository->get_meta();
$this->assertEquals($repositorypluginname, $info->type);
}
public function test_get_unused_filename(): void {
global $USER;
$this->resetAfterTest(true);
$this->setAdminUser();
$fs = get_file_storage();
$draftitemid = null;
$context = \context_user::instance($USER->id);
file_prepare_draft_area($draftitemid, $context->id, 'phpunit', 'test_get_unused_filename', 1);
$dummy = array(
'contextid' => $context->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $draftitemid,
'filepath' => '/',
'filename' => ''
);
// Create some files.
$existingfiles = array(
'test',
'test.txt',
'test (1).txt',
'test1.txt',
'test1 (1).txt',
'test1 (2).txt',
'test1 (3).txt',
'test1 (My name is Bob).txt',
'test2 (555).txt',
'test3 (1000).txt',
'test3 (1000MB).txt',
);
foreach ($existingfiles as $filename) {
$dummy['filename'] = $filename;
$file = $fs->create_file_from_string($dummy, 'blah! ' . $filename);
$this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
}
// Actual testing.
$this->assertEquals('free.txt', repository::get_unused_filename($draftitemid, '/', 'free.txt'));
$this->assertEquals('test (1)', repository::get_unused_filename($draftitemid, '/', 'test'));
$this->assertEquals('test (2).txt', repository::get_unused_filename($draftitemid, '/', 'test.txt'));
$this->assertEquals('test1 (4).txt', repository::get_unused_filename($draftitemid, '/', 'test1.txt'));
$this->assertEquals('test1 (8).txt', repository::get_unused_filename($draftitemid, '/', 'test1 (8).txt'));
$this->assertEquals('test1 ().txt', repository::get_unused_filename($draftitemid, '/', 'test1 ().txt'));
$this->assertEquals('test2 (556).txt', repository::get_unused_filename($draftitemid, '/', 'test2 (555).txt'));
$this->assertEquals('test3 (1001).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000).txt'));
$this->assertEquals('test3 (1000MB) (1).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000MB).txt'));
$this->assertEquals('test4 (1).txt', repository::get_unused_filename($draftitemid, '/', 'test4 (1).txt'));
}
public function test_draftfile_exists(): void {
global $USER;
$this->resetAfterTest(true);
$this->setAdminUser();
$fs = get_file_storage();
$draftitemid = file_get_unused_draft_itemid();
$context = \context_user::instance($USER->id);
$dummy = array(
'contextid' => $context->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $draftitemid,
'filepath' => '/',
'filename' => ''
);
// Create some files.
$existingfiles = array(
'The Matrix.movie',
'Astalavista.txt',
'foobar',
);
foreach ($existingfiles as $filename) {
$dummy['filename'] = $filename;
$file = $fs->create_file_from_string($dummy, 'Content of ' . $filename);
$this->assertInstanceOf('stored_file', $file);
}
// Doing the text.
foreach ($existingfiles as $filename) {
$this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
}
foreach (array('Terminator.movie', 'Where is Wally?', 'barfoo') as $filename) {
$this->assertFalse(repository::draftfile_exists($draftitemid, '/', $filename));
}
}
public function test_delete_selected_files(): void {
global $USER;
$this->resetAfterTest(true);
$this->setAdminUser();
$fs = get_file_storage();
$draftitemid = file_get_unused_draft_itemid();
$context = \context_user::instance($USER->id);
$dummy = [
'contextid' => $context->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $draftitemid,
'filepath' => '/',
'filename' => ''
];
// Create some files.
$existingfiles = [
'The Matrix.movie',
'Astalavista.txt',
'foobar',
];
$selectedfiles = [
'The Matrix.movie' => [],
'Astalavista.txt' => []
];
foreach ($existingfiles as $filename) {
$dummy['filename'] = $filename;
$file = $fs->create_file_from_string($dummy, 'Content of ' . $filename);
if (array_key_exists($filename, $selectedfiles)) {
$selectedfiles[$filename] = (object)[
'filename' => $filename,
'filepath' => $file->get_filepath()
];
}
}
// Get area files with default options.
$areafiles = $fs->get_area_files($context->id, 'user', 'draft', $draftitemid);
// Should be the 3 files we added plus the folder.
$this->assertEquals(4, count($areafiles));
repository_delete_selected_files($context, 'user', 'draft', $draftitemid, $selectedfiles);
$areafiles = $fs->get_area_files($context->id, 'user', 'draft', $draftitemid);
// Should be the 1 file left plus the folder.
$this->assertEquals(2, count($areafiles));
}
public function test_can_be_edited_by_user(): void {
$this->resetAfterTest(true);
$syscontext = \context_system::instance();
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
$roleid = create_role('A role', 'arole', 'A role', '');
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Instance on a site level.
$this->getDataGenerator()->create_repository_type('flickr_public');
$repoid = $this->getDataGenerator()->create_repository('flickr_public')->id;
$systemrepo = repository::get_repository_by_id($repoid, $syscontext);
role_assign($roleid, $user->id, $syscontext->id);
assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext, true);
assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
accesslib_clear_all_caches_for_unit_testing();
$this->assertTrue($systemrepo->can_be_edited_by_user());
assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $syscontext, true);
assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
accesslib_clear_all_caches_for_unit_testing();
$this->assertFalse($systemrepo->can_be_edited_by_user());
assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
accesslib_clear_all_caches_for_unit_testing();
$this->assertFalse($systemrepo->can_be_edited_by_user());
role_unassign($roleid, $user->id, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Instance on a course level.
$this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
$params = array('contextid' => $coursecontext->id);
$repoid = $this->getDataGenerator()->create_repository('flickr_public', $params)->id;
$courserepo = repository::get_repository_by_id($repoid, $coursecontext);
assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $coursecontext, true);
accesslib_clear_all_caches_for_unit_testing();
$this->assertTrue($courserepo->can_be_edited_by_user());
assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
accesslib_clear_all_caches_for_unit_testing();
$this->assertFalse($courserepo->can_be_edited_by_user());
assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
accesslib_clear_all_caches_for_unit_testing();
$this->assertFalse($courserepo->can_be_edited_by_user());
role_unassign($roleid, $user->id, $coursecontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Instance on a user level.
$otheruser = $this->getDataGenerator()->create_user();
$otherusercontext = \context_user::instance($otheruser->id);
role_assign($roleid, $user->id, $syscontext->id);
assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
accesslib_clear_all_caches_for_unit_testing();
// Editing someone else's instance.
$record = array('contextid' => $otherusercontext->id);
$repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
$userrepo = repository::get_repository_by_id($repoid, $syscontext);
$this->assertFalse($userrepo->can_be_edited_by_user());
// Editing my own instance.
$usercontext = \context_user::instance($user->id);
$record = array('contextid' => $usercontext->id);
$repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
$userrepo = repository::get_repository_by_id($repoid, $syscontext);
$this->assertTrue($userrepo->can_be_edited_by_user());
}
public function test_check_capability(): void {
$this->resetAfterTest(true);
$syscontext = \context_system::instance();
$course1 = $this->getDataGenerator()->create_course();
$course1context = \context_course::instance($course1->id);
$course2 = $this->getDataGenerator()->create_course();
$course2context = \context_course::instance($course2->id);
$forumdata = new \stdClass();
$forumdata->course = $course1->id;
$forumc1 = $this->getDataGenerator()->create_module('forum', $forumdata);
$forumc1context = \context_module::instance($forumc1->cmid);
$forumdata->course = $course2->id;
$forumc2 = $this->getDataGenerator()->create_module('forum', $forumdata);
$forumc2context = \context_module::instance($forumc2->cmid);
$blockdata = new \stdClass();
$blockdata->parentcontextid = $course1context->id;
$blockc1 = $this->getDataGenerator()->create_block('online_users', $blockdata);
$blockc1context = \context_block::instance($blockc1->id);
$blockdata->parentcontextid = $course2context->id;
$blockc2 = $this->getDataGenerator()->create_block('online_users', $blockdata);
$blockc2context = \context_block::instance($blockc2->id);
$user1 = $this->getDataGenerator()->create_user();
$user1context = \context_user::instance($user1->id);
$user2 = $this->getDataGenerator()->create_user();
$user2context = \context_user::instance($user2->id);
// New role prohibiting Flickr Public access.
$roleid = create_role('No Flickr Public', 'noflickrpublic', 'No Flickr Public', '');
assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $syscontext, true);
// Disallow system access to Flickr Public to user 2.
role_assign($roleid, $user2->id, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Enable repositories.
$this->getDataGenerator()->create_repository_type('flickr_public');
$this->getDataGenerator()->create_repository_type('dropbox');
// Instance on a site level.
$repoid = $this->getDataGenerator()->create_repository('flickr_public')->id;
$systemrepo = repository::get_repository_by_id($repoid, $syscontext);
// Check that everyone with right capability can view a site-wide repository.
$this->setUser($user1);
$this->assertTrue($systemrepo->check_capability());
// Without the capability, we cannot view a site-wide repository.
$this->setUser($user2);
$caughtexception = false;
try {
$systemrepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Instance on a course level.
$record = new \stdClass();
$record->contextid = $course1context->id;
$courserepoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
// Within the course, I can view the repository.
$courserepo = repository::get_repository_by_id($courserepoid, $course1context);
$this->setUser($user1);
$this->assertTrue($courserepo->check_capability());
// But not without the capability.
$this->setUser($user2);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// From another course I cannot, with or without the capability.
$courserepo = repository::get_repository_by_id($courserepoid, $course2context);
$this->setUser($user1);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
$this->setUser($user2);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// From a module within the course, I can view the repository.
$courserepo = repository::get_repository_by_id($courserepoid, $forumc1context);
$this->setUser($user1);
$this->assertTrue($courserepo->check_capability());
// But not without the capability.
$this->setUser($user2);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// From a module in the wrong course, I cannot view the repository.
$courserepo = repository::get_repository_by_id($courserepoid, $forumc2context);
$this->setUser($user1);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// From a block within the course, I can view the repository.
$courserepo = repository::get_repository_by_id($courserepoid, $blockc1context);
$this->setUser($user1);
$this->assertTrue($courserepo->check_capability());
// But not without the capability.
$this->setUser($user2);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// From a block in the wrong course, I cannot view the repository.
$courserepo = repository::get_repository_by_id($courserepoid, $blockc2context);
$this->setUser($user1);
$caughtexception = false;
try {
$courserepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Instance on a user level.
// Instance on a course level.
$record = new \stdClass();
$record->contextid = $user1context->id;
$user1repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
$record->contextid = $user2context->id;
$user2repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
// Check that a user can see its own repository.
$userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
$this->setUser($user1);
$this->assertTrue($userrepo->check_capability());
// But not without the capability.
$userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
$this->setUser($user2);
$caughtexception = false;
try {
$userrepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Check that a user cannot see someone's repository.
$userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
$this->setUser($user1);
$caughtexception = false;
try {
$userrepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Make sure the repo from user 2 was accessible.
role_unassign($roleid, $user2->id, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();
$this->setUser($user2);
$this->assertTrue($userrepo->check_capability());
role_assign($roleid, $user2->id, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();
// Check that a user can view SOME repositories when logged in as someone else.
$params = new \stdClass();
$params->name = 'Dropbox';
$params->dropbox_issuerid = '2';
$privaterepoid = $this->getDataGenerator()->create_repository('dropbox')->id;
$notprivaterepoid = $this->getDataGenerator()->create_repository('upload')->id;
$privaterepo = repository::get_repository_by_id($privaterepoid, $syscontext);
$notprivaterepo = repository::get_repository_by_id($notprivaterepoid, $syscontext);
$userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
$this->setAdminUser();
\core\session\manager::loginas($user1->id, $syscontext);
// Logged in as, I cannot view a user instance.
$caughtexception = false;
try {
$userrepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Logged in as, I cannot view a private instance.
$caughtexception = false;
try {
$privaterepo->check_capability();
} catch (repository_exception $e) {
$caughtexception = true;
}
$this->assertTrue($caughtexception);
// Logged in as, I can view a non-private instance.
$this->assertTrue($notprivaterepo->check_capability());
}
function test_delete_all_for_context(): void {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->create_repository_type('flickr_public');
$this->getDataGenerator()->create_repository_type('filesystem');
$coursecontext = \context_course::instance($course->id);
$usercontext = \context_user::instance($user->id);
// Creating course instances.
$repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
$courserepo1 = repository::get_repository_by_id($repo->id, $coursecontext);
$this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
$repo = $this->getDataGenerator()->create_repository('filesystem', array('contextid' => $coursecontext->id));
$courserepo2 = repository::get_repository_by_id($repo->id, $coursecontext);
$this->assertEquals(2, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
// Creating user instances.
$repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
$userrepo1 = repository::get_repository_by_id($repo->id, $usercontext);
$this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
$repo = $this->getDataGenerator()->create_repository('filesystem', array('contextid' => $usercontext->id));
$userrepo2 = repository::get_repository_by_id($repo->id, $usercontext);
$this->assertEquals(2, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
// Simulation of course deletion.
repository::delete_all_for_context($coursecontext->id);
$this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
$this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $courserepo1->id)));
$this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $courserepo2->id)));
$this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $courserepo1->id)));
$this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $courserepo2->id)));
// Simulation of user deletion.
repository::delete_all_for_context($usercontext->id);
$this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
$this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $userrepo1->id)));
$this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $userrepo2->id)));
$this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $userrepo1->id)));
$this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $userrepo2->id)));
// Checking deletion upon course context deletion.
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
$repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
$courserepo = repository::get_repository_by_id($repo->id, $coursecontext);
$this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
$coursecontext->delete();
$this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
// Checking deletion upon user context deletion.
$user = $this->getDataGenerator()->create_user();
$usercontext = \context_user::instance($user->id);
$repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
$userrepo = repository::get_repository_by_id($repo->id, $usercontext);
$this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
$usercontext->delete();
$this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
// Checking deletion upon course deletion.
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
$repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
$courserepo = repository::get_repository_by_id($repo->id, $coursecontext);
$this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
delete_course($course, false);
$this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
// Checking deletion upon user deletion.
$user = $this->getDataGenerator()->create_user();
$usercontext = \context_user::instance($user->id);
$repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
$userrepo = repository::get_repository_by_id($repo->id, $usercontext);
$this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
delete_user($user);
$this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
}
/**
* Create test file in user private files
*
* @param string $filepath file path
* @param string $filename file name
*/
private function create_user_private_file(string $filepath, string $filename): void {
global $USER;
$filerecord = [];
$filerecord['contextid'] = \context_user::instance($USER->id)->id;
$filerecord['component'] = 'user';
$filerecord['filearea'] = 'private';
$filerecord['itemid'] = 0;
$filerecord['filepath'] = $filepath;
$filerecord['filename'] = $filename;
$filerecord['userid'] = $USER->id;
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, hash("md5", $filepath . $filename));
}
public function test_listing_and_filter(): void {
$this->resetAfterTest(true);
$this->setUser($this->getDataGenerator()->create_user());
$repoid = $this->getDataGenerator()->create_repository('user')->id;
$this->create_user_private_file('/', 'image1.jpg');
$this->create_user_private_file('/', 'file1.txt');
$this->create_user_private_file('/folder/', 'image2.jpg');
$this->create_user_private_file('/folder/', 'file2.txt');
$this->create_user_private_file('/ftexts/', 'file3.txt');
// Listing without filters returns 4 records (2 files and 2 directories).
$repo = repository::get_repository_by_id($repoid, \context_system::instance());
$this->assertCount(4, $repo->get_listing()['list']);
// Listing with filters returns 3 records (1 files and 2 directories).
$_POST['accepted_types'] = ['.jpg'];
$this->assertCount(3, $repo->get_listing()['list']);
}
}