From 5548b91e3ea7332ff675e2403e8d6ab544a9c86d Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Wed, 19 Apr 2023 14:23:59 +0700 Subject: [PATCH] MDL-77959 core_moodlenet: Convert activity intro to plaintext --- lib/classes/moodlenet/activity_sender.php | 31 ++++++++++--- lib/tests/moodlenet/activity_sender_test.php | 46 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/lib/classes/moodlenet/activity_sender.php b/lib/classes/moodlenet/activity_sender.php index 2ce5b0fdb2a..6a7c718911e 100644 --- a/lib/classes/moodlenet/activity_sender.php +++ b/lib/classes/moodlenet/activity_sender.php @@ -122,13 +122,7 @@ class activity_sender { } // MoodleNet only accept plaintext descriptions. - $resourcedescription = $DB->get_field($this->cminfo->modname, 'intro', ['id' => $this->cminfo->instance]); - $resourcedescription = strip_tags($resourcedescription); - $resourcedescription = format_text( - $resourcedescription, - FORMAT_PLAIN, - ['context' => $coursecontext] - ); + $resourcedescription = $this->get_resource_description($coursecontext); $response = $this->moodlenetclient->create_resource_from_file($filedata, $this->cminfo->name, $resourcedescription); $responsecode = $response->getStatusCode(); @@ -208,4 +202,27 @@ class activity_sender { self::SHARE_FORMAT_BACKUP, ]; } + + /** + * Fetch the description for the resource being created, in a supported text format. + * + * @param \context $coursecontext The course context being shared from. + * @return string Converted activity description. + */ + protected function get_resource_description( + \context $coursecontext, + ): string { + global $PAGE, $DB; + // We need to set the page context here because content_to_text and format_text will need the page context to work. + $PAGE->set_context($coursecontext); + + $intro = $DB->get_record($this->cminfo->modname, ['id' => $this->cminfo->instance], 'intro, introformat', MUST_EXIST); + $processeddescription = strip_tags($intro->intro); + $processeddescription = content_to_text(format_text( + $processeddescription, + $intro->introformat, + ), $intro->introformat); + + return $processeddescription; + } } diff --git a/lib/tests/moodlenet/activity_sender_test.php b/lib/tests/moodlenet/activity_sender_test.php index 8646f4aa373..f17c67dc71c 100644 --- a/lib/tests/moodlenet/activity_sender_test.php +++ b/lib/tests/moodlenet/activity_sender_test.php @@ -135,6 +135,52 @@ class activity_sender_test extends \advanced_testcase { $this->assertInstanceOf(\stored_file::class, $package['storedfile']); } + /** + * Test get_resource_description method. + * + * @covers ::get_resource_description + * @return void + */ + public function test_get_resource_description() { + global $USER; + $this->setAdminUser(); + + $activity = $this->generator->create_module('assign', [ + 'course' => $this->course->id, + 'intro' => '

This is an example Moodle activity description.

+

 

+

This is a formatted intro

+

 

+

This thing has many lines.

+

 

+

The last word of this sentence is in bold

' + ]); + + $httpclient = new http_client(); + $moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient); + + // Set get_resource_description method accessibility. + $method = new ReflectionMethod(activity_sender::class, 'get_resource_description'); + $method->setAccessible(true); + + // Test the processed description. + $processeddescription = $method->invoke(new activity_sender( + $activity->cmid, + $USER->id, + $moodlenetclient, + $this->mockoauthclient, + activity_sender::SHARE_FORMAT_BACKUP + ), $this->coursecontext); + + $this->assertEquals('This is an example Moodle activity description. +  +This is a formatted intro +  +This thing has many lines. +  +The last word of this sentence is in bold', $processeddescription); + } + /** * Test share_activity() method. *