HookDocTest.php

Go to the documentation of this file.
00001 <?php
00011 class HookDocTest extends MediaWikiTestCase {
00012     protected $documented = array();
00013     protected $used = array();
00014     protected $paths = array(
00015         'php' => array(
00016             '',
00017             'api',
00018             'ffs',
00019             'messagegroups',
00020             'specials',
00021             'tag',
00022             'translationaids',
00023             'ttmserver',
00024             'utils',
00025             'webservices',
00026         ),
00027         'js' => array(
00028             'resources/js',
00029         ),
00030     );
00031 
00032     protected function setUp() {
00033         parent::setUp();
00034         $contents = file_get_contents( __DIR__ . '/../../hooks.txt' );
00035         $blocks = preg_split( '/\n\n/', $contents );
00036         $type = false;
00037 
00038         foreach ( $blocks as $block ) {
00039             if ( $block === '=== PHP events ===' ) {
00040                 $type = 'php';
00041                 continue;
00042             } elseif ( $block === '=== JavaScript events ===' ) {
00043                 $type = 'js';
00044                 continue;
00045             } elseif ( !$type ) {
00046                 continue;
00047             }
00048 
00049             if ( $type ) {
00050                 list( $name, $params ) = self::parseDocBlock( $block );
00051                 $this->documented[$type][$name] = $params;
00052             }
00053         }
00054 
00055         $prefix = __DIR__ . '/../..';
00056         foreach ( $this->paths['php'] as $path ) {
00057             $path = "$prefix/$path/";
00058             $hooks = self::getHooksFromPath( $path, 'self::getPHPHooksFromFile' );
00059             foreach ( $hooks as $name => $params ) {
00060                 $this->used['php'][$name] = $params;
00061             }
00062         }
00063 
00064         foreach ( $this->paths['js'] as $path ) {
00065             $path = "$prefix/$path/";
00066             $hooks = self::getHooksFromPath( $path, 'self::getJSHooksFromFile' );
00067             foreach ( $hooks as $name => $params ) {
00068                 $this->used['js'][$name] = $params;
00069             }
00070         }
00071     }
00072 
00073     protected static function getJSHooksFromFile( $file ) {
00074         $content = file_get_contents( $file );
00075         $m = array();
00076         preg_match_all( '/(?:mw\.translateHooks\.run)\(\s*([\'"])(.*?)\1/', $content, $m );
00077         $hooks = array();
00078         foreach ( $m[2] as $hook ) {
00079             $hooks[$hook] = array();
00080         }
00081 
00082         return $hooks;
00083     }
00084 
00085     protected static function getPHPHooksFromFile( $file ) {
00086         $content = file_get_contents( $file );
00087         $m = array();
00088         preg_match_all( '/(?:wfRunHooks|Hooks\:\:run)\(\s*([\'"])(.*?)\1/', $content, $m );
00089         $hooks = array();
00090         foreach ( $m[2] as $hook ) {
00091             $hooks[$hook] = array();
00092         }
00093 
00094         return $hooks;
00095     }
00096 
00097     protected static function getHooksFromPath( $path, $callback ) {
00098         $hooks = array();
00099         $dh = opendir( $path );
00100         if ( $dh ) {
00101             while ( ( $file = readdir( $dh ) ) !== false ) {
00102                 if ( filetype( $path . $file ) == 'file' ) {
00103                     $hooks = array_merge( $hooks, call_user_func( $callback, $path . $file ) );
00104                 }
00105             }
00106             closedir( $dh );
00107         }
00108 
00109         return $hooks;
00110     }
00111 
00112     protected static function parseDocBlock( $block ) {
00113         preg_match( '/^;([^ ]+):/', $block, $match );
00114         $name = $match[1];
00115         preg_match_all( '/^ ([^ ]+)\s+([ ^])/', $block, $matches, PREG_SET_ORDER );
00116         $params = array();
00117         foreach ( $matches as $match ) {
00118             $params[$match[2]] = $match[1];
00119         }
00120 
00121         return array( $name, $params );
00122     }
00123 
00124     public function testHookIsDocumentedPHP() {
00125         foreach ( $this->used['php'] as $hook => $params ) {
00126             $this->assertArrayHasKey( $hook, $this->documented['php'], "PHP hook $hook is documented" );
00127         }
00128     }
00129 
00130     public function testHookExistsPHP() {
00131         foreach ( $this->documented['php'] as $hook => $params ) {
00132             $this->assertArrayHasKey( $hook, $this->used['php'], "Documented php hook $hook exists" );
00133         }
00134     }
00135 
00136     public function testHookIsDocumentedJS() {
00137         foreach ( $this->used['js'] as $hook => $params ) {
00138             $this->assertArrayHasKey( $hook, $this->documented['js'], "Js hook $hook is documented" );
00139         }
00140     }
00141 
00142     public function testHookExistsJS() {
00143         foreach ( $this->documented['js'] as $hook => $params ) {
00144             $this->assertArrayHasKey( $hook, $this->used['js'], "Documented js hook $hook exists" );
00145         }
00146     }
00147 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3