Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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
 * Unit tests for MoodleQuickForm_date_selector
19
 *
20
 * Contains test cases for testing MoodleQuickForm_date_selector
21
 *
22
 * @package    core_form
23
 * @category   test
24
 * @copyright  2012 Rajesh Taneja
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
namespace core_form;
29
 
30
use moodleform;
31
use MoodleQuickForm_date_selector;
32
 
33
defined('MOODLE_INTERNAL') || die();
34
 
35
global $CFG;
36
require_once($CFG->libdir . '/form/dateselector.php');
37
require_once($CFG->libdir.'/formslib.php');
38
 
39
/**
40
 * Unit tests for MoodleQuickForm_date_selector
41
 *
42
 * Contains test cases for testing MoodleQuickForm_date_selector
43
 *
44
 * @package    core_form
45
 * @category   test
46
 * @copyright  2012 Rajesh Taneja
47
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 */
49
class dateselector_test extends \advanced_testcase {
50
    /** @var \MoodleQuickForm Keeps reference of dummy form object */
51
    private $mform;
52
    /** @var array test fixtures */
53
    private $testvals;
54
 
55
    /**
56
     * Initalize test wide variable, it is called in start of the testcase
57
     */
58
    protected function setUp(): void {
59
        global $CFG;
60
        parent::setUp();
61
 
62
        $this->resetAfterTest();
63
        $this->setAdminUser();
64
 
65
        $this->setTimezone('Australia/Perth');
66
 
67
        // Get form data.
68
        $form = new temp_form_date();
69
        $this->mform = $form->getform();
70
 
71
        // Set test values.
72
        $this->testvals = array(
73
            array (
74
                'day' => 1,
75
                'month' => 7,
76
                'year' => 2011,
77
                'usertimezone' => 'America/Moncton',
78
                'timezone' => 'America/Moncton',
79
                'timestamp' => 1309489200
80
            ),
81
            array (
82
                'day' => 1,
83
                'month' => 7,
84
                'year' => 2011,
85
                'usertimezone' => 'America/Moncton',
86
                'timezone' => 99,
87
                'timestamp' => 1309489200
88
            ),
89
            array (
90
                'day' => 30,
91
                'month' => 6,
92
                'year' => 2011,
93
                'usertimezone' => 'America/Moncton',
94
                'timezone' => -4,
95
                'timestamp' => 1309406400
96
            ),
97
            array (
98
                'day' => 30,
99
                'month' => 6,
100
                'year' => 2011,
101
                'usertimezone' => -4,
102
                'timezone' => 99,
103
                'timestamp' => 1309406400
104
            ),
105
            array (
106
                'day' => 1,
107
                'month' => 7,
108
                'year' => 2011,
109
                'usertimezone' => 0.0,
110
                'timezone' => 0.0,
111
                'timestamp' => 1309478400 // 6am at UTC+0
112
            ),
113
            array (
114
                'day' => 1,
115
                'month' => 7,
116
                'year' => 2011,
117
                'usertimezone' => 0.0,
118
                'timezone' => 99,
119
                'timestamp' => 1309478400 // 6am at UTC+0
120
            )
121
        );
122
    }
123
 
124
    /**
125
     * Testcase to check exportvalue
126
     */
127
    public function test_exportvalue() {
128
        global $USER;
129
        $testvals = $this->testvals;
130
 
131
        foreach ($testvals as $vals) {
132
            // Set user timezone to test value.
133
            $USER->timezone = $vals['usertimezone'];
134
 
135
            // Create dateselector element with different timezones.
136
            $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
137
            $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
138
            $this->assertTrue($el instanceof MoodleQuickForm_date_selector);
139
            $submitvalues = array('dateselector' => $vals);
140
 
141
            $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues, true),
142
                    "Please check if timezones are updated (Site adminstration -> location -> update timezone)");
143
        }
144
    }
145
 
146
    /**
147
     * Testcase to check onQuickformEvent
148
     */
149
    public function test_onquickformevent() {
150
        global $USER;
151
        $testvals = $this->testvals;
152
        // Get dummy form for data.
153
        $mform = $this->mform;
154
 
155
        foreach ($testvals as $vals) {
156
            // Set user timezone to test value.
157
            $USER->timezone = $vals['usertimezone'];
158
 
159
            // Create dateselector element with different timezones.
160
            $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
161
            $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
162
            $this->assertTrue($el instanceof MoodleQuickForm_date_selector);
163
            $expectedvalues = array(
164
                'day' => array($vals['day']),
165
                'month' => array($vals['month']),
166
                'year' => array($vals['year'])
167
                );
168
            $mform->_submitValues = array('dateselector' => $vals['timestamp']);
169
            $el->onQuickFormEvent('updateValue', null, $mform);
170
            $this->assertSame($expectedvalues, $el->getValue());
171
        }
172
    }
173
}
174
 
175
/**
176
 * Form object to be used in test case.
177
 */
178
class temp_form_date extends moodleform {
179
    /**
180
     * Form definition.
181
     */
182
    public function definition() {
183
        // No definition required.
184
    }
185
    /**
186
     * Returns form reference
187
     * @return \MoodleQuickForm
188
     */
189
    public function getform() {
190
        $mform = $this->_form;
191
        // set submitted flag, to simulate submission
192
        $mform->_flagSubmitted = true;
193
        return $mform;
194
    }
195
}