MDL-44321 events: debug on errors in add_record_snapshot()

also clone object returned by get_record_snapshot() to prevent accidental changing of $COURSE or other important objects
This commit is contained in:
Marina Glancy
2014-02-28 16:37:31 +08:00
parent 1e242b41af
commit 5e70ea26bb
2 changed files with 11 additions and 3 deletions
+8 -1
View File
@@ -573,6 +573,13 @@ abstract class base implements \IteratorAggregate {
if ($CFG->debugdeveloper) {
if (!$DB->get_manager()->table_exists($tablename)) {
debugging("Invalid table name '$tablename' specified, database table does not exist.", DEBUG_DEVELOPER);
} else {
$columns = $DB->get_columns($tablename);
$missingfields = array_diff(array_keys($columns), array_keys((array)$record));
if (!empty($missingfields)) {
debugging("Fields list in snapshot record does not match fields list in '$tablename'. Record is missing fields: ".
join(', ', $missingfields), DEBUG_DEVELOPER);
}
}
}
$this->recordsnapshots[$tablename][$record->id] = $record;
@@ -595,7 +602,7 @@ abstract class base implements \IteratorAggregate {
}
if (isset($this->recordsnapshots[$tablename][$id])) {
return $this->recordsnapshots[$tablename][$id];
return clone($this->recordsnapshots[$tablename][$id]);
}
$record = $DB->get_record($tablename, array('id'=>$id));
+3 -2
View File
@@ -709,8 +709,9 @@ class core_event_testcase extends advanced_testcase {
$event->add_record_snapshot('course', $course1);
$result = $event->get_record_snapshot('course', 1, $course1);
$this->assertSame($course1, $result);
$result = $event->get_record_snapshot('course', 1);
// Convert to arrays because record snapshot returns a clone of the object.
$this->assertSame((array)$course1, (array)$result);
$user = $event->get_record_snapshot('user', 1);
$this->assertEquals(1, $user->id);