Skip to content

Commit

Permalink
Merge branch 'MDL-38041_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed May 5, 2013
2 parents 8f9ae1d + bbd802f commit 07c9a6d
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
57 changes: 57 additions & 0 deletions lib/behat/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,60 @@ function behat_error($errorcode, $text = '') {
testing_error($errorcode, $text);
}

/**
* PHP errors handler to use when running behat tests.
*
* Adds specific CSS classes to identify
* the messages.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @param array $errcontext
* @return bool
*/
function behat_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
global $OUTPUT;

// Only after something has been writen.
if (!$OUTPUT->has_started()) {
return false;
}

// If is preceded by an @ we don't show it.
if (!error_reporting()) {
return true;
}

// Using the default one in case there is a fatal catchable error.
default_error_handler($errno, $errstr, $errfile, $errline, $errcontext);

switch ($errno) {
case E_USER_ERROR:
$errnostr = 'Fatal error';
break;
case E_WARNING:
case E_USER_WARNING:
$errnostr = 'Warning';
break;
case E_NOTICE:
case E_USER_NOTICE:
case E_STRICT:
$errnostr = 'Notice';
break;
case E_RECOVERABLE_ERROR:
$errnostr = 'Catchable';
break;
default:
$errnostr = 'Unknown error type';
}

// Wrapping the output.
echo '<div class="phpdebugmessage">' . PHP_EOL;
echo "$errnostr: $errstr in $errfile on line $errline" . PHP_EOL;
echo '</div>';

// Also use the internal error handler so we keep the usual behaviour.
return false;
}
6 changes: 6 additions & 0 deletions lib/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@
set_error_handler('default_error_handler', E_ALL | E_STRICT);
}

// Acceptance tests needs special output to capture the errors.
if (!empty($CFG->originaldataroot) && !defined('BEHAT_RUNNING')) {
require_once(__DIR__ . '/behat/lib.php');
set_error_handler('behat_error_handler', E_ALL | E_STRICT);
}

// If there are any errors in the standard libraries we want to know!
error_reporting(E_ALL | E_STRICT);

Expand Down
72 changes: 72 additions & 0 deletions lib/tests/behat/behat_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public function before_scenario($event) {
// Assing valid data to admin user (some generator-related code needs a valid user).
$user = $DB->get_record('user', array('username' => 'admin'));
session_set_user($user);

// Start always in the the homepage.
$this->getSession()->visit($this->locate_path('/'));
}

/**
Expand Down Expand Up @@ -178,4 +181,73 @@ public function after_step_javascript($event) {
}
}

/**
* Internal step definition to find exceptions, debugging() messages and PHP debug messages.
*
* Part of behat_hooks class as is part of the testing framework, is auto-executed
* after each step so no features will splicitly use it.
*
* @Given /^I look for exceptions$/
* @see Moodle\BehatExtension\Tester\MoodleStepTester
*/
public function i_look_for_exceptions() {

// Exceptions.
if ($errormsg = $this->getSession()->getPage()->find('css', '.errorbox p.errormessage')) {

// Getting the debugging info and the backtrace.
$errorinfoboxes = $this->getSession()->getPage()->findAll('css', 'div.notifytiny');
$errorinfo = $this->get_debug_text($errorinfoboxes[0]->getHtml()) . "\n" .
$this->get_debug_text($errorinfoboxes[1]->getHtml());

$msg = "Moodle exception: " . $errormsg->getText() . "\n" . $errorinfo;
throw new \Exception(html_entity_decode($msg));
}

// Debugging messages.
if ($debuggingmessages = $this->getSession()->getPage()->findAll('css', '.debuggingmessage')) {
$msgs = array();
foreach ($debuggingmessages as $debuggingmessage) {
$msgs[] = $this->get_debug_text($debuggingmessage->getHtml());
}
$msg = "debugging() message/s found:\n" . implode("\n", $msgs);
throw new \Exception(html_entity_decode($msg));
}

// PHP debug messages.
if ($phpmessages = $this->getSession()->getPage()->findAll('css', '.phpdebugmessage')) {

$msgs = array();
foreach ($phpmessages as $phpmessage) {
$msgs[] = $this->get_debug_text($phpmessage->getHtml());
}
$msg = "PHP debug message/s found:\n" . implode("\n", $msgs);
throw new \Exception(html_entity_decode($msg));
}

// Any other backtrace.
$backtracespattern = '/(line [0-9]* of [^:]*: call to [\->&;:a-zA-Z_\x7f-\xff][\->&;:a-zA-Z0-9_\x7f-\xff]*)/';
if (preg_match_all($backtracespattern, $this->getSession()->getPage()->getContent(), $backtraces)) {
$msgs = array();
foreach ($backtraces[0] as $backtrace) {
$msgs[] = $backtrace . '()';
}
$msg = "Other backtraces found:\n" . implode("\n", $msgs);
throw new \Exception(htmlentities($msg));
}
}

/**
* Converts HTML tags to line breaks to display the info in CLI
*
* @param string $html
* @return string
*/
protected function get_debug_text($html) {

// Replacing HTML tags for new lines and keeping only the text.
$notags = preg_replace('/<+\s*\/*\s*([A-Z][A-Z0-9]*)\b[^>]*\/*\s*>*/i', "\n", $html);
return preg_replace("/(\n)+/s", "\n", $notags);
}

}
2 changes: 1 addition & 1 deletion lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2792,7 +2792,7 @@ function debugging($message = '', $level = DEBUG_NORMAL, $backtrace = null) {
if (CLI_SCRIPT) {
echo "++ $message ++\n$from";
} else {
echo '<div class="notifytiny">' . $message . $from . '</div>';
echo '<div class="notifytiny debuggingmessage">' . $message . $from . '</div>';
}

} else {
Expand Down

0 comments on commit 07c9a6d

Please sign in to comment.