From 65ae0a1bde83e344a10dce02f222479857a15898 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 | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/user/profile/field/menu/field.class.php b/user/profile/field/menu/field.class.php index 51b4f74fa40..99c3c3dbe2c 100644 --- a/user/profile/field/menu/field.class.php +++ b/user/profile/field/menu/field.class.php @@ -49,9 +49,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 index ad4e2887732..0bd21b44636 100644 --- a/user/tests/profilelib_test.php +++ b/user/tests/profilelib_test.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; -require_once($CFG->dirroot . '/user/profile/lib.php'); /** * Unit tests for user/profile/lib.php. @@ -40,7 +39,8 @@ class core_user_profilelib_testcase extends advanced_testcase { * with profile_user_record. */ public function test_get_custom_fields() { - global $DB; + global $DB, $CFG; + require_once($CFG->dirroot . '/user/profile/lib.php'); $this->resetAfterTest(); $user = $this->getDataGenerator()->create_user(); @@ -85,4 +85,20 @@ class core_user_profilelib_testcase extends advanced_testcase { // Check profile_user_record returns same field. $this->assertEquals(array('frogname'), array_keys((array)profile_user_record($user->id))); } + + /** + * 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/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); + } + } }