From 28f4ab461e7691bc4a2bfc0b3472c37f29e95fb8 Mon Sep 17 00:00:00 2001 From: Sujith Haridasan Date: Thu, 30 Sep 2021 09:29:01 +0530 Subject: [PATCH] MDL-72689 core_navigation: Respect homepage preference If defaulthomepage is set to Dashboard then Site home link in the primary navigation should point to "/?redirect=0" --- lib/classes/navigation/views/primary.php | 9 +++-- lib/tests/navigation/views/primary_test.php | 4 +-- my/index.php | 1 - user/tests/behat/set_default_homepage.feature | 35 +++++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/classes/navigation/views/primary.php b/lib/classes/navigation/views/primary.php index 84f79427bbb..e7fd0a125ef 100644 --- a/lib/classes/navigation/views/primary.php +++ b/lib/classes/navigation/views/primary.php @@ -33,6 +33,7 @@ class primary extends view { * Initialise the primary navigation node */ public function initialise(): void { + global $CFG; if (during_initial_install() || $this->initialised) { return; } @@ -46,9 +47,13 @@ class primary extends view { self::TYPE_SETTING, null, 'myhome', new \pix_icon('i/dashboard', '')); } else if ($homepage === HOMEPAGE_MY) { $this->add(get_string('myhome'), new \moodle_url('/my/'), self::TYPE_SYSTEM, - null, 'home', new \pix_icon('i/home', '')); + null, 'myhome', new \pix_icon('i/dashboard', '')); $this->rootnodes['home'] = $this->add(get_string('sitehome'), new \moodle_url('/'), - self::TYPE_SETTING, null, 'myhome', new \pix_icon('i/dashboard', '')); + self::TYPE_SETTING, null, 'home', new \pix_icon('i/home', '')); + if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) { + // We need to stop automatic redirection. + $this->rootnodes['home']->action->param('redirect', '0'); + } } } diff --git a/lib/tests/navigation/views/primary_test.php b/lib/tests/navigation/views/primary_test.php index 2138df2672b..0564a13321f 100644 --- a/lib/tests/navigation/views/primary_test.php +++ b/lib/tests/navigation/views/primary_test.php @@ -62,8 +62,8 @@ class primary_test extends \advanced_testcase { public function test_setting_initialise_provider() { return [ 'Testing as a guest user' => ['guest', ['courses']], - 'Testing as an admin' => ['admin', ['home', 'myhome', 'courses', 'siteadminnode']], - 'Testing as a regular user' => ['user', ['home', 'myhome', 'courses']] + 'Testing as an admin' => ['admin', ['myhome', 'home', 'courses', 'siteadminnode']], + 'Testing as a regular user' => ['user', ['myhome', 'home', 'courses']] ]; } diff --git a/my/index.php b/my/index.php index c8f78eb95a7..fe684a6bba8 100644 --- a/my/index.php +++ b/my/index.php @@ -92,7 +92,6 @@ $PAGE->set_heading($header); if (!isguestuser()) { // Skip default home page for guests if (get_home_page() != HOMEPAGE_MY) { - $PAGE->set_primary_active_tab('myhome'); if (optional_param('setdefaulthome', false, PARAM_BOOL)) { set_user_preference('user_home_page_preference', HOMEPAGE_MY); } else if (!empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_USER) { diff --git a/user/tests/behat/set_default_homepage.feature b/user/tests/behat/set_default_homepage.feature index 917a007810f..8c5ec2c5232 100644 --- a/user/tests/behat/set_default_homepage.feature +++ b/user/tests/behat/set_default_homepage.feature @@ -71,3 +71,38 @@ Feature: Set the site home page and dashboard as the default home page | preference | breadcrumb | | Site | Home | | Dashboard | Dashboard | + + @javascript @theme_boost + Scenario Outline: Admin sets defaulthomepage to 0,1 and verify the landing page and site home link + Given I log in as "admin" + And the following config values are set as admin: + | defaulthomepage | | + And I am on homepage + And I should see "" in the "//a[contains(@class,'nav-link active') and contains(., '')]" "xpath_element" + And I should see "" in the "" "xpath_element" + + Examples: + | defaulthomepageset | homepage | sitehome | linkelement | + | 0 | Home | Home | //a[contains(@class, 'nav-link active') and contains(@tabindex, 0) and not(contains(@href, 'redirect=0'))] | + | 1 | Dashboard | Site home | //a[contains(@class, 'nav-link') and contains(@tabindex, -1) and (contains(@href, 'redirect=0'))] | + + @javascript @theme_boost + Scenario Outline: Admin sets defaulthomepage to 2 and verify the landing page based on user preference set + Given I log in as "admin" + And I navigate to "Appearance > Navigation" in site administration + And I set the field "Home page for users" to "User preference" + And I press "Save changes" + And I follow "Preferences" in the user menu + And I follow "Home page" + And I set the field "Home page" to "" + And I press "Save changes" + And the following config values are set as admin: + | defaulthomepage | 2 | + And I log out + And I log in as "admin" + And I should see "" in the "//a[contains(@class,'nav-link active') and contains(., '')]" "xpath_element" + + Examples: + | userpreference | homepage | + | Site | Home | + | Dashboard | Dashboard |