PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tests/fixtures/max_input_vars.php

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