From 53b5c4f5810b016092c66bae330f2a5cb882140a Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Fri, 29 Nov 2019 17:19:04 +1100 Subject: [PATCH] MDL-65849 mod_forum: Add course param to the author's profile url --- mod/forum/classes/local/exporters/author.php | 8 ++++--- .../local/exporters/discussion_summary.php | 1 + mod/forum/classes/local/factories/url.php | 6 +++-- mod/forum/tests/exporters_author_test.php | 9 ++++--- mod/forum/tests/externallib_test.php | 24 +++++++++---------- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/mod/forum/classes/local/exporters/author.php b/mod/forum/classes/local/exporters/author.php index 93f995bd8e8..31352c09ba4 100644 --- a/mod/forum/classes/local/exporters/author.php +++ b/mod/forum/classes/local/exporters/author.php @@ -147,6 +147,7 @@ class author extends exporter { $authorcontextid = $this->authorcontextid; $urlfactory = $this->related['urlfactory']; $context = $this->related['context']; + $forum = $this->related['forum']; if ($this->canview) { if ($author->is_deleted()) { @@ -156,7 +157,7 @@ class author extends exporter { 'isdeleted' => true, 'groups' => [], 'urls' => [ - 'profile' => ($urlfactory->get_author_profile_url($author))->out(false), + 'profile' => ($urlfactory->get_author_profile_url($author, $forum->get_course_id()))->out(false), 'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false) ] ]; @@ -192,7 +193,7 @@ class author extends exporter { 'isdeleted' => false, 'groups' => $groups, 'urls' => [ - 'profile' => ($urlfactory->get_author_profile_url($author))->out(false), + 'profile' => ($urlfactory->get_author_profile_url($author, $forum->get_course_id()))->out(false), 'profileimage' => ($urlfactory->get_author_profile_image_url($author, $authorcontextid))->out(false) ] ]; @@ -220,7 +221,8 @@ class author extends exporter { protected static function define_related() { return [ 'urlfactory' => 'mod_forum\local\factories\url', - 'context' => 'context' + 'context' => 'context', + 'forum' => 'mod_forum\local\entities\forum', ]; } } diff --git a/mod/forum/classes/local/exporters/discussion_summary.php b/mod/forum/classes/local/exporters/discussion_summary.php index 4637cb9b080..13a075f0e5a 100644 --- a/mod/forum/classes/local/exporters/discussion_summary.php +++ b/mod/forum/classes/local/exporters/discussion_summary.php @@ -140,6 +140,7 @@ class discussion_summary extends exporter { $related = [ 'urlfactory' => $this->related['urlfactory'], 'context' => $this->related['forum']->get_context(), + 'forum' => $forum, ]; $firstpostauthor = new author( diff --git a/mod/forum/classes/local/factories/url.php b/mod/forum/classes/local/factories/url.php index cfb6b4b083f..caae3d711ef 100644 --- a/mod/forum/classes/local/factories/url.php +++ b/mod/forum/classes/local/factories/url.php @@ -394,11 +394,13 @@ class url { * Get the url to view an author's profile. * * @param author_entity $author The author + * @param int $courseid The course id * @return moodle_url */ - public function get_author_profile_url(author_entity $author) : moodle_url { + public function get_author_profile_url(author_entity $author, int $courseid) : moodle_url { return new moodle_url('/user/view.php', [ - 'id' => $author->get_id() + 'id' => $author->get_id(), + 'course' => $courseid ]); } diff --git a/mod/forum/tests/exporters_author_test.php b/mod/forum/tests/exporters_author_test.php index 3b5dc5755c3..7e85c102e3d 100644 --- a/mod/forum/tests/exporters_author_test.php +++ b/mod/forum/tests/exporters_author_test.php @@ -63,7 +63,8 @@ class mod_forum_exporters_author_testcase extends advanced_testcase { $exporter = new author_exporter($author, 1, [], true, [ 'urlfactory' => \mod_forum\local\container::get_url_factory(), - 'context' => $context + 'context' => $context, + 'forum' => $forum, ]); $exportedauthor = $exporter->export($renderer); @@ -104,7 +105,8 @@ class mod_forum_exporters_author_testcase extends advanced_testcase { $exporter = new author_exporter($author, 1, [$group], true, [ 'urlfactory' => \mod_forum\local\container::get_url_factory(), - 'context' => $context + 'context' => $context, + 'forum' => $forum, ]); $exportedauthor = $exporter->export($renderer); @@ -142,7 +144,8 @@ class mod_forum_exporters_author_testcase extends advanced_testcase { $exporter = new author_exporter($author, 1, [$group], false, [ 'urlfactory' => \mod_forum\local\container::get_url_factory(), - 'context' => $context + 'context' => $context, + 'forum' => $forum, ]); $exportedauthor = $exporter->export($renderer); diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 1e3878d653b..3ba370a61ba 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -527,7 +527,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { * Tests is similar to the get_forum_discussion_posts only utilizing the new return structure and entities */ public function test_mod_forum_get_discussion_posts() { - global $CFG, $PAGE; + global $CFG; $this->resetAfterTest(true); @@ -538,6 +538,9 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $legacyfactory = mod_forum\local\container::get_legacy_data_mapper_factory(); $entityfactory = mod_forum\local\container::get_entity_factory(); + // Create course to add the module. + $course1 = self::getDataGenerator()->create_course(); + // Create a user who can track forums. $record = new stdClass(); $record->trackforums = true; @@ -551,7 +554,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'isdeleted' => false, 'groups' => [], 'urls' => [ - 'profile' => $urlfactory->get_author_profile_url($user2entity), + 'profile' => $urlfactory->get_author_profile_url($user2entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user2entity), ] ]; @@ -565,7 +568,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'groups' => [], 'isdeleted' => false, 'urls' => [ - 'profile' => $urlfactory->get_author_profile_url($user3entity), + 'profile' => $urlfactory->get_author_profile_url($user3entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user3entity), ] ]; @@ -575,9 +578,6 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { // Set the first created user to the test user. self::setUser($user1); - // Create course to add the module. - $course1 = self::getDataGenerator()->create_course(); - // Forum with tracking off. $record = new stdClass(); $record->course = $course1->id; @@ -652,7 +652,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'groups' => [], 'isdeleted' => true, 'urls' => [ - 'profile' => $urlfactory->get_author_profile_url($user3entity), + 'profile' => $urlfactory->get_author_profile_url($user3entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user3entity), ] ]; @@ -2618,6 +2618,9 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $legacydatamapper = mod_forum\local\container::get_legacy_data_mapper_factory(); $legacypostmapper = $legacydatamapper->get_post_data_mapper(); + // Create course to add the module. + $course1 = self::getDataGenerator()->create_course(); + $user1 = self::getDataGenerator()->create_user(); $user1entity = $entityfactory->get_author_from_stdclass($user1); $exporteduser1 = [ @@ -2625,7 +2628,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'fullname' => fullname($user1), 'groups' => [], 'urls' => [ - 'profile' => $urlfactory->get_author_profile_url($user1entity), + 'profile' => $urlfactory->get_author_profile_url($user1entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user1entity), ], 'isdeleted' => false, @@ -2638,7 +2641,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { 'fullname' => fullname($user2), 'groups' => [], 'urls' => [ - 'profile' => $urlfactory->get_author_profile_url($user2entity), + 'profile' => $urlfactory->get_author_profile_url($user2entity, $course1->id)->out(false), 'profileimage' => $urlfactory->get_author_profile_image_url($user2entity), ], 'isdeleted' => false, @@ -2650,9 +2653,6 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { // Set the first created user to the test user. self::setUser($user1); - // Create course to add the module. - $course1 = self::getDataGenerator()->create_course(); - // Forum with tracking off. $record = new stdClass(); $record->course = $course1->id;