MDL-52226 tests: Fix behat phpunit naming conflict
[moodle.git] / lib / tests / fixtures / max_input_vars.php
CommitLineData
6d4858d5 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 * Fixture for Behat test of the max_input_vars handling for large forms.
19 *
20 * @package core
21 * @copyright 2015 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require(__DIR__ . '/../../../config.php');
26require_once($CFG->libdir . '/formslib.php');
27
28// Behat test fixture only.
29defined('BEHAT_SITE_RUNNING') || die('Only available on Behat test server');
30
31/**
32 * Form for testing max_input_vars.
33 *
34 * @copyright 2015 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
a49fd983 37class core_max_input_vars_form extends moodleform {
6d4858d5 38 /**
39 * Form definition.
40 */
41 public function definition() {
42 global $CFG, $PAGE;
43
44 $mform =& $this->_form;
45
46 $mform->addElement('header', 'general', '');
47 $mform->addElement('hidden', 'type', $this->_customdata['type']);
48 $mform->setType('type', PARAM_ALPHA);
49
50 // This is similar to how the selects are created for the role tables,
51 // without using a Moodle form element.
52 $select = html_writer::select(array(13 => 'ArrayOpt13', 42 => 'ArrayOpt4', 666 => 'ArrayOpt666'),
53 'arraytest[]', array(13, 42), false, array('multiple' => 'multiple', 'size' => 10));
54 $mform->addElement('static', 'arraybit', $select);
55
56 switch ($this->_customdata['control']) {
57 case 'c' :
58 // Create a whole stack of checkboxes.
59 for ($i = 0; $i < $this->_customdata['fieldcount']; $i++) {
60 $mform->addElement('advcheckbox', 'test_c' . $i, 'Checkbox ' . $i);
61 }
62 break;
63
64 case 'a' :
65 // Create a very large array input type field.
66 $options = array();
67 $values = array();
68 for ($i = 0; $i < $this->_customdata['fieldcount']; $i++) {
69 $options[$i] = 'BigArray ' . $i;
70 if ($i !== 3) {
71 $values[] = $i;
72 }
73 }
74 $select = html_writer::select($options,
75 'test_a[]', $values, false, array('multiple' => 'multiple', 'size' => 50));
76 $mform->addElement('static', 'bigarraybit', $select);
77 break;
78 }
79
80 // For the sake of it, let's have a second array.
81 $select = html_writer::select(array(13 => 'Array2Opt13', 42 => 'Array2Opt4', 666 => 'Array2Opt666'),
82 'array2test[]', array(13, 42), false, array('multiple' => 'multiple', 'size' => 10));
83 $mform->addElement('static', 'array2bit', $select);
84
85 $mform->addElement('submit', 'submitbutton', 'Submit here!');
86 }
87}
88
89require_login();
90
91$context = context_system::instance();
92
93$type = optional_param('type', '', PARAM_ALPHA);
94
95// Set up the page details.
a49fd983 96$PAGE->set_url(new moodle_url('/lib/tests/fixtures/max_input_vars.php'));
6d4858d5 97$PAGE->set_context($context);
98
99if ($type) {
100 // Make it work regardless of max_input_vars setting on server, within reason.
101 if ($type[1] === 's') {
102 // Small enough to definitely fit in the area.
103 $fieldcount = 10;
104 } else if ($type[1] === 'm') {
105 // Just under the limit (will go over for advancedcheckbox).
106 $fieldcount = (int)ini_get('max_input_vars') - 100;
107 } else if ($type[1] === 'e') {
108 // Exactly on the PHP limit, taking into account extra form fields
109 // and the double fields for checkboxes.
110 if ($type[0] === 'c') {
111 $fieldcount = (int)ini_get('max_input_vars') / 2 - 2;
112 } else {
113 $fieldcount = (int)ini_get('max_input_vars') - 11;
114 }
115 } else if ($type[1] === 'l') {
116 // Just over the limit.
117 $fieldcount = (int)ini_get('max_input_vars') + 100;
118 }
119
a49fd983 120 $mform = new core_max_input_vars_form('max_input_vars.php',
6d4858d5 121 array('type' => $type, 'fieldcount' => $fieldcount, 'control' => $type[0]));
122 if ($type[0] === 'c') {
123 $data = array();
124 for ($i = 0; $i < $fieldcount; $i++) {
125 if ($i === 3) {
126 // Everything is set except number 3.
127 continue;
128 }
129 $data['test_c' . $i] = 1;
130 }
131 $mform->set_data($data);
132 }
133}
134
135echo $OUTPUT->header();
136
137if ($type && ($result = $mform->get_data())) {
138 $testc = array();
139 $testa = array();
140 foreach ($_POST as $key => $value) {
141 $matches = array();
142 // Handle the 'bulk' ones separately so we can show success/fail rather
143 // than outputting a thousand items; also makes it possible to Behat-test
144 // without depending on specific value of max_input_vars.
145 if (preg_match('~^test_c([0-9]+)$~', $key, $matches)) {
146 $testc[(int)$matches[1]] = $value;
147 } else if ($key === 'test_a') {
148 $testa = $value;
149 } else {
150 // Other fields are output straight off.
151 if (is_array($value)) {
152 echo html_writer::div(s($key) . '=[' . s(implode(',', $value)) . ']');
153 } else {
154 echo html_writer::div(s($key) . '=' . s($value));
155 }
156 }
157 }
158
159 // Confirm that the bulk results are correct.
160 switch ($type[0]) {
161 case 'c' :
162 $success = true;
163 for ($i = 0; $i < $fieldcount; $i++) {
164 if (!array_key_exists($i, $testc)) {
165 $success = false;
166 break;
167 }
168 if ($testc[$i] != ($i == 3 ? 0 : 1)) {
169 $success = false;
170 break;
171 }
172 }
173 if (array_key_exists($fieldcount, $testc)) {
174 $success = false;
175 }
176 // Check using Moodle form and _param functions too.
177 $key = 'test_c' . ($fieldcount - 1);
178 if (empty($result->{$key})) {
179 $success = false;
180 }
181 if (optional_param($key, 0, PARAM_INT) !== 1) {
182 $success = false;
183 }
184 echo html_writer::div('Bulk checkbox success: ' . ($success ? 'true' : 'false'));
185 break;
186
187 case 'a' :
188 $success = true;
189 for ($i = 0; $i < $fieldcount; $i++) {
190 if ($i === 3) {
191 if (in_array($i, $testa)) {
192 $success = false;
193 break;
194 }
195 } else {
196 if (!in_array($i, $testa)) {
197 $success = false;
198 break;
199 }
200 }
201 }
202 if (in_array($fieldcount, $testa)) {
203 $success = false;
204 }
205 // Check using Moodle _param function. The form does not include these
206 // fields so it won't be in the form result.
207 $array = optional_param_array('test_a', array(), PARAM_INT);
208 if ($array != $testa) {
209 $success = false;
210 }
211 echo html_writer::div('Bulk array success: ' . ($success ? 'true' : 'false'));
212 break;
213 }
214
215} else if ($type) {
216 $mform->display();
217}
218
219// Show links to each available type of test.
220echo html_writer::start_tag('ul');
221foreach (array('c' => 'Advanced checkboxes',
222 'a' => 'Select options') as $control => $controlname) {
223 foreach (array('s' => 'Small', 'm' => 'Below limit', 'e' => 'Exact PHP limit',
224 'l' => 'Above limit') as $size => $sizename) {
a49fd983 225 echo html_writer::tag('li', html_writer::link('max_input_vars.php?type=' .
6d4858d5 226 $control . $size, $controlname . ' / ' . $sizename));
227 }
228}
229echo html_writer::end_tag('ul');
230
231echo $OUTPUT->footer();