Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  
  25  require(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir . '/formslib.php');
  27  
  28  // Behat test fixture only.
  29  defined('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   */
  37  class core_max_input_vars_form extends moodleform {
  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  
  89  require_login();
  90  
  91  $context = context_system::instance();
  92  
  93  $type = optional_param('type', '', PARAM_ALPHA);
  94  
  95  // Set up the page details.
  96  $PAGE->set_url(new moodle_url('/lib/tests/fixtures/max_input_vars.php'));
  97  $PAGE->set_context($context);
  98  
  99  if ($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      }
 116  
 117      $mform = new core_max_input_vars_form('max_input_vars.php',
 118              array('type' => $type, 'fieldcount' => $fieldcount, 'control' => $type[0]));
 119      if ($type[0] === 'c') {
 120          $data = array();
 121          for ($i = 0; $i < $fieldcount; $i++) {
 122              if ($i === 3) {
 123                  // Everything is set except number 3.
 124                  continue;
 125              }
 126              $data['test_c' . $i] = 1;
 127          }
 128          $mform->set_data($data);
 129      }
 130  }
 131  
 132  echo $OUTPUT->header();
 133  
 134  if ($type && ($result = $mform->get_data())) {
 135      $testc = array();
 136      $testa = array();
 137      foreach ($_POST as $key => $value) {
 138          $matches = array();
 139          // Handle the 'bulk' ones separately so we can show success/fail rather
 140          // than outputting a thousand items; also makes it possible to Behat-test
 141          // without depending on specific value of max_input_vars.
 142          if (preg_match('~^test_c([0-9]+)$~', $key, $matches)) {
 143              $testc[(int)$matches[1]] = $value;
 144          } else if ($key === 'test_a') {
 145              $testa = $value;
 146          } else {
 147              // Other fields are output straight off.
 148              if (is_array($value)) {
 149                  echo html_writer::div(s($key) . '=[' . s(implode(',', $value)) . ']');
 150              } else {
 151                  echo html_writer::div(s($key) . '=' . s($value));
 152              }
 153          }
 154      }
 155  
 156      // Confirm that the bulk results are correct.
 157      switch ($type[0]) {
 158          case 'c' :
 159              $success = true;
 160              for ($i = 0; $i < $fieldcount; $i++) {
 161                  if (!array_key_exists($i, $testc)) {
 162                      $success = false;
 163                      break;
 164                  }
 165                  if ($testc[$i] != ($i == 3 ? 0 : 1)) {
 166                      $success = false;
 167                      break;
 168                  }
 169              }
 170              if (array_key_exists($fieldcount, $testc)) {
 171                  $success = false;
 172              }
 173              // Check using Moodle form and _param functions too.
 174              $key = 'test_c' . ($fieldcount - 1);
 175              if (empty($result->{$key})) {
 176                  $success = false;
 177              }
 178              if (optional_param($key, 0, PARAM_INT) !== 1) {
 179                  $success = false;
 180              }
 181              echo html_writer::div('Bulk checkbox success: ' . ($success ? 'true' : 'false'));
 182              break;
 183  
 184          case 'a' :
 185              $success = true;
 186              for ($i = 0; $i < $fieldcount; $i++) {
 187                  if ($i === 3) {
 188                      if (in_array($i, $testa)) {
 189                          $success = false;
 190                          break;
 191                      }
 192                  } else {
 193                      if (!in_array($i, $testa)) {
 194                          $success = false;
 195                          break;
 196                      }
 197                  }
 198              }
 199              if (in_array($fieldcount, $testa)) {
 200                  $success = false;
 201              }
 202              // Check using Moodle _param function. The form does not include these
 203              // fields so it won't be in the form result.
 204              $array = optional_param_array('test_a', array(), PARAM_INT);
 205              if ($array != $testa) {
 206                  $success = false;
 207              }
 208              echo html_writer::div('Bulk array success: ' . ($success ? 'true' : 'false'));
 209              break;
 210      }
 211  
 212  } else if ($type) {
 213      $mform->display();
 214  }
 215  
 216  // Show links to each available type of test.
 217  echo html_writer::start_tag('ul');
 218  foreach (array('c' => 'Advanced checkboxes',
 219          'a' => 'Select options') as $control => $controlname) {
 220      foreach (array('s' => 'Small', 'm' => 'Below limit', 'e' => 'Exact PHP limit') as $size => $sizename) {
 221          echo html_writer::tag('li', html_writer::link('max_input_vars.php?type=' .
 222                  $control . $size, $controlname . ' / ' . $sizename));
 223      }
 224  }
 225  echo html_writer::end_tag('ul');
 226  
 227  echo $OUTPUT->footer();