MDL-36457 testing Relocating classes
[moodle.git] / lib / testing / generator / module_generator.php
CommitLineData
7e7cfe7a
PS
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 * Module generator base class.
19 *
20 * @package core
f16e1af2 21 * @category testing
7e7cfe7a
PS
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27/**
28 * Module generator base class.
29 *
30 * Extend in mod/xxxx/tests/generator/lib.php as class mod_xxxx_generator.
31 *
32 * @package core
f16e1af2 33 * @category testing
7e7cfe7a
PS
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37abstract class phpunit_module_generator {
38 /** @var phpunit_data_generator@var */
39 protected $datagenerator;
40
41 /** @var number of created instances */
42 protected $instancecount = 0;
43
44 public function __construct(phpunit_data_generator $datagenerator) {
45 $this->datagenerator = $datagenerator;
46 }
47
48 /**
49 * To be called from data reset code only,
50 * do not use in tests.
51 * @return void
52 */
53 public function reset() {
54 $this->instancecount = 0;
55 }
56
57 /**
58 * Returns module name
59 * @return string name of module that this class describes
60 * @throws coding_exception if class invalid
61 */
62 public function get_modulename() {
63 $matches = null;
64 if (!preg_match('/^mod_([a-z0-9]+)_generator$/', get_class($this), $matches)) {
65 throw new coding_exception('Invalid module generator class name: '.get_class($this));
66 }
67
68 if (empty($matches[1])) {
69 throw new coding_exception('Invalid module generator class name: '.get_class($this));
70 }
71 return $matches[1];
72 }
73
74 /**
75 * Create course module and link it to course
76 * @param int $courseid
77 * @param array $options: section, visible
78 * @return int $cm instance id
79 */
80 protected function precreate_course_module($courseid, array $options) {
81 global $DB, $CFG;
82 require_once("$CFG->dirroot/course/lib.php");
83
84 $modulename = $this->get_modulename();
722e6ba9 85 $sectionnum = isset($options['section']) ? $options['section'] : 0;
46453565 86 unset($options['section']); // Prevent confusion, it would be overridden later in course_add_cm_to_section() anyway.
7e7cfe7a
PS
87
88 $cm = new stdClass();
89 $cm->course = $courseid;
90 $cm->module = $DB->get_field('modules', 'id', array('name'=>$modulename));
91 $cm->instance = 0;
722e6ba9 92 $cm->section = 0;
7e7cfe7a
PS
93 $cm->idnumber = isset($options['idnumber']) ? $options['idnumber'] : 0;
94 $cm->added = time();
95
96 $columns = $DB->get_columns('course_modules');
97 foreach ($options as $key=>$value) {
98 if ($key === 'id' or !isset($columns[$key])) {
99 continue;
100 }
101 if (property_exists($cm, $key)) {
102 continue;
103 }
104 $cm->$key = $value;
105 }
106
107 $cm->id = $DB->insert_record('course_modules', $cm);
7e7cfe7a 108
722e6ba9 109 course_add_cm_to_section($courseid, $cm->id, $sectionnum);
7e7cfe7a
PS
110
111 return $cm->id;
112 }
113
114 /**
115 * Called after *_add_instance()
116 * @param int $id
117 * @param int $cmid
118 * @return stdClass module instance
119 */
120 protected function post_add_instance($id, $cmid) {
121 global $DB;
122
123 $DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid));
124
125 $instance = $DB->get_record($this->get_modulename(), array('id'=>$id), '*', MUST_EXIST);
126
127 $cm = get_coursemodule_from_id($this->get_modulename(), $cmid, $instance->course, true, MUST_EXIST);
128 context_module::instance($cm->id);
129
130 $instance->cmid = $cm->id;
131
132 return $instance;
133 }
134
135 /**
136 * Create a test module
137 * @param array|stdClass $record
138 * @param array $options
139 * @return stdClass activity record
140 */
141 abstract public function create_instance($record = null, array $options = null);
142}