Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Nasty strings to use in tests.
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2013 David Monllaó
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  /**
  29   * Nasty strings manager.
  30   *
  31   * Responds to nasty strings requests with a random string of the list
  32   * to try with different combinations in different places.
  33   *
  34   * @package   core
  35   * @category  test
  36   * @copyright 2013 David Monllaó
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class nasty_strings {
  40  
  41      /**
  42       * List of different strings to fill fields and assert against them
  43       *
  44       * Non of these strings can be a part of another one, this would not be good
  45       * when using more one string at the same time and asserting results.
  46       *
  47       * @static
  48       * @var array
  49       */
  50      protected static $strings = array(
  51          '< > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ @@TEST@@ \\\" \\ , ; : . 日本語­% %%',
  52          '&amp; \' \\" \ \'$@NULL@$ < > & &lt; &gt; @@TEST@@ \\\" \\ , ; : . 日本語­% %%',
  53          '< > & &lt; &gt; &amp; \' \\" \ \\\" \\ , ; : . \'$@NULL@$ @@TEST@@ 日本語­% %%',
  54          '< > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ 日本語­% %%@@TEST@@ \. \\" \\ , ; :',
  55          '< > & &lt; &gt; \\\" \\ , ; : . 日本語&amp; \' \\" \ \'$@NULL@$ @@TEST@@­% %%',
  56          '\' \\" \ \'$@NULL@$ @@TEST@@ < > & &lt; &gt; &amp; \\\" \\ , ; : . 日本語­% %%',
  57          '\\\" \\ , ; : . 日本語­% < > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ @@TEST@@ %%',
  58          '< > & &lt; &gt; &amp; \' \\" \ \'$@NULL@$ 日本語­% %% @@TEST@@ \\\" \\ . , ; :',
  59          '. 日本語&amp; \' \\" < > & &lt; &gt; \\ , ; : \ \'$@NULL@$ \\\" @@TEST@@­% %%',
  60          '&amp; \' \\" \ < > & &lt; &gt; \\\" \\ , ; : . 日本語\'$@NULL@$ @@TEST@@­% %%',
  61      );
  62  
  63      /**
  64       * Already used nasty strings.
  65       *
  66       * This array will be cleaned before each scenario.
  67       *
  68       * @static
  69       * @var array
  70       */
  71      protected static $usedstrings = array();
  72  
  73      /**
  74       * Returns a nasty string and stores the key mapping.
  75       *
  76       * @static
  77       * @param string $key The key
  78       * @return string
  79       */
  80      public static function get($key) {
  81  
  82          // If have been used during the this tests return it.
  83          if (isset(self::$usedstrings[$key])) {
  84              return self::$strings[self::$usedstrings[$key]];
  85          }
  86  
  87          // Getting non-used random string.
  88          do {
  89              $index = self::random_index();
  90          } while (in_array($index, self::$usedstrings));
  91  
  92          // Mark the string as already used.
  93          self::$usedstrings[$key] = $index;
  94  
  95          return self::$strings[$index];
  96      }
  97  
  98      /**
  99       * Resets the used strings var.
 100       * @static
 101       * @return void
 102       */
 103      public static function reset_used_strings() {
 104          self::$usedstrings = array();
 105      }
 106  
 107      /**
 108       * Returns a random index.
 109       * @static
 110       * @return int
 111       */
 112      protected static function random_index() {
 113          return mt_rand(0, count(self::$strings) - 1);
 114      }
 115  
 116  }