Commit 9b5e61ac authored by Frederic Massart's avatar Frederic Massart
Browse files

MDL-39477 repository: Introducing data generators for repositories

parent 73f560c7
......@@ -555,6 +555,37 @@ EOD;
return groups_assign_grouping($record['groupingid'], $record['groupid']);
}
/**
* Create an instance of a repository.
*
* @param string type of repository to create an instance for.
* @param array|stdClass $record data to use to up set the instance.
* @param array $options options
* @return stdClass repository instance record
* @since 2.6
*/
public function create_repository($type, $record=null, array $options = null) {
global $CFG;
$generator = $this->get_plugin_generator('repository_'.$type);
return $generator->create_instance($record, $options);
}
/**
* Create an instance of a repository.
*
* @param string type of repository to create an instance for.
* @param array|stdClass $record data to use to up set the instance.
* @param array $options options
* @return repository_type object
* @since 2.6
*/
public function create_repository_type($type, $record=null, array $options = null) {
global $CFG;
$generator = $this->get_plugin_generator('repository_'.$type);
return $generator->create_type($record, $options);
}
/**
* Create a test scale
* @param array|stdClass $record
......
......@@ -29,4 +29,5 @@ require_once(__DIR__.'/data_generator.php');
require_once(__DIR__.'/component_generator_base.php');
require_once(__DIR__.'/module_generator.php');
require_once(__DIR__.'/block_generator.php');
require_once(__DIR__.'/repository_generator.php');
<?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 data generator
*
* @package repository
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Repository data generator class
*
* @package core
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 2.6
*/
class testing_repository_generator extends component_generator_base {
/**
* Number of instances created
* @var int
*/
protected $instancecount = 0;
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->instancecount = 0;
}
/**
* Returns repository type name
*
* @return string name of the type of repository
* @throws coding_exception if class invalid
*/
public function get_typename() {
$matches = null;
if (!preg_match('/^repository_([a-z0-9_]+)_generator$/', get_class($this), $matches)) {
throw new coding_exception('Invalid repository generator class name: '.get_class($this));
}
if (empty($matches[1])) {
throw new coding_exception('Invalid repository generator class name: '.get_class($this));
}
return $matches[1];
}
/**
* Fill in record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_record(array $record) {
if (!isset($record['name'])) {
$record['name'] = $this->get_typename() . ' ' . $this->instancecount;
}
if (!isset($record['contextid'])) {
$record['contextid'] = context_system::instance()->id;
}
return $record;
}
/**
* Fill in type record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_type_record(array $record) {
if (!isset($record['pluginname'])) {
$record['pluginname'] = '';
}
if (!isset($record['enableuserinstances'])) {
$record['enableuserinstances'] = 1;
}
if (!isset($record['enablecourseinstances'])) {
$record['enablecourseinstances'] = 1;
}
return $record;
}
/**
* Create a test repository instance.
*
* @param array|stdClass $record
* @param array $options
* @return stdClass repository instance record
*/
public function create_instance($record = null, array $options = null) {
global $CFG, $DB;
require_once($CFG->dirroot . '/repository/lib.php');
$this->instancecount++;
$record = (array) $record;
$typeid = $DB->get_field('repository', 'id', array('type' => $this->get_typename()), MUST_EXIST);
$instanceoptions = repository::static_function($this->get_typename(), 'get_instance_option_names');
if (empty($instanceoptions)) {
// There can only be one instance of this repository, and it should have been created along with the type.
$id = $DB->get_field('repository_instances', 'id', array('typeid' => $typeid), MUST_EXIST);
} else {
// Create the new instance, but first make sure all the required parameters are set.
$record = $this->prepare_record($record);
if (empty($record['contextid'])) {
throw new coding_exception('contextid must be present in testing_repository_generator::create_instance() $record');
}
foreach ($instanceoptions as $option) {
if (!isset($record[$option])) {
throw new coding_exception("$option must be present in testing_repository_generator::create_instance() \$record");
}
}
$context = context::instance_by_id($record['contextid']);
unset($record['contextid']);
if (!in_array($context->contextlevel, array(CONTEXT_SYSTEM, CONTEXT_COURSE, CONTEXT_USER))) {
throw new coding_exception('Wrong contextid passed in testing_repository_generator::create_instance() $record');
}
$id = repository::static_function($this->get_typename(), 'create', $this->get_typename(), 0, $context, $record);
}
return $DB->get_record('repository_instances', array('id' => $id), '*', MUST_EXIST);
}
/**
* Create the type of repository.
*
* @param stdClass|array $record data to use to set up the type
* @param array $options options for the set up of the type
*
* @return stdClass repository type record
*/
public function create_type($record = null, array $options = null) {
global $CFG, $DB;
require_once($CFG->dirroot . '/repository/lib.php');
$record = (array) $record;
$type = $this->get_typename();
$typeoptions = repository::static_function($type, 'get_type_option_names');
$instanceoptions = repository::static_function($type, 'get_instance_option_names');
// The type allow for user and course instances.
if (!empty($instanceoptions)) {
$typeoptions[] = 'enableuserinstances';
$typeoptions[] = 'enablecourseinstances';
}
// Make sure all the parameters are set.
$record = $this->prepare_type_record($record);
foreach ($typeoptions as $option) {
if (!isset($record[$option])) {
throw new coding_exception("$option must be present in testing::create_repository_type() $record");
}
}
// Limit to allowed options.
$record = array_intersect_key($record, array_flip($typeoptions));
// Create the type.
$plugintype = new repository_type($type, $record);
$plugintype->create(false);
return $DB->get_record('repository', array('type' => $type));
}
}
<?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/>.
/**
* Alfresco repository data generator
*
* @package repository_alfresco
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Alfresco repository data generator class
*
* @package repository_alfresco
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_alfresco_generator extends testing_repository_generator {
/**
* Fill in record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_record(array $record) {
$record = parent::prepare_record($record);
if (!isset($record['alfresco_url'])) {
$record['alfresco_url'] = 'http://no.where.com/alfresco/api';
}
return $record;
}
}
<?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/>.
/**
* Box.net repository data generator
*
* @package repository_boxnet
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Box.net repository data generator class
*
* @package repository_boxnet
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_boxnet_generator extends testing_repository_generator {
/**
* Fill in type record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_type_record(array $record) {
$record = parent::prepare_type_record($record);
if (!isset($record['api_key'])) {
$record['api_key'] = 'api_key';
}
return $record;
}
}
<?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/>.
/**
* Course files repository data generator
*
* @package repository_coursefiles
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Course files repository data generator class
*
* @package repository_coursefiles
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_coursefiles_generator extends testing_repository_generator {
}
<?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/>.
/**
* Dropbox repository data generator
*
* @package repository_dropbox
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Dropbox repository data generator class
*
* @package repository_dropbox
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_dropbox_generator extends testing_repository_generator {
/**
* Fill in type record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_type_record(array $record) {
$record = parent::prepare_type_record($record);
if (!isset($record['dropbox_key'])) {
$record['dropbox_key'] = 'key';
}
if (!isset($record['dropbox_secret'])) {
$record['dropbox_secret'] = 'secret';
}
if (!isset($record['dropbox_cachelimit'])) {
$record['dropbox_cachelimit'] = 0;
}
return $record;
}
}
<?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/>.
/**
* Equella repository data generator
*
* @package repository_equella
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Equella repository data generator class
*
* @package repository_equella
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_equella_generator extends testing_repository_generator {
/**
* Fill in record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_record(array $record) {
$record = parent::prepare_record($record);
if (!isset($record['equella_url'])) {
$record['equella_url'] = 'http://dummy.url.com';
}
if (!isset($record['equella_select_restriction'])) {
$record['equella_select_restriction'] = 'none';
}
if (!isset($record['equella_options'])) {
$record['equella_options'] = '';
}
if (!isset($record['equella_shareid'])) {
$record['equella_shareid'] = 'id';
}
if (!isset($record['equella_sharesecret'])) {
$record['equella_url'] = 'secret';
}
return $record;
}
}
<?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/>.
/**
* File system repository data generator
*
* @package repository_filesystem
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* File system repository data generator class
*
* @package repository_filesystem
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_filesystem_generator extends testing_repository_generator {
/**
* Fill in record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_record(array $record) {
$record = parent::prepare_record($record);
if (!isset($record['fs_path'])) {
$record['fs_path'] = '/i/do/not/exist';
}
return $record;
}
}
<?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/>.
/**
* Flickr repository data generator
*
* @package repository_flickr
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Flickr repository data generator class
*
* @package repository_flickr
* @category test
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_flickr_generator extends testing_repository_generator {
/**
* Fill in type record defaults.
*
* @param array $record
* @return array
*/
protected function prepare_type_record(array $record) {
$record = parent::prepare_type_record($record);
if (!isset($record['api_key'])) {
$record['api_key'] = 'api_key';
}
if (!isset($record['secret'])) {
$record['secret'] = 'secret';
}
return $record;
}
}
<?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.
//