Skip to content
Browse files

MDL-40176 mock_submit method for testing

Conflicts:

	lib/formslib.php
  • Loading branch information...
1 parent d3dc9a3 commit 304343830b4e38870d7d91325373192a4b8cce1c @jamiepratt jamiepratt committed with damyon Jun 20, 2013
Showing with 34 additions and 0 deletions.
  1. +31 −0 lib/formslib.php
  2. +3 −0 lib/upgrade.txt
View
31 lib/formslib.php
@@ -1227,6 +1227,37 @@ public static function get_js_module() {
)
);
}
+
+ /**
+ * Used by tests to simulate submitted form data submission from the user.
+ *
+ * For form fields where no data is submitted the default for that field as set by set_data or setDefault will be passed to
+ * get_data.
+ *
+ * This method sets $_POST or $_GET and $_FILES with the data supplied. Our unit test code empties all these
+ * global arrays after each test.
+ *
+ * @param array $simulatedsubmitteddata An associative array of form values (same format as $_POST).
+ * @param array $simulatedsubmittedfiles An associative array of files uploaded (same format as $_FILES). Can be omitted.
+ * @param string $method 'post' or 'get', defaults to 'post'.
+ * @param null $formidentifier the default is to use the class name for this class but you may need to provide
+ * a different value here for some forms that are used more than once on the
+ * same page.
+ */
+ public static function mock_submit($simulatedsubmitteddata, $simulatedsubmittedfiles = array(), $method = 'post',
+ $formidentifier = null) {
+ $_FILES = $simulatedsubmittedfiles;
+ if ($formidentifier === null) {
+ $formidentifier = get_called_class();
+ }
+ $simulatedsubmitteddata['_qf__'.$formidentifier] = 1;
+ $simulatedsubmitteddata['sesskey'] = sesskey();
+ if (strtolower($method) === 'get') {
+ $_GET = $simulatedsubmitteddata;
+ } else {
+ $_POST = $simulatedsubmitteddata;
+ }
+ }
}
/**
View
3 lib/upgrade.txt
@@ -1,6 +1,9 @@
This files describes API changes in core lbraries and APIs,
information provided here is intended especially for developers.
+=== 2.4.6 ===
+* Use new function moodleform::mock_submit() to simulate form submission in unit tests.
+
=== 2.4.4 ===
* condition_info:get_condition_user_fields($formatoptions) now accepts the optional
param $formatoptions, that will determine if the field names are processed by

0 comments on commit 3043438

Please sign in to comment.
Something went wrong with that request. Please try again.