MDL-67468 filter_activitynames: use MUC
[moodle.git] / filter / activitynames / tests / filter_test.php
index b21c924..98776b7 100644 (file)
@@ -102,4 +102,56 @@ class filter_activitynames_filter_testcase extends advanced_testcase {
         $this->assertEquals($page->cmid, $matches[2][0]);
         $this->assertEquals($page->name, $matches[3][0]);
     }
+
+    public function test_cache() {
+        $this->resetAfterTest(true);
+
+        // Create a test courses.
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $context1 = context_course::instance($course1->id);
+        $context2 = context_course::instance($course2->id);
+
+        // Create page 1.
+        $page1 = $this->getDataGenerator()->create_module('page',
+            ['course' => $course1->id, 'name' => 'Test 1']);
+        // Format text with page 1 in HTML.
+        $html = '<p>Please read the two pages Test 1 and Test 2.</p>';
+        $filtered1 = format_text($html, FORMAT_HTML, array('context' => $context1));
+        // Find all the activity links in the result.
+        $matches = [];
+        preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~',
+            $filtered1, $matches);
+        // There should be 1 link.
+        $this->assertCount(1, $matches[1]);
+        $this->assertEquals($page1->name, $matches[1][0]);
+
+        // Create page 2.
+        $page2 = $this->getDataGenerator()->create_module('page',
+        ['course' => $course1->id, 'name' => 'Test 2']);
+        // Filter the text again.
+        $filtered2 = format_text($html, FORMAT_HTML, array('context' => $context1));
+        // The filter result does not change due to caching.
+        $this->assertEquals($filtered1, $filtered2);
+
+        // Change context, so that cache for course 1 is cleared.
+        $filtered3 = format_text($html, FORMAT_HTML, array('context' => $context2));
+        $this->assertNotEquals($filtered1, $filtered3);
+        $matches = [];
+        preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~',
+            $filtered3, $matches);
+        // There should be no links.
+        $this->assertCount(0, $matches[1]);
+
+        // Filter the text for course 1.
+        $filtered4 = format_text($html, FORMAT_HTML, array('context' => $context1));
+        // Find all the activity links in the result.
+        $matches = [];
+        preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~',
+            $filtered4, $matches);
+        // There should be 2 links.
+        $this->assertCount(2, $matches[1]);
+        $this->assertEquals($page1->name, $matches[1][0]);
+        $this->assertEquals($page2->name, $matches[1][1]);
+    }
 }