MDL-71683 navigation: Update the primary nav unit tests

- Part of: MDL-69588
This commit is contained in:
Mihail Geshoski 2021-06-11 16:00:43 +08:00 committed by Mathew May
parent 68237ac761
commit 5f998b1249

View file

@ -50,16 +50,24 @@ class primary_test extends \advanced_testcase {
* @dataProvider test_primary_export_provider * @dataProvider test_primary_export_provider
* @param bool $withcustom Setup with custom menu * @param bool $withcustom Setup with custom menu
* @param bool $withlang Setup with langs * @param bool $withlang Setup with langs
* @param string $userloggedin The type of user ('admin' or 'guest') if creating setup with logged in user,
* otherwise consider the user as non-logged in
* @param array $expecteditems An array of nodes expected with content in them. * @param array $expecteditems An array of nodes expected with content in them.
*/ */
public function test_primary_export(bool $withcustom, bool $withlang, array $expecteditems) { public function test_primary_export(bool $withcustom, bool $withlang, string $userloggedin, array $expecteditems) {
global $PAGE, $CFG; global $PAGE, $CFG;
if ($withcustom) { if ($withcustom) {
$CFG->custommenuitems = "Course search|/course/search.php $CFG->custommenuitems = "Course search|/course/search.php
Google|https://google.com.au/ Google|https://google.com.au/
Netflix|https://netflix.com/au"; Netflix|https://netflix.com/au";
} }
$this->setAdminUser(); if ($userloggedin === 'admin') {
$this->setAdminUser();
} else if ($userloggedin === 'guest') {
$this->setGuestUser();
} else {
$this->setUser(0);
}
// Mimic multiple langs installed. To trigger responses 'get_list_of_translations'. // Mimic multiple langs installed. To trigger responses 'get_list_of_translations'.
// Note: The text/title of the nodes generated will be 'English(fr), English(de)' but we don't care about this. // Note: The text/title of the nodes generated will be 'English(fr), English(de)' but we don't care about this.
@ -67,15 +75,36 @@ class primary_test extends \advanced_testcase {
if ($withlang) { if ($withlang) {
mkdir("$CFG->dataroot/lang/de", 0777, true); mkdir("$CFG->dataroot/lang/de", 0777, true);
mkdir("$CFG->dataroot/lang/fr", 0777, true); mkdir("$CFG->dataroot/lang/fr", 0777, true);
// Ensure the new langs are picked up and not taken from the cache.
$stringmanager = get_string_manager();
$stringmanager->reset_caches(true);
} }
$primary = new primary($PAGE); $primary = new primary($PAGE);
$renderer = $PAGE->get_renderer('core'); $renderer = $PAGE->get_renderer('core');
$data = $primary->export_for_template($renderer); $data = array_filter($primary->export_for_template($renderer));
// Assert that the number of returned menu items equals the expected result.
$this->assertCount(count($expecteditems), $data);
// Assert that returned menu items match the expected items.
foreach ($data as $menutype => $value) { foreach ($data as $menutype => $value) {
if ($value) { $this->assertTrue(in_array($menutype, $expecteditems));
$this->assertTrue(in_array($menutype, $expecteditems)); }
// When the user is logged in (excluding guest access), assert that lang menu is included as a part of the
// user menu when multiple languages are installed.
if (isloggedin() && !isguestuser()) {
// Look for a language menu item within the user menu items.
$usermenulang = array_filter($data['user']['items'], function($usermenuitem) {
return $usermenuitem->title === get_string('language');
});
if ($withlang) { // If multiple languages are installed.
// Assert that the language menu exists within the user menu.
$this->assertNotEmpty($usermenulang);
} else { // If the aren't any additional installed languages.
$this->assertEmpty($usermenulang);
} }
} else { // Otherwise assert that the user menu does not contain any items.
$this->assertArrayNotHasKey('items', $data['user']);
} }
} }
@ -86,17 +115,32 @@ class primary_test extends \advanced_testcase {
*/ */
public function test_primary_export_provider(): array { public function test_primary_export_provider(): array {
return [ return [
"Export the menu data with custom and lang menu" => [ "Export the menu data when: custom menu exists; multiple langs installed; user is not logged in." => [
true, true, ['primary', 'custom', 'lang', 'user'] true, true, '', ['primary', 'custom', 'lang', 'user']
], ],
"Export the menu data with custom menu" => [ "Export the menu data when: custom menu exists; langs not installed; user is not logged in." => [
true, false, ['primary', 'custom', 'user'] true, false, '', ['primary', 'custom', 'user']
], ],
"Export the menu data with lang menu" => [ "Export the menu data when: custom menu exists; multiple langs installed; logged in as admin." => [
false, true, ['primary', 'lang', 'user'] true, true, 'admin', ['primary', 'custom', 'user']
], ],
"Export the menu data without the custom and lang menu" => [ "Export the menu data when: custom menu exists; langs not installed; logged in as admin." => [
false, false, ['primary', 'user'] true, false, 'admin', ['primary', 'custom', 'user']
],
"Export the menu data when: custom menu exists; multiple langs installed; logged in as guest." => [
true, true, 'guest', ['primary', 'custom', 'lang', 'user']
],
"Export the menu data when: custom menu exists; langs not installed; logged in as guest." => [
true, false, 'guest', ['primary', 'custom', 'user']
],
"Export the menu data when: custom menu does not exist; multiple langs installed; logged in as guest." => [
false, true, 'guest', ['primary', 'lang', 'user']
],
"Export the menu data when: custom menu does not exist; multiple langs installed; logged in as admin." => [
false, true, 'admin', ['primary', 'user']
],
"Export the menu data when: custom menu does not exist; langs not installed; user is not logged in." => [
false, false, '', ['primary', 'user']
], ],
]; ];
} }
@ -112,36 +156,45 @@ class primary_test extends \advanced_testcase {
public function test_get_lang_menu(bool $withadditionallangs, string $language, array $expected) { public function test_get_lang_menu(bool $withadditionallangs, string $language, array $expected) {
global $CFG, $PAGE; global $CFG, $PAGE;
force_current_language($language);
// Mimic multiple langs installed. To trigger responses 'get_list_of_translations'. // Mimic multiple langs installed. To trigger responses 'get_list_of_translations'.
// Note: The text/title of the nodes generated will be 'English(fr), English(de)' but we don't care about this. // Note: The text/title of the nodes generated will be 'English(fr), English(de)' but we don't care about this.
// We are testing whether the nodes gets generated when the lang menu is available. // We are testing whether the nodes gets generated when the lang menu is available.
if ($withadditionallangs) { if ($withadditionallangs) {
mkdir("$CFG->dataroot/lang/de", 0777, true); mkdir("$CFG->dataroot/lang/de", 0777, true);
mkdir("$CFG->dataroot/lang/fr", 0777, true); mkdir("$CFG->dataroot/lang/fr", 0777, true);
// Ensure the new langs are picked up and not taken from the cache.
$stringmanager = get_string_manager();
$stringmanager->reset_caches(true);
} }
force_current_language($language);
$output = new primary($PAGE); $output = new primary($PAGE);
$method = new ReflectionMethod('core\navigation\output\primary', 'get_lang_menu'); $method = new ReflectionMethod('core\navigation\output\primary', 'get_lang_menu');
$method->setAccessible(true); $method->setAccessible(true);
$renderer = $PAGE->get_renderer('core'); $renderer = $PAGE->get_renderer('core');
$response = $method->invoke($output, $renderer); $response = $method->invoke($output, $renderer);
if (!$withadditionallangs) {
if ($withadditionallangs) { // If there are multiple languages installed.
// Assert that the title of the language menu matches the expected one.
$this->assertEquals($expected['title'], $response['title']);
// Assert that the number of language menu items matches the number of the expected items.
$this->assertEquals(count($expected['items']), count($response['items']));
foreach ($expected['items'] as $expecteditem) {
// We need to manually generate the url key and its value in the expected item array as this cannot
// be done in the data provider due to the change of the state of $PAGE.
$expecteditem['url'] = $expecteditem['isactive'] ? new \moodle_url('#') :
new \moodle_url($PAGE->url, ['lang' => $expecteditem['lang']]);
// The lang value is only used to generate the url, so this key can be removed.
unset($expecteditem['lang']);
// Assert that the given expected item exists in the returned items.
$this->assertTrue(in_array($expecteditem, $response['items']));
}
} else { // No multiple languages.
$this->assertEquals($expected, $response); $this->assertEquals($expected, $response);
} }
$expectedurls = array_map(function ($value) use ($PAGE) {
$url = new \moodle_url($PAGE->url, ['lang' => $value]);
return $url->out();
}, array_keys($expected));
$expectedurls[] = "#";
// Make sure the urls match up.
foreach ($response as $lang) {
$this->assertTrue(in_array($lang['url']->out(), $expectedurls));
}
} }
/** /**
@ -156,15 +209,60 @@ class primary_test extends \advanced_testcase {
], ],
'Lang menu with only multiple languages installed' => [ 'Lang menu with only multiple languages installed' => [
true, 'en', [ true, 'en', [
'de' => 'English (de)', 'title' => 'English (en)',
'fr' => 'English (fr)', 'items' => [
] [
'title' => 'English (en)',
'text' => 'English (en)',
'link' => true,
'isactive' => true,
'lang' => 'en'
],
[
'title' => 'English (de)',
'text' => 'English (de)',
'link' => true,
'isactive' => false,
'lang' => 'de'
],
[
'title' => 'English (fr)',
'text' => 'English (fr)',
'link' => true,
'isactive' => false,
'lang' => 'fr'
],
],
],
], ],
'Lang menu with only multiple languages installed and other than EN set active.' => [ 'Lang menu with only multiple languages installed and other than EN set active.' => [
true, 'de', [ true, 'de', [
'en' => 'English (en)', 'title' => 'English (de)',
'fr' => 'English (fr)', 'items' => [
] [
'title' => 'English (en)',
'text' => 'English (en)',
'link' => true,
'isactive' => false,
'lang' => 'en'
],
[
'title' => 'English (de)',
'text' => 'English (de)',
'link' => true,
'isactive' => true,
'lang' => 'de'
],
[
'title' => 'English (fr)',
'text' => 'English (fr)',
'link' => true,
'isactive' => false,
'lang' => 'fr'
],
],
],
], ],
]; ];
} }