MediaWiki  REL1_22
ApiQueryBasicTest.php
Go to the documentation of this file.
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         ) )
00073     );
00074 
00075     private static $templates = array(
00076         array( 'prop' => 'templates', 'titles' => 'AQBT-All' ),
00077         array( 'pages' => array(
00078             '1' => array(
00079                 'pageid' => 1,
00080                 'ns' => 0,
00081                 'title' => 'AQBT-All',
00082                 'templates' => array(
00083                     array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
00084                 )
00085             )
00086         ) )
00087     );
00088 
00089     private static $categories = array(
00090         array( 'prop' => 'categories', 'titles' => 'AQBT-All' ),
00091         array( 'pages' => array(
00092             '1' => array(
00093                 'pageid' => 1,
00094                 'ns' => 0,
00095                 'title' => 'AQBT-All',
00096                 'categories' => array(
00097                     array( 'ns' => 14, 'title' => 'Category:AQBT-Cat' ),
00098                 )
00099             )
00100         ) )
00101     );
00102 
00103     private static $allpages = array(
00104         array( 'list' => 'allpages', 'apprefix' => 'AQBT-' ),
00105         array( 'allpages' => array(
00106             array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
00107             array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ),
00108             array( 'pageid' => 3, 'ns' => 0, 'title' => 'AQBT-Links' ),
00109             array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ),
00110         ) )
00111     );
00112 
00113     private static $alllinks = array(
00114         array( 'list' => 'alllinks', 'alprefix' => 'AQBT-' ),
00115         array( 'alllinks' => array(
00116             array( 'ns' => 0, 'title' => 'AQBT-All' ),
00117             array( 'ns' => 0, 'title' => 'AQBT-Categories' ),
00118             array( 'ns' => 0, 'title' => 'AQBT-Links' ),
00119             array( 'ns' => 0, 'title' => 'AQBT-Templates' ),
00120         ) )
00121     );
00122 
00123     private static $alltransclusions = array(
00124         array( 'list' => 'alltransclusions', 'atprefix' => 'AQBT-' ),
00125         array( 'alltransclusions' => array(
00126             array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
00127             array( 'ns' => 10, 'title' => 'Template:AQBT-T' ),
00128         ) )
00129     );
00130 
00131     private static $allcategories = array(
00132         array( 'list' => 'allcategories', 'acprefix' => 'AQBT-' ),
00133         array( 'allcategories' => array(
00134             array( '*' => 'AQBT-Cat' ),
00135         ) )
00136     );
00137 
00138     private static $backlinks = array(
00139         array( 'list' => 'backlinks', 'bltitle' => 'AQBT-Links' ),
00140         array( 'backlinks' => array(
00141             array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
00142         ) )
00143     );
00144 
00145     private static $embeddedin = array(
00146         array( 'list' => 'embeddedin', 'eititle' => 'Template:AQBT-T' ),
00147         array( 'embeddedin' => array(
00148             array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
00149             array( 'pageid' => 4, 'ns' => 0, 'title' => 'AQBT-Templates' ),
00150         ) )
00151     );
00152 
00153     private static $categorymembers = array(
00154         array( 'list' => 'categorymembers', 'cmtitle' => 'Category:AQBT-Cat' ),
00155         array( 'categorymembers' => array(
00156             array( 'pageid' => 1, 'ns' => 0, 'title' => 'AQBT-All' ),
00157             array( 'pageid' => 2, 'ns' => 0, 'title' => 'AQBT-Categories' ),
00158         ) )
00159     );
00160 
00161     private static $generatorAllpages = array(
00162         array( 'generator' => 'allpages', 'gapprefix' => 'AQBT-' ),
00163         array( 'pages' => array(
00164             '1' => array(
00165                 'pageid' => 1,
00166                 'ns' => 0,
00167                 'title' => 'AQBT-All' ),
00168             '2' => array(
00169                 'pageid' => 2,
00170                 'ns' => 0,
00171                 'title' => 'AQBT-Categories' ),
00172             '3' => array(
00173                 'pageid' => 3,
00174                 'ns' => 0,
00175                 'title' => 'AQBT-Links' ),
00176             '4' => array(
00177                 'pageid' => 4,
00178                 'ns' => 0,
00179                 'title' => 'AQBT-Templates' ),
00180         ) )
00181     );
00182 
00183     private static $generatorLinks = array(
00184         array( 'generator' => 'links', 'titles' => 'AQBT-Links' ),
00185         array( 'pages' => array(
00186             '1' => array(
00187                 'pageid' => 1,
00188                 'ns' => 0,
00189                 'title' => 'AQBT-All' ),
00190             '2' => array(
00191                 'pageid' => 2,
00192                 'ns' => 0,
00193                 'title' => 'AQBT-Categories' ),
00194             '4' => array(
00195                 'pageid' => 4,
00196                 'ns' => 0,
00197                 'title' => 'AQBT-Templates' ),
00198         ) )
00199     );
00200 
00201     private static $generatorLinksPropLinks = array(
00202         array( 'prop' => 'links' ),
00203         array( 'pages' => array(
00204             '1' => array( 'links' => array(
00205                 array( 'ns' => 0, 'title' => 'AQBT-Links' ),
00206             ) )
00207         ) )
00208     );
00209 
00210     private static $generatorLinksPropTemplates = array(
00211         array( 'prop' => 'templates' ),
00212         array( 'pages' => array(
00213             '1' => array( 'templates' => array(
00214                 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ),
00215             '4' => array( 'templates' => array(
00216                 array( 'ns' => 10, 'title' => 'Template:AQBT-T' ) ) ),
00217         ) )
00218     );
00219 
00223     public function testProps() {
00224         $this->check( self::$links );
00225         $this->check( self::$templates );
00226         $this->check( self::$categories );
00227     }
00228 
00232     public function testLists() {
00233         $this->check( self::$allpages );
00234         $this->check( self::$alllinks );
00235         $this->check( self::$alltransclusions );
00236         // This test is temporarily disabled until a sqlite bug is fixed
00237         // $this->check( self::$allcategories );
00238         $this->check( self::$backlinks );
00239         $this->check( self::$embeddedin );
00240         $this->check( self::$categorymembers );
00241     }
00242 
00246     public function testAllTogether() {
00247 
00248         // All props together
00249         $this->check( $this->merge(
00250             self::$links,
00251             self::$templates,
00252             self::$categories
00253         ) );
00254 
00255         // All lists together
00256         $this->check( $this->merge(
00257             self::$allpages,
00258             self::$alllinks,
00259             self::$alltransclusions,
00260             // This test is temporarily disabled until a sqlite bug is fixed
00261             // self::$allcategories,
00262             self::$backlinks,
00263             self::$embeddedin,
00264             self::$categorymembers
00265         ) );
00266 
00267         // All props+lists together
00268         $this->check( $this->merge(
00269             self::$links,
00270             self::$templates,
00271             self::$categories,
00272             self::$allpages,
00273             self::$alllinks,
00274             self::$alltransclusions,
00275             // This test is temporarily disabled until a sqlite bug is fixed
00276             // self::$allcategories,
00277             self::$backlinks,
00278             self::$embeddedin,
00279             self::$categorymembers
00280         ) );
00281     }
00282 
00286     public function testGenerator() {
00287         // generator=allpages
00288         $this->check( self::$generatorAllpages );
00289         // generator=allpages & list=allpages
00290         $this->check( $this->merge(
00291             self::$generatorAllpages,
00292             self::$allpages ) );
00293         // generator=links
00294         $this->check( self::$generatorLinks );
00295         // generator=links & prop=links
00296         $this->check( $this->merge(
00297             self::$generatorLinks,
00298             self::$generatorLinksPropLinks ) );
00299         // generator=links & prop=templates
00300         $this->check( $this->merge(
00301             self::$generatorLinks,
00302             self::$generatorLinksPropTemplates ) );
00303         // generator=links & prop=links|templates
00304         $this->check( $this->merge(
00305             self::$generatorLinks,
00306             self::$generatorLinksPropLinks,
00307             self::$generatorLinksPropTemplates ) );
00308         // generator=links & prop=links|templates & list=allpages|...
00309         $this->check( $this->merge(
00310             self::$generatorLinks,
00311             self::$generatorLinksPropLinks,
00312             self::$generatorLinksPropTemplates,
00313             self::$allpages,
00314             self::$alllinks,
00315             self::$alltransclusions,
00316             // This test is temporarily disabled until a sqlite bug is fixed
00317             // self::$allcategories,
00318             self::$backlinks,
00319             self::$embeddedin,
00320             self::$categorymembers ) );
00321     }
00322 
00326     public function testGeneratorRedirects() {
00327         $this->editPage( 'AQBT-Target', 'test' );
00328         $this->editPage( 'AQBT-Redir', '#REDIRECT [[AQBT-Target]]' );
00329         $this->check( array(
00330             array( 'generator' => 'backlinks', 'gbltitle' => 'AQBT-Target', 'redirects' => '1' ),
00331             array(
00332                 'redirects' => array(
00333                     array(
00334                         'from' => 'AQBT-Redir',
00335                         'to' => 'AQBT-Target',
00336                     )
00337                 ),
00338                 'pages' => array(
00339                     '6' => array(
00340                         'pageid' => 6,
00341                         'ns' => 0,
00342                         'title' => 'AQBT-Target',
00343                     )
00344                 ),
00345             )
00346         ) );
00347     }
00348 
00352     private function mergeExpected( &$all, $item ) {
00353         foreach ( $item as $k => $v ) {
00354             if ( array_key_exists( $k, $all ) ) {
00355                 if ( is_array( $all[$k] ) ) {
00356                     $this->mergeExpected( $all[$k], $v );
00357                 } else {
00358                     $this->assertEquals( $all[$k], $v );
00359                 }
00360             } else {
00361                 $all[$k] = $v;
00362             }
00363         }
00364     }
00365 
00371     private function assertQueryResults( $expected, $result ) {
00372         reset( $expected );
00373         reset( $result );
00374         while ( true ) {
00375             $e = each( $expected );
00376             $r = each( $result );
00377             // If either of the arrays is shorter, abort. If both are done, success.
00378             $this->assertEquals( (bool)$e, (bool)$r );
00379             if ( !$e ) {
00380                 break; // done
00381             }
00382             // continue only if keys are identical or both keys are numeric
00383             $this->assertTrue( $e['key'] === $r['key'] || ( is_numeric( $e['key'] ) && is_numeric( $r['key'] ) ) );
00384             // don't compare pageids
00385             if ( $e['key'] !== 'pageid' ) {
00386                 // If values are arrays, compare recursively, otherwise compare with ===
00387                 if ( is_array( $e['value'] ) && is_array( $r['value'] ) ) {
00388                     $this->assertQueryResults( $e['value'], $r['value'] );
00389                 } else {
00390                     $this->assertEquals( $e['value'], $r['value'] );
00391                 }
00392             }
00393         }
00394     }
00395 }