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