Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / CleanupTitlesTest.php
blobacd7d8277cc0bf2a0a55cc39569621c04f612228
1 <?php
2 use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
4 /**
5 * @covers \TitleCleanup
6 * @group Database
7 */
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' )
19 ->ignore()
20 ->row( [
21 'iw_prefix' => 'custom',
22 'iw_url' => 'https://local.example/w/index.php?title=$1',
23 // Specify all fields to avoid errors on Postgres
24 'iw_api' => '',
25 'iw_wikiid' => '',
26 'iw_local' => 0,
27 'iw_trans' => 0
28 ] )
29 ->caller( __METHOD__ )
30 ->execute();
33 protected function getMaintenanceClass() {
34 return TitleCleanup::class;
37 public static function provideInvalidTitles() {
38 # Valid title
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 ];
44 # Unknown namespace
45 yield [ 9999, 'Foo', 0, 'Broken/NS9999:Foo', null ];
46 # Illegal characters
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()
62 # doesn't cure)
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 ];
65 # Special namespace
66 yield [ 0, "Special:Foo", 0, "Broken/Special:Foo", null ];
67 # Media namespace
68 yield [ 0, "Media:Foo", 0, "Broken/Media:Foo", null ];
69 # With prefix
70 yield [ 0, '<', 0, 'Prefix/\x3c', 'Prefix' ];
71 # Incorrectly encoded talk page of namespace
72 yield [ 1, 'Project:Foo', 5, 'Foo', null ];
73 # Of special page
74 yield [ 1, 'Special:Foo', 0, 'Broken/Talk:Special:Foo', null ];
75 # Of interwiki
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 ];
81 /**
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()
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 ] );
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 );
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',
123 ->where( [
124 'page_id' => $pageId
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 );