MDL-65803 recentlyaccesseditems: Restrict response to valid courses.

* Additional hooks to cleanup data when a course or module is deleted
* Augmented unit tests to cover the situation when a course is deleted
* Upgrade step to cleanup existing data
This commit is contained in:
Peter 2019-05-31 09:18:21 +08:00
parent f3507273e9
commit 39498b6833
5 changed files with 171 additions and 1 deletions

View File

@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file keeps track of upgrades to the recentlyaccesseditems block
*
* Sometimes, changes between versions involve alterations to database structures
* and other major things that may break installations.
*
* The upgrade function in this file will attempt to perform all the necessary
* actions to upgrade your older installation to the current version.
*
* If there's something it cannot do itself, it will tell you what you need to do.
*
* The commands in here will all be database-neutral, using the methods of
* database_manager class
*
* Please do not forget to use upgrade_set_timeout()
* before any action that may take longer time to finish.
*
* @package block_recentlyaccesseditems
* @copyright 2019 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Upgrade the recentlyaccesseditems db table.
*
* @param $oldversion
* @return bool
*/
function xmldb_block_recentlyaccesseditems_upgrade($oldversion, $block) {
global $DB;
// Automatically generated Moodle v3.7.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2019052001) {
$sql = "courseid NOT IN (SELECT c.id from {course} c) OR cmid NOT IN (SELECT cm.id from {course_modules} cm)";
$DB->delete_records_select("block_recentlyaccesseditems", $sql);
upgrade_block_savepoint(true, 2019052001, 'recentlyaccesseditems', false);
}
return true;
}

View File

@ -0,0 +1,47 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The interface library between the core and the subsystem.
*
* @package block_recentlyaccesseditems
* @copyright 2019 Peter Dias <peter@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Pre-delete course module hook to cleanup any records with references to the deleted module.
*
* @param stdClass $cm The deleted course module
*/
function block_recentlyaccesseditems_pre_course_module_delete($cm) {
global $DB;
$DB->delete_records('block_recentlyaccesseditems', ['cmid' => $cm->id]);
}
/**
* Pre-delete course hook to cleanup any records with references to the deleted course.
*
* @param stdClass $course The deleted course
*/
function block_recentlyaccesseditems_pre_course_delete($course) {
global $DB;
$DB->delete_records('block_recentlyaccesseditems', ['courseid' => $course->id]);
}

View File

@ -103,5 +103,15 @@ class block_recentlyaccesseditems_externallib_testcase extends externallib_advan
}
$this->assertTrue($record->timeaccess < $result[$key - 1]->timeaccess);
}
// Delete a course and confirm it's activities don't get returned.
delete_course($courses[0], false);
$result = \block_recentlyaccesseditems\external::get_recent_items();
$this->assertCount((count($forum) + count($chat)) - 2, $result);
// Delete a single course module should still return.
course_delete_module($forum[1]->cmid);
$result = \block_recentlyaccesseditems\external::get_recent_items();
$this->assertCount((count($forum) + count($chat)) - 3, $result);
}
}

View File

@ -207,6 +207,60 @@ class block_recentlyaccesseditems_privacy_testcase extends \core_privacy\tests\p
// Confirm student's data is exported.
$writer = \core_privacy\local\request\writer::with_context($studentcontext);
$this->assertTrue($writer->has_any_data());
delete_course($course, false);
$sc = context_user::instance($student->id);
$approvedlist = new approved_contextlist($student, $component, [$sc->id]);
provider::export_user_data($approvedlist);
$writer = \core_privacy\local\request\writer::with_context($sc);
$this->assertTrue($writer->has_any_data());
}
/**
* Test exporting data for an approved contextlist with a deleted course
*/
public function test_export_user_data_with_deleted_course() {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$component = 'block_recentlyaccesseditems';
$student = $generator->create_user();
$studentcontext = context_user::instance($student->id);
// Enrol user in course and add course items.
$course = $generator->create_course();
$generator->enrol_user($student->id, $course->id, 'student');
$forum = $generator->create_module('forum', ['course' => $course]);
$chat = $generator->create_module('chat', ['course' => $course]);
// Generate some recent activity.
$this->setUser($student);
$event = \mod_forum\event\course_module_viewed::create(['context' => context_module::instance($forum->cmid),
'objectid' => $forum->id]);
$event->trigger();
$event = \mod_chat\event\course_module_viewed::create(['context' => context_module::instance($chat->cmid),
'objectid' => $chat->id]);
$event->trigger();
// Confirm data is present.
$params = [
'courseid' => $course->id,
'userid' => $student->id,
];
$result = $DB->count_records('block_recentlyaccesseditems', $params);
$this->assertEquals(2, $result);
delete_course($course, false);
// Export data for student.
$approvedlist = new approved_contextlist($student, $component, [$studentcontext->id]);
provider::export_user_data($approvedlist);
// Confirm student's data is exported.
$writer = \core_privacy\local\request\writer::with_context($studentcontext);
$this->assertFalse($writer->has_any_data());
}
/**

View File

@ -22,6 +22,6 @@
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2019052000; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2019052001; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2019051100; // Requires this Moodle version.
$plugin->component = 'block_recentlyaccesseditems'; // Full name of the plugin (used for diagnostics).