MDL-83296 user: store defaulthomepage as a relative url

This commit is contained in:
Marina Glancy 2024-09-27 11:55:39 +01:00
parent 97c2beae70
commit 7015c67d7c
8 changed files with 84 additions and 17 deletions

View File

@ -1421,5 +1421,12 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2024092000.01);
}
if ($oldversion < 2024092400.01) {
upgrade_store_relative_url_sitehomepage();
// Main savepoint reached.
upgrade_main_savepoint(true, 2024092400.01);
}
return true;
}

View File

@ -1906,3 +1906,25 @@ function upgrade_change_binary_column_to_int(
return true;
}
/**
* Upgrade script replacing absolute URLs in defaulthomepage setting with relative URLs
*/
function upgrade_store_relative_url_sitehomepage() {
global $CFG, $DB;
if (str_starts_with((string)$CFG->defaulthomepage, $CFG->wwwroot . '/')) {
set_config('defaulthomepage', substr((string)$CFG->defaulthomepage, strlen($CFG->wwwroot)));
}
$records = $DB->get_records_select('user_preferences', "name = :name AND " . $DB->sql_like('value', ':pattern'),
['name' => 'user_home_page_preference', 'pattern' => 'http%']);
foreach ($records as $record) {
if (str_starts_with($record->value, $CFG->wwwroot . '/')) {
$DB->update_record('user_preferences', [
'id' => $record->id,
'value' => substr($record->value, strlen($CFG->wwwroot)),
]);
}
}
}

View File

@ -9896,11 +9896,11 @@ function get_home_page() {
if (empty($CFG->enabledashboard) && $userhomepage == HOMEPAGE_MY) {
// If the user was using the dashboard but it's disabled, return the default home page.
$userhomepage = $defaultpage;
} else if (clean_param($userhomepage, PARAM_LOCALURL)) {
} else if (get_default_home_page_url()) {
return HOMEPAGE_URL;
}
return (int) $userhomepage;
} else if (clean_param($CFG->defaulthomepage, PARAM_LOCALURL)) {
} else if (get_default_home_page_url()) {
return HOMEPAGE_URL;
}
}
@ -9930,13 +9930,15 @@ function get_default_home_page(): int {
function get_default_home_page_url(): ?\core\url {
global $CFG;
if ($defaulthomepage = clean_param($CFG->defaulthomepage, PARAM_LOCALURL)) {
if (substr((string)$CFG->defaulthomepage, 0, 1) === '/' &&
($defaulthomepage = clean_param($CFG->wwwroot . $CFG->defaulthomepage, PARAM_LOCALURL))) {
return new \core\url($defaulthomepage);
}
if ($CFG->defaulthomepage == HOMEPAGE_USER) {
$userhomepage = get_user_preferences('user_home_page_preference');
if ($userhomepage = clean_param($userhomepage, PARAM_LOCALURL)) {
if (substr((string)$userhomepage, 0, 1) === '/' &&
($userhomepage = clean_param($CFG->wwwroot . $userhomepage, PARAM_LOCALURL))) {
return new \core\url($userhomepage);
}
}

View File

@ -5301,7 +5301,7 @@ EOT;
'Logged user. URL set as default home page.' => [
'user' => 'logged',
'expected' => HOMEPAGE_URL,
'defaulthomepage' => "{$CFG->wwwroot}/home",
'defaulthomepage' => "/home",
],
'Logged user. User preference set as default page with dashboard enabled and user preference set to dashboard' => [
'user' => 'logged',
@ -5336,7 +5336,7 @@ EOT;
'expected' => HOMEPAGE_URL,
'defaulthomepage' => HOMEPAGE_USER,
'enabledashboard' => null,
'userpreference' => "{$CFG->wwwroot}/home",
'userpreference' => "/home",
],
];
}
@ -5374,15 +5374,23 @@ EOT;
$this->assertNull(get_default_home_page_url());
// Site configuration.
$CFG->defaulthomepage = "{$CFG->wwwroot}/home";
$this->assertEquals($CFG->defaulthomepage, get_default_home_page_url());
$CFG->defaulthomepage = "/home";
$this->assertEquals("{$CFG->wwwroot}/home", get_default_home_page_url());
// Site configuration with invalid value.
$CFG->defaulthomepage = "home";
$this->assertNull(get_default_home_page_url());
// User preference.
$CFG->defaulthomepage = HOMEPAGE_USER;
$userpreference = "{$CFG->wwwroot}/about";
$userpreference = "/about";
set_user_preference('user_home_page_preference', $userpreference);
$this->assertEquals($userpreference, get_default_home_page_url());
$this->assertEquals("{$CFG->wwwroot}/about", get_default_home_page_url());
// User preference with invalid value.
set_user_preference('user_home_page_preference', "about");
$this->assertNull(get_default_home_page_url());
}
/**

View File

@ -1735,4 +1735,32 @@ calendar,core_calendar|/calendar/view.php?view=month',
// Cleanup.
$dbman->drop_table($table);
}
/**
* Test for upgrade script replacing full urls with relative urls in defaulthomepage setting
*
* @covers ::upgrade_change_binary_column_to_int()
*/
public function test_upgrade_store_relative_url_sitehomepage(): void {
global $CFG;
$this->resetAfterTest();
// Check updating the value for the defaulthomepage.
$CFG->defaulthomepage = $CFG->wwwroot . '/page1';
upgrade_store_relative_url_sitehomepage();
$this->assertEquals('/page1', $CFG->defaulthomepage);
$CFG->defaulthomepage = HOMEPAGE_SITE;
upgrade_store_relative_url_sitehomepage();
$this->assertEquals(HOMEPAGE_SITE, $CFG->defaulthomepage);
// Check updating user preferences.
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
set_user_preference('user_home_page_preference', $CFG->wwwroot . '/page2', $user1);
set_user_preference('user_home_page_preference', HOMEPAGE_MY, $user2);
upgrade_store_relative_url_sitehomepage();
$this->assertEquals('/page2', get_user_preferences('user_home_page_preference', null, $user1->id));
$this->assertEquals(HOMEPAGE_MY, get_user_preferences('user_home_page_preference', null, $user2->id));
}
}

View File

@ -50,11 +50,11 @@ final class extend_default_homepage {
/**
* To be called by callback to add an option
*
* @param url $url
* @param url $url URL that can be used as a site homepage. Must be a local URL.
* @param lang_string|string $title
*/
public function add_option(url $url, lang_string|string $title): void {
$this->options[(string) $url] = $title;
$this->options[$url->out_as_local_url()] = $title;
}
/**

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2024092400.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2024092400.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.5dev+ (Build: 20240924)'; // Human-friendly version name

View File

@ -303,23 +303,23 @@ final class externallib_test extends externallib_advanced_testcase {
$this->assertEquals(HOMEPAGE_MY, $result['userhomepage']);
$this->assertArrayNotHasKey('userhomepageurl', $result);
$CFG->defaulthomepage = "{$CFG->wwwroot}/home";
$CFG->defaulthomepage = "/home";
$result = \core_webservice_external::get_site_info();
$result = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result);
$this->assertEquals(HOMEPAGE_URL, $result['userhomepage']);
$this->assertEquals($CFG->defaulthomepage, $result['userhomepageurl']);
$this->assertEquals("{$CFG->wwwroot}/home", $result['userhomepageurl']);
// User preference.
$CFG->defaulthomepage = HOMEPAGE_USER;
$userpreference = "{$CFG->wwwroot}/about";
$userpreference = "/about";
set_user_preference('user_home_page_preference', $userpreference);
$result = \core_webservice_external::get_site_info();
$result = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result);
$this->assertEquals(HOMEPAGE_URL, $result['userhomepage']);
$this->assertEquals($userpreference, $result['userhomepageurl']);
$this->assertEquals("{$CFG->wwwroot}/about", $result['userhomepageurl']);
}
}