| 1 |
<?php |
| 2 |
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class CleanupTitlesTest extends MaintenanceBaseTestCase { |
| 9 |
|
| 10 |
public function addDBDataOnce() { |
| 11 |
|
| 12 |
$this->insertPage( 'User talk:195.175.37.8' ); |
| 13 |
$this->insertPage( 'User talk:195.175.37.10' ); |
| 14 |
$this->insertPage( 'Project talk:Existing' ); |
| 15 |
|
| 16 |
|
| 17 |
$this->getDb()->newInsertQueryBuilder() |
| 18 |
->insertInto( 'interwiki' ) |
| 19 |
->ignore() |
| 20 |
->row( [ |
| 21 |
'iw_prefix' => 'custom', |
| 22 |
'iw_url' => 'https://local.example/w/index.php?title=$1', |
| 23 |
|
| 24 |
'iw_api' => '', |
| 25 |
'iw_wikiid' => '', |
| 26 |
'iw_local' => 0, |
| 27 |
'iw_trans' => 0 |
| 28 |
] ) |
| 29 |
->caller( __METHOD__ ) |
| 30 |
->execute(); |
| 31 |
} |
| 32 |
|
| 33 |
protected function getMaintenanceClass() { |
| 34 |
return TitleCleanup::class; |
| 35 |
} |
| 36 |
|
| 37 |
public static function provideInvalidTitles() { |
| 38 |
|
| 39 |
yield [ 0, 'Foo', 0, 'Foo', null ]; |
| 40 |
|
| 41 |
yield [ 0, 'Project:Foo', 4, 'Foo', null ]; |
| 42 |
|
| 43 |
yield [ 0, 'custom:Foo', 0, 'Broken/custom:Foo', null ]; |
| 44 |
|
| 45 |
yield [ 9999, 'Foo', 0, 'Broken/NS9999:Foo', null ]; |
| 46 |
|
| 47 |
yield [ 0, '<', 0, 'Broken/\x3c', null ]; |
| 48 |
|
| 49 |
yield [ 9999, '<', 0, 'Broken/NS9999:\x3c', null ]; |
| 50 |
|
| 51 |
yield [ 3, '195.175.037.8', 0, 'Broken/User_talk:195.175.37.8', null ]; |
| 52 |
|
| 53 |
yield [ 3, '195.175.037.9', 3, '195.175.37.9', null ]; |
| 54 |
|
| 55 |
|
| 56 |
yield [ 0, 'User talk:195.175.037.10', 0, 'Broken/User_talk:195.175.37.10', null ]; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
yield [ 0, '<Википедия', 0, 'Broken/\x3cВикипедия', null ]; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
yield [ 1, '%25Википедия', 0, 'Broken/Talk:\x2525\xd0\x92\xd0\xb8\xd0\xba\xd0\xb8\xd0\xbf\xd0\xb5\xd0\xb4\xd0\xb8\xd1\x8f', null ]; |
| 65 |
|
| 66 |
yield [ 0, "Special:Foo", 0, "Broken/Special:Foo", null ]; |
| 67 |
|
| 68 |
yield [ 0, "Media:Foo", 0, "Broken/Media:Foo", null ]; |
| 69 |
|
| 70 |
yield [ 0, '<', 0, 'Prefix/\x3c', 'Prefix' ]; |
| 71 |
|
| 72 |
yield [ 1, 'Project:Foo', 5, 'Foo', null ]; |
| 73 |
|
| 74 |
yield [ 1, 'Special:Foo', 0, 'Broken/Talk:Special:Foo', null ]; |
| 75 |
|
| 76 |
yield [ 1, 'custom:Foo', 0, 'Broken/Talk:custom:Foo', null ]; |
| 77 |
|
| 78 |
yield [ 1, 'Project:Existing', 0, 'Broken/Talk:Project:Existing', null ]; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function testCleanup( $namespace, $title, $expectedNamespace, $expectedTitle, $prefix ) { |
| 85 |
$pageId = $this->insertPage( 'Mangleme' )['id']; |
| 86 |
$dbw = $this->getDB(); |
| 87 |
$dbw->newUpdateQueryBuilder() |
| 88 |
->update( 'page' ) |
| 89 |
->set( [ |
| 90 |
'page_namespace' => $namespace, |
| 91 |
'page_title' => $title, |
| 92 |
] ) |
| 93 |
->where( [ |
| 94 |
'page_id' => $pageId |
| 95 |
] ) |
| 96 |
->caller( __METHOD__ ) |
| 97 |
->execute(); |
| 98 |
if ( $prefix ) { |
| 99 |
$this->maintenance->loadWithArgv( [ "--prefix", $prefix ] ); |
| 100 |
} |
| 101 |
$this->maintenance->execute(); |
| 102 |
$newRow = $dbw->newSelectQueryBuilder() |
| 103 |
->select( [ 'page_namespace', 'page_title' ] ) |
| 104 |
->from( 'page' ) |
| 105 |
->where( [ |
| 106 |
'page_id' => $pageId |
| 107 |
] )->fetchRow(); |
| 108 |
$this->assertEquals( $expectedNamespace, $newRow->page_namespace ); |
| 109 |
$this->assertEquals( $expectedTitle, $newRow->page_title ); |
| 110 |
} |
| 111 |
|
| 112 |
public function testBrokenIdFailsafe() { |
| 113 |
$this->insertPage( 'Talk:ABC' ); |
| 114 |
$this->insertPage( 'Broken/Talk:ABC' ); |
| 115 |
$pageId = $this->insertPage( 'Mangleme' )['id']; |
| 116 |
$dbw = $this->getDB(); |
| 117 |
$dbw->newUpdateQueryBuilder() |
| 118 |
->update( "page" ) |
| 119 |
->set( [ |
| 120 |
'page_namespace' => 0, |
| 121 |
'page_title' => 'Talk:ABC', |
| 122 |
] ) |
| 123 |
->where( [ |
| 124 |
'page_id' => $pageId |
| 125 |
] ) |
| 126 |
->caller( __METHOD__ ) |
| 127 |
->execute(); |
| 128 |
$this->maintenance->execute(); |
| 129 |
$newRow = $dbw->newSelectQueryBuilder() |
| 130 |
->select( [ 'page_namespace', 'page_title' ] ) |
| 131 |
->from( 'page' ) |
| 132 |
->where( [ |
| 133 |
'page_id' => $pageId |
| 134 |
] )->fetchRow(); |
| 135 |
$this->assertSame( 0, (int)$newRow->page_namespace ); |
| 136 |
$this->assertEquals( 'Broken/id:_' . $pageId, $newRow->page_title ); |
| 137 |
} |
| 138 |
|
| 139 |
} |