Merge "Remove hitcounters and associated code"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 1166817..8bcfee0 100644 (file)
@@ -88,6 +88,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->backupStaticAttributes = false;
        }
 
+       public function __destruct() {
+               // Complain if self::setUp() was called, but not self::tearDown()
+               // $this->called['setUp'] will be checked by self::testMediaWikiTestCaseParentSetupCalled()
+               if ( isset( $this->called['setUp'] ) && !isset( $this->called['tearDown'] ) ) {
+                       throw new MWException( get_called_class() . "::tearDown() must call parent::tearDown()" );
+               }
+       }
+
        public function run( PHPUnit_Framework_TestResult $result = null ) {
                /* Some functions require some kind of caching, and will end up using the db,
                 * which we can't allow, as that would open a new connection for mysql.
@@ -192,7 +200,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function setUp() {
                wfProfileIn( __METHOD__ );
                parent::setUp();
-               $this->called['setUp'] = 1;
+               $this->called['setUp'] = true;
 
                $this->phpErrorLevel = intval( ini_get( 'error_reporting' ) );
 
@@ -221,6 +229,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function tearDown() {
                wfProfileIn( __METHOD__ );
 
+               $this->called['tearDown'] = true;
                // Cleaning up temporary files
                foreach ( $this->tmpFiles as $fileName ) {
                        if ( is_file( $fileName ) || ( is_link( $fileName ) ) ) {
@@ -419,6 +428,34 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                return false;
        }
 
+       /**
+        * Insert a new page.
+        *
+        * Should be called from addDBData().
+        *
+        * @since 1.25
+        * @param string $pageName Page name
+        * @param string $text Page's content
+        * @return array Title object and page id
+        */
+       protected function insertPage( $pageName, $text = 'Sample page for unit test.' ) {
+               $title = Title::newFromText( $pageName, 0 );
+
+               $user = User::newFromName( 'WikiSysop' );
+               $comment = __METHOD__ . ': Sample page for unit test.';
+
+               // Avoid memory leak...?
+               LinkCache::singleton()->clear();
+
+               $page = WikiPage::factory( $title );
+               $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
+
+               return array(
+                       'title' => $title,
+                       'id' => $page->getId(),
+               );
+       }
+
        /**
         * Stub. If a test needs to add additional data to the database, it should
         * implement this method and do so
@@ -444,7 +481,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                'page_namespace' => 0,
                                'page_title' => ' ',
                                'page_restrictions' => null,
-                               'page_counter' => 0,
                                'page_is_redirect' => 0,
                                'page_is_new' => 0,
                                'page_random' => 0,
@@ -455,7 +491,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                User::resetIdByNameCache();
 
-               //Make sysop user
+               // Make sysop user
                $user = User::newFromName( 'UTSysop' );
 
                if ( $user->idForName() == 0 ) {
@@ -467,7 +503,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        $user->saveSettings();
                }
 
-               //Make 1 page with 1 revision
+               // Make 1 page with 1 revision
                $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
                if ( $page->getId() == 0 ) {
                        $page->doEditContent(
@@ -475,7 +511,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                'UTPageSummary',
                                EDIT_NEW,
                                false,
-                               User::newFromName( 'UTSysop' ) );
+                               User::newFromName( 'UTSysop' )
+                       );
                }
        }
 
@@ -1110,10 +1147,24 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->assertEmpty( $errors, implode( "\n", $errors ) );
        }
 
+       /**
+        * @param array $matcher
+        * @param string $actual
+        * @param bool $isHtml
+        *
+        * @return bool
+        */
+       private static function tagMatch( $matcher, $actual, $isHtml = true ) {
+               $dom = PHPUnit_Util_XML::load( $actual, $isHtml );
+               $tags = PHPUnit_Util_XML::findNodes( $dom, $matcher, $isHtml );
+               return count( $tags ) > 0 && $tags[0] instanceof DOMNode;
+       }
+
        /**
         * Note: we are overriding this method to remove the deprecated error
         * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=69505
         * @see https://github.com/sebastianbergmann/phpunit/issues/1292
+        * @deprecated
         *
         * @param array $matcher
         * @param string $actual
@@ -1123,10 +1174,21 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        public static function assertTag( $matcher, $actual, $message = '', $isHtml = true ) {
                //trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
 
-               $dom = PHPUnit_Util_XML::load( $actual, $isHtml );
-               $tags = PHPUnit_Util_XML::findNodes( $dom, $matcher, $isHtml );
-               $matched = count( $tags ) > 0 && $tags[0] instanceof DOMNode;
+               self::assertTrue( self::tagMatch( $matcher, $actual, $isHtml ), $message );
+       }
+
+       /**
+        * @see MediaWikiTestCase::assertTag
+        * @deprecated
+        *
+        * @param array $matcher
+        * @param string $actual
+        * @param string $message
+        * @param bool $isHtml
+        */
+       public static function assertNotTag( $matcher, $actual, $message = '', $isHtml = true ) {
+               //trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
 
-               self::assertTrue( $matched, $message );
+               self::assertFalse( self::tagMatch( $matcher, $actual, $isHtml ), $message );
        }
 }