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:
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user