Commit | Line | Data |
---|---|---|
81f1e31a AN |
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 | * Unit Tests for the request transform helper. | |
19 | * | |
20 | * @package core_privacy | |
21 | * @category test | |
22 | * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | global $CFG; | |
29 | ||
30 | use \core_privacy\local\request\transform; | |
31 | ||
32 | /** | |
33 | * Tests for the \core_privacy API's request transform helper functionality. | |
34 | * | |
35 | * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> | |
36 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
37 | */ | |
38 | class request_transform_test extends advanced_testcase { | |
39 | /** | |
40 | * Test that user translation currently does nothing. | |
41 | * | |
42 | * We have not determined if we will do this or not, but we provide the functionality and encourgae people to use | |
43 | * it so that it can be retrospectively fitted if required. | |
44 | */ | |
45 | public function test_user() { | |
46 | // Note: This test currently sucks, but there's no point creating users just to test this. | |
47 | for ($i = 0; $i < 10; $i++) { | |
48 | $this->assertEquals($i, transform::user($i)); | |
49 | } | |
50 | } | |
51 | ||
52 | /** | |
53 | * Test that the datetime is translated into a string. | |
54 | */ | |
55 | public function test_datetime() { | |
63d17064 MN |
56 | $time = 1; |
57 | ||
58 | $datestr = transform::datetime($time); | |
59 | ||
60 | // Assert it is a string. | |
61 | $this->assertInternalType('string', $datestr); | |
62 | ||
63 | // To prevent failures on MAC where we are returned with a lower-case 'am' we want to convert this to 'AM'. | |
64 | $datestr = str_replace('am', 'AM', $datestr); | |
65 | ||
66 | // Assert the formatted date is correct. | |
67 | $dateobj = new DateTime(); | |
68 | $dateobj->setTimestamp($time); | |
69 | $this->assertEquals($dateobj->format('l, j F Y, g:i A'), $datestr); | |
81f1e31a AN |
70 | } |
71 | ||
72 | /** | |
73 | * Test that the date is translated into a string. | |
74 | */ | |
75 | public function test_date() { | |
63d17064 MN |
76 | $time = 1; |
77 | ||
78 | $datestr = transform::date($time); | |
79 | ||
80 | // Assert it is a string. | |
81 | $this->assertInternalType('string', $datestr); | |
82 | ||
83 | // Assert the formatted date is correct. | |
84 | $dateobj = new DateTime(); | |
85 | $dateobj->setTimestamp($time); | |
86 | $this->assertEquals($dateobj->format('j F Y'), $datestr); | |
81f1e31a AN |
87 | } |
88 | ||
89 | /** | |
90 | * Ensure that the yesno function translates correctly. | |
91 | * | |
92 | * @dataProvider yesno_provider | |
93 | * @param mixed $input The input to test | |
94 | * @param string $expected The expected value | |
95 | */ | |
96 | public function test_yesno($input, $expected) { | |
97 | $this->assertEquals($expected, transform::yesno($input)); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Data provider for tests of the yesno transformation. | |
102 | * | |
103 | * @return array | |
104 | */ | |
105 | public function yesno_provider() { | |
106 | return [ | |
107 | 'Bool False' => [ | |
108 | false, | |
109 | get_string('no'), | |
110 | ], | |
111 | 'Bool true' => [ | |
112 | true, | |
113 | get_string('yes'), | |
114 | ], | |
115 | 'Int 0' => [ | |
116 | 0, | |
117 | get_string('no'), | |
118 | ], | |
119 | 'Int 1' => [ | |
120 | 1, | |
121 | get_string('yes'), | |
122 | ], | |
123 | 'String 0' => [ | |
124 | '0', | |
125 | get_string('no'), | |
126 | ], | |
127 | 'String 1' => [ | |
128 | '1', | |
129 | get_string('yes'), | |
130 | ], | |
131 | ]; | |
132 | } | |
133 | } |