diff --git a/admin/tool/dataprivacy/classes/expired_contexts_manager.php b/admin/tool/dataprivacy/classes/expired_contexts_manager.php index 90e7b2a730d..7672da340a6 100644 --- a/admin/tool/dataprivacy/classes/expired_contexts_manager.php +++ b/admin/tool/dataprivacy/classes/expired_contexts_manager.php @@ -920,9 +920,17 @@ class expired_contexts_manager { * @return bool */ protected static function is_course_context_expired_or_unprotected_for_user(\context $context, \stdClass $user) { - $expiryrecords = self::get_nested_expiry_info_for_courses($context->path); - $info = $expiryrecords[$context->path]->info; + if ($context->get_course_context()->instanceid == SITEID) { + // The is an activity in the site course (front page). + $purpose = data_registry::get_effective_contextlevel_value(CONTEXT_SYSTEM, 'purpose'); + $info = static::get_expiry_info($purpose); + + } else { + $expiryrecords = self::get_nested_expiry_info_for_courses($context->path); + $info = $expiryrecords[$context->path]->info; + } + if ($info->is_fully_expired()) { // This context is fully expired. return true; diff --git a/admin/tool/dataprivacy/tests/expired_contexts_test.php b/admin/tool/dataprivacy/tests/expired_contexts_test.php index 08ffb80067a..6deb9192b81 100644 --- a/admin/tool/dataprivacy/tests/expired_contexts_test.php +++ b/admin/tool/dataprivacy/tests/expired_contexts_test.php @@ -2222,6 +2222,40 @@ class tool_dataprivacy_expired_contexts_testcase extends advanced_testcase { $this->assertTrue(expired_contexts_manager::is_context_expired_or_unprotected_for_user($blockcontext, $user)); } + /** + * Test the is_context_expired functions when supplied with the front page course. + */ + public function test_is_context_expired_frontpage() { + $this->resetAfterTest(); + + $purposes = $this->setup_basics('PT1H', 'PT1H', 'P1D'); + + $frontcourse = get_site(); + $frontcoursecontext = \context_course::instance($frontcourse->id); + + $sitenews = $this->getDataGenerator()->create_module('forum', ['course' => $frontcourse->id]); + $cm = get_coursemodule_from_instance('forum', $sitenews->id); + $sitenewscontext = \context_module::instance($cm->id); + + $user = $this->getDataGenerator()->create_user(['lastaccess' => time() - YEARSECS]); + + $this->assertFalse(expired_contexts_manager::is_context_expired($frontcoursecontext)); + $this->assertFalse(expired_contexts_manager::is_context_expired($sitenewscontext)); + + $this->assertTrue(expired_contexts_manager::is_context_expired_or_unprotected_for_user($frontcoursecontext, $user)); + $this->assertTrue(expired_contexts_manager::is_context_expired_or_unprotected_for_user($sitenewscontext, $user)); + + // Protecting the course contextlevel does not impact the front page. + $purposes->course->set('protected', 1)->save(); + $this->assertTrue(expired_contexts_manager::is_context_expired_or_unprotected_for_user($frontcoursecontext, $user)); + $this->assertTrue(expired_contexts_manager::is_context_expired_or_unprotected_for_user($sitenewscontext, $user)); + + // Protecting the system contextlevel affects the front page, too. + $purposes->system->set('protected', 1)->save(); + $this->assertFalse(expired_contexts_manager::is_context_expired_or_unprotected_for_user($frontcoursecontext, $user)); + $this->assertFalse(expired_contexts_manager::is_context_expired_or_unprotected_for_user($sitenewscontext, $user)); + } + /** * Test the is_context_expired functions when supplied with an expired course. */