@@ -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