Split up testHelpers.inc, break off fuzz testing
[lhc/web/wiklou.git] / tests / parser / DbTestPreviewer.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Testing
20 */
21
22 class DbTestPreviewer extends TestRecorder {
23 protected $lb; // /< Database load balancer
24 protected $db; // /< Database connection to the main DB
25 protected $curRun; // /< run ID number for the current run
26 protected $prevRun; // /< run ID number for the previous run, if any
27 protected $results; // /< Result array
28
29 /**
30 * This should be called before the table prefix is changed
31 * @param TestRecorder $parent
32 */
33 function __construct( $parent ) {
34 parent::__construct( $parent );
35
36 $this->lb = wfGetLBFactory()->newMainLB();
37 // This connection will have the wiki's table prefix, not parsertest_
38 $this->db = $this->lb->getConnection( DB_MASTER );
39 }
40
41 /**
42 * Set up result recording; insert a record for the run with the date
43 * and all that fun stuff
44 */
45 function start() {
46 parent::start();
47
48 if ( !$this->db->tableExists( 'testrun', __METHOD__ )
49 || !$this->db->tableExists( 'testitem', __METHOD__ )
50 ) {
51 print "WARNING> `testrun` table not found in database.\n";
52 $this->prevRun = false;
53 } else {
54 // We'll make comparisons against the previous run later...
55 $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
56 }
57
58 $this->results = [];
59 }
60
61 function getName( $test, $subtest ) {
62 if ( $subtest ) {
63 return "$test subtest #$subtest";
64 } else {
65 return $test;
66 }
67 }
68
69 function record( $test, $subtest, $result ) {
70 parent::record( $test, $subtest, $result );
71 $this->results[ $this->getName( $test, $subtest ) ] = $result;
72 }
73
74 function report() {
75 if ( $this->prevRun ) {
76 // f = fail, p = pass, n = nonexistent
77 // codes show before then after
78 $table = [
79 'fp' => 'previously failing test(s) now PASSING! :)',
80 'pn' => 'previously PASSING test(s) removed o_O',
81 'np' => 'new PASSING test(s) :)',
82
83 'pf' => 'previously passing test(s) now FAILING! :(',
84 'fn' => 'previously FAILING test(s) removed O_o',
85 'nf' => 'new FAILING test(s) :(',
86 'ff' => 'still FAILING test(s) :(',
87 ];
88
89 $prevResults = [];
90
91 $res = $this->db->select( 'testitem', [ 'ti_name', 'ti_success' ],
92 [ 'ti_run' => $this->prevRun ], __METHOD__ );
93
94 foreach ( $res as $row ) {
95 if ( !$this->parent->regex
96 || preg_match( "/{$this->parent->regex}/i", $row->ti_name )
97 ) {
98 $prevResults[$row->ti_name] = $row->ti_success;
99 }
100 }
101
102 $combined = array_keys( $this->results + $prevResults );
103
104 # Determine breakdown by change type
105 $breakdown = [];
106 foreach ( $combined as $test ) {
107 if ( !isset( $prevResults[$test] ) ) {
108 $before = 'n';
109 } elseif ( $prevResults[$test] == 1 ) {
110 $before = 'p';
111 } else /* if ( $prevResults[$test] == 0 )*/ {
112 $before = 'f';
113 }
114
115 if ( !isset( $this->results[$test] ) ) {
116 $after = 'n';
117 } elseif ( $this->results[$test] == 1 ) {
118 $after = 'p';
119 } else /*if ( $this->results[$test] == 0 ) */ {
120 $after = 'f';
121 }
122
123 $code = $before . $after;
124
125 if ( isset( $table[$code] ) ) {
126 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
127 }
128 }
129
130 # Write out results
131 foreach ( $table as $code => $label ) {
132 if ( !empty( $breakdown[$code] ) ) {
133 $count = count( $breakdown[$code] );
134 printf( "\n%4d %s\n", $count, $label );
135
136 foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
137 print " * $differing_test_name [$statusInfo]\n";
138 }
139 }
140 }
141 } else {
142 print "No previous test runs to compare against.\n";
143 }
144
145 print "\n";
146 parent::report();
147 }
148
149 /**
150 * Returns a string giving information about when a test last had a status change.
151 * Could help to track down when regressions were introduced, as distinct from tests
152 * which have never passed (which are more change requests than regressions).
153 * @param string $testname
154 * @param string $after
155 * @return string
156 */
157 private function getTestStatusInfo( $testname, $after ) {
158 // If we're looking at a test that has just been removed, then say when it first appeared.
159 if ( $after == 'n' ) {
160 $changedRun = $this->db->selectField( 'testitem',
161 'MIN(ti_run)',
162 [ 'ti_name' => $testname ],
163 __METHOD__ );
164 $appear = $this->db->selectRow( 'testrun',
165 [ 'tr_date', 'tr_mw_version' ],
166 [ 'tr_id' => $changedRun ],
167 __METHOD__ );
168
169 return "First recorded appearance: "
170 . date( "d-M-Y H:i:s", strtotime( $appear->tr_date ) )
171 . ", " . $appear->tr_mw_version;
172 }
173
174 // Otherwise, this test has previous recorded results.
175 // See when this test last had a different result to what we're seeing now.
176 $conds = [
177 'ti_name' => $testname,
178 'ti_success' => ( $after == 'f' ? "1" : "0" ) ];
179
180 if ( $this->curRun ) {
181 $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun );
182 }
183
184 $changedRun = $this->db->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
185
186 // If no record of ever having had a different result.
187 if ( is_null( $changedRun ) ) {
188 if ( $after == "f" ) {
189 return "Has never passed";
190 } else {
191 return "Has never failed";
192 }
193 }
194
195 // Otherwise, we're looking at a test whose status has changed.
196 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
197 // In this situation, give as much info as we can as to when it changed status.
198 $pre = $this->db->selectRow( 'testrun',
199 [ 'tr_date', 'tr_mw_version' ],
200 [ 'tr_id' => $changedRun ],
201 __METHOD__ );
202 $post = $this->db->selectRow( 'testrun',
203 [ 'tr_date', 'tr_mw_version' ],
204 [ "tr_id > " . $this->db->addQuotes( $changedRun ) ],
205 __METHOD__,
206 [ "LIMIT" => 1, "ORDER BY" => 'tr_id' ]
207 );
208
209 if ( $post ) {
210 $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date ) ) . ", {$post->tr_mw_version}";
211 } else {
212 $postDate = 'now';
213 }
214
215 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
216 . date( "d-M-Y H:i:s", strtotime( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
217 . " and $postDate";
218 }
219
220 /**
221 * Close the DB connection
222 */
223 function end() {
224 $this->lb->closeAll();
225 parent::end();
226 }
227 }
228