diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php index c5a75c80939..f7f2fca6462 100644 --- a/lib/tests/weblib_test.php +++ b/lib/tests/weblib_test.php @@ -212,4 +212,92 @@ class web_testcase extends advanced_testcase { $PAGE->set_url('/course/view.php', array('id'=>1)); $this->assertEquals($CFG->wwwroot.'/course/view.php?id=1', qualified_me()); } + + public function test_null_progres_trace() { + $this->resetAfterTest(false); + + $trace = new null_progress_trace(); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $output = ob_get_contents(); + $this->assertSame('', $output); + $this->expectOutputString(''); + } + + public function test_text_progres_trace() { + $this->resetAfterTest(false); + + $trace = new text_progress_trace(); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $this->expectOutputString("do\n re\n mi\n"); + } + + public function test_html_progres_trace() { + $this->resetAfterTest(false); + + $trace = new html_progress_trace(); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $this->expectOutputString("

do

\n

  re

\n

    mi

\n"); + } + + public function test_html_list_progress_trace() { + $this->resetAfterTest(false); + + $trace = new html_list_progress_trace(); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $this->expectOutputString("\n"); + } + + public function test_progres_trace_buffer() { + $this->resetAfterTest(false); + + $trace = new progress_trace_buffer(new html_progress_trace()); + ob_start(); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertSame("

do

\n

  re

\n

    mi

\n", $output); + $this->assertSame($output, $trace->get_buffer()); + + $trace = new progress_trace_buffer(new html_progress_trace(), false); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $this->assertSame("

do

\n

  re

\n

    mi

\n", $trace->get_buffer()); + $this->assertSame("

do

\n

  re

\n

    mi

\n", $trace->get_buffer()); + $trace->reset_buffer(); + $this->assertSame('', $trace->get_buffer()); + $this->expectOutputString(''); + } + + public function test_combined_progres_trace() { + $this->resetAfterTest(false); + + $trace1 = new progress_trace_buffer(new html_progress_trace(), false); + $trace2 = new progress_trace_buffer(new text_progress_trace(), false); + + $trace = new combined_progress_trace(array($trace1, $trace2)); + $trace->output('do'); + $trace->output('re', 1); + $trace->output('mi', 2); + $trace->finished(); + $this->assertSame("

do

\n

  re

\n

    mi

\n", $trace1->get_buffer()); + $this->assertSame("do\n re\n mi\n", $trace2->get_buffer()); + $this->expectOutputString(''); + } } diff --git a/lib/weblib.php b/lib/weblib.php index a82d3637227..9b93a3a39eb 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -3159,7 +3159,8 @@ EOT; */ abstract class progress_trace { /** - * Ouput an progress message in whatever format. + * Output an progress message in whatever format. + * * @param string $message the message to output. * @param integer $depth indent depth for this message. */ @@ -3198,7 +3199,7 @@ class null_progress_trace extends progress_trace { */ class text_progress_trace extends progress_trace { /** - * Output the trace message + * Output the trace message. * * @param string $message * @param int $depth @@ -3218,7 +3219,7 @@ class text_progress_trace extends progress_trace { */ class html_progress_trace extends progress_trace { /** - * Output the trace message + * Output the trace message. * * @param string $message * @param int $depth @@ -3280,6 +3281,150 @@ class html_list_progress_trace extends progress_trace { } } +/** + * This subclass of progress_trace outputs to error log. + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package moodlecore + */ +class error_log_progress_trace extends progress_trace { + /** @var string log prefix */ + protected $prefix; + + /** + * Constructor. + * @param string $prefix optional log prefix + */ + public function __construct($prefix = '') { + $this->prefix = $prefix; + } + + /** + * Output the trace message. + * + * @param string $message + * @param int $depth + * @return void Output is sent to error log. + */ + public function output($message, $depth = 0) { + error_log($this->prefix . str_repeat(' ', $depth) . $message); + } +} + +/** + * Special type of trace that can be used for catching of + * output of other traces. + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package moodlecore + */ +class progress_trace_buffer extends progress_trace { + /** @var progres_trace */ + protected $trace; + /** @var bool do we pass output out */ + protected $passthrough; + /** @var string output buffer */ + protected $buffer; + + /** + * Constructor. + * + * @param progress_trace $trace + * @param bool $passthrough true means output and buffer, false means just buffer and no output + */ + public function __construct(progress_trace $trace, $passthrough = true) { + $this->trace = $trace; + $this->passthrough = $passthrough; + $this->buffer = ''; + } + + /** + * Output the trace message. + * + * @param string $message the message to output. + * @param int $depth indent depth for this message. + * @return void output stored in buffer + */ + public function output($message, $depth = 0) { + ob_start(); + $this->trace->output($message, $depth); + $this->buffer .= ob_get_contents(); + if ($this->passthrough) { + ob_end_flush(); + } else { + ob_end_clean(); + } + } + + /** + * Called when the processing is finished. + */ + public function finished() { + ob_start(); + $this->trace->finished(); + $this->buffer .= ob_get_contents(); + if ($this->passthrough) { + ob_end_flush(); + } else { + ob_end_clean(); + } + } + + /** + * Reset internal text buffer. + */ + public function reset_buffer() { + $this->buffer = ''; + } + + /** + * Return internal text buffer. + * @return string buffered plain text + */ + public function get_buffer() { + return $this->buffer; + } +} + +/** + * Special type of trace that can be used for redirecting to multiple + * other traces. + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package moodlecore + */ +class combined_progress_trace extends progress_trace { + protected $traces; + + /** + * @param array $traces multiple traces + */ + public function __construct(array $traces) { + $this->traces = $traces; + } + + /** + * Output an progress message in whatever format. + * + * @param string $message the message to output. + * @param integer $depth indent depth for this message. + */ + public function output($message, $depth = 0) { + foreach($this->traces as $trace) { + $trace->output($message, $depth); + } + } + + /** + * Called when the processing is finished. + */ + public function finished() { + foreach($this->traces as $trace) { + $trace->finished(); + } + } +} + /** * Returns a localized sentence in the current language summarizing the current password policy *