Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
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.
8
//
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.
13
//
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/>.
16
 
17
namespace tool_moodlenet;
18
 
19
/**
20
 * Unit tests for the profile manager
21
 *
22
 * @package    tool_moodlenet
23
 * @category   test
24
 * @copyright  2020 Adrian Greeve <adrian@moodle.com>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class profile_manager_test extends \advanced_testcase {
28
 
29
    /**
30
     * Test that on this site we use the user table to hold moodle net profile information.
31
     */
11 efrain 32
    public function test_official_profile_exists(): void {
1 efrain 33
        $this->assertTrue(\tool_moodlenet\profile_manager::official_profile_exists());
34
    }
35
 
36
    /**
37
     * Test a null is returned when the user's mnet profile field is not set.
38
     */
11 efrain 39
    public function test_get_moodlenet_user_profile_no_profile_set(): void {
1 efrain 40
        $this->resetAfterTest();
41
        $user = $this->getDataGenerator()->create_user();
42
 
43
        $result = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($user->id);
44
        $this->assertNull($result);
45
    }
46
 
47
    /**
48
     * Test a null is returned when the user's mnet profile field is not set.
49
     */
11 efrain 50
    public function test_moodlenet_user_profile_creation_no_profile_set(): void {
1 efrain 51
        $this->resetAfterTest();
52
        $user = $this->getDataGenerator()->create_user();
53
 
54
        $this->expectException(\moodle_exception::class);
55
        $this->expectExceptionMessage(get_string('invalidmoodlenetprofile', 'tool_moodlenet'));
56
        $result = new \tool_moodlenet\moodlenet_user_profile("", $user->id);
57
    }
58
 
59
    /**
60
     * Test the return of a moodle net profile.
61
     */
11 efrain 62
    public function test_get_moodlenet_user_profile(): void {
1 efrain 63
        $this->resetAfterTest();
64
        $user = $this->getDataGenerator()->create_user(['moodlenetprofile' => '@matt@hq.mnet']);
65
 
66
        $result = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($user->id);
67
        $this->assertEquals($user->moodlenetprofile, $result->get_profile_name());
68
    }
69
 
70
    /**
71
     * Test the creation of a user profile category.
72
     */
11 efrain 73
    public function test_create_user_profile_category(): void {
1 efrain 74
        global $DB;
75
        $this->resetAfterTest();
76
 
77
        $basecategoryname = get_string('pluginname', 'tool_moodlenet');
78
 
79
        \tool_moodlenet\profile_manager::create_user_profile_category();
80
        $categoryname = \tool_moodlenet\profile_manager::get_category_name();
81
        $this->assertEquals($basecategoryname, $categoryname);
82
        \tool_moodlenet\profile_manager::create_user_profile_category();
83
 
84
        $recordcount = $DB->count_records('user_info_category', ['name' => $basecategoryname]);
85
        $this->assertEquals(1, $recordcount);
86
 
87
        // Test the duplication of categories to ensure a unique name is always used.
88
        $categoryname = \tool_moodlenet\profile_manager::get_category_name();
89
        $this->assertEquals($basecategoryname . 1, $categoryname);
90
        \tool_moodlenet\profile_manager::create_user_profile_category();
91
        $categoryname = \tool_moodlenet\profile_manager::get_category_name();
92
        $this->assertEquals($basecategoryname . 2, $categoryname);
93
    }
94
 
95
    /**
96
     * Test the creating of the custom user profile field to hold the moodle net profile.
97
     */
11 efrain 98
    public function test_create_user_profile_text_field(): void {
1 efrain 99
        global $DB;
100
        $this->resetAfterTest();
101
 
102
        $shortname = 'mnetprofile';
103
 
104
        $categoryid = \tool_moodlenet\profile_manager::create_user_profile_category();
105
        \tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
106
 
107
        $record = $DB->get_record('user_info_field', ['shortname' => $shortname]);
108
        $this->assertEquals($shortname, $record->shortname);
109
        $this->assertEquals($categoryid, $record->categoryid);
110
 
111
        // Test for a unique name if 'mnetprofile' is already in use.
112
        \tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
113
        $profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
114
        $this->assertEquals($shortname . 1, $profilename);
115
        \tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
116
        $profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
117
        $this->assertEquals($shortname . 2, $profilename);
118
    }
119
 
120
    /**
121
     * Test that the user moodlenet profile is saved.
122
     */
11 efrain 123
    public function test_save_moodlenet_user_profile(): void {
1 efrain 124
        $this->resetAfterTest();
125
 
126
        $user = $this->getDataGenerator()->create_user();
127
        $profilename = '@matt@hq.mnet';
128
 
129
        $moodlenetprofile = new \tool_moodlenet\moodlenet_user_profile($profilename, $user->id);
130
 
131
        \tool_moodlenet\profile_manager::save_moodlenet_user_profile($moodlenetprofile);
132
 
133
        $userdata = \core_user::get_user($user->id);
134
        $this->assertEquals($profilename, $userdata->moodlenetprofile);
135
    }
136
}