Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
/**
18
 * Auth email external functions tests.
19
 *
20
 * @package    auth_email
21
 * @category   external
22
 * @copyright  2016 Juan Leyva
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @since      Moodle 3.2
25
 */
26
 
27
namespace auth_email\external;
28
 
29
use auth_email_external;
30
use externallib_advanced_testcase;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
 
36
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
37
 
38
/**
39
 * External auth email API tests.
40
 *
41
 * @package     auth_email
42
 * @copyright   2016 Juan Leyva
43
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 * @since       Moodle 3.2
45
 */
1441 ariadna 46
final class external_test extends externallib_advanced_testcase {
1 efrain 47
 
48
    /** @var int custom profile field1 ID. */
49
    protected $field1;
50
 
51
    /** @var int custom profile field2 ID. */
52
    protected $field2;
53
 
54
    /**
55
     * Set up for every test
56
     */
57
    public function setUp(): void {
58
        global $CFG;
1441 ariadna 59
        parent::setUp();
1 efrain 60
 
61
        $this->resetAfterTest(true);
62
        $CFG->registerauth = 'email';
63
 
64
        $this->field1 = $this->getDataGenerator()->create_custom_profile_field(array(
65
                'shortname' => 'frogname', 'name' => 'Name of frog',
66
                'datatype' => 'text', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 1))->id;
67
        $this->field2 = $this->getDataGenerator()->create_custom_profile_field(array(
68
                'shortname' => 'sometext', 'name' => 'Some text in textarea',
69
                'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2))->id;
70
    }
71
 
11 efrain 72
    public function test_get_signup_settings(): void {
1 efrain 73
        global $CFG;
74
 
75
        $CFG->defaultcity = 'Bcn';
76
        $CFG->country = 'ES';
77
        $CFG->sitepolicy = 'https://moodle.org';
78
 
79
        $result = auth_email_external::get_signup_settings();
80
        $result = \core_external\external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result);
81
 
82
        // Check expected data.
83
        $this->assertEquals(array('firstname', 'lastname'), $result['namefields']);
84
        $this->assertEquals($CFG->defaultcity, $result['defaultcity']);
85
        $this->assertEquals($CFG->country, $result['country']);
86
        $this->assertEquals($CFG->sitepolicy, $result['sitepolicy']);
87
        $this->assertEquals(print_password_policy(), $result['passwordpolicy']);
88
        $this->assertNotContains('recaptchachallengehash', $result);
89
        $this->assertNotContains('recaptchachallengeimage', $result);
90
        // Check if the extended username chars is returning false when is not set.
91
        $this->assertFalse($result['extendedusernamechars']);
92
 
93
        // Whip up a array with named entries to easily check against.
94
        $namedarray = array();
95
        foreach ($result['profilefields'] as $key => $value) {
96
            $namedarray[$value['shortname']] = array(
97
                'datatype' => $value['datatype']
98
            );
99
        }
100
 
101
        // Just check if we have the fields from this test. If a plugin adds fields we'll let it slide.
102
        $this->assertArrayHasKey('frogname', $namedarray);
103
        $this->assertArrayHasKey('sometext', $namedarray);
104
 
105
        $this->assertEquals('text', $namedarray['frogname']['datatype']);
106
        $this->assertEquals('textarea', $namedarray['sometext']['datatype']);
107
 
108
        $CFG->extendedusernamechars = true;
109
        $result = auth_email_external::get_signup_settings();
110
        $result = \core_external\external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result);
111
        $this->assertTrue($result['extendedusernamechars']);
112
    }
113
 
114
    /**
115
     * Test get_signup_settings with mathjax in a profile field.
116
     */
11 efrain 117
    public function test_get_signup_settings_with_mathjax_in_profile_fields(): void {
1 efrain 118
        // Enable MathJax filter in content and headings.
119
        $this->configure_filters([
120
            ['name' => 'mathjaxloader', 'state' => TEXTFILTER_ON, 'move' => -1, 'applytostrings' => true],
121
        ]);
122
 
123
        // Create category with MathJax and a new field with MathJax.
124
        $categoryname = 'Cat $$(a+b)=2$$';
125
        $fieldname = 'Some text $$(a+b)=2$$';
126
        $categoryid = $this->getDataGenerator()->create_custom_profile_field_category(['name' => $categoryname])->id;
127
        $this->getDataGenerator()->create_custom_profile_field(array(
128
                'shortname' => 'mathjaxname', 'name' => $fieldname, 'categoryid' => $categoryid,
129
                'datatype' => 'textarea', 'signup' => 1, 'visible' => 1, 'required' => 1, 'sortorder' => 2));
130
 
131
        $result = auth_email_external::get_signup_settings();
132
        $result = \core_external\external_api::clean_returnvalue(auth_email_external::get_signup_settings_returns(), $result);
133
 
134
        // Format the original data.
135
        $sitecontext = \context_system::instance();
136
        $categoryname = \core_external\util::format_string($categoryname, $sitecontext->id);
137
        $fieldname = \core_external\util::format_string($fieldname, $sitecontext->id);
138
 
139
        // Whip up a array with named entries to easily check against.
140
        $namedarray = array();
141
        foreach ($result['profilefields'] as $key => $value) {
142
            $namedarray[$value['shortname']] = $value;
143
        }
144
 
145
        // Check the new profile field.
146
        $this->assertArrayHasKey('mathjaxname', $namedarray);
147
        $this->assertStringContainsString('<span class="filter_mathjaxloader_equation">',
148
                $namedarray['mathjaxname']['categoryname']);
149
        $this->assertStringContainsString('<span class="filter_mathjaxloader_equation">',
150
                $namedarray['mathjaxname']['name']);
151
        $this->assertEquals($categoryname, $namedarray['mathjaxname']['categoryname']);
152
        $this->assertEquals($fieldname, $namedarray['mathjaxname']['name']);
153
    }
154
 
11 efrain 155
    public function test_signup_user(): void {
1 efrain 156
        global $DB;
157
 
158
        $username = 'pepe';
159
        $password = 'abcdefAª.ªª!!3';
160
        $firstname = 'Pepe';
161
        $lastname = 'Pérez';
162
        $email = 'myemail@no.zbc';
163
        $city = 'Bcn';
164
        $country = 'ES';
165
        $customprofilefields = array(
166
            array(
167
                'type' => 'text',
168
                'name' => 'profile_field_frogname',
169
                'value' => 'random text',
170
            ),
171
            array(
172
                'type' => 'textarea',
173
                'name' => 'profile_field_sometext',
174
                'value' => json_encode(
175
                    array(
176
                        'text' => 'blah blah',
177
                        'format' => FORMAT_HTML
178
                    )
179
                ),
180
            )
181
        );
182
 
183
        // Create new user.
184
        $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country,
185
                                                    '', '', $customprofilefields);
186
        $result = \core_external\external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
187
        $this->assertTrue($result['success']);
188
        $this->assertEmpty($result['warnings']);
189
        $user = $DB->get_record('user', array('username' => $username));
190
        $this->assertEquals($firstname, $user->firstname);
191
        $this->assertEquals($lastname, $user->lastname);
192
        $this->assertEquals($email, $user->email);
193
        $this->assertEquals($city, $user->city);
194
        $this->assertEquals($country, $user->country);
195
        $this->assertEquals(0, $user->confirmed);
196
        $this->assertEquals(current_language(), $user->lang);
197
        $this->assertEquals('email', $user->auth);
198
        $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field1));
199
        $this->assertEquals($customprofilefields[0]['value'], $infofield->data);
200
        $infofield = $DB->get_record('user_info_data', array('userid' => $user->id, 'fieldid' => $this->field2));
201
        $this->assertEquals(json_decode($customprofilefields[1]['value'])->text, $infofield->data);
202
 
203
        // Try to create a user with the same username, email and password. We ommit also the profile fields.
204
        $password = 'abc';
205
        $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country,
206
                                                    '', '', $customprofilefields);
207
        $result = \core_external\external_api::clean_returnvalue(auth_email_external::signup_user_returns(), $result);
208
        $this->assertFalse($result['success']);
209
        $this->assertCount(3, $result['warnings']);
210
        $expectederrors = array('username', 'email', 'password');
211
        $finalerrors = [];
212
        foreach ($result['warnings'] as $warning) {
213
            $finalerrors[] = $warning['item'];
214
        }
215
        $this->assertEquals($expectederrors, $finalerrors);
216
 
217
        // Do not pass the required profile fields.
218
        $this->expectException('invalid_parameter_exception');
219
        $result = auth_email_external::signup_user($username, $password, $firstname, $lastname, $email, $city,  $country);
220
    }
221
}