From f01e4739fd62174d6b829d19dc01ba856b400aaa Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 21 May 2014 11:40:12 +0800 Subject: [PATCH] MDL-45469 profile: make sure profile fields can be initialised without arguments --- user/profile/field/menu/field.class.php | 8 +++- user/tests/profilelib_test.php | 53 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 user/tests/profilelib_test.php diff --git a/user/profile/field/menu/field.class.php b/user/profile/field/menu/field.class.php index c0b8694134b..23069c7c31e 100644 --- a/user/profile/field/menu/field.class.php +++ b/user/profile/field/menu/field.class.php @@ -14,9 +14,13 @@ class profile_field_menu extends profile_field_base { $this->profile_field_base($fieldid, $userid); /// Param 1 for menu type is the options - $options = explode("\n", $this->field->param1); + if (isset($this->field->param1)) { + $options = explode("\n", $this->field->param1); + } else { + $options = array(); + } $this->options = array(); - if ($this->field->required){ + if (!empty($this->field->required)) { $this->options[''] = get_string('choose').'...'; } foreach($options as $key => $option) { diff --git a/user/tests/profilelib_test.php b/user/tests/profilelib_test.php new file mode 100644 index 00000000000..fe58f4abdc5 --- /dev/null +++ b/user/tests/profilelib_test.php @@ -0,0 +1,53 @@ +. + +/** + * Unit tests for user/profile/lib.php. + * + * @package core_user + * @copyright 2014 The Open University + * @licensehttp://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +/** + * Unit tests for user/profile/lib.php. + * + * @package core_user + * @copyright 2014 The Open University + * @licensehttp://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_user_profilelib_testcase extends advanced_testcase { + /** + * Make sure that all profile fields can be initialised without arguments. + */ + public function test_default_constructor() { + global $DB, $CFG; + require_once($CFG->dirroot . '/user/profile/lib.php'); + require_once($CFG->dirroot . '/user/profile/definelib.php'); + $datatypes = profile_list_datatypes(); + foreach ($datatypes as $datatype => $datatypename) { + require_once($CFG->dirroot . '/user/profile/field/' . + $datatype . '/field.class.php'); + $newfield = 'profile_field_' . $datatype; + $formfield = new $newfield(); + $this->assertNotNull($formfield); + } + } +}