<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[47041] trunk/tests/phpunit/includes: Docs: Improve documentation for `tests/phpunit/includes/testcase-ajax.php`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/47041">47041</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/47041","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-01-05 15:31:10 +0000 (Sun, 05 Jan 2020)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Docs: Improve documentation for `tests/phpunit/includes/testcase-ajax.php`.
Props andizer, SergeyBiryukov.
Fixes <a href="https://core.trac.wordpress.org/ticket/47568">#47568</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunitincludesexceptionsphp">trunk/tests/phpunit/includes/exceptions.php</a></li>
<li><a href="#trunktestsphpunitincludestestcaseajaxphp">trunk/tests/phpunit/includes/testcase-ajax.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunitincludesexceptionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/exceptions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/exceptions.php 2020-01-05 13:09:52 UTC (rev 47040)
+++ trunk/tests/phpunit/includes/exceptions.php 2020-01-05 15:31:10 UTC (rev 47041)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5,14 +5,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * General exception for wp_die()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * General exception for wp_die().
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class WPDieException extends Exception {}
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Exception for cases of wp_die(), for ajax tests.
- * This means there was an error (no output, and a call to wp_die)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Exception for cases of wp_die(), for Ajax tests.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * This means there was an error (no output, and a call to wp_die).
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @package WordPress
</span><span class="cx" style="display: block; padding: 0 10px"> * @subpackage Unit Tests
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -20,12 +21,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class WPAjaxDieStopException extends WPDieException {}
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Exception for cases of wp_die(), for ajax tests.
- * This means execution of the ajax function should be halted, but the unit
- * test can continue. The function finished normally and there was not an
- * error (output happened, but wp_die was called to end execution) This is
- * used with WP_Ajax_Response::send
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Exception for cases of wp_die(), for Ajax tests.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * This means the execution of the Ajax function should be halted, but the unit test
+ * can continue. The function finished normally and there was no error (output happened,
+ * but wp_die was called to end execution). This is used with WP_Ajax_Response::send().
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @package WordPress
</span><span class="cx" style="display: block; padding: 0 10px"> * @subpackage Unit Tests
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.4.0
</span></span></pre></div>
<a id="trunktestsphpunitincludestestcaseajaxphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/testcase-ajax.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/testcase-ajax.php 2020-01-05 13:09:52 UTC (rev 47040)
+++ trunk/tests/phpunit/includes/testcase-ajax.php 2020-01-05 15:31:10 UTC (rev 47041)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,13 +1,5 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Ajax test cases
- *
- * @package WordPress
- * @subpackage UnitTests
- * @since 3.4.0
- */
-
-/**
</del><span class="cx" style="display: block; padding: 0 10px"> * Ajax test case class
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @package WordPress
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,7 +9,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Last AJAX response. This is set via echo -or- wp_die.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Last Ajax response. This is set via echo -or- wp_die.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @var string
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,7 +16,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected $_last_response = '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * List of ajax actions called via GET
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * List of Ajax actions called via GET.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -40,7 +32,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Saved error reporting level
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Saved error reporting level.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @var int
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -47,7 +39,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected $_error_level = 0;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * List of ajax actions called via POST
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * List of Ajax actions called via POST.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -139,8 +131,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Set up the test fixture.
- * Override wp_die(), pretend to be ajax, and suppress E_WARNINGs
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Sets up the test fixture.
+ *
+ * Overrides wp_die(), pretends to be Ajax, and suppresses E_WARNINGs.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function setUp() {
</span><span class="cx" style="display: block; padding: 0 10px"> parent::setUp();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -162,8 +155,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Tear down the test fixture.
- * Reset $_POST, remove the wp_die() override, restore error reporting
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tears down the test fixture.
+ *
+ * Resets $_POST, removes the wp_die() override, restores error reporting.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function tearDown() {
</span><span class="cx" style="display: block; padding: 0 10px"> parent::tearDown();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,7 +172,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Clear login cookies, unset the current user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Clears login cookies, unsets the current user.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function logout() {
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $GLOBALS['current_user'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -189,7 +183,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Return our callback handler
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Returns our callback handler
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return callback
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -198,8 +192,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Handler for wp_die()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Handler for wp_die().
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * Save the output for analysis, stop execution by throwing an exception.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * Error conditions (no output, just die) will throw <code>WPAjaxDieStopException( $message )</code>
</span><span class="cx" style="display: block; padding: 0 10px"> * You can test for this with:
</span><span class="cx" style="display: block; padding: 0 10px"> * <code>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -211,7 +207,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * $this->setExpectedException( 'WPAjaxDieContinueException', 'something contained in $message' );
</span><span class="cx" style="display: block; padding: 0 10px"> * </code>
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $message
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $message The message to set.
+ *
+ * @throws WPAjaxDieStopException Thrown to stop further execution.
+ * @throws WPAjaxDieContinueException Thrown to stop execution of the Ajax function,
+ * but continue the unit test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function dieHandler( $message ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->_last_response .= ob_get_clean();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -228,10 +228,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Switch between user roles
- * E.g. administrator, editor, author, contributor, subscriber
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Switches between user roles.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $role
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * E.g. administrator, editor, author, contributor, subscriber.
+ *
+ * @param string $role The role to set.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function _setRole( $role ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $post = $_POST;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -241,11 +242,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Mimic the ajax handling of admin-ajax.php
- * Capture the output via output buffering, and if there is any, store
- * it in $this->_last_response.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Mimics the Ajax handling of admin-ajax.php.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $action
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Captures the output via output buffering, and if there is any,
+ * stores it in $this->_last_response.
+ *
+ * @param string $action The action to handle.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function _handleAjax( $action ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>