Skip to content

Commit

Permalink
MDL_13766 Refactoring of core repository code and added first unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasconnault committed Nov 26, 2008
1 parent bcc234b commit dbc0194
Show file tree
Hide file tree
Showing 19 changed files with 776 additions and 718 deletions.
2 changes: 1 addition & 1 deletion admin/mnet/adminlib.php
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions admin/repository.php
Expand Up @@ -39,7 +39,7 @@

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();
Expand All @@ -56,7 +56,7 @@
//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
Expand All @@ -72,7 +72,7 @@
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;
Expand Down Expand Up @@ -108,8 +108,8 @@
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;
}
Expand All @@ -123,9 +123,9 @@

//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);
}
}

Expand All @@ -134,12 +134,12 @@
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);
Expand All @@ -157,7 +157,7 @@
$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);
}

Expand Down
16 changes: 8 additions & 8 deletions admin/repositoryinstance.php
Expand Up @@ -42,8 +42,8 @@

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();
Expand Down Expand Up @@ -75,7 +75,7 @@
}
$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) {
Expand All @@ -99,14 +99,14 @@
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');
Expand All @@ -129,7 +129,7 @@
}

if (!empty($return)) {

redirect($baseurl);
}
admin_externalpage_print_footer();
admin_externalpage_print_footer();
6 changes: 3 additions & 3 deletions admin/settings/plugins.php
Expand Up @@ -228,12 +228,12 @@
$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(),
Expand Down
2 changes: 1 addition & 1 deletion blocks/admin/block_admin.php
Expand Up @@ -188,7 +188,7 @@ function get_content() {

/// 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=""/>';
Expand Down
8 changes: 4 additions & 4 deletions lib/adminlib.php
Expand Up @@ -6012,7 +6012,7 @@ function is_related($query) {
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;
Expand All @@ -6030,7 +6030,7 @@ function output_html($data, $query='') {
$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;
Expand All @@ -6041,8 +6041,8 @@ function output_html($data, $query='') {
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";
Expand Down
67 changes: 67 additions & 0 deletions lib/simpletest/testrepositorylib.php
@@ -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 nicolasconnault@gmail.com
* @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
}
}
}
?>
4 changes: 2 additions & 2 deletions repository/flickr/repository.class.php
Expand Up @@ -271,7 +271,7 @@ public function type_config_form(&$mform) {
$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);
Expand All @@ -280,7 +280,7 @@ public function type_config_form(&$mform) {
$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');
}
Expand Down
4 changes: 2 additions & 2 deletions repository/flickr_public/repository.class.php
Expand Up @@ -335,8 +335,8 @@ public static function get_type_option_names() {
*/
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);
}
}

11 changes: 10 additions & 1 deletion repository/javascript.php
Expand Up @@ -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
Expand Down Expand Up @@ -966,7 +975,7 @@ function _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";
Expand Down

0 comments on commit dbc0194

Please sign in to comment.