MDL-9123:
[moodle-linuxchix.git] / lib / simpletestlib / ui / webunit_reporter.php
blob038d244e2e8abeacf0fb0917a9410d6a135339db
1 <?php
2 /**
3 * base include file for SimpleTest PUnit reporter
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id$
7 */
9 /**
10 * @ignore originally defined in simple_test.php
12 if (!defined("SIMPLE_TEST")) {
13 define("SIMPLE_TEST", "simpletest/");
15 require_once(SIMPLE_TEST . 'runner.php');
16 require_once(SIMPLE_TEST . 'reporter.php');
17 /**
18 * Main sprintf template for the start of the page.
19 * Sequence of parameters is:
20 * - title - string
21 * - script path - string
22 * - script path - string
23 * - css path - string
24 * - additional css - string
25 * - title - string
26 * - image path - string
28 define('SIMPLETEST_WEBUNIT_HEAD', <<<EOS
29 <html>
30 <head>
31 <title>%s</title>
32 <script type="text/javascript" src="%sx.js"></script>
33 <script type="text/javascript" src="%swebunit.js"></script>
34 <link rel="stylesheet" type="text/css" href="%swebunit.css" title="Default"></link>
35 <style type="text/css">
37 </style>
38 </head>
39 <body>
40 <div id="wait">
41 <h1>&nbsp;Running %s&nbsp;</h1>
42 Please wait...<br />
43 <img src="%swait.gif" border="0"><br />&nbsp;
44 </div>
45 <script type="text/javascript">
46 wait_start();
47 </script>
48 <div id="webunit">
49 <div id="run"></div><br />
50 <div id="tabs">
51 <div id="visible_tab">visible tab content</div>
52 &nbsp;&nbsp;<span id="failtab" class="activetab">&nbsp;&nbsp;<a href="javascript:activate_tab('fail');">Fail</a>&nbsp;&nbsp;</span>
53 <span id="treetab" class="inactivetab">&nbsp;&nbsp;<a href="javascript:activate_tab('tree');">Tree</a>&nbsp;&nbsp;</span>
54 </div>
55 <div id="msg">Click on a failed test case method in the tree tab to view output here.</div>
56 </div>
57 <div id="fail"></div>
58 <div id="tree"></div>
59 <!-- open a new script to capture js vars as the tests run -->
60 <script type="text/javascript">
61 layout();
63 EOS
66 /**
67 * Not used yet.
68 * May be needed for localized styles we need at runtime, not in the stylesheet.
70 define('SIMPLETEST_WEBUNIT_CSS', '/* this space reseved for future use */');
72 /**
73 * Sample minimal test displayer. Generates only
74 * failure messages and a pass count.
75 * @package SimpleTest
76 * @subpackage UnitTester
78 class WebUnitReporter extends SimpleReporter {
79 /**
80 * @var string Base directory for PUnit script, images and style sheets.
81 * Needs to be a relative path from where the test scripts are run
82 * (and obviously, visible in the document root).
84 var $path;
86 /**
87 * Does nothing yet. The first output will
88 * be sent on the first test start. For use
89 * by a web browser.
90 * @access public
92 function WebUnitReporter($path='../ui/') {
93 $this->SimpleReporter();
94 $this->path = $path;
97 /**
98 * Paints the top of the web page setting the
99 * title to the name of the starting test.
100 * @param string $test_name Name class of test.
101 * @access public
103 function paintHeader($test_name) {
104 $this->sendNoCacheHeaders();
105 echo sprintf(
106 SIMPLETEST_WEBUNIT_HEAD
107 ,$test_name
108 ,$this->path.'js/'
109 ,$this->path.'js/'
110 ,$this->path.'css/'
111 ,$this->_getCss()
112 ,$test_name
113 ,$this->path.'img/'
115 flush();
119 * Send the headers necessary to ensure the page is
120 * reloaded on every request. Otherwise you could be
121 * scratching your head over out of date test data.
122 * @access public
123 * @static
125 function sendNoCacheHeaders() {
126 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
127 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
128 header("Cache-Control: no-store, no-cache, must-revalidate");
129 header("Cache-Control: post-check=0, pre-check=0", false);
130 header("Pragma: no-cache");
134 * Paints the CSS. Add additional styles here.
135 * @return string CSS code as text.
136 * @access protected
138 function _getCss() {
139 return SIMPLETEST_WEBUNIT_CSS;
143 * Paints the end of the test with a summary of
144 * the passes and failures.
145 * @param string $test_name Name class of test.
146 * @access public
148 function paintFooter($test_name) {
149 echo 'make_tree();</script>'.$this->outputScript("xHide('wait');");
150 $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
151 $content = "<h1>$test_name</h1>\n";
152 $content .= "<div style=\"";
153 $content .= "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
154 $content .= "\">";
155 $content .= $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
156 $content .= " test cases complete:\n";
157 $content .= "<strong>" . $this->getPassCount() . "</strong> passes, ";
158 $content .= "<strong>" . $this->getFailCount() . "</strong> fails and ";
159 $content .= "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
160 $content .= "</div>\n";
162 echo $this->outputScript('foo = "'.$this->toJsString($content).'";'."\nset_div_content('run', foo);");
163 echo "\n</body>\n</html>\n";
168 * Paints formatted text such as dumped variables.
169 * @param string $message Text to show.
170 * @access public
172 function paintFormattedMessage($message) {
173 echo "add_log(\"".$this->toJsString("<pre>$message</pre>", true)."\");\n";
177 * Paints the start of a group test. Will also paint
178 * the page header and footer if this is the
179 * first test. Will stash the size if the first
180 * start.
181 * @param string $test_name Name of test that is starting.
182 * @param integer $size Number of test cases starting.
183 * @access public
185 function paintGroupStart($test_name, $size) {
186 Parent::paintGroupStart($test_name, $size);
187 echo "add_group('$test_name');\n";
191 * Paints the start of a test case. Will also paint
192 * the page header and footer if this is the
193 * first test. Will stash the size if the first
194 * start.
195 * @param string $test_name Name of test that is starting.
196 * @access public
198 function paintCaseStart($test_name) {
199 Parent::paintCaseStart($test_name);
200 echo "add_case('$test_name');\n";
205 * Paints the start of a test method.
206 * @param string $test_name Name of test that is starting.
207 * @access public
209 function paintMethodStart($test_name) {
210 Parent::paintMethodStart($test_name);
211 echo "add_method('$test_name');\n";
215 * Paints the end of a test method.
216 * @param string $test_name Name of test that is ending.
217 * @access public
219 function paintMethodEnd($test_name) {
220 Parent::paintMethodEnd($test_name);
224 * Paints the test failure with a breadcrumbs
225 * trail of the nesting test suites below the
226 * top level test.
227 * @param string $message Failure message displayed in
228 * the context of the other tests.
229 * @access public
231 function paintFail($message) {
232 parent::paintFail($message);
233 $msg = "<span class=\"fail\">Fail</span>: ";
234 $breadcrumb = $this->getTestList();
235 array_shift($breadcrumb);
236 $msg .= implode("-&gt;", $breadcrumb);
237 $msg .= "-&gt;" . htmlentities($message) . "<br />";
238 echo "add_fail('$msg');\n";
242 * Paints a PHP error or exception.
243 * @param string $message Message is ignored.
244 * @access public
245 * @abstract
247 function paintException($message) {
248 parent::paintException($message);
249 $msg = "<span class=\"fail\">Exception</span>: ";
250 $breadcrumb = $this->getTestList();
251 array_shift($breadcrumb);
252 $msg .= implode("-&gt;", $breadcrumb);
253 $msg .= "-&gt;<strong>" . htmlentities($message) . "</strong><br />";
254 echo "add_fail('$msg');\n";
258 * Returns the script passed in wrapped in script tags.
260 * @param string $script the script to output
261 * @return string the script wrapped with script tags
263 function outputScript($script)
265 return "<script type=\"text/javascript\">\n".$script."\n</script>\n";
270 * Transform a string into a format acceptable to JavaScript
271 * @param string $str the string to transform
272 * @return string
274 function toJsString($str, $preserveCr=false) {
275 $cr = ($preserveCr) ? '\\n' : '';
276 return str_replace(
277 array('"'
278 ,"\n")
279 ,array('\"'
280 ,"$cr\"\n\t+\"")
281 ,$str