See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 * PHPUnit data generator testcase. 19 * 20 * @package mod_data 21 * @category phpunit 22 * @copyright 2012 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @coversDefaultClass \mod_data_generator 25 */ 26 class mod_data_generator_testcase extends advanced_testcase { 27 /** 28 * @covers ::create_instance 29 */ 30 public function test_generator() { 31 global $DB; 32 33 $this->resetAfterTest(true); 34 35 $this->assertEquals(0, $DB->count_records('data')); 36 37 $course = $this->getDataGenerator()->create_course(); 38 39 /** @var mod_data_generator $generator */ 40 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 41 $this->assertInstanceOf('mod_data_generator', $generator); 42 $this->assertEquals('data', $generator->get_modulename()); 43 44 $generator->create_instance(array('course' => $course->id)); 45 $generator->create_instance(array('course' => $course->id)); 46 $data = $generator->create_instance(array('course' => $course->id)); 47 $this->assertEquals(3, $DB->count_records('data')); 48 49 $cm = get_coursemodule_from_instance('data', $data->id); 50 $this->assertEquals($data->id, $cm->instance); 51 $this->assertEquals('data', $cm->modname); 52 $this->assertEquals($course->id, $cm->course); 53 54 $context = context_module::instance($cm->id); 55 $this->assertEquals($data->cmid, $context->instanceid); 56 57 // test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE! 58 $data = $generator->create_instance(array('course' => $course->id, 'assessed' => 1, 'scale' => 100)); 59 $gitem = $DB->get_record('grade_items', array('courseid' => $course->id, 'itemtype' => 'mod', 60 'itemmodule' => 'data', 'iteminstance' => $data->id)); 61 $this->assertNotEmpty($gitem); 62 $this->assertEquals(100, $gitem->grademax); 63 $this->assertEquals(0, $gitem->grademin); 64 $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype); 65 } 66 67 /** 68 * @covers ::create_field 69 */ 70 public function test_create_field() { 71 global $DB; 72 73 $this->resetAfterTest(true); 74 75 $this->setAdminUser(); 76 $this->assertEquals(0, $DB->count_records('data')); 77 78 $course = $this->getDataGenerator()->create_course(); 79 80 /** @var mod_data_generator $generator */ 81 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 82 $this->assertInstanceOf('mod_data_generator', $generator); 83 $this->assertEquals('data', $generator->get_modulename()); 84 85 $data = $generator->create_instance(array('course' => $course->id)); 86 $this->assertEquals(1, $DB->count_records('data')); 87 88 $cm = get_coursemodule_from_instance('data', $data->id); 89 $this->assertEquals($data->id, $cm->instance); 90 $this->assertEquals('data', $cm->modname); 91 $this->assertEquals($course->id, $cm->course); 92 93 $context = context_module::instance($cm->id); 94 $this->assertEquals($data->cmid, $context->instanceid); 95 96 $fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url'); 97 98 $count = 1; 99 100 // Creating test Fields with default parameter values. 101 foreach ($fieldtypes as $fieldtype) { 102 // Creating variables dynamically. 103 $fieldname = 'field-' . $count; 104 $record = new StdClass(); 105 $record->name = $fieldname; 106 $record->type = $fieldtype; 107 108 ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data); 109 110 $this->assertInstanceOf('data_field_' . $fieldtype, ${$fieldname}); 111 $count++; 112 } 113 114 $this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', array('dataid' => $data->id))); 115 116 $addtemplate = $DB->get_record('data', array('id' => $data->id), 'addtemplate'); 117 $addtemplate = $addtemplate->addtemplate; 118 119 for ($i = 1; $i < $count; $i++) { 120 $fieldname = 'field-' . $i; 121 $position = strpos($addtemplate, '[[' . $fieldname . ']]'); 122 $this->assertIsNumeric($position); 123 $this->assertGreaterThanOrEqual(0, $position); 124 } 125 } 126 127 /** 128 * @covers ::create_entry 129 */ 130 public function test_create_entry() { 131 global $DB; 132 133 $this->resetAfterTest(true); 134 135 $this->setAdminUser(); 136 $this->assertEquals(0, $DB->count_records('data')); 137 138 $user1 = $this->getDataGenerator()->create_user(); 139 $course = $this->getDataGenerator()->create_course(); 140 $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student'); 141 142 $groupa = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'name' => 'groupA')); 143 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $groupa->id)); 144 145 /** @var mod_data_generator $generator */ 146 $generator = $this->getDataGenerator()->get_plugin_generator('mod_data'); 147 $this->assertInstanceOf('mod_data_generator', $generator); 148 $this->assertEquals('data', $generator->get_modulename()); 149 150 $data = $generator->create_instance(array('course' => $course->id)); 151 $this->assertEquals(1, $DB->count_records('data')); 152 153 $cm = get_coursemodule_from_instance('data', $data->id); 154 $this->assertEquals($data->id, $cm->instance); 155 $this->assertEquals('data', $cm->modname); 156 $this->assertEquals($course->id, $cm->course); 157 158 $context = context_module::instance($cm->id); 159 $this->assertEquals($data->cmid, $context->instanceid); 160 161 $fieldtypes = array('checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url', 162 'latlong', 'file', 'picture'); 163 164 $count = 1; 165 166 // Creating test Fields with default parameter values. 167 foreach ($fieldtypes as $fieldtype) { 168 // Creating variables dynamically. 169 $fieldname = 'field-' . $count; 170 $record = new StdClass(); 171 $record->name = $fieldname; 172 $record->type = $fieldtype; 173 $record->required = 1; 174 175 $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data); 176 $count++; 177 } 178 179 $fields = $DB->get_records('data_fields', array('dataid' => $data->id), 'id'); 180 181 $contents = array(); 182 $contents[] = array('opt1', 'opt2', 'opt3', 'opt4'); 183 $contents[] = '01-01-2037'; // It should be lower than 2038, to avoid failing on 32-bit windows. 184 $contents[] = 'menu1'; 185 $contents[] = array('multimenu1', 'multimenu2', 'multimenu3', 'multimenu4'); 186 $contents[] = '12345'; 187 $contents[] = 'radioopt1'; 188 $contents[] = 'text for testing'; 189 $contents[] = '<p>text area testing<br /></p>'; 190 $contents[] = array('example.url', 'sampleurl'); 191 $contents[] = [-31.9489873, 115.8382036]; // Latlong. 192 $contents[] = 'Filename.pdf'; // File - filename. 193 $contents[] = array('Cat1234.jpg', 'Cat'); // Picture - filename with alt text. 194 $count = 0; 195 $fieldcontents = array(); 196 foreach ($fields as $fieldrecord) { 197 $fieldcontents[$fieldrecord->id] = $contents[$count++]; 198 } 199 200 $tags = ['Cats', 'mice']; 201 202 $this->setUser($user1); 203 $datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($data, 204 $fieldcontents, $groupa->id, $tags); 205 206 $this->assertEquals(1, $DB->count_records('data_records', array('dataid' => $data->id))); 207 $this->assertEquals(count($contents), $DB->count_records('data_content', array('recordid' => $datarecordid))); 208 209 $entry = $DB->get_record('data_records', array('id' => $datarecordid)); 210 $this->assertEquals($entry->groupid, $groupa->id); 211 212 $contents = $DB->get_records('data_content', array('recordid' => $datarecordid), 'id'); 213 214 $contentstartid = 0; 215 $flag = 0; 216 foreach ($contents as $key => $content) { 217 if (!$flag++) { 218 $contentstartid = $key; 219 } 220 $this->assertFalse($content->content == null); 221 } 222 223 $this->assertEquals($contents[$contentstartid]->content, 'opt1##opt2##opt3##opt4'); 224 $this->assertEquals($contents[++$contentstartid]->content, '2114380800'); 225 $this->assertEquals($contents[++$contentstartid]->content, 'menu1'); 226 $this->assertEquals($contents[++$contentstartid]->content, 'multimenu1##multimenu2##multimenu3##multimenu4'); 227 $this->assertEquals($contents[++$contentstartid]->content, '12345'); 228 $this->assertEquals($contents[++$contentstartid]->content, 'radioopt1'); 229 $this->assertEquals($contents[++$contentstartid]->content, 'text for testing'); 230 $this->assertEquals($contents[++$contentstartid]->content, '<p>text area testing<br /></p>'); 231 $this->assertEquals($contents[$contentstartid]->content1, '1'); 232 $this->assertEquals($contents[++$contentstartid]->content, 'http://example.url'); 233 $this->assertEquals($contents[$contentstartid]->content1, 'sampleurl'); 234 $this->assertEquals(array('Cats', 'mice'), 235 array_values(core_tag_tag::get_item_tags_array('mod_data', 'data_records', $datarecordid))); 236 } 237 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body