From 7ce44d8edcd32ef773723cd68ce4c3d58328fcce Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Mon, 14 Aug 2017 08:28:19 +0000 Subject: [PATCH] MDL-59669 forms: add helper to get submission keys for unit tests --- lib/formslib.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/formslib.php b/lib/formslib.php index 965b0a4f51a..05612735683 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -1390,6 +1390,27 @@ abstract class moodleform { $_POST = $simulatedsubmitteddata; } } + + /** + * Used by tests to generate valid submit keys for moodle forms that are + * submitted with ajax data. + * + * @throws \moodle_exception If called outside unit test environment + * @param array $data Existing form data you wish to add the keys to. + * @return array + */ + public static function mock_generate_submit_keys($data = []) { + if (!defined('PHPUNIT_TEST') || !PHPUNIT_TEST) { + throw new \moodle_exception("This function can only be used for unit testing."); + } + + $formidentifier = get_called_class(); + $formidentifier = str_replace('\\', '_', $formidentifier); // See MDL-56233 for more information. + $data['sesskey'] = sesskey(); + $data['_qf__' . $formidentifier] = 1; + + return $data; + } } /**