diff --git a/lib/testing/classes/nasty_strings.php b/lib/testing/classes/nasty_strings.php new file mode 100644 index 00000000000..e200263ad9d --- /dev/null +++ b/lib/testing/classes/nasty_strings.php @@ -0,0 +1,116 @@ +. + +/** + * Nasty strings to use in tests. + * + * @package core + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +/** + * Nasty strings manager. + * + * Responds to nasty strings requests with a random string of the list + * to try with different combinations in different places. + * + * @package core + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class nasty_strings { + + /** + * List of different strings to fill fields and assert against them + * + * Non of these strings can be a part of another one, this would not be good + * when using more one string at the same time and asserting results. + * + * @static + * @var array + */ + protected static $strings = array( + '< > & < > & \' \\" \ \'$@NULL@$ @@TEST@@ \\\" \\ , ; : .  日本語 ­ % %% ', + '& \' \\" \ \'$@NULL@$ < > & < > @@TEST@@ \\\" \\ , ; : .  日本語 ­ % %% ', + '< > & < > & \' \\" \ \\\" \\ , ; : .  \'$@NULL@$ @@TEST@@ 日本語 ­ % %% ', + '< > & < > & \' \\" \ \'$@NULL@$ 日本語 ­ % %%@@TEST@@ \\\" \\ , ; : .  ', + '< > & < > \\\" \\ , ; : .  日本語& \' \\" \ \'$@NULL@$ @@TEST@@ ­ % %% ', + '\' \\" \ \'$@NULL@$ @@TEST@@ < > & < > & \\\" \\ , ; : .  日本語 ­ % %% ', + '\\\" \\ , ; : .  日本語 ­ % < > & < > & \' \\" \ \'$@NULL@$ @@TEST@@ %% ', + '< > & < > & \' \\" \ \'$@NULL@$ 日本語 ­ % %% @@TEST@@ \\\" \\ , ; : .  ', + '.  日本語& \' \\"< > & < > \\\" \\ , ; : \ \'$@NULL@$ @@TEST@@ ­ % %% ', + '& \' \\" \ < > & < > \\\" \\ , ; : .  日本語\'$@NULL@$ @@TEST@@ ­ % %% ', + ); + + /** + * Already used nasty strings. + * + * This array will be cleaned before each scenario. + * + * @static + * @var array + */ + protected static $usedstrings = array(); + + /** + * Returns a nasty string and stores the key mapping. + * + * @static + * @param string $key The key + * @return string + */ + public static function get($key) { + + // If have been used during the this tests return it. + if (isset(self::$usedstrings[$key])) { + return self::$strings[self::$usedstrings[$key]]; + } + + // Getting non-used random string. + do { + $index = self::random_index(); + } while (in_array($index, self::$usedstrings)); + + // Mark the string as already used. + self::$usedstrings[$key] = $index; + + return self::$strings[$index]; + } + + /** + * Resets the used strings var. + * @static + * @return void + */ + public static function reset_used_strings() { + self::$usedstrings = array(); + } + + /** + * Returns a random index. + * @static + * @return int + */ + protected static function random_index() { + return mt_rand(0, count(self::$strings) - 1); + } + +}