2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for user/editlib.php.
22 * @copyright 2013 Adrian Greeve <adrian@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
.'/user/editlib.php');
32 * Unit tests for user editlib api.
36 * @copyright 2013 Adrian Greeve <adrian@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_user_editlib_testcase
extends advanced_testcase
{
42 * Test that the required fields are returned in the correct order.
44 function test_useredit_get_required_name_fields() {
46 // Back up config settings for restore later.
47 $originalcfg = new stdClass();
48 $originalcfg->fullnamedisplay
= $CFG->fullnamedisplay
;
50 $CFG->fullnamedisplay
= 'language';
51 $expectedresult = array(5 => 'firstname', 21 => 'lastname');
52 $this->assertEquals(useredit_get_required_name_fields(), $expectedresult);
53 $CFG->fullnamedisplay
= 'firstname';
54 $expectedresult = array(5 => 'firstname', 21 => 'lastname');
55 $this->assertEquals(useredit_get_required_name_fields(), $expectedresult);
56 $CFG->fullnamedisplay
= 'lastname firstname';
57 $expectedresult = array('lastname', 9 => 'firstname');
58 $this->assertEquals(useredit_get_required_name_fields(), $expectedresult);
59 $CFG->fullnamedisplay
= 'firstnamephonetic lastnamephonetic';
60 $expectedresult = array(5 => 'firstname', 21 => 'lastname');
61 $this->assertEquals(useredit_get_required_name_fields(), $expectedresult);
63 // Tidy up after we finish testing.
64 $CFG->fullnamedisplay
= $originalcfg->fullnamedisplay
;
68 * Test that the enabled fields are returned in the correct order.
70 function test_useredit_get_enabled_name_fields() {
72 // Back up config settings for restore later.
73 $originalcfg = new stdClass();
74 $originalcfg->fullnamedisplay
= $CFG->fullnamedisplay
;
76 $CFG->fullnamedisplay
= 'language';
77 $expectedresult = array();
78 $this->assertEquals(useredit_get_enabled_name_fields(), $expectedresult);
79 $CFG->fullnamedisplay
= 'firstname lastname firstnamephonetic';
80 $expectedresult = array(19 => 'firstnamephonetic');
81 $this->assertEquals(useredit_get_enabled_name_fields(), $expectedresult);
82 $CFG->fullnamedisplay
= 'firstnamephonetic, lastname lastnamephonetic (alternatename)';
83 $expectedresult = array('firstnamephonetic', 28 => 'lastnamephonetic', 46 => 'alternatename');
84 $this->assertEquals(useredit_get_enabled_name_fields(), $expectedresult);
85 $CFG->fullnamedisplay
= 'firstnamephonetic lastnamephonetic alternatename middlename';
86 $expectedresult = array('firstnamephonetic', 18 => 'lastnamephonetic', 35 => 'alternatename', 49 => 'middlename');
87 $this->assertEquals(useredit_get_enabled_name_fields(), $expectedresult);
89 // Tidy up after we finish testing.
90 $CFG->fullnamedisplay
= $originalcfg->fullnamedisplay
;
94 * Test that the disabled fields are returned.
96 function test_useredit_get_disabled_name_fields() {
98 // Back up config settings for restore later.
99 $originalcfg = new stdClass();
100 $originalcfg->fullnamedisplay
= $CFG->fullnamedisplay
;
102 $CFG->fullnamedisplay
= 'language';
103 $expectedresult = array('firstnamephonetic' => 'firstnamephonetic', 'lastnamephonetic' => 'lastnamephonetic',
104 'middlename' => 'middlename', 'alternatename' => 'alternatename');
105 $this->assertEquals(useredit_get_disabled_name_fields(), $expectedresult);
106 $CFG->fullnamedisplay
= 'firstname lastname firstnamephonetic';
107 $expectedresult = array('lastnamephonetic' => 'lastnamephonetic', 'middlename' => 'middlename', 'alternatename' => 'alternatename');
108 $this->assertEquals(useredit_get_disabled_name_fields(), $expectedresult);
109 $CFG->fullnamedisplay
= 'firstnamephonetic, lastname lastnamephonetic (alternatename)';
110 $expectedresult = array('middlename' => 'middlename');
111 $this->assertEquals(useredit_get_disabled_name_fields(), $expectedresult);
112 $CFG->fullnamedisplay
= 'firstnamephonetic lastnamephonetic alternatename middlename';
113 $expectedresult = array();
114 $this->assertEquals(useredit_get_disabled_name_fields(), $expectedresult);
116 // Tidy up after we finish testing.
117 $CFG->fullnamedisplay
= $originalcfg->fullnamedisplay
;