Permalink
Browse files

MDL-40176 mock_submit method for testing

  • Loading branch information...
1 parent 8204320 commit 81e01645d860cee7f382d2e51dbf1ab6f04843b1 @jamiepratt jamiepratt committed with damyon Jun 20, 2013
Showing with 33 additions and 0 deletions.
  1. +31 −0 lib/formslib.php
  2. +2 −0 lib/upgrade.txt
View
@@ -1307,6 +1307,37 @@ private function detectMissingSetType() {
}
}
}
+
+ /**
+ * 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
@@ -1,5 +1,7 @@
This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.
+=== 2.5.2 ===
+* Use new function moodleform::mock_submit() to simulate form submission in unit tests.
=== 2.5.1 ===

0 comments on commit 81e0164

Please sign in to comment.