From 0852bbaed52866fbc35c99071756d9bd882189f9 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 2 May 2013 18:14:58 +0800 Subject: [PATCH] MDL-39477 repository: Introducing data generators for repositories --- lib/testing/generator/data_generator.php | 31 +++ lib/testing/generator/lib.php | 1 + .../generator/repository_generator.php | 191 +++++++++++++++++ repository/alfresco/tests/generator/lib.php | 50 +++++ repository/boxnet/tests/generator/lib.php | 50 +++++ .../coursefiles/tests/generator/lib.php | 35 ++++ repository/dropbox/tests/generator/lib.php | 56 +++++ repository/equella/tests/generator/lib.php | 62 ++++++ repository/filesystem/tests/generator/lib.php | 50 +++++ repository/flickr/tests/generator/lib.php | 53 +++++ .../flickr_public/tests/generator/lib.php | 67 ++++++ repository/googledocs/tests/generator/lib.php | 53 +++++ repository/local/tests/generator/lib.php | 35 ++++ repository/merlot/tests/generator/lib.php | 50 +++++ repository/picasa/tests/generator/lib.php | 53 +++++ repository/recent/tests/generator/lib.php | 50 +++++ repository/s3/tests/generator/lib.php | 53 +++++ repository/tests/generator_test.php | 194 ++++++++++++++++++ repository/upload/tests/generator/lib.php | 35 ++++ repository/url/tests/generator/lib.php | 35 ++++ repository/user/tests/generator/lib.php | 35 ++++ repository/webdav/tests/generator/lib.php | 69 +++++++ repository/wikimedia/tests/generator/lib.php | 35 ++++ repository/youtube/tests/generator/lib.php | 35 ++++ 24 files changed, 1378 insertions(+) create mode 100644 lib/testing/generator/repository_generator.php create mode 100644 repository/alfresco/tests/generator/lib.php create mode 100644 repository/boxnet/tests/generator/lib.php create mode 100644 repository/coursefiles/tests/generator/lib.php create mode 100644 repository/dropbox/tests/generator/lib.php create mode 100644 repository/equella/tests/generator/lib.php create mode 100644 repository/filesystem/tests/generator/lib.php create mode 100644 repository/flickr/tests/generator/lib.php create mode 100644 repository/flickr_public/tests/generator/lib.php create mode 100644 repository/googledocs/tests/generator/lib.php create mode 100644 repository/local/tests/generator/lib.php create mode 100644 repository/merlot/tests/generator/lib.php create mode 100644 repository/picasa/tests/generator/lib.php create mode 100644 repository/recent/tests/generator/lib.php create mode 100644 repository/s3/tests/generator/lib.php create mode 100644 repository/tests/generator_test.php create mode 100644 repository/upload/tests/generator/lib.php create mode 100644 repository/url/tests/generator/lib.php create mode 100644 repository/user/tests/generator/lib.php create mode 100644 repository/webdav/tests/generator/lib.php create mode 100644 repository/wikimedia/tests/generator/lib.php create mode 100644 repository/youtube/tests/generator/lib.php diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 129dfa5996e..a3b61146907 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -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 diff --git a/lib/testing/generator/lib.php b/lib/testing/generator/lib.php index 99c6a9ad49b..7e2b19099ce 100644 --- a/lib/testing/generator/lib.php +++ b/lib/testing/generator/lib.php @@ -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'); diff --git a/lib/testing/generator/repository_generator.php b/lib/testing/generator/repository_generator.php new file mode 100644 index 00000000000..a02fccda5a6 --- /dev/null +++ b/lib/testing/generator/repository_generator.php @@ -0,0 +1,191 @@ +. + +/** + * 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)); + } +} diff --git a/repository/alfresco/tests/generator/lib.php b/repository/alfresco/tests/generator/lib.php new file mode 100644 index 00000000000..78f1c675db8 --- /dev/null +++ b/repository/alfresco/tests/generator/lib.php @@ -0,0 +1,50 @@ +. + +/** + * 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; + } + +} diff --git a/repository/boxnet/tests/generator/lib.php b/repository/boxnet/tests/generator/lib.php new file mode 100644 index 00000000000..82308ed1cf9 --- /dev/null +++ b/repository/boxnet/tests/generator/lib.php @@ -0,0 +1,50 @@ +. + +/** + * 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; + } + +} diff --git a/repository/coursefiles/tests/generator/lib.php b/repository/coursefiles/tests/generator/lib.php new file mode 100644 index 00000000000..4efe67cc35e --- /dev/null +++ b/repository/coursefiles/tests/generator/lib.php @@ -0,0 +1,35 @@ +. + +/** + * 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 { +} diff --git a/repository/dropbox/tests/generator/lib.php b/repository/dropbox/tests/generator/lib.php new file mode 100644 index 00000000000..1f4db858265 --- /dev/null +++ b/repository/dropbox/tests/generator/lib.php @@ -0,0 +1,56 @@ +. + +/** + * 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; + } + +} diff --git a/repository/equella/tests/generator/lib.php b/repository/equella/tests/generator/lib.php new file mode 100644 index 00000000000..eb9cad4bfa5 --- /dev/null +++ b/repository/equella/tests/generator/lib.php @@ -0,0 +1,62 @@ +. + +/** + * 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; + } + +} diff --git a/repository/filesystem/tests/generator/lib.php b/repository/filesystem/tests/generator/lib.php new file mode 100644 index 00000000000..8600686ac02 --- /dev/null +++ b/repository/filesystem/tests/generator/lib.php @@ -0,0 +1,50 @@ +. + +/** + * 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; + } + +} diff --git a/repository/flickr/tests/generator/lib.php b/repository/flickr/tests/generator/lib.php new file mode 100644 index 00000000000..e924e081544 --- /dev/null +++ b/repository/flickr/tests/generator/lib.php @@ -0,0 +1,53 @@ +. + +/** + * 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; + } + +} diff --git a/repository/flickr_public/tests/generator/lib.php b/repository/flickr_public/tests/generator/lib.php new file mode 100644 index 00000000000..82a6a0a37e7 --- /dev/null +++ b/repository/flickr_public/tests/generator/lib.php @@ -0,0 +1,67 @@ +. + +/** + * Flickr Public repository data generator + * + * @package repository_flickr_public + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Flickr Public repository data generator class + * + * @package repository_flickr_public + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_flickr_public_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['email_address'])) { + $record['email_address'] = ''; + } + if (!isset($record['usewatermarks'])) { + $record['usewatermarks'] = 0; + } + return $record; + } + + /** + * 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; + } + +} diff --git a/repository/googledocs/tests/generator/lib.php b/repository/googledocs/tests/generator/lib.php new file mode 100644 index 00000000000..af7f955278f --- /dev/null +++ b/repository/googledocs/tests/generator/lib.php @@ -0,0 +1,53 @@ +. + +/** + * Google Docs repository data generator + * + * @package repository_googledocs + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Google Docs repository data generator class + * + * @package repository_googledocs + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_googledocs_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['clientid'])) { + $record['clientid'] = 'clientid'; + } + if (!isset($record['secret'])) { + $record['secret'] = 'secret'; + } + return $record; + } + +} diff --git a/repository/local/tests/generator/lib.php b/repository/local/tests/generator/lib.php new file mode 100644 index 00000000000..4c1c983311b --- /dev/null +++ b/repository/local/tests/generator/lib.php @@ -0,0 +1,35 @@ +. + +/** + * Local repository data generator + * + * @package repository_local + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Local repository data generator class + * + * @package repository_local + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_local_generator extends testing_repository_generator { +} diff --git a/repository/merlot/tests/generator/lib.php b/repository/merlot/tests/generator/lib.php new file mode 100644 index 00000000000..79ec691fe54 --- /dev/null +++ b/repository/merlot/tests/generator/lib.php @@ -0,0 +1,50 @@ +. + +/** + * Merlot repository data generator + * + * @package repository_merlot + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Merlot repository data generator class + * + * @package repository_merlot + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_merlot_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['licensekey'])) { + $record['licensekey'] = 'licensekey'; + } + return $record; + } + +} diff --git a/repository/picasa/tests/generator/lib.php b/repository/picasa/tests/generator/lib.php new file mode 100644 index 00000000000..c8fe1864a59 --- /dev/null +++ b/repository/picasa/tests/generator/lib.php @@ -0,0 +1,53 @@ +. + +/** + * Picasa repository data generator + * + * @package repository_picasa + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Picasa repository data generator class + * + * @package repository_picasa + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_picasa_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['clientid'])) { + $record['clientid'] = 'clientid'; + } + if (!isset($record['secret'])) { + $record['secret'] = 'secret'; + } + return $record; + } + +} diff --git a/repository/recent/tests/generator/lib.php b/repository/recent/tests/generator/lib.php new file mode 100644 index 00000000000..ffc7e27dfc0 --- /dev/null +++ b/repository/recent/tests/generator/lib.php @@ -0,0 +1,50 @@ +. + +/** + * Recent repository data generator + * + * @package repository_recent + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Recent repository data generator class + * + * @package repository_recent + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_recent_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['recentfilesnumber'])) { + $record['recentfilesnumber'] = ''; + } + return $record; + } + +} diff --git a/repository/s3/tests/generator/lib.php b/repository/s3/tests/generator/lib.php new file mode 100644 index 00000000000..8bbac54359b --- /dev/null +++ b/repository/s3/tests/generator/lib.php @@ -0,0 +1,53 @@ +. + +/** + * Amazon S3 repository data generator + * + * @package repository_s3 + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Amazon S3 repository data generator class + * + * @package repository_s3 + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_s3_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['access_key'])) { + $record['access_key'] = 'access_key'; + } + if (!isset($record['secret_key'])) { + $record['secret_key'] = 'secret_key'; + } + return $record; + } + +} diff --git a/repository/tests/generator_test.php b/repository/tests/generator_test.php new file mode 100644 index 00000000000..9eadefbe71f --- /dev/null +++ b/repository/tests/generator_test.php @@ -0,0 +1,194 @@ +. + +/** + * Repository generator tests + * + * @package repository + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); +global $CFG; + +/** + * 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 repository_generator_testcase extends advanced_testcase { + + /** + * Basic test of creation of repository types. + * + * @return void + */ + public function test_create_type() { + global $DB; + $this->resetAfterTest(true); + + // All the repository types. + $all = array('alfresco', 'boxnet', 'coursefiles', 'dropbox', 'equella', 'filesystem', 'flickr', + 'flickr_public', 'googledocs', 'local', 'merlot', 'picasa', 'recent', 's3', 'upload', 'url', + 'user', 'webdav', 'wikimedia', 'youtube'); + + // The ones enabled during installation. + $alreadyenabled = array('local', 'recent', 'upload', 'url', 'user', 'wikimedia', 'youtube'); + + // 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() { + 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)); + + $record = new stdClass(); + $record->pluginname = 'Custom Dropbox'; + $record->dropbox_key = '12345'; + $record->dropbox_secret = '67890'; + $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() { + 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->setExpectedException('coding_exception'); + $record->contextid = context_block::instance($block->id)->id; + $instance = $this->getDataGenerator()->create_repository('webdav', $record); + } + +} diff --git a/repository/upload/tests/generator/lib.php b/repository/upload/tests/generator/lib.php new file mode 100644 index 00000000000..34ed59fd000 --- /dev/null +++ b/repository/upload/tests/generator/lib.php @@ -0,0 +1,35 @@ +. + +/** + * Upload repository data generator + * + * @package repository_upload + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Upload repository data generator class + * + * @package repository_upload + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_upload_generator extends testing_repository_generator { +} diff --git a/repository/url/tests/generator/lib.php b/repository/url/tests/generator/lib.php new file mode 100644 index 00000000000..1f7ff8710d5 --- /dev/null +++ b/repository/url/tests/generator/lib.php @@ -0,0 +1,35 @@ +. + +/** + * URL repository data generator + * + * @package repository_url + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * URL repository data generator class + * + * @package repository_url + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_url_generator extends testing_repository_generator { +} diff --git a/repository/user/tests/generator/lib.php b/repository/user/tests/generator/lib.php new file mode 100644 index 00000000000..d1d7cdff3f3 --- /dev/null +++ b/repository/user/tests/generator/lib.php @@ -0,0 +1,35 @@ +. + +/** + * User private files repository data generator + * + * @package repository_user + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * User private files repository data generator class + * + * @package repository_user + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_user_generator extends testing_repository_generator { +} diff --git a/repository/webdav/tests/generator/lib.php b/repository/webdav/tests/generator/lib.php new file mode 100644 index 00000000000..a4657c1fb21 --- /dev/null +++ b/repository/webdav/tests/generator/lib.php @@ -0,0 +1,69 @@ +. + +/** + * WebDAV repository data generator + * + * @package repository_webdav + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * WebDAV repository data generator class + * + * @package repository_webdav + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_webdav_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['webdav_type'])) { + $record['webdav_type'] = 0; + } + if (!isset($record['webdav_server'])) { + $record['webdav_server'] = 'webdav.server.local'; + } + if (!isset($record['webdav_port'])) { + $record['webdav_port'] = ''; + } + if (!isset($record['webdav_path'])) { + $record['webdav_path'] = '/'; + } + if (!isset($record['webdav_user'])) { + $record['webdav_user'] = ''; + } + if (!isset($record['webdav_password'])) { + $record['webdav_password'] = ''; + } + if (!isset($record['webdav_auth'])) { + $record['webdav_auth'] = 'none'; + } + return $record; + } + + +} diff --git a/repository/wikimedia/tests/generator/lib.php b/repository/wikimedia/tests/generator/lib.php new file mode 100644 index 00000000000..db1f557ab53 --- /dev/null +++ b/repository/wikimedia/tests/generator/lib.php @@ -0,0 +1,35 @@ +. + +/** + * Wikimedia repository data generator + * + * @package repository_wikimedia + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Wikimedia repository data generator class + * + * @package repository_wikimedia + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_wikimedia_generator extends testing_repository_generator { +} diff --git a/repository/youtube/tests/generator/lib.php b/repository/youtube/tests/generator/lib.php new file mode 100644 index 00000000000..3df0ad07b35 --- /dev/null +++ b/repository/youtube/tests/generator/lib.php @@ -0,0 +1,35 @@ +. + +/** + * Youtube repository data generator + * + * @package repository_youtube + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Youtube repository data generator class + * + * @package repository_youtube + * @category test + * @copyright 2013 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_youtube_generator extends testing_repository_generator { +}