MDL-71680 navigation: Add unit tests for force_nodes_into_more_menu()

- Part of: MDL-69588
This commit is contained in:
Mihail Geshoski 2021-06-03 23:18:11 +08:00 committed by Mathew May
parent 73154233e0
commit e3f35a2f22

View File

@ -138,4 +138,124 @@ class secondary_test extends \advanced_testcase {
'Testing in a site admin' => ['system', 'siteadminnode', 'homeheader', 'Site administration'],
];
}
/**
* Test the force_nodes_into_more_menu method.
*
* @param array $secondarynavnodesdata The array which contains the data used to generate the secondary navigation
* @param array $defaultmoremenunodes The array containing the keys of the navigation nodes which should be added
* to the "more" menu by default
* @param array $expecedmoremenunodes The array containing the keys of the expected navigation nodes which are
* forced into the "more" menu
* @dataProvider test_force_nodes_into_more_menu_provider
*/
public function test_force_nodes_into_more_menu(array $secondarynavnodesdata, array $defaultmoremenunodes,
array $expecedmoremenunodes) {
global $PAGE;
// Create a dummy secondary navigation.
$secondary = new secondary($PAGE);
foreach ($secondarynavnodesdata as $nodedata) {
$secondary->add($nodedata['text'], '#', secondary::TYPE_SETTING, null, $nodedata['key']);
}
$method = new ReflectionMethod('core\navigation\views\secondary', 'force_nodes_into_more_menu');
$method->setAccessible(true);
$method->invoke($secondary, $defaultmoremenunodes);
$actualmoremenunodes = [];
foreach ($secondary->children as $node) {
if ($node->forceintomoremenu) {
$actualmoremenunodes[] = $node->key;
}
}
// Assert that the actual nodes forced into the "more" menu matches the expected ones.
$this->assertEquals($expecedmoremenunodes, $actualmoremenunodes);
}
/**
* Data provider for the test_force_nodes_into_more_menu function.
*
* @return array
*/
public function test_force_nodes_into_more_menu_provider(): array {
return [
'The total number of navigation nodes exceeds the max display limit (6); ' .
'navnode2 and navnode4 are forced into "more" menu by default.' =>
[
[
[ 'text' => 'Navigation node 1', 'key' => 'navnode1'],
[ 'text' => 'Navigation node 2', 'key' => 'navnode2'],
[ 'text' => 'Navigation node 3', 'key' => 'navnode3'],
[ 'text' => 'Navigation node 4', 'key' => 'navnode4'],
[ 'text' => 'Navigation node 5', 'key' => 'navnode5'],
[ 'text' => 'Navigation node 6', 'key' => 'navnode6'],
[ 'text' => 'Navigation node 7', 'key' => 'navnode7'],
[ 'text' => 'Navigation node 8', 'key' => 'navnode8'],
[ 'text' => 'Navigation node 9', 'key' => 'navnode9'],
],
[
'navnode2',
'navnode4',
],
[
'navnode2',
'navnode4',
'navnode9',
],
],
'The total number of navigation nodes does not exceed the max display limit (6); ' .
'navnode2 and navnode4 are forced into "more" menu by default.' =>
[
[
[ 'text' => 'Navigation node 1', 'key' => 'navnode1'],
[ 'text' => 'Navigation node 2', 'key' => 'navnode2'],
[ 'text' => 'Navigation node 3', 'key' => 'navnode3'],
[ 'text' => 'Navigation node 4', 'key' => 'navnode4'],
[ 'text' => 'Navigation node 5', 'key' => 'navnode5'],
],
[
'navnode2',
'navnode4',
],
[
'navnode2',
'navnode4',
],
],
'The total number of navigation nodes exceeds the max display limit (6); ' .
'no forced navigation nodes into "more" menu by default.' =>
[
[
[ 'text' => 'Navigation node 1', 'key' => 'navnode1'],
[ 'text' => 'Navigation node 2', 'key' => 'navnode2'],
[ 'text' => 'Navigation node 3', 'key' => 'navnode3'],
[ 'text' => 'Navigation node 4', 'key' => 'navnode4'],
[ 'text' => 'Navigation node 5', 'key' => 'navnode5'],
[ 'text' => 'Navigation node 6', 'key' => 'navnode6'],
[ 'text' => 'Navigation node 7', 'key' => 'navnode7'],
[ 'text' => 'Navigation node 8', 'key' => 'navnode8'],
],
[],
[
'navnode7',
'navnode8',
],
],
'The total number of navigation nodes does not exceed the max display limit (6); ' .
'no forced navigation nodes into "more" menu by default.' =>
[
[
[ 'text' => 'Navigation node 1', 'key' => 'navnode1'],
[ 'text' => 'Navigation node 2', 'key' => 'navnode2'],
[ 'text' => 'Navigation node 3', 'key' => 'navnode3'],
[ 'text' => 'Navigation node 4', 'key' => 'navnode4'],
[ 'text' => 'Navigation node 5', 'key' => 'navnode5'],
[ 'text' => 'Navigation node 6', 'key' => 'navnode6'],
],
[],
[],
],
];
}
}