Skip to content

Commit a41dfd8

Browse files
committed
MDL-87259 course: Correct content item user cache key
1 parent a280753 commit a41dfd8

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

public/course/classes/local/repository/caching_content_item_readonly_repository.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ public function __construct(\cache $cachestore, content_item_readonly_repository
6363
* @return array the array of content items.
6464
*/
6565
public function find_all_for_course(\stdClass $course, \stdClass $user): array {
66-
global $USER;
6766
// Try to find this data in the cache first.
68-
$key = $USER->id . '_' . $course->id;
67+
$key = $user->id . '_' . $course->id;
6968
$contentitems = $this->cachestore->get($key);
7069
if ($contentitems !== false) {
7170
return $contentitems;

public/course/tests/caching_content_item_readonly_repository_test.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,60 @@ public function test_find_all_for_course(): void {
7878
$this->assertEquals($module->name, $cacheditemsfiltered[0]->get_name());
7979
$this->assertEmpty($itemsfiltered);
8080
}
81+
82+
/**
83+
* Test verifying that cached content items are returned from the cache as the correct user.
84+
*
85+
* @covers \core_course\local\repository\caching_content_item_readonly_repository::find_all_for_course
86+
*/
87+
public function test_find_all_for_course_user_cache(): void {
88+
global $DB, $CFG;
89+
require_once($CFG->dirroot . '/mod/lti/locallib.php');
90+
91+
$this->resetAfterTest();
92+
$admin = get_admin();
93+
94+
$course = $this->getDataGenerator()->create_course();
95+
$editingteacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
96+
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
97+
$cir = new content_item_readonly_repository();
98+
$ccir = new caching_content_item_readonly_repository(\cache::make('core', 'user_course_content_items'), $cir);
99+
100+
// Create lti that is only available to editingteacher.
101+
$type = new \stdClass();
102+
$type->course = SITEID;
103+
$type->name = 'Editing Teacher Only LTI Tool';
104+
$type->baseurl = 'https://example.com/lti/launch';
105+
$type->tooldomain = 'example.com';
106+
$type->state = LTI_TOOL_STATE_CONFIGURED;
107+
$type->coursevisible = LTI_COURSEVISIBLE_ACTIVITYCHOOSER;
108+
$type->createdby = $admin->id;
109+
$type->timecreated = time();
110+
$type->timemodified = time();
111+
112+
$typeid = $DB->insert_record('lti_types', $type);
113+
114+
// Ensure we are working as editingteacher.
115+
$this->setUser($editingteacher);
116+
117+
// Get cached content items for each user.
118+
// We run this twice, first to build the cache, second to query cache.
119+
$temp = $ccir->find_all_for_course($course, $editingteacher);
120+
$cachededitingteacheritems = $ccir->find_all_for_course($course, $editingteacher);
121+
122+
$temp = $ccir->find_all_for_course($course, $teacher);
123+
$cachedteacheritems = $ccir->find_all_for_course($course, $teacher);
124+
125+
// The lti will only appear for editingteacher.
126+
$cachededitingteacheritemsfiltered = array_values(array_filter($cachededitingteacheritems, function ($item) {
127+
return $item->get_title()->get_value() == 'Editing Teacher Only LTI Tool';
128+
}));
129+
$this->assertCount(1, $cachededitingteacheritemsfiltered);
130+
131+
// The lti will not appear for teacher.
132+
$cachedteacheritemsfiltered = array_values(array_filter($cachedteacheritems, function ($item) {
133+
return $item->get_title()->get_value() == 'Editing Teacher Only LTI Tool';
134+
}));
135+
$this->assertEmpty($cachedteacheritemsfiltered);
136+
}
81137
}

0 commit comments

Comments
 (0)