.
/**
* A SimpleTest report format for Moodle.
*
* @package tool
* @subpackage unittest
* @copyright © 2006 The Open University
* @author N.D.Freear@open.ac.uk, T.J.Hunt@open.ac.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir . '/simpletestlib/reporter.php');
/**
* Extended in-browser test displayer. HtmlReporter generates
* only failure messages and a pass count. ExHtmlReporter also
* generates pass messages and a time-stamp.
*
* @package SimpleTestEx
*/
class ExHtmlReporter extends HtmlReporter {
// Options set when the class is created.
var $showpasses;
// Lang strings. Set in the constructor.
var $strrunonlyfolder;
var $strrunonlyfile;
var $strseparator;
var $timestart;
/**
* Constructor.
*
* @param bool $showpasses Whether this reporter should output anything for passes.
*/
function ExHtmlReporter($showpasses) {
$this->HtmlReporter();
$this->showpasses = $showpasses;
$this->strrunonlyfolder = $this->get_string('runonlyfolder');
$this->strrunonlyfile = $this->get_string('runonlyfile');
$this->strseparator = get_separator();
}
/**
* Called when a pass needs to be output.
*/
function paintPass($message) {
//(Implicitly call grandparent, as parent not implemented.)
parent::paintPass($message);
if ($this->showpasses) {
$this->_paintPassFail('pass', $message);
}
}
/**
* Called when a fail needs to be output.
*/
function paintFail($message) {
// Explicitly call grandparent, not parent::paintFail.
SimpleScorer::paintFail($message);
$this->_paintPassFail('fail', $message, debug_backtrace());
}
/**
* Called when a skip needs to be output.
*/
function paintSkip($message) {
// Explicitly call grandparent, not parent::paintFail.
SimpleScorer::paintSkip($message);
$this->_paintPassFail('skip', $message);
}
/**
* Called when an error (uncaught exception or PHP error) needs to be output.
*/
function paintError($message) {
// Explicitly call grandparent, not parent::paintError.
SimpleScorer::paintError($message);
$this->_paintPassFail('exception', $message);
}
/**
* Called when a caught exception needs to be output.
*/
function paintException($exception) {
// Explicitly call grandparent, not parent::paintException.
SimpleScorer::paintException($exception);
if (is_a($exception, 'moodle_exception') &&
!get_string_manager()->string_exists($exception->errorcode, $exception->module)) {
$exceptionmessage = 'Exception with missing language string {' .
$exception->errorcode . '} from language file {' . $exception->module . '}';
if (!empty($exception->a)) {
if (is_string($exception->a)) {
$data = $exception->a;
} else {
$data = array();
foreach ((array)$exception->a as $name => $value) {
$data[] = $name . ' => [' . $value . ']';
}
$data = implode(', ', $data);
}
$exceptionmessage .= ' with data {' . $data . '}';
}
} else {
$exceptionmessage = $exception->getMessage();
}
$message = 'Unexpected exception of type [' . get_class($exception) .
'] with message ['. $exceptionmessage .
'] in ['. $exception->getFile() .
' line ' . $exception->getLine() . ']';
$debuginfo = null;
if (!empty($exception->debuginfo)) {
$debuginfo = $exception->debuginfo;
}
$this->_paintPassFail('exception', $message, $exception->getTrace(), $debuginfo);
}
/**
* Private method. Used by printPass/Fail/Skip/Error.
*/
function _paintPassFail($passorfail, $message, $stacktrace = null, $debuginfo = null) {
global $FULLME, $CFG, $OUTPUT;
echo $OUTPUT->box_start($passorfail . ' generalbox ');
$url = $this->_htmlEntities($this->_stripParameterFromUrl($FULLME, 'path'));
echo '', $this->get_string($passorfail), ': ';
$breadcrumb = $this->getTestList();
array_shift($breadcrumb);
$file = array_shift($breadcrumb);
$pathbits = preg_split('/\/|\\\\/', substr($file, strlen($CFG->dirroot) + 1));
$file = array_pop($pathbits);
$folder = '';
foreach ($pathbits as $pathbit) {
$folder .= $pathbit . '/';
echo "strrunonlyfolder\">$pathbit/";
}
echo "strrunonlyfile\">$file";
echo $this->strseparator, implode($this->strseparator, $breadcrumb);
echo '
', $this->_htmlEntities($message), "\n\n";
if (!empty($debuginfo)) {
print_object('Debug info:');
print_object($debuginfo);
}
if ($stacktrace) {
$dotsadded = false;
$interestinglines = 0;
$filteredstacktrace = array();
foreach ($stacktrace as $frame) {
if (empty($frame['file']) || (strpos($frame['file'], 'simpletestlib') === false &&
strpos($frame['file'], 'simpletestcoveragelib') === false
&& strpos($frame['file'], 'tool/unittest') === false)) {
$filteredstacktrace[] = $frame;
$interestinglines += 1;
$dotsadded = false;
} else if (!$dotsadded) {
$filteredstacktrace[] = array('line' => '...', 'file' => '...');
$dotsadded = true;
}
}
if ($interestinglines > 1 || ($passorfail == 'exception' && $interestinglines > 0)) {
echo '