MDL-62463 mod_glossary: Fix SQL query
[moodle.git] / privacy / tests / request_transform_test.php
blob3926f5f1aedc918a2d507f26738b77ed8a6636c3
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.
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/>.
17 /**
18 * Unit Tests for the request transform helper.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 use \core_privacy\local\request\transform;
32 /**
33 * Tests for the \core_privacy API's request transform helper functionality.
35 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class request_transform_test extends advanced_testcase {
39 /**
40 * Test that user translation currently does nothing.
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.
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));
52 /**
53 * Test that the datetime is translated into a string.
55 public function test_datetime() {
56 $time = 1;
58 $datestr = transform::datetime($time);
60 // Assert it is a string.
61 $this->assertInternalType('string', $datestr);
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);
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);
72 /**
73 * Test that the date is translated into a string.
75 public function test_date() {
76 $time = 1;
78 $datestr = transform::date($time);
80 // Assert it is a string.
81 $this->assertInternalType('string', $datestr);
83 // Assert the formatted date is correct.
84 $dateobj = new DateTime();
85 $dateobj->setTimestamp($time);
86 $this->assertEquals($dateobj->format('j F Y'), $datestr);
89 /**
90 * Ensure that the yesno function translates correctly.
92 * @dataProvider yesno_provider
93 * @param mixed $input The input to test
94 * @param string $expected The expected value
96 public function test_yesno($input, $expected) {
97 $this->assertEquals($expected, transform::yesno($input));
101 * Data provider for tests of the yesno transformation.
103 * @return array
105 public function yesno_provider() {
106 return [
107 'Bool False' => [
108 false,
109 get_string('no'),
111 'Bool true' => [
112 true,
113 get_string('yes'),
115 'Int 0' => [
117 get_string('no'),
119 'Int 1' => [
121 get_string('yes'),
123 'String 0' => [
124 '0',
125 get_string('no'),
127 'String 1' => [
128 '1',
129 get_string('yes'),