MDL-63495 comment: Added provider::get_users_in_context_from_sql
[moodle.git] / lib / testing / classes / nasty_strings.php
blob826a8ed287d0f168f9afb1ab8ca4bbb1dd04f2bb
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.
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/>.
17 /**
18 * Nasty strings to use in tests.
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
26 defined('MOODLE_INTERNAL') || die;
28 /**
29 * Nasty strings manager.
31 * Responds to nasty strings requests with a random string of the list
32 * to try with different combinations in different places.
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
39 class nasty_strings {
41 /**
42 * List of different strings to fill fields and assert against them
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.
47 * @static
48 * @var array
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@@­% %%',
63 /**
64 * Already used nasty strings.
66 * This array will be cleaned before each scenario.
68 * @static
69 * @var array
71 protected static $usedstrings = array();
73 /**
74 * Returns a nasty string and stores the key mapping.
76 * @static
77 * @param string $key The key
78 * @return string
80 public static function get($key) {
82 // If have been used during the this tests return it.
83 if (isset(self::$usedstrings[$key])) {
84 return self::$strings[self::$usedstrings[$key]];
87 // Getting non-used random string.
88 do {
89 $index = self::random_index();
90 } while (in_array($index, self::$usedstrings));
92 // Mark the string as already used.
93 self::$usedstrings[$key] = $index;
95 return self::$strings[$index];
98 /**
99 * Resets the used strings var.
100 * @static
101 * @return void
103 public static function reset_used_strings() {
104 self::$usedstrings = array();
108 * Returns a random index.
109 * @static
110 * @return int
112 protected static function random_index() {
113 return mt_rand(0, count(self::$strings) - 1);