Skip to content

Commit

Permalink
MDL-40176 mock_submit method for testing
Browse files Browse the repository at this point in the history
Conflicts:

	lib/formslib.php
  • Loading branch information
jamiepratt authored and Damyon Wiese committed Jul 9, 2013
1 parent d3dc9a3 commit 3043438
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/formslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3043438

Please sign in to comment.