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 profilefield_text;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
global $CFG;
|
|
|
22 |
|
|
|
23 |
require_once($CFG->dirroot.'/user/profile/lib.php');
|
|
|
24 |
require_once($CFG->dirroot.'/user/profile/field/text/field.class.php');
|
|
|
25 |
|
|
|
26 |
use profile_field_text;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Unit tests for the profilefield_text.
|
|
|
30 |
*
|
|
|
31 |
* @package profilefield_text
|
|
|
32 |
* @copyright 2022 The Open University
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
* @covers \profilefield_text\profile_field_text
|
|
|
35 |
*/
|
|
|
36 |
class field_class_test extends \advanced_testcase {
|
|
|
37 |
/**
|
|
|
38 |
* Test that the profile text data is formatted and required filters applied
|
|
|
39 |
*
|
|
|
40 |
* @covers \profile_field_text::display_data
|
|
|
41 |
* @dataProvider filter_profile_field_text_provider
|
|
|
42 |
* @param string $input
|
|
|
43 |
* @param string $expected
|
|
|
44 |
*/
|
|
|
45 |
public function test_filter_display_data(string $input, string $expected): void {
|
|
|
46 |
$this->resetAfterTest();
|
|
|
47 |
$field = new profile_field_text();
|
|
|
48 |
$field->data = $input;
|
|
|
49 |
|
|
|
50 |
filter_set_global_state('multilang', TEXTFILTER_ON);
|
|
|
51 |
filter_set_global_state('emoticon', TEXTFILTER_ON);
|
|
|
52 |
filter_set_applies_to_strings('multilang', true);
|
|
|
53 |
|
|
|
54 |
$actual = $field->display_data();
|
|
|
55 |
$this->assertEquals($expected, $actual);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Data provider for {@see test_filter_display_data}
|
|
|
60 |
*
|
|
|
61 |
* @return string[]
|
|
|
62 |
*/
|
|
|
63 |
public function filter_profile_field_text_provider(): array {
|
|
|
64 |
return [
|
|
|
65 |
'simple_string' => ['Simple string', 'Simple string'],
|
|
|
66 |
'format_string' => ['HTML & is escaped', 'HTML & is escaped'],
|
|
|
67 |
'multilang_filter' =>
|
|
|
68 |
['<span class="multilang" lang="en">English</span><span class="multilang" lang="fr">French</span>', 'English'],
|
|
|
69 |
'emoticons_filter' => ['No emoticons filter :-(', 'No emoticons filter :-(']
|
|
|
70 |
];
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Test preprocess data validation
|
|
|
75 |
*/
|
|
|
76 |
public function test_edit_save_data_preprocess(): void {
|
|
|
77 |
$this->resetAfterTest();
|
|
|
78 |
|
|
|
79 |
$fielddata = $this->getDataGenerator()->create_custom_profile_field([
|
|
|
80 |
'datatype' => 'text',
|
|
|
81 |
'name' => 'Test',
|
|
|
82 |
'shortname' => 'test',
|
|
|
83 |
'param2' => 5, // Max length.
|
|
|
84 |
]);
|
|
|
85 |
$field = new profile_field_text(0, 0, $fielddata);
|
|
|
86 |
|
|
|
87 |
$value = $field->edit_save_data_preprocess('ABCDE', new \stdClass());
|
|
|
88 |
$this->assertEquals('ABCDE', $value);
|
|
|
89 |
|
|
|
90 |
// Exceed max length.
|
|
|
91 |
$value = $field->edit_save_data_preprocess('ABCDEF', new \stdClass());
|
|
|
92 |
$this->assertEquals('ABCDE', $value);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Test external data validation
|
|
|
97 |
*/
|
|
|
98 |
public function test_convert_external_data(): void {
|
|
|
99 |
$this->resetAfterTest();
|
|
|
100 |
|
|
|
101 |
$fielddata = $this->getDataGenerator()->create_custom_profile_field([
|
|
|
102 |
'datatype' => 'text',
|
|
|
103 |
'name' => 'Test',
|
|
|
104 |
'shortname' => 'test',
|
|
|
105 |
'param2' => 5, // Max length.
|
|
|
106 |
]);
|
|
|
107 |
$field = new profile_field_text(0, 0, $fielddata);
|
|
|
108 |
|
|
|
109 |
$value = $field->convert_external_data('ABCDE');
|
|
|
110 |
$this->assertEquals('ABCDE', $value);
|
|
|
111 |
|
|
|
112 |
// Exceed max length.
|
|
|
113 |
$value = $field->convert_external_data('ABCDEF');
|
|
|
114 |
$this->assertNull($value);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|