mirror of
				https://github.com/diocloid/LinkTitles.git
				synced 2025-10-22 05:42:32 +02:00 
			
		
		
		
	First working test.
This commit is contained in:
		@@ -34,7 +34,8 @@
 | 
			
		||||
        },
 | 
			
		||||
        "AutoloadClasses": {
 | 
			
		||||
                "LinkTitles\\Extension": "includes/LinkTitles_Extension.php",
 | 
			
		||||
                "LinkTitles\\Special": "includes/LinkTitles_Special.php"
 | 
			
		||||
                "LinkTitles\\Special": "includes/LinkTitles_Special.php",
 | 
			
		||||
                "LinkTitles\\TestCase": "tests/phpunit/TestCase.php"
 | 
			
		||||
        },
 | 
			
		||||
        "SpecialPages": {
 | 
			
		||||
                "LinkTitles": "LinkTitles\\Special"
 | 
			
		||||
 
 | 
			
		||||
@@ -73,6 +73,12 @@ class Extension {
 | 
			
		||||
		self::BuildDelimiters();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Helper method to be used in unit testing, were everything takes place
 | 
			
		||||
	/// in one request.
 | 
			
		||||
	public static function invalidateCache() {
 | 
			
		||||
		self::fetchPageTitles();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// Event handler that is hooked to the PageContentSave event.
 | 
			
		||||
	public static function onPageContentSave( &$wikiPage, &$user, &$content, &$summary,
 | 
			
		||||
			$isMinor, $isWatch, $section, &$flags, &$status ) {
 | 
			
		||||
@@ -135,7 +141,7 @@ class Extension {
 | 
			
		||||
 | 
			
		||||
		if ( !isset( self::$pageTitles ) || ( $currentNamespace != self::$currentNamespace ) ) {
 | 
			
		||||
			self::$currentNamespace = $currentNamespace;
 | 
			
		||||
			self::$pageTitles = self::fetchPageTitles( $currentNamespace );
 | 
			
		||||
			self::fetchPageTitles();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Iterate through the page titles
 | 
			
		||||
@@ -269,8 +275,7 @@ class Extension {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Fetches the page titles from the database.
 | 
			
		||||
	// @param $currentNamespace String holding the namespace of the page currently being processed.
 | 
			
		||||
	private static function fetchPageTitles( $currentNamespace ) {
 | 
			
		||||
	private static function fetchPageTitles() {
 | 
			
		||||
		global $wgLinkTitlesPreferShortTitles;
 | 
			
		||||
		global $wgLinkTitlesMinimumTitleLength;
 | 
			
		||||
		global $wgLinkTitlesBlackList;
 | 
			
		||||
@@ -282,8 +287,8 @@ class Extension {
 | 
			
		||||
		$blackList = str_replace( ' ', '_', '("' . implode( '","',$wgLinkTitlesBlackList ) . '")' );
 | 
			
		||||
 | 
			
		||||
		// Build our weight list. Make sure current namespace is first element
 | 
			
		||||
		$namespaces = array_diff( $wgLinkTitlesNamespaces, [ $currentNamespace ] );
 | 
			
		||||
		array_unshift( $namespaces,  $currentNamespace );
 | 
			
		||||
		$namespaces = array_diff( $wgLinkTitlesNamespaces, [ self::$currentNamespace ] );
 | 
			
		||||
		array_unshift( $namespaces,  self::$currentNamespace );
 | 
			
		||||
 | 
			
		||||
		// No need for sanitiy check. we are sure that we have at least one element in the array
 | 
			
		||||
		$weightSelect = "CASE page_namespace ";
 | 
			
		||||
@@ -326,7 +331,8 @@ class Extension {
 | 
			
		||||
			);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return $res;
 | 
			
		||||
		self::$pageTitles = $res;
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Build an anonymous callback function to be used in simple mode.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										17
									
								
								tests/phpunit/ParseOnEditTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								tests/phpunit/ParseOnEditTest.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * @group bovender
 | 
			
		||||
 * @group Database
 | 
			
		||||
 */
 | 
			
		||||
class ParseOnEditTest extends LinkTitles\TestCase {
 | 
			
		||||
 | 
			
		||||
  public function testParseOnEdit() {
 | 
			
		||||
    $this->setMwGlobals( [
 | 
			
		||||
      'wgLinkTitlesParseOnEdit' => true,
 | 
			
		||||
      'wgLinkTitlesParseOnRender' => true
 | 
			
		||||
    ] );
 | 
			
		||||
    $pageId = $this->insertPage( 'test page', 'This page should link to the link target' )['id'];
 | 
			
		||||
    $page = WikiPage::newFromId( $pageId );
 | 
			
		||||
    $this->assertSame( 'This page should link to the [[link target]]', self::getPageText( $page ) );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								tests/phpunit/TestCase.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								tests/phpunit/TestCase.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace LinkTitles;
 | 
			
		||||
 | 
			
		||||
abstract class TestCase extends \MediaWikiTestCase {
 | 
			
		||||
  protected function setUp() {
 | 
			
		||||
    parent::setUp();
 | 
			
		||||
    $this->insertPage( 'link target', 'This page serves as a link target' );
 | 
			
		||||
    Extension::invalidateCache();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  protected function tearDown() {
 | 
			
		||||
    parent::tearDown();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  protected function getPageText( \WikiPage $page ) {
 | 
			
		||||
    $content = $page->getContent();
 | 
			
		||||
    return $page->getContentHandler()->serializeContent( $content );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user