[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * Unit tests for lib.php 19 * 20 * @package mod_data 21 * @category phpunit 22 * @copyright 2013 Adrian Greeve 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/mod/data/lib.php'); 30 31 /** 32 * Unit tests for lib.php 33 * 34 * @package mod_data 35 * @copyright 2013 Adrian Greeve 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class data_lib_testcase extends advanced_testcase { 39 40 function test_data_delete_record() { 41 global $DB; 42 43 $this->resetAfterTest(); 44 45 // Create a record for deleting. 46 $this->setAdminUser(); 47 $course = $this->getDataGenerator()->create_course(); 48 $record = new stdClass(); 49 $record->course = $course->id; 50 $record->name = "Mod data delete test"; 51 $record->intro = "Some intro of some sort"; 52 53 $module = $this->getDataGenerator()->create_module('data', $record); 54 55 $field = data_get_field_new('text', $module); 56 57 $fielddetail = new stdClass(); 58 $fielddetail->d = $module->id; 59 $fielddetail->mode = 'add'; 60 $fielddetail->type = 'text'; 61 $fielddetail->sesskey = sesskey(); 62 $fielddetail->name = 'Name'; 63 $fielddetail->description = 'Some name'; 64 65 $field->define_field($fielddetail); 66 $field->insert_field(); 67 $recordid = data_add_record($module); 68 69 $datacontent = array(); 70 $datacontent['fieldid'] = $field->field->id; 71 $datacontent['recordid'] = $recordid; 72 $datacontent['content'] = 'Asterix'; 73 74 $contentid = $DB->insert_record('data_content', $datacontent); 75 $cm = get_coursemodule_from_instance('data', $module->id, $course->id); 76 77 // Check to make sure that we have a database record. 78 $data = $DB->get_records('data', array('id' => $module->id)); 79 $this->assertEquals(1, count($data)); 80 81 $datacontent = $DB->get_records('data_content', array('id' => $contentid)); 82 $this->assertEquals(1, count($datacontent)); 83 84 $datafields = $DB->get_records('data_fields', array('id' => $field->field->id)); 85 $this->assertEquals(1, count($datafields)); 86 87 $datarecords = $DB->get_records('data_records', array('id' => $recordid)); 88 $this->assertEquals(1, count($datarecords)); 89 90 // Test to see if a failed delete returns false. 91 $result = data_delete_record(8798, $module, $course->id, $cm->id); 92 $this->assertFalse($result); 93 94 // Delete the record. 95 $result = data_delete_record($recordid, $module, $course->id, $cm->id); 96 97 // Check that all of the record is gone. 98 $datacontent = $DB->get_records('data_content', array('id' => $contentid)); 99 $this->assertEquals(0, count($datacontent)); 100 101 $datarecords = $DB->get_records('data_records', array('id' => $recordid)); 102 $this->assertEquals(0, count($datarecords)); 103 104 // Make sure the function returns true on a successful deletion. 105 $this->assertTrue($result); 106 } 107 108 /** 109 * Test comment_created event. 110 */ 111 public function test_data_comment_created_event() { 112 global $CFG, $DB; 113 require_once($CFG->dirroot . '/comment/lib.php'); 114 115 $this->resetAfterTest(); 116 117 // Create a record for deleting. 118 $this->setAdminUser(); 119 $course = $this->getDataGenerator()->create_course(); 120 $record = new stdClass(); 121 $record->course = $course->id; 122 $record->name = "Mod data delete test"; 123 $record->intro = "Some intro of some sort"; 124 $record->comments = 1; 125 126 $module = $this->getDataGenerator()->create_module('data', $record); 127 $field = data_get_field_new('text', $module); 128 129 $fielddetail = new stdClass(); 130 $fielddetail->name = 'Name'; 131 $fielddetail->description = 'Some name'; 132 133 $field->define_field($fielddetail); 134 $field->insert_field(); 135 $recordid = data_add_record($module); 136 137 $datacontent = array(); 138 $datacontent['fieldid'] = $field->field->id; 139 $datacontent['recordid'] = $recordid; 140 $datacontent['content'] = 'Asterix'; 141 142 $contentid = $DB->insert_record('data_content', $datacontent); 143 $cm = get_coursemodule_from_instance('data', $module->id, $course->id); 144 145 $context = context_module::instance($module->cmid); 146 $cmt = new stdClass(); 147 $cmt->context = $context; 148 $cmt->course = $course; 149 $cmt->cm = $cm; 150 $cmt->area = 'database_entry'; 151 $cmt->itemid = $recordid; 152 $cmt->showcount = true; 153 $cmt->component = 'mod_data'; 154 $comment = new comment($cmt); 155 156 // Triggering and capturing the event. 157 $sink = $this->redirectEvents(); 158 $comment->add('New comment'); 159 $events = $sink->get_events(); 160 $this->assertCount(1, $events); 161 $event = reset($events); 162 163 // Checking that the event contains the expected values. 164 $this->assertInstanceOf('\mod_data\event\comment_created', $event); 165 $this->assertEquals($context, $event->get_context()); 166 $url = new moodle_url('/mod/data/view.php', array('id' => $cm->id)); 167 $this->assertEquals($url, $event->get_url()); 168 $this->assertEventContextNotUsed($event); 169 } 170 171 /** 172 * Test comment_deleted event. 173 */ 174 public function test_data_comment_deleted_event() { 175 global $CFG, $DB; 176 require_once($CFG->dirroot . '/comment/lib.php'); 177 178 $this->resetAfterTest(); 179 180 // Create a record for deleting. 181 $this->setAdminUser(); 182 $course = $this->getDataGenerator()->create_course(); 183 $record = new stdClass(); 184 $record->course = $course->id; 185 $record->name = "Mod data delete test"; 186 $record->intro = "Some intro of some sort"; 187 $record->comments = 1; 188 189 $module = $this->getDataGenerator()->create_module('data', $record); 190 $field = data_get_field_new('text', $module); 191 192 $fielddetail = new stdClass(); 193 $fielddetail->name = 'Name'; 194 $fielddetail->description = 'Some name'; 195 196 $field->define_field($fielddetail); 197 $field->insert_field(); 198 $recordid = data_add_record($module); 199 200 $datacontent = array(); 201 $datacontent['fieldid'] = $field->field->id; 202 $datacontent['recordid'] = $recordid; 203 $datacontent['content'] = 'Asterix'; 204 205 $contentid = $DB->insert_record('data_content', $datacontent); 206 $cm = get_coursemodule_from_instance('data', $module->id, $course->id); 207 208 $context = context_module::instance($module->cmid); 209 $cmt = new stdClass(); 210 $cmt->context = $context; 211 $cmt->course = $course; 212 $cmt->cm = $cm; 213 $cmt->area = 'database_entry'; 214 $cmt->itemid = $recordid; 215 $cmt->showcount = true; 216 $cmt->component = 'mod_data'; 217 $comment = new comment($cmt); 218 $newcomment = $comment->add('New comment 1'); 219 220 // Triggering and capturing the event. 221 $sink = $this->redirectEvents(); 222 $comment->delete($newcomment->id); 223 $events = $sink->get_events(); 224 $this->assertCount(1, $events); 225 $event = reset($events); 226 227 // Checking that the event contains the expected values. 228 $this->assertInstanceOf('\mod_data\event\comment_deleted', $event); 229 $this->assertEquals($context, $event->get_context()); 230 $url = new moodle_url('/mod/data/view.php', array('id' => $module->cmid)); 231 $this->assertEquals($url, $event->get_url()); 232 $this->assertEventContextNotUsed($event); 233 } 234 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |