[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/testing/generator/ -> repository_generator.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Repository data generator
  19   *
  20   * @package    repository
  21   * @category   test
  22   * @copyright  2013 Frédéric Massart
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Repository data generator class
  30   *
  31   * @package    core
  32   * @category   test
  33   * @copyright  2013 Frédéric Massart
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since      Moodle 2.5.1
  36   */
  37  class testing_repository_generator extends component_generator_base {
  38  
  39      /**
  40       * Number of instances created
  41       * @var int
  42       */
  43      protected $instancecount = 0;
  44  
  45      /**
  46       * To be called from data reset code only,
  47       * do not use in tests.
  48       * @return void
  49       */
  50      public function reset() {
  51          $this->instancecount = 0;
  52      }
  53  
  54      /**
  55       * Returns repository type name
  56       *
  57       * @return string name of the type of repository
  58       * @throws coding_exception if class invalid
  59       */
  60      public function get_typename() {
  61          $matches = null;
  62          if (!preg_match('/^repository_([a-z0-9_]+)_generator$/', get_class($this), $matches)) {
  63              throw new coding_exception('Invalid repository generator class name: '.get_class($this));
  64          }
  65          if (empty($matches[1])) {
  66              throw new coding_exception('Invalid repository generator class name: '.get_class($this));
  67          }
  68          return $matches[1];
  69      }
  70  
  71      /**
  72       * Fill in record defaults.
  73       *
  74       * @param array $record
  75       * @return array
  76       */
  77      protected function prepare_record(array $record) {
  78          if (!isset($record['name'])) {
  79              $record['name'] = $this->get_typename() . ' ' . $this->instancecount;
  80          }
  81          if (!isset($record['contextid'])) {
  82              $record['contextid'] = context_system::instance()->id;
  83          }
  84          return $record;
  85      }
  86  
  87      /**
  88       * Fill in type record defaults.
  89       *
  90       * @param array $record
  91       * @return array
  92       */
  93      protected function prepare_type_record(array $record) {
  94          if (!isset($record['pluginname'])) {
  95              $record['pluginname'] = '';
  96          }
  97          if (!isset($record['enableuserinstances'])) {
  98              $record['enableuserinstances'] = 1;
  99          }
 100          if (!isset($record['enablecourseinstances'])) {
 101              $record['enablecourseinstances'] = 1;
 102          }
 103          return $record;
 104      }
 105  
 106      /**
 107       * Create a test repository instance.
 108       *
 109       * @param array|stdClass $record
 110       * @param array $options
 111       * @return stdClass repository instance record
 112       */
 113      public function create_instance($record = null, array $options = null) {
 114          global $CFG, $DB;
 115          require_once($CFG->dirroot . '/repository/lib.php');
 116  
 117          $this->instancecount++;
 118          $record = (array) $record;
 119  
 120          $typeid = $DB->get_field('repository', 'id', array('type' => $this->get_typename()), MUST_EXIST);
 121          $instanceoptions = repository::static_function($this->get_typename(), 'get_instance_option_names');
 122  
 123          if (empty($instanceoptions)) {
 124              // There can only be one instance of this repository, and it should have been created along with the type.
 125              $id = $DB->get_field('repository_instances', 'id', array('typeid' => $typeid), MUST_EXIST);
 126          } else {
 127              // Create the new instance, but first make sure all the required parameters are set.
 128              $record = $this->prepare_record($record);
 129  
 130              if (empty($record['contextid'])) {
 131                  throw new coding_exception('contextid must be present in testing_repository_generator::create_instance() $record');
 132              }
 133  
 134              foreach ($instanceoptions as $option) {
 135                  if (!isset($record[$option])) {
 136                      throw new coding_exception("$option must be present in testing_repository_generator::create_instance() \$record");
 137                  }
 138              }
 139  
 140              $context = context::instance_by_id($record['contextid']);
 141              unset($record['contextid']);
 142              if (!in_array($context->contextlevel, array(CONTEXT_SYSTEM, CONTEXT_COURSE, CONTEXT_USER))) {
 143                  throw new coding_exception('Wrong contextid passed in testing_repository_generator::create_instance() $record');
 144              }
 145  
 146              $id = repository::static_function($this->get_typename(), 'create', $this->get_typename(), 0, $context, $record);
 147          }
 148  
 149          return $DB->get_record('repository_instances', array('id' => $id), '*', MUST_EXIST);
 150      }
 151  
 152      /**
 153       * Create the type of repository.
 154       *
 155       * @param stdClass|array $record data to use to set up the type
 156       * @param array $options options for the set up of the type
 157       *
 158       * @return stdClass repository type record
 159       */
 160      public function create_type($record = null, array $options = null) {
 161          global $CFG, $DB;
 162          require_once($CFG->dirroot . '/repository/lib.php');
 163  
 164          $record = (array) $record;
 165          $type = $this->get_typename();
 166  
 167          $typeoptions = repository::static_function($type, 'get_type_option_names');
 168          $instanceoptions = repository::static_function($type, 'get_instance_option_names');
 169  
 170          // The type allow for user and course instances.
 171          if (!empty($instanceoptions)) {
 172              $typeoptions[] = 'enableuserinstances';
 173              $typeoptions[] = 'enablecourseinstances';
 174          }
 175  
 176          // Make sure all the parameters are set.
 177          $record = $this->prepare_type_record($record);
 178          foreach ($typeoptions as $option) {
 179              if (!isset($record[$option])) {
 180                  throw new coding_exception("$option must be present in testing::create_repository_type() for $type");
 181              }
 182          }
 183  
 184          // Limit to allowed options.
 185          $record = array_intersect_key($record, array_flip($typeoptions));
 186  
 187          // Create the type.
 188          $plugintype = new repository_type($type, $record);
 189          $plugintype->create(false);
 190  
 191          return $DB->get_record('repository', array('type' => $type));
 192      }
 193  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1