Merge branch 'MDL-61566-33' of git://github.com/andrewnicols/moodle into MOODLE_33_STABLE
[moodle.git] / notes / tests / generator / lib.php
bloba280a7161d05b00bd5328e9204c0ba63fea09563
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.
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/>.
17 /**
18 * core_notes data generator.
20 * @package core_notes
21 * @category test
22 * @copyright 2013 Ankit Agarwal
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * core_notes data generator class.
31 * @package core_notes
32 * @category test
33 * @copyright 2013 Ankit Agarwal
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_notes_generator extends component_generator_base {
38 /**
39 * @var number of created instances
41 protected $instancecount = 0;
43 /**
44 * To be called from data reset code only,
45 * do not use in tests.
46 * @return void
48 public function reset() {
49 $this->instancecount = 0;
52 /**
53 * Create a new note.
55 * @param array|stdClass $record
56 * @throws coding_exception
57 * @return stdClass activity record with extra cmid field
59 public function create_instance($record = null) {
60 global $CFG, $USER;
61 require_once("$CFG->dirroot/notes/lib.php");
63 $this->instancecount++;
64 $i = $this->instancecount;
65 $record = (object)(array)$record;
67 if (empty($record->courseid)) {
68 throw new coding_exception('Module generator requires $record->courseid.');
70 if (empty($record->userid)) {
71 throw new coding_exception('Module generator requires $record->userid.');
73 if (!isset($record->module)) {
74 $record->module = 'notes';
76 if (!isset($record->groupid)) {
77 $record->groupid = 0;
79 if (!isset($record->moduleid)) {
80 $record->moduleid = 0;
82 if (!isset($record->coursemoduleid)) {
83 $record->coursemoduleid = 0;
85 if (!isset($record->subject)) {
86 $record->subject = '';
88 if (!isset($record->summary)) {
89 $record->summary = null;
91 if (!isset($record->content)) {
92 $record->content = "This is test generated note - $i .";
94 if (!isset($record->uniquehash)) {
95 $record->uniquehash = '';
97 if (!isset($record->rating)) {
98 $record->rating = 0;
100 if (!isset($record->format)) {
101 $record->format = FORMAT_PLAIN;
103 if (!isset($record->summaryformat)) {
104 $record->summaryformat = FORMAT_MOODLE;
106 if (!isset($record->attachment)) {
107 $record->attachment = null;
109 if (!isset($record->publishstate)) {
110 $record->publishstate = NOTES_STATE_SITE;
112 if (!isset($record->lastmodified)) {
113 $record->lastmodified = time();
115 if (!isset($record->created)) {
116 $record->created = time();
118 if (!isset($record->usermodified)) {
119 $record->usermodified = $USER->id;
122 note_save($record);
123 return $record;