MDL-48542 user_menu: Improve user menu tests

Add additional test cases, and actually test that the output is roughly
what we expect.
This commit is contained in:
Andrew Nicols 2015-01-28 11:54:07 +08:00
parent 0977d3d404
commit 8b7ca27d48

View File

@ -23,20 +23,33 @@
*/ */
class core_user_menu_testcase extends advanced_testcase { class core_user_menu_testcase extends advanced_testcase {
public function test_custom_user_menu() { /**
* Custom user menu data for the test_custom_user_menu test.
*
* @return array containing testing data
*/
public function custom_user_menu_data() {
return array(
// These are fillers only.
array('###', 0, 1),
array('#####', 0, 1),
global $CFG, $OUTPUT, $USER, $PAGE; // These are invalid and will not generate any entry or filler.
array('-----', 0, 0),
array('_____', 0, 0),
array('test', 0, 0),
$this->resetAfterTest(true); // These are unusual, but valid and will generate a menu entry (no filler).
$this->expectOutputString(''); array('-|-|-|-', 1, 0),
array('-|-|-', 1, 0),
array('-|-', 1, 0),
array('#f234|2', 1, 0),
// Test using an admin user at the root of Moodle; this way we // This is a pretty typical entry.
// don't have to create a test user with avatar. array('messages,message|/message/index.php|message', 1, 0),
$this->setAdminUser();
$PAGE->set_url('/');
// Build a test string for the custom user menu items setting. // And these are combinations containing both valid and invalid.
$usermenuitems = 'messages,message|/message/index.php|message array('messages,message|/message/index.php|message
myfiles,moodle|/user/files.php|download myfiles,moodle|/user/files.php|download
### ###
mybadges,badges|/badges/mybadges.php|award mybadges,badges|/badges/mybadges.php|award
@ -44,26 +57,42 @@ mybadges,badges|/badges/mybadges.php|award
test test
- -
##### #####
#f234|2'; #f234|2', 5, 2),
set_config('customusermenuitems', $usermenuitems); );
}
// Fail the test and dump output if an exception is thrown /**
// during user menu creation. * Test the custom user menu.
$valid = true; *
try { * @dataProvider custom_user_menu_data
$usermenu = $OUTPUT->user_menu($USER); * @param string $input The menu text to test
} catch (moodle_exception $me) { * @param int $entrycount The numbers of entries expected
$valid = false; */
printf( public function test_custom_user_menu($data, $entrycount, $dividercount) {
"[%s] %s: %s\n%s\n", global $CFG, $OUTPUT, $USER, $PAGE;
$me->module,
$me->errorcode,
$me->message,
$me->debuginfo
);
}
$this->assertTrue($valid);
// Must reset because of config and user modifications.
$this->resetAfterTest(true);
// Test using an admin user at the root of Moodle; this way we don't have to create a test user with avatar.
$this->setAdminUser();
$PAGE->set_url('/');
// Set the configuration.
set_config('customusermenuitems', $data);
// We always add two dividers as standard.
$dividercount += 2;
// The basic entry count will additionally include the wrapper menu, My home, My profile, and the Logout link.
$entrycount += 4;
$output = $OUTPUT->user_menu($USER);
preg_match_all('/<a [^>]+role="menuitem"[^>]+>/', $output, $results);
$this->assertCount($entrycount, $results[0]);
preg_match_all('/<span class="filler">/', $output, $results);
$this->assertCount($dividercount, $results[0]);
} }
} }