2 use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase
;
5 * @covers \TitleCleanup
8 class CleanupTitlesTest
extends MaintenanceBaseTestCase
{
10 public function addDBDataOnce() {
11 // Add some existing pages to test normalization clashes
12 $this->insertPage( 'User talk:195.175.37.8' );
13 $this->insertPage( 'User talk:195.175.37.10' );
14 $this->insertPage( 'Project talk:Existing' );
16 // Create an interwiki link to test titles with interwiki prefixes
17 $this->getDb()->newInsertQueryBuilder()
18 ->insertInto( 'interwiki' )
21 'iw_prefix' => 'custom',
22 'iw_url' => 'https://local.example/w/index.php?title=$1',
23 // Specify all fields to avoid errors on Postgres
29 ->caller( __METHOD__
)
33 protected function getMaintenanceClass() {
34 return TitleCleanup
::class;
37 public static function provideInvalidTitles() {
39 yield
[ 0, 'Foo', 0, 'Foo', null ];
40 # Projectspace title encoded as mainspace
41 yield
[ 0, 'Project:Foo', 4, 'Foo', null ];
42 # Interwiki title encoded as mainspace
43 yield
[ 0, 'custom:Foo', 0, 'Broken/custom:Foo', null ];
45 yield
[ 9999, 'Foo', 0, 'Broken/NS9999:Foo', null ];
47 yield
[ 0, '<', 0, 'Broken/\x3c', null ];
48 # Illegal characters and unknown namespace
49 yield
[ 9999, '<', 0, 'Broken/NS9999:\x3c', null ];
50 # IP normalization that clashes (this IP is added above)
51 yield
[ 3, '195.175.037.8', 0, 'Broken/User_talk:195.175.37.8', null ];
52 # IP normalization (valid)
53 yield
[ 3, '195.175.037.9', 3, '195.175.37.9', null ];
54 # Page in main namespace whose title needs additional normalization after resolving
55 # the namespace prefix, and then clashes with another title when fully normalized
56 yield
[ 0, 'User talk:195.175.037.10', 0, 'Broken/User_talk:195.175.37.10', null ];
57 # Non-ascii characters (and otherwise invalid)
58 # The point of this is to make sure it escapes the invalid < character without also
59 # escaping the non-ASCII characters in the other parts of the title
60 yield
[ 0, '<Википедия', 0, 'Broken/\x3cВикипедия', null ];
61 # Non-ascii charaters (and otherwise invalid in a way that removing characters not in Title::legalChars()
63 # This output is unideal, and just a failsafe to avoid "Broken/id:" titles
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 ];
66 yield
[ 0, "Special:Foo", 0, "Broken/Special:Foo", null ];
68 yield
[ 0, "Media:Foo", 0, "Broken/Media:Foo", null ];
70 yield
[ 0, '<', 0, 'Prefix/\x3c', 'Prefix' ];
71 # Incorrectly encoded talk page of namespace
72 yield
[ 1, 'Project:Foo', 5, 'Foo', null ];
74 yield
[ 1, 'Special:Foo', 0, 'Broken/Talk:Special:Foo', null ];
76 yield
[ 1, 'custom:Foo', 0, 'Broken/Talk:custom:Foo', null ];
77 # Of page that already exists
78 yield
[ 1, 'Project:Existing', 0, 'Broken/Talk:Project:Existing', null ];
82 * @dataProvider provideInvalidTitles
84 public function testCleanup( $namespace, $title, $expectedNamespace, $expectedTitle, $prefix ) {
85 $pageId = $this->insertPage( 'Mangleme' )['id'];
86 $dbw = $this->getDB();
87 $dbw->newUpdateQueryBuilder()
90 'page_namespace' => $namespace,
91 'page_title' => $title,
96 ->caller( __METHOD__
)
99 $this->maintenance
->loadWithArgv( [ "--prefix", $prefix ] );
101 $this->maintenance
->execute();
102 $newRow = $dbw->newSelectQueryBuilder()
103 ->select( [ 'page_namespace', 'page_title' ] )
108 $this->assertEquals( $expectedNamespace, $newRow->page_namespace
);
109 $this->assertEquals( $expectedTitle, $newRow->page_title
);
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()
120 'page_namespace' => 0,
121 'page_title' => 'Talk:ABC',
126 ->caller( __METHOD__
)
128 $this->maintenance
->execute();
129 $newRow = $dbw->newSelectQueryBuilder()
130 ->select( [ 'page_namespace', 'page_title' ] )
135 $this->assertSame( 0, (int)$newRow->page_namespace
);
136 $this->assertEquals( 'Broken/id:_' . $pageId, $newRow->page_title
);