2016-12-02 16:11:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Linter\Test;
|
|
|
|
|
2021-06-11 17:36:24 +00:00
|
|
|
use ContentHandler;
|
2016-12-02 16:11:16 +00:00
|
|
|
use MediaWiki\Linter\Database;
|
|
|
|
use MediaWiki\Linter\LintError;
|
|
|
|
use MediaWiki\Linter\RecordLintJob;
|
2022-08-03 18:48:32 +00:00
|
|
|
use stdClass;
|
2016-12-02 16:11:16 +00:00
|
|
|
use Title;
|
2021-06-11 17:36:24 +00:00
|
|
|
use User;
|
2016-12-02 16:11:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Database
|
|
|
|
* @covers MediaWiki\Linter\RecordLintJob
|
|
|
|
*/
|
2021-10-12 19:52:50 +00:00
|
|
|
class RecordLintJobTest extends \MediaWikiIntegrationTestCase {
|
2016-12-02 16:11:16 +00:00
|
|
|
/**
|
2022-08-03 18:48:32 +00:00
|
|
|
* @param string $titleText
|
2021-06-11 17:36:24 +00:00
|
|
|
* @return array
|
2016-12-02 16:11:16 +00:00
|
|
|
*/
|
2022-08-03 18:48:32 +00:00
|
|
|
private function createTitleAndPage( string $titleText ) {
|
2021-06-11 17:36:24 +00:00
|
|
|
$userName = 'LinterUser';
|
|
|
|
$baseText = 'wikitext test content';
|
|
|
|
|
|
|
|
$ns = $this->getDefaultWikitextNS();
|
|
|
|
$title = Title::newFromText( $titleText, $ns );
|
|
|
|
$user = User::newFromName( $userName );
|
|
|
|
if ( $user->getId() === 0 ) {
|
|
|
|
$user->addToDatabase();
|
|
|
|
}
|
2021-12-16 21:19:15 +00:00
|
|
|
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
|
2016-12-02 16:11:16 +00:00
|
|
|
|
2021-06-11 17:36:24 +00:00
|
|
|
$content = ContentHandler::makeContent( $baseText, $title );
|
|
|
|
$page->doUserEditContent( $content, $user, "base text for test" );
|
|
|
|
|
|
|
|
return [ 'title' => $title,
|
|
|
|
'pageID' => $page->getRevisionRecord()->getPageId(),
|
|
|
|
'revID' => $page->getRevisionRecord()->getID()
|
|
|
|
];
|
2016-12-02 16:11:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 18:48:32 +00:00
|
|
|
/**
|
|
|
|
* Get just the lint error linter_tag field value for a page
|
|
|
|
*
|
|
|
|
* @param int $pageId
|
|
|
|
* @return stdClass|bool
|
|
|
|
*/
|
|
|
|
private static function getTagForPage( int $pageId ) {
|
|
|
|
return Database::getDBConnectionRef( DB_REPLICA )->selectRow(
|
|
|
|
'linter',
|
|
|
|
[
|
|
|
|
'linter_tag'
|
|
|
|
],
|
|
|
|
[ 'linter_page' => $pageId ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get just the lint error linter_template field value for a page
|
|
|
|
*
|
|
|
|
* @param int $pageId
|
|
|
|
* @return stdClass|bool
|
|
|
|
*/
|
|
|
|
private static function getTemplateForPage( int $pageId ) {
|
|
|
|
return Database::getDBConnectionRef( DB_REPLICA )->selectRow(
|
|
|
|
'linter',
|
|
|
|
[
|
|
|
|
'linter_template'
|
|
|
|
],
|
|
|
|
[ 'linter_page' => $pageId ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:11:16 +00:00
|
|
|
public function testRun() {
|
|
|
|
$error = [
|
|
|
|
'type' => 'fostered',
|
|
|
|
'location' => [ 0, 10 ],
|
|
|
|
'params' => [],
|
2017-12-07 22:30:14 +00:00
|
|
|
'dbid' => null,
|
2016-12-02 16:11:16 +00:00
|
|
|
];
|
2022-08-03 18:48:32 +00:00
|
|
|
$titleAndPage = $this->createTitleAndPage( 'TestPage' );
|
|
|
|
$job = new RecordLintJob( $titleAndPage[ 'title' ], [
|
2016-12-02 16:11:16 +00:00
|
|
|
'errors' => [ $error ],
|
2022-08-03 18:48:32 +00:00
|
|
|
'revision' => $titleAndPage[ 'revID' ]
|
2016-12-02 16:11:16 +00:00
|
|
|
] );
|
|
|
|
$this->assertTrue( $job->run() );
|
2022-08-03 18:48:32 +00:00
|
|
|
|
|
|
|
$db = new Database( $titleAndPage[ 'pageID' ] );
|
2016-12-02 16:11:16 +00:00
|
|
|
/** @var LintError[] $errorsFromDb */
|
2022-08-03 18:48:32 +00:00
|
|
|
$errorsFromDb = array_values( $db->getForPage() );
|
|
|
|
$this->assertCount( 1, $errorsFromDb );
|
|
|
|
$this->assertInstanceOf( LintError::class, $errorsFromDb[ 0 ] );
|
|
|
|
$this->assertEquals( $error[ 'type' ], $errorsFromDb[ 0 ]->category );
|
|
|
|
$this->assertEquals( $error[ 'location' ], $errorsFromDb[ 0 ]->location );
|
|
|
|
$this->assertEquals( $error[ 'params' ], $errorsFromDb[ 0 ]->params );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWriteTagAndTemplate() {
|
|
|
|
$this->overrideConfigValue( 'LinterWriteTagAndTemplateColumnsStage', true );
|
|
|
|
|
|
|
|
$error = [
|
|
|
|
'type' => 'obsolete-tag',
|
|
|
|
'location' => [ 0, 10 ],
|
|
|
|
'params' => [ "name" => "center",
|
|
|
|
"templateInfo" => [ "name" => "Template:Echo" ] ],
|
|
|
|
'dbid' => null,
|
|
|
|
];
|
|
|
|
$titleAndPage = $this->createTitleAndPage( 'TestPage2' );
|
|
|
|
$job = new RecordLintJob( $titleAndPage[ 'title' ], [
|
|
|
|
'errors' => [ $error ],
|
|
|
|
'revision' => $titleAndPage[ 'revID' ]
|
|
|
|
] );
|
|
|
|
$this->assertTrue( $job->run() );
|
|
|
|
|
|
|
|
$pageId = $titleAndPage[ 'pageID' ];
|
|
|
|
$db = new Database( $pageId );
|
|
|
|
$errorsFromDb = array_values( $db->getForPage() );
|
2016-12-02 16:11:16 +00:00
|
|
|
$this->assertCount( 1, $errorsFromDb );
|
|
|
|
$this->assertInstanceOf( LintError::class, $errorsFromDb[0] );
|
2022-08-03 18:48:32 +00:00
|
|
|
$this->assertEquals( $error[ 'type' ], $errorsFromDb[0]->category );
|
|
|
|
$this->assertEquals( $error[ 'location' ], $errorsFromDb[0]->location );
|
|
|
|
$this->assertEquals( $error[ 'params' ], $errorsFromDb[0]->params );
|
|
|
|
$tag = self::getTagForPage( $pageId )->linter_tag ?? '';
|
|
|
|
$this->assertEquals( $error[ 'params' ][ 'name' ], $tag );
|
|
|
|
$template = self::getTemplateForPage( $pageId )->linter_template ?? '';
|
|
|
|
$this->assertEquals( $error[ 'params' ][ 'templateInfo' ][ 'name' ], $template );
|
2016-12-02 16:11:16 +00:00
|
|
|
}
|
2022-01-17 18:51:10 +00:00
|
|
|
|
|
|
|
public function testDropInlineMediaCaptionLints() {
|
|
|
|
$error = [
|
|
|
|
'type' => 'inline-media-caption',
|
|
|
|
'location' => [ 0, 10 ],
|
|
|
|
'params' => [],
|
|
|
|
'dbid' => null,
|
|
|
|
];
|
2022-08-03 18:48:32 +00:00
|
|
|
$titleAndPage = $this->createTitleAndPage( 'TestPage3' );
|
2022-01-17 18:51:10 +00:00
|
|
|
$job = new RecordLintJob( $titleAndPage['title'], [
|
|
|
|
'errors' => [ $error ],
|
|
|
|
'revision' => $titleAndPage['revID']
|
|
|
|
] );
|
|
|
|
$this->assertTrue( $job->run() );
|
|
|
|
/** @var LintError[] $errorsFromDb */
|
|
|
|
$errorsFromDb = array_values( ( new Database( $titleAndPage['pageID'] ) )->getForPage() );
|
|
|
|
$this->assertCount( 0, $errorsFromDb );
|
|
|
|
}
|
2016-12-02 16:11:16 +00:00
|
|
|
}
|