MediaWiki
REL1_21
|
00001 <?php 00027 require_once( 'ApiQueryTestBase.php' ); 00028 00035 class ApiQueryBasicTest extends ApiQueryTestBase { 00040 function addDBData() { 00041 try { 00042 if ( Title::newFromText( 'AQBT-All' )->exists() ) { 00043 return; 00044 } 00045 00046 // Ordering is important, as it will be returned in the same order as stored in the index 00047 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' ); 00048 $this->editPage( 'AQBT-Categories', '[[Category:AQBT-Cat]]' ); 00049 $this->editPage( 'AQBT-Links', '[[AQBT-All]] [[AQBT-Categories]] [[AQBT-Templates]]' ); 00050 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' ); 00051 $this->editPage( 'AQBT-T', 'Content', '', NS_TEMPLATE ); 00052 00053 // Refresh due to the bug with listing transclusions as links if they don't exist 00054 $this->editPage( 'AQBT-All', '[[Category:AQBT-Cat]] [[AQBT-Links]] {{AQBT-T}}' ); 00055 $this->editPage( 'AQBT-Templates', '{{AQBT-T}}' ); 00056 } catch ( Exception $e ) { 00057 $this->exceptionFromAddDBData = $e; 00058 } 00059 } 00060 00061 private static $links = array( 00062 array( 'prop' => 'links', 'titles' => 'AQBT-All' ), 00063 array( 'pages' => array( 00064 '1' => array( 00065 'pageid' => 1, 00066 'ns' => 0, 00067 'title' => 'AQBT-All', 00068 'links' => array( 00069 array( 'ns' => 0, 'title' => 'AQBT-Links' ), 00070 ) ) ) ) ); 00071 00072 private static $templates = array( 00073 array( 'prop' => 'templates', 'titles' => 'AQBT-All' ), 00074 array( 'pages' => array( 00075 '1' => array( 00076 'pageid' => 1, 00077 'ns' => 0, 00078 'title' => 'AQBT-All', 00079 'templates' => array( 00080 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ), 00081 ) ) ) ) ); 00082 00083 private static $categories = array( 00084 array( 'prop' => 'categories', 'titles' => 'AQBT-All' ), 00085 array( 'pages' => array( 00086 '1' => array( 00087 'pageid' => 1, 00088 'ns' => 0, 00089 'title' => 'AQBT-All', 00090 'categories' => array( 00091 array( 'ns' => 14, 'title' => 'Category:AQBT-Cat' ), 00092 ) ) ) ) ); 00093 00094 private static $allpages = array( 00095 array( 'list' => 'allpages', 'apprefix' => 'AQBT-' ), 00096 array( 'allpages' => array( 00097 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ), 00098 array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ), 00099 array( 'pageid' => 3, 'ns' => 0, 'title' => 'AQBT-Links' ), 00100 array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ), 00101 ) ) ); 00102 00103 private static $alllinks = array( 00104 array( 'list' => 'alllinks', 'alprefix' => 'AQBT-' ), 00105 array( 'alllinks' => array( 00106 array( 'ns' => 0, 'title' => 'AQBT-All' ), 00107 array( 'ns' => 0, 'title' => 'AQBT-Categories' ), 00108 array( 'ns' => 0, 'title' => 'AQBT-Links' ), 00109 array( 'ns' => 0, 'title' => 'AQBT-Templates' ), 00110 ) ) ); 00111 00112 private static $alltransclusions = array( 00113 array( 'list' => 'alltransclusions', 'atprefix' => 'AQBT-' ), 00114 array( 'alltransclusions' => array( 00115 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ), 00116 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ), 00117 ) ) ); 00118 00119 private static $allcategories = array( 00120 array( 'list' => 'allcategories', 'acprefix' => 'AQBT-' ), 00121 array( 'allcategories' => array( 00122 array( '*' => 'AQBT-Cat' ), 00123 ) ) ); 00124 00125 private static $backlinks = array( 00126 array( 'list' => 'backlinks', 'bltitle' => 'AQBT-Links' ), 00127 array( 'backlinks' => array( 00128 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ), 00129 ) ) ); 00130 00131 private static $embeddedin = array( 00132 array( 'list' => 'embeddedin', 'eititle' => 'Template:AQBT-T' ), 00133 array( 'embeddedin' => array( 00134 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ), 00135 array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ), 00136 ) ) ); 00137 00138 private static $categorymembers = array( 00139 array( 'list' => 'categorymembers', 'cmtitle' => 'Category:AQBT-Cat' ), 00140 array( 'categorymembers' => array( 00141 array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ), 00142 array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ), 00143 ) ) ); 00144 00145 private static $generatorAllpages = array( 00146 array( 'generator' => 'allpages', 'gapprefix' => 'AQBT-' ), 00147 array( 'pages' => array( 00148 '1' => array( 00149 'pageid' => 1, 00150 'ns' => 0, 00151 'title' => 'AQBT-All' ), 00152 '2' => array( 00153 'pageid' => 2, 00154 'ns' => 0, 00155 'title' => 'AQBT-Categories' ), 00156 '3' => array( 00157 'pageid' => 3, 00158 'ns' => 0, 00159 'title' => 'AQBT-Links' ), 00160 '4' => array( 00161 'pageid' => 4, 00162 'ns' => 0, 00163 'title' => 'AQBT-Templates' ), 00164 ) ) ); 00165 00166 private static $generatorLinks = array( 00167 array( 'generator' => 'links', 'titles' => 'AQBT-Links' ), 00168 array( 'pages' => array( 00169 '1' => array( 00170 'pageid' => 1, 00171 'ns' => 0, 00172 'title' => 'AQBT-All' ), 00173 '2' => array( 00174 'pageid' => 2, 00175 'ns' => 0, 00176 'title' => 'AQBT-Categories' ), 00177 '4' => array( 00178 'pageid' => 4, 00179 'ns' => 0, 00180 'title' => 'AQBT-Templates' ), 00181 ) ) ); 00182 00183 private static $generatorLinksPropLinks = array( 00184 array( 'prop' => 'links' ), 00185 array( 'pages' => array( 00186 '1' => array( 'links' => array( 00187 array( 'ns' => 0, 'title' => 'AQBT-Links' ), 00188 ) ) ) ) ); 00189 00190 private static $generatorLinksPropTemplates = array( 00191 array( 'prop' => 'templates' ), 00192 array( 'pages' => array( 00193 '1' => array( 'templates' => array( 00194 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ), 00195 '4' => array( 'templates' => array( 00196 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ), 00197 ) ) ); 00198 00202 public function testProps() { 00203 $this->check( self::$links ); 00204 $this->check( self::$templates ); 00205 $this->check( self::$categories ); 00206 } 00207 00211 public function testLists() { 00212 $this->check( self::$allpages ); 00213 $this->check( self::$alllinks ); 00214 $this->check( self::$alltransclusions ); 00215 // This test is temporarily disabled until a sqlite bug is fixed 00216 // $this->check( self::$allcategories ); 00217 $this->check( self::$backlinks ); 00218 $this->check( self::$embeddedin ); 00219 $this->check( self::$categorymembers ); 00220 } 00221 00225 public function testAllTogether() { 00226 00227 // All props together 00228 $this->check( $this->merge( 00229 self::$links, 00230 self::$templates, 00231 self::$categories 00232 ) ); 00233 00234 // All lists together 00235 $this->check( $this->merge( 00236 self::$allpages, 00237 self::$alllinks, 00238 self::$alltransclusions, 00239 // This test is temporarily disabled until a sqlite bug is fixed 00240 // self::$allcategories, 00241 self::$backlinks, 00242 self::$embeddedin, 00243 self::$categorymembers 00244 ) ); 00245 00246 // All props+lists together 00247 $this->check( $this->merge( 00248 self::$links, 00249 self::$templates, 00250 self::$categories, 00251 self::$allpages, 00252 self::$alllinks, 00253 self::$alltransclusions, 00254 // This test is temporarily disabled until a sqlite bug is fixed 00255 // self::$allcategories, 00256 self::$backlinks, 00257 self::$embeddedin, 00258 self::$categorymembers 00259 ) ); 00260 } 00261 00265 public function testGenerator() { 00266 // generator=allpages 00267 $this->check( self::$generatorAllpages ); 00268 // generator=allpages & list=allpages 00269 $this->check( $this->merge( 00270 self::$generatorAllpages, 00271 self::$allpages ) ); 00272 // generator=links 00273 $this->check( self::$generatorLinks ); 00274 // generator=links & prop=links 00275 $this->check( $this->merge( 00276 self::$generatorLinks, 00277 self::$generatorLinksPropLinks ) ); 00278 // generator=links & prop=templates 00279 $this->check( $this->merge( 00280 self::$generatorLinks, 00281 self::$generatorLinksPropTemplates ) ); 00282 // generator=links & prop=links|templates 00283 $this->check( $this->merge( 00284 self::$generatorLinks, 00285 self::$generatorLinksPropLinks, 00286 self::$generatorLinksPropTemplates ) ); 00287 // generator=links & prop=links|templates & list=allpages|... 00288 $this->check( $this->merge( 00289 self::$generatorLinks, 00290 self::$generatorLinksPropLinks, 00291 self::$generatorLinksPropTemplates, 00292 self::$allpages, 00293 self::$alllinks, 00294 self::$alltransclusions, 00295 // This test is temporarily disabled until a sqlite bug is fixed 00296 // self::$allcategories, 00297 self::$backlinks, 00298 self::$embeddedin, 00299 self::$categorymembers ) ); 00300 } 00301 00305 private function mergeExpected( &$all, $item ) { 00306 foreach ( $item as $k => $v ) { 00307 if ( array_key_exists( $k, $all ) ) { 00308 if ( is_array( $all[$k] ) ) { 00309 $this->mergeExpected( $all[$k], $v ); 00310 } else { 00311 $this->assertEquals( $all[$k], $v ); 00312 } 00313 } else { 00314 $all[$k] = $v; 00315 } 00316 } 00317 } 00318 00324 private function assertQueryResults( $expected, $result ) { 00325 reset( $expected ); 00326 reset( $result ); 00327 while ( true ) { 00328 $e = each( $expected ); 00329 $r = each( $result ); 00330 // If either of the arrays is shorter, abort. If both are done, success. 00331 $this->assertEquals( (bool)$e, (bool)$r ); 00332 if ( !$e ) { 00333 break; // done 00334 } 00335 // continue only if keys are identical or both keys are numeric 00336 $this->assertTrue( $e['key'] === $r['key'] || ( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) ); 00337 // don't compare pageids 00338 if ( $e['key'] !== 'pageid' ) { 00339 // If values are arrays, compare recursively, otherwise compare with === 00340 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) { 00341 $this->assertQueryResults( $e['value'], $r['value'] ); 00342 } else { 00343 $this->assertEquals( $e['value'], $r['value'] ); 00344 } 00345 } 00346 } 00347 } 00348 }