Merge branch 'MDL-78806-master' of https://github.com/junpataleta/moodle

This commit is contained in:
Ilya Tregubov 2023-09-13 10:54:54 +08:00
commit 94c9ce0595
No known key found for this signature in database
GPG key ID: 0F58186F748E55C1
52 changed files with 223 additions and 82 deletions

View file

@ -8722,8 +8722,6 @@ function admin_externalpage_setup($section, $extrabutton = '', array $extraurlpa
$USER->editing = $adminediting;
}
$visiblepathtosection = array_reverse($extpage->visiblepath);
if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
if ($PAGE->user_is_editing()) {
$caption = get_string('blockseditoff');
@ -8735,7 +8733,7 @@ function admin_externalpage_setup($section, $extrabutton = '', array $extraurlpa
$PAGE->set_button($OUTPUT->single_button($url, $caption, 'get'));
}
$PAGE->set_title("$SITE->shortname: " . implode(": ", $visiblepathtosection));
$PAGE->set_title(implode(moodle_page::TITLE_SEPARATOR, $extpage->visiblepath));
$PAGE->set_heading($SITE->fullname);
if ($hassiteconfig && empty($options['nosearch'])) {

View file

@ -344,7 +344,7 @@ function install_print_header($config, $stagename, $heading, $stagetext, $stagec
<link rel="shortcut icon" href="theme/clean/pix/favicon.ico" />';
echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/install/css.php" />
<title>'.get_string('installation','install').' - Moodle '.$CFG->target_release.'</title>
<title>'.get_string('installation', 'install') . moodle_page::TITLE_SEPARATOR . 'Moodle '.$CFG->target_release.'</title>
<meta name="robots" content="noindex">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />

View file

@ -124,6 +124,11 @@ class moodle_page {
*/
const STATE_DONE = 3;
/**
* The separator used for separating page title elements.
*/
const TITLE_SEPARATOR = ' | ';
/**
* @var int The current state of the page. The state a page is within
* determines what actions are possible for it.
@ -1367,14 +1372,47 @@ class moodle_page {
/**
* Sets the title for the page.
*
* This is normally used within the title tag in the head of the page.
*
* Some tips for providing a meaningful page title:
* - The page title must be accurate and informative.
* - If the page causes a change of context (e.g. a search functionality), it should describe the result or change of context
* to the user.
* - It should be concise.
* - If possible, it should uniquely identify the page.
* - The most identifying information should come first. (e.g. Submit assignment | Assignment | Moodle)
*
* For more information, see
* {@link https://www.w3.org/WAI/WCAG21/Understanding/page-titled Understanding Success Criterion 2.4.2: Page Titled}
*
* @param string $title the title that should go in the <head> section of the HTML of this page.
* @param bool $appendsitename Appends site name at the end of the given title. It is encouraged to append the site name as this
* especially helps with accessibility. If it's necessary to override this, please keep in mind
* to ensure that the title provides a concise summary of the page being displayed.
*/
public function set_title($title) {
public function set_title($title, bool $appendsitename = true) {
global $CFG;
$title = format_string($title);
$title = strip_tags($title);
$title = str_replace('"', '&quot;', $title);
if ($appendsitename) {
// Append the site name at the end of the page title.
$sitenamedisplay = 'shortname';
if (!empty($CFG->sitenameintitle)) {
$sitenamedisplay = $CFG->sitenameintitle;
}
$site = get_site();
if (empty(trim($site->{$sitenamedisplay}))) {
// If for some reason the site name is not yet set, fall back to 'Moodle'.
$title .= self::TITLE_SEPARATOR . 'Moodle';
} else {
$title .= self::TITLE_SEPARATOR . format_string($site->{$sitenamedisplay});
}
}
$this->_title = $title;
}
@ -1778,11 +1816,7 @@ class moodle_page {
'/settings.php?section=maintenancemode">' . get_string('maintenancemode', 'admin') .
'</a> ' . $this->button);
$title = $this->title;
if ($title) {
$title .= ' - ';
}
$this->set_title($title . get_string('maintenancemode', 'admin'));
$this->set_title(get_string('maintenancemode', 'admin'));
}
$this->initialise_standard_body_classes();

View file

@ -2358,4 +2358,36 @@ EOF;
}
}
/**
* Check that the page title contains a given string.
*
* @Given the page title should contain ":title"
* @param string $title The string that should be present on the page title.
*/
public function the_page_title_should_contain(string $title): void {
$session = $this->getSession();
if ($this->running_javascript()) {
// When running on JS, the page title can be changed via JS, so it's more reliable to get the actual page title via JS.
$actualtitle = $session->evaluateScript("return document.title");
} else {
$titleelement = $session->getPage()->find('css', 'head title');
if ($titleelement === null) {
// Throw an exception if a page title is not present on the page.
throw new ElementNotFoundException(
$this->getSession(),
'<title> element',
'css',
'head title'
);
}
$actualtitle = $titleelement->getText();
}
if (!str_contains($actualtitle, $title)) {
throw new ExpectationException(
"'$title' was not found from the current page title '$actualtitle'",
$session
);
}
}
}

View file

@ -520,7 +520,7 @@ EOF;
'or that your web server is correctly set up and started.';
$this->find(
"xpath", "//head/child::title[normalize-space(.)='" . behat_util::BEHATSITENAME . "']",
"xpath", "//head/child::title[contains(., '" . behat_util::BEHATSITENAME . "')]",
new ExpectationException($message, $session)
);

View file

@ -325,11 +325,66 @@ class moodle_page_test extends \advanced_testcase {
$this->assertSame('a heading <a href="#">edit</a><p></p>', $this->testpage->heading);
}
public function test_set_title() {
// Exercise SUT.
$this->testpage->set_title('a title');
/**
* Data provider for {@see test_set_title}.
*
* @return array
*/
public function set_title_provider(): array {
return [
'Do not append the site name' => [
'shortname', false, '', false
],
'Site not yet installed not configured defaults to site shortname' => [
null, true, 'shortname'
],
'$CFG->sitenameintitle not configured defaults to site shortname' => [
null, true, 'shortname'
],
'$CFG->sitenameintitle set to shortname' => [
'shortname', true, 'shortname'
],
'$CFG->sitenameintitle set to fullname' => [
'fullname', true, 'fullname'
],
];
}
/**
* Test for set_title
*
* @dataProvider set_title_provider
* @param string|null $config The config value for $CFG->sitenameintitle.
* @param bool $appendsitename The $appendsitename parameter
* @param string $expected The expected site name to be appended to the title.
* @param bool $sitenameset To simulate the absence of the site name being set in the site.
* @return void
* @covers ::set_title
*/
public function test_set_title(?string $config, bool $appendsitename, string $expected, bool $sitenameset = true): void {
global $CFG, $SITE;
if ($config !== null) {
$CFG->sitenameintitle = $config;
}
$title = "A title";
if ($appendsitename) {
if ($sitenameset) {
$expectedtitle = $title . moodle_page::TITLE_SEPARATOR . $SITE->{$expected};
} else {
// Simulate site fullname and shortname being empty for any reason.
$SITE->fullname = null;
$SITE->shortname = null;
$expectedtitle = $title . moodle_page::TITLE_SEPARATOR . 'Moodle';
}
} else {
$expectedtitle = $title;
}
$this->testpage->set_title($title, $appendsitename);
// Validated.
$this->assertSame('a title', $this->testpage->title);
$this->assertSame($expectedtitle, $this->testpage->title);
}
public function test_default_pagelayout() {

View file

@ -169,6 +169,14 @@ being forced open in all behat tests.
* Added a new method called exceeds_password_length in moodlelib.php to validate the password length.
* The core/modal_factory has been deprecated. From Moodle 4.3 onwards please instantiate new modals using the ModalType.create method instead.
Please note that this method does not support the `trigger` option.
* \moodle_page::set_title() has been updated to append the site name depending on the value of $CFG->sitenameintitle and whether
the site's fullname/shortname has been set. So there's no need to manually add the site name whenever calling $PAGE->set_title().
If it's necessary to override this, pass `false` to its new optional parameter `$appendsitename`.
* New page title separator constant `moodle_page:TITLE_SEPARATOR` has been created to help standardise the separators used in page
titles.
* New Behat step \behat_general::the_page_title_should_contain() has been added to allow checking of page titles. You can use this
when writing feature files to check that the page title contains the expected string.
e.g. `And the page title should contain "Some title"`
=== 4.2 ===

View file

@ -1584,7 +1584,7 @@ function upgrade_started($preinstall=false) {
$strupgrade = get_string('upgradingversion', 'admin');
$PAGE->set_pagelayout('maintenance');
upgrade_init_javascript();
$PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
$PAGE->set_title($strupgrade . moodle_page::TITLE_SEPARATOR . 'Moodle ' . $CFG->target_release);
$PAGE->set_heading($strupgrade);
$PAGE->navbar->add($strupgrade);
$PAGE->set_cacheable(false);

View file

@ -3210,7 +3210,6 @@ function print_maintenance_message() {
$PAGE->set_pagetype('maintenance-message');
$PAGE->set_pagelayout('maintenance');
$PAGE->set_title(strip_tags($SITE->fullname));
$PAGE->set_heading($SITE->fullname);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('sitemaintenance', 'admin'));