MDL_13766 Refactoring of core repository code and added first unit tests

This commit is contained in:
nicolasconnault
2008-11-26 07:03:10 +00:00
parent bcc234b0a7
commit dbc0194458
19 changed files with 776 additions and 718 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ function mnet_get_functions($type, $parentname) {
$relname = '/repository/' . $parentname . '/'. $docname;
$filename = $CFG->dirroot . $relname;
require_once($CFG->dirroot . '/repository/lib.php');
$publishes = (array)repository_static_function($parentname, 'mnet_publishes');
$publishes = (array)repository::static_function($parentname, 'mnet_publishes');
} else {
// auth or enrol
$relname = '/'.$type.'/'.$parentname.'/'.$docname;
+10 -10
View File
@@ -39,7 +39,7 @@ $return = true;
if (!empty($edit) || !empty($new)) {
if (!empty($edit)) {
$repositorytype = repository_get_type_by_typename($edit);
$repositorytype = repository::get_type_by_typename($edit);
$classname = 'repository_' . $repositorytype->get_typename();
$configs = call_user_func(array($classname,'get_type_option_names'));
$plugin = $repositorytype->get_typename();
@@ -56,7 +56,7 @@ if (!empty($edit) || !empty($new)) {
//detect if we create a new type without config (in this case if don't want to display a setting page during creation)
$createnewtype = false;
if (!empty($new)) {
$adminconfignames = repository_static_function($new, 'get_type_option_names');
$adminconfignames = repository::static_function($new, 'get_type_option_names');
$createnewtype = empty($adminconfignames);
}
// end setup, begin output
@@ -72,7 +72,7 @@ if (!empty($edit) || !empty($new)) {
foreach($configs as $config) {
$settings[$config] = $fromform->$config;
}
$instanceoptionnames = repository_static_function($edit, 'get_instance_option_names');
$instanceoptionnames = repository::static_function($edit, 'get_instance_option_names');
if (!empty($instanceoptionnames)) {
if (array_key_exists('enablecourseinstances', $fromform)) {
$settings['enablecourseinstances'] = $fromform->enablecourseinstances;
@@ -108,8 +108,8 @@ if (!empty($edit) || !empty($new)) {
print_heading(get_string('configplugin', 'repository_'.$plugin));
$displaysettingform = true;
if ($edit) {
$typeoptionnames = repository_static_function($edit, 'get_type_option_names');
$instanceoptionnames = repository_static_function($edit, 'get_instance_option_names');
$typeoptionnames = repository::static_function($edit, 'get_type_option_names');
$instanceoptionnames = repository::static_function($edit, 'get_instance_option_names');
if (empty($typeoptionnames) && empty($instanceoptionnames)) {
$displaysettingform = false;
}
@@ -123,9 +123,9 @@ if (!empty($edit) || !empty($new)) {
//display instances list and creation form
if ($edit){
$instanceoptionnames = repository_static_function($edit, 'get_instance_option_names');
$instanceoptionnames = repository::static_function($edit, 'get_instance_option_names');
if (!empty($instanceoptionnames)){
repository_display_instances_list(get_context_instance(CONTEXT_SYSTEM), $edit);
repository::display_instances_list(get_context_instance(CONTEXT_SYSTEM), $edit);
}
}
@@ -134,12 +134,12 @@ if (!empty($edit) || !empty($new)) {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
$repositorytype = repository_get_type_by_typename($hide);
$repositorytype = repository::get_type_by_typename($hide);
$repositorytype->switch_and_update_visibility();
$return = true;
} else if (!empty($delete)) {
admin_externalpage_print_header();
$repositorytype = repository_get_type_by_typename($delete);
$repositorytype = repository::get_type_by_typename($delete);
if ($sure) {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
@@ -157,7 +157,7 @@ if (!empty($edit) || !empty($new)) {
$return = false;
}
else if (!empty($move) && !empty($type)) {
$repositorytype = repository_get_type_by_typename($type);
$repositorytype = repository::get_type_by_typename($type);
$repositorytype->move_order($move);
}
+8 -8
View File
@@ -42,8 +42,8 @@ $return = true;
if (!empty($edit) || !empty($new)) {
if (!empty($edit)) {
$instance = repository_get_instance($edit);
$instancetype = repository_get_type_by_id($instance->typeid);
$instance = repository::get_instance($edit);
$instancetype = repository::get_type_by_id($instance->typeid);
$classname = 'repository_' . $instancetype->get_typename();
$configs = $instance->get_instance_option_names();
$plugin = $instancetype->get_typename();
@@ -75,7 +75,7 @@ if (!empty($edit) || !empty($new)) {
}
$success = $instance->set_option($settings);
} else {
$success = repository_static_function($plugin, 'create', $plugin, 0, get_system_context(), $fromform);
$success = repository::static_function($plugin, 'create', $plugin, 0, get_system_context(), $fromform);
$data = data_submitted();
}
if ($success) {
@@ -99,14 +99,14 @@ if (!empty($edit) || !empty($new)) {
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', '', $baseurl);
}
$instance = repository_get_type_by_typename($hide);
$instance = repository::get_type_by_typename($hide);
var_dump($instance);
var_dump($hide);
$instance->hide();
$return = true;
} else if (!empty($delete)) {
} else if (!empty($delete)) {
admin_externalpage_print_header();
$instance = repository_get_instance($delete);
$instance = repository::get_instance($delete);
//if you try to delete an instance set as readonly, display an error message
if ($instance->readonly) {
throw new repository_exception('readonlyinstance', 'repository');
@@ -129,7 +129,7 @@ if (!empty($edit) || !empty($new)) {
}
if (!empty($return)) {
redirect($baseurl);
}
admin_externalpage_print_footer();
admin_externalpage_print_footer();
+3 -3
View File
@@ -228,12 +228,12 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext))
$ADMIN->add('repositorysettings', new admin_externalpage('repositoryinstanceedit',
get_string('editrepositoryinstance', 'repository'), $url, 'moodle/site:config', true),
'', $url);
foreach (repository_get_types()
foreach (repository::get_types()
as $repositorytype)
{
//display setup page for plugins with: general options or multiple instances (e.g. has instance config)
$typeoptionnames = repository_static_function($repositorytype->get_typename(), 'get_type_option_names');
$instanceoptionnames = repository_static_function($repositorytype->get_typename(), 'get_instance_option_names');
$typeoptionnames = repository::static_function($repositorytype->get_typename(), 'get_type_option_names');
$instanceoptionnames = repository::static_function($repositorytype->get_typename(), 'get_instance_option_names');
if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
$ADMIN->add('repositorysettings',
new admin_externalpage('repositorysettings'.$repositorytype->get_typename(),
+1 -1
View File
@@ -188,7 +188,7 @@ class block_admin extends block_list {
/// Repository Instances
require_once($CFG->dirroot.'/repository/lib.php');
$editabletypes = repository_get_editable_types($context);
$editabletypes = repository::get_editable_types($context);
if ($course->id !== SITEID && has_capability('moodle/course:update', $context) && !empty($editabletypes)) {
$this->content->items[]='<a href="'.$CFG->wwwroot.'/repository/manage_instances.php?contextid='.$context->id.'">'.get_string('repositories').'</a>';
$this->content->icons[]='<img src="'.$CFG->pixpath.'/i/repository.png" alt=""/>';
+4 -4
View File
@@ -6012,7 +6012,7 @@ class admin_setting_managerepository extends admin_setting {
return true;
}
}
foreach (repository_get_types() as $instance) {
foreach (repository::get_types() as $instance) {
$title = $instance->get_typename();
if (strpos($textlib->strtolower($title), $query) !== false) {
return true;
@@ -6030,7 +6030,7 @@ class admin_setting_managerepository extends admin_setting {
$hiddenstr = get_string('hiddenshow', 'repository');
$deletestr = get_string('delete');
$plugins = get_list_of_plugins('repository');
$instances = repository_get_types();
$instances = repository::get_types();
$instancesnumber = count($instances);
$alreadyplugins = array();
$table = new StdClass;
@@ -6041,8 +6041,8 @@ class admin_setting_managerepository extends admin_setting {
foreach ($instances as $i) {
$settings = '';
//display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
$typeoptionnames = repository_static_function($i->get_typename(), 'get_type_option_names');
$instanceoptionnames = repository_static_function($i->get_typename(), 'get_instance_option_names');
$typeoptionnames = repository::static_function($i->get_typename(), 'get_type_option_names');
$instanceoptionnames = repository::static_function($i->get_typename(), 'get_instance_option_names');
if ( !empty($typeoptionnames) || !empty($instanceoptionnames)) {
$settings .= '<a href="' . $this->baseurl . '&amp;edit=' . $i->get_typename() . '">'
. $settingsstr .'</a>' . "\n";
+67
View File
@@ -0,0 +1,67 @@
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// This program 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: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* Unit tests for ../repositorylib.php.
*
* @author [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->dirroot . '/admin/generator.php');
// Generate a mock class for each plugin subclass present
$repository_plugins = get_list_of_plugins('repository');
foreach ($repository_plugins as $plugin) {
require_once($CFG->dirroot . "/repository/$plugin/repository.class.php");
Mock::generatePartial("repository_$plugin", "partialmock_$plugin", array('send_package'));
}
class repositorylib_test extends MoodleUnitTestCase {
function setup() {
parent::setup();
}
public function test_plugins() {
$plugins = get_list_of_plugins('repository');
foreach ($plugins as $plugin) {
// Instantiate a fake plugin instance
$plugin_class = "partialmock_$plugin";
$plugin = new $plugin_class(&$this);
// add common plugin tests here
}
}
}
?>
+2 -2
View File
@@ -271,7 +271,7 @@ class repository_flickr extends repository {
$mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
//retrieve the flickr instances
$instances = repository_get_instances(array(),null,false,"flickr");
$instances = repository::get_instances(array(),null,false,"flickr");
if (empty($instances)) {
$callbackurl = get_string("callbackwarning","repository_flickr");
$mform->addElement('static', null, '', $callbackurl);
@@ -280,7 +280,7 @@ class repository_flickr extends repository {
$callbackurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&amp;repo_id='.$instances[0]->id;
$mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
}
$mform->addRule('api_key', $strrequired, 'required', null, 'client');
$mform->addRule('secret', $strrequired, 'required', null, 'client');
}
@@ -335,8 +335,8 @@ class repository_flickr_public extends repository {
*/
public static function plugin_init() {
//here we create a default instance for this type
repository_static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null));
}
repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null),1);
}
}
+10 -1
View File
@@ -90,6 +90,15 @@ function repository_get_client($context, $filetypes = '*', $returnvalue = '*') {
.fp-grid .label{height:48px;text-align:center}
.fp-grid span{color:gray}
</style>
<!--[if IE 6]>
<style type="text/css">
/* Fix for IE6 */
.yui-skin-sam .yui-panel .hd{
}
</style>
<![endif]-->
EOD;
$js = <<<EOD
@@ -966,7 +975,7 @@ return _client;
EOD;
$user_context = get_context_instance(CONTEXT_USER, $USER->id);
$repos = repository_get_instances(array($user_context, $context, get_system_context()), null, true, null, $filetypes, $returnvalue);
$repos = repository::get_instances(array($user_context, $context, get_system_context()));
foreach ($repos as $repo) {
$info = $repo->ajax_info();
$js .= "\r\n";
+624 -642
View File
File diff suppressed because it is too large Load Diff
+6 -6
View File
@@ -77,15 +77,15 @@ class repository_local extends repository {
*/
public function get_listing($encodedpath = '', $search = '') {
global $CFG;
try {
return repository_get_user_file_tree($search);
return repository::get_user_file_tree($search);
}
catch (Exception $e) {
throw new repository_exception('emptyfilelist', 'repository_local');
}
}
/**
* Download a file, this function can be overridden by
* subclass.
@@ -121,10 +121,10 @@ class repository_local extends repository {
$fs = get_file_storage();
$sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename);
$contents = $sf->get_content();
$fp = fopen($dir.$file, 'w');
$fp = fopen($dir.$file, 'w');
fwrite($fp,$contents);
fclose($fp);
return $dir.$file;
}
@@ -143,4 +143,4 @@ class repository_local extends repository {
return get_string('repositoryname', 'repository_local');;
}
}
?>
?>
+11 -11
View File
@@ -55,13 +55,13 @@
/// Security: we cannot perform any action if the type is not visible or if the context has been disabled
if (!empty($new)){
$type = repository_get_type_by_typename($new);
$type = repository::get_type_by_typename($new);
} else if (!empty($edit)){
$instance = repository_get_instance($edit);
$type = repository_get_type_by_id($instance->typeid);
$instance = repository::get_instance($edit);
$type = repository::get_type_by_id($instance->typeid);
} else if (!empty($delete)){
$instance = repository_get_instance($delete);
$type = repository_get_type_by_id($instance->typeid);
$instance = repository::get_instance($delete);
$type = repository::get_type_by_id($instance->typeid);
}
if (isset($type) && ( !$type->get_visible() || (!$type->get_contextvisibility($context->contextlevel)) ) ) {
print_error('typenotvisible', 'repository', $baseurl);
@@ -76,7 +76,7 @@
'type' => 'misc');
$navlinks[] = array('name' => $pagename,
'link' => null,
'type' => 'misc');
'type' => 'misc');
$fullname = $course->fullname;
} else {
$fullname = fullname($user);
@@ -106,12 +106,12 @@
if (!empty($edit) || !empty($new)) {
if (!empty($edit)) {
$instance = repository_get_instance($edit);
$instance = repository::get_instance($edit);
//if you try to edit an instance set as readonly, display an error message
if ($instance->readonly) {
throw new repository_exception('readonlyinstance', 'repository');
}
$instancetype = repository_get_type_by_id($instance->typeid);
$instancetype = repository::get_type_by_id($instance->typeid);
$classname = 'repository_' . $instancetype->get_typename();
$configs = $instance->get_instance_option_names();
$plugin = $instancetype->get_typename();
@@ -142,7 +142,7 @@
}
$success = $instance->set_option($settings);
} else {
$success = repository_static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
$success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform);
$data = data_submitted();
}
if ($success) {
@@ -164,7 +164,7 @@
}
} else if (!empty($delete)) {
// admin_externalpage_print_header();
$instance = repository_get_instance($delete);
$instance = repository::get_instance($delete);
//if you try to delete an instance set as readonly, display an error message
if ($instance->readonly) {
throw new repository_exception('readonlyinstance', 'repository');
@@ -185,7 +185,7 @@
notice_yesno(get_string('confirmdelete', 'repository', $instance->name), $baseurl . '&amp;delete=' . $delete . '&amp;sure=yes', $baseurl);
$return = false;
} else {
repository_display_instances_list($context);
repository::display_instances_list($context);
$return = false;
}
+19 -19
View File
@@ -60,13 +60,13 @@ class repository_remotemoodle extends repository {
exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle', $username)));
}
$file = unserialize(base64_decode($source));
$file = unserialize(base64_decode($source));
$contextid = $file[0];
$filearea = $file[1];
$itemid = $file[2];
$filepath = $file[3];
$filename = $file[4];
///check that the user has read permission on this file
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath, $filename);
@@ -100,9 +100,9 @@ class repository_remotemoodle extends repository {
if (empty($USER)) {
exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle', $username)));
}
try {
return repository_get_user_file_tree($search);
return repository::get_user_file_tree($search);
}
catch (Exception $e) {
exit(mnet_server_fault(9016, get_string('failtoretrievelist', 'repository_remotemoodle')));
@@ -125,7 +125,7 @@ class repository_remotemoodle extends repository {
* @param <type> $search_text
* @return <type>
*/
public function search($search_text) {
public function search($search_text) {
return $this->get_listing('', $search_text);
}
@@ -134,7 +134,7 @@ class repository_remotemoodle extends repository {
* @global <type> $MNET
*/
private function ensure_environment() {
global $MNET;
global $MNET;
if (empty($MNET)) {
$MNET = new mnet_environment();
$MNET->init();
@@ -184,7 +184,7 @@ class repository_remotemoodle extends repository {
echo json_encode(array('e'=>get_string('connectionfailure','repository_remotemoodle')));
exit;
}
///connect to the remote moodle and retrieve the list of files
$client->set_method('repository/remotemoodle/repository.class.php/getFileList');
$client->add_param($USER->username);
@@ -195,11 +195,11 @@ class repository_remotemoodle extends repository {
$message =" ";
foreach ($client->error as $errormessage) {
$message .= "ERROR: $errormessage . ";
}
}
echo json_encode(array('e'=>$message)); //display all error messages
exit;
}
$services = $client->response;
///display error message if we could retrieve the list or if nothing were returned
if (empty($services)) {
@@ -210,7 +210,7 @@ class repository_remotemoodle extends repository {
return $services;
}
/**
* Download a file
@@ -224,7 +224,7 @@ class repository_remotemoodle extends repository {
global $CFG, $DB, $USER;
///set mnet environment and set the mnet host
require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
require_once($CFG->dirroot . '/mnet/xmlrpc/client.php');
$this->ensure_environment();
$host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //retrieve the host url
$mnet_peer = new mnet_peer();
@@ -246,7 +246,7 @@ class repository_remotemoodle extends repository {
exit;
}
$services = $client->response; //service contains the file content in the first case of the array,
$services = $client->response; //service contains the file content in the first case of the array,
//and the filename in the second
//the content has been encoded in base64, need to decode it
@@ -271,9 +271,9 @@ class repository_remotemoodle extends repository {
$fp = fopen($dir.$file, 'w');
fwrite($fp,$content);
fclose($fp);
return $dir.$file;
}
/**
@@ -284,7 +284,7 @@ class repository_remotemoodle extends repository {
*/
public function instance_config_form(&$mform) {
global $CFG, $DB;
//retrieve only Moodle peers
$hosts = $DB->get_records_sql(' SELECT
h.id,
@@ -311,13 +311,13 @@ class repository_remotemoodle extends repository {
array($CFG->mnet_localhost_id, 'moodle', 'All Hosts'));
$peers = array();
foreach($hosts as $host) {
$peers[$host->id] = $host->name;
$peers[$host->id] = $host->name;
}
$mform->addElement('select', 'peer', get_string('peer', 'repository_remotemoodle'),$peers);
$mform->addRule('peer', get_string('required'), 'required', null, 'client');
if (empty($peers)) {
$mform->addElement('static', null, '', get_string('nopeer','repository_remotemoodle'));
}
@@ -332,4 +332,4 @@ class repository_remotemoodle extends repository {
return array('peer');
}
}
?>
?>
+2 -2
View File
@@ -26,7 +26,7 @@ class repository_upload extends repository {
parent::__construct($repositoryid, $context, $options);
if($action=='upload'){
$filepath = '/'.uniqid().'/';
$this->info = repository_store_to_filepool('repo_upload_file', 'user_draft', $filepath);
$this->info = repository::store_to_filepool('repo_upload_file', 'user_draft', $filepath);
}
}
@@ -83,7 +83,7 @@ class repository_upload extends repository {
/**
*
* @return <type>
* @return <type>
*/
public function get_name(){
return get_string('repositoryname', 'repository_upload');;
+3 -3
View File
@@ -23,7 +23,7 @@
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
/// Check permissions
if (! (isloggedin() && repository_check_context($ctx_id)) ) {
if (! (isloggedin() && repository::check_context($ctx_id)) ) {
$err = new stdclass;
$err->e = get_string('nopermissiontoaccess', 'repository');
die(json_encode($err));
@@ -36,7 +36,7 @@
switch ($action) {
case 'gsearch': // Global Search
$repos = repository_get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
$repos = repository::get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
$list = array();
foreach($repos as $repo){
if ($repo->global_search()) {
@@ -158,7 +158,7 @@ EOD;
if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) {
echo json_encode(array('url'=>$path, 'id'=>$path, 'file'=>$path));
} else {
$info = repository_move_to_filepool($path, $title, $itemid);
$info = repository::move_to_filepool($path, $title, $itemid);
if ($env == 'form'){
echo json_encode($info);
} else if ($env == 'editor') {
+2 -2
View File
@@ -35,7 +35,7 @@ include('tabs.php');
print_heading($configstr);
print_simple_box_start();
if (!$instances = repository_get_instances($COURSE->context, $USER->id)) {
if (!$instances = repository::get_instances($COURSE->context, $USER->id)) {
print_error('noinstances', 'repository', $CFG->wwwroot . '/user/view.php');
}
@@ -46,7 +46,7 @@ $table->data = array();
foreach ($instances as $i) {
$path = '/repository/'.$i->type.'/settings.php';
$settings = file_exists($CFG->dirroot.$path);
$table->data[] = array($i->name, $i->type,
$table->data[] = array($i->name, $i->type,
$settings ? '<a href="'.$CFG->wwwroot.$path.'">'
.get_string('settings', 'repository').'</a>' : '');
}
+1 -1
View File
@@ -271,7 +271,7 @@
if (!empty($user) and $user->id == $USER->id) {
require_once($CFG->dirroot . '/repository/lib.php');
$usercontext = get_context_instance(CONTEXT_USER,$user->id);
$editabletypes = repository_get_editable_types($usercontext);
$editabletypes = repository::get_editable_types($usercontext);
if (!empty($usercontext) && $usercontext->contextlevel == CONTEXT_USER && !empty($editabletypes)) {
$toprow[] = new tabobject('repositories', $CFG->wwwroot .'/repository/manage_instances.php?contextid='.$usercontext->id, get_string('repositories', 'repository'));
}