mediawiki-extensions-Echo/tests/phpunit/maintenance/SupressionMaintenanceTest.php
Roan Kattouw 99377e5525 Clean up and fix updateEchoSchemaForSuppression.php
This script was supposed to be run in production in 2013, but that
never happened. It was also never added to update.php.

* Use makeTitleSafe instead of newFromText, for correctness
* Fetch the columns that the update generator needs
* Replace wrapper for private method with closure
* Make the maintenance script logged

Bug: T136427
Bug: T50059
Change-Id: I6c2972120189f035483b5ca49610c008c4ba2c88
2016-06-06 23:56:28 +02:00

133 lines
3.7 KiB
PHP

<?php
/**
* @group Echo
*/
class SuppressionMaintenanceTest extends MediaWikiTestCase {
public static function provider_updateRow() {
$input = array(
'event_id' => 2,
'event_type' => 'mention',
'event_variant' => null,
'event_agent_id' => 3,
'event_agent_ip' => null,
'event_page_title' => '',
'event_page_namespace' => 0,
'event_page_extra' => null,
'event_extra' => null,
'event_page_id' => null,
);
return array(
array( 'Unrelated row must result in no update', array(), $input ),
array(
'Page title and namespace for non-existant page must move into event_extra',
array( // expected update
'event_extra' => serialize( array(
'page_title' => 'Yabba Dabba Do',
'page_namespace' => NS_MAIN
) ),
),
array( // input row
'event_page_title' => 'Yabba Dabba Do',
'event_page_namespace' => NS_MAIN,
) + $input,
),
array(
'Page title and namespace for existing page must be result in update to event_page_id',
array( // expected update
'event_page_id' => 42,
),
array( // input row
'event_page_title' => 'Mount Rushmore',
'event_page_namespace' => NS_MAIN,
) + $input,
self::attachTitleFor( 42, 'Mount Rushmore', NS_MAIN )
),
array(
'When updating non-existant page must keep old extra data',
array( // expected update
'event_extra' => serialize( array(
'foo' => 'bar',
'page_title' => 'Yabba Dabba Do',
'page_namespace' => NS_MAIN
) ),
),
array( // input row
'event_page_title' => 'Yabba Dabba Do',
'event_page_namespace' => NS_MAIN,
'event_extra' => serialize( array( 'foo' => 'bar' ) ),
) + $input,
),
array(
'Must update link-from-title/namespace to link-from-page-id for page-linked events',
array( // expected update
'event_extra' => serialize( array( 'link-from-page-id' => 99 ) ),
),
array( // input row
'event_type' => 'page-linked',
'event_extra' => serialize( array(
'link-from-title' => 'Horse',
'link-from-namespace' => NS_USER_TALK
) ),
) + $input,
self::attachTitleFor( 99, 'Horse', NS_USER_TALK )
),
array(
'Must perform both generic update and page-linked update at same time',
array( // expected update
'event_extra' => serialize( array( 'link-from-page-id' => 8675309 ) ),
'event_page_id' => 8675309,
),
array( // input row
'event_type' => 'page-linked',
'event_extra' => serialize( array(
'link-from-title' => 'Jenny',
'link-from-namespace' => NS_MAIN,
) ),
'event_page_title' => 'Jenny',
'event_page_namespace' => NS_MAIN,
) + $input,
self::attachTitleFor( 8675309, 'Jenny', NS_MAIN ),
),
);
}
protected static function attachTitleFor( $id, $providedText, $providedNamespace ) {
return function ( $test, $gen ) use ( $id, $providedText, $providedNamespace ) {
$title = $test->getMock( 'Title' );
$title->expects( $test->any() )
->method( 'getArticleId' )
->will( $test->returnValue( $id ) );
$titles = array( $providedNamespace => array( $providedText => $title ) );
$gen->setNewTitleFromNsAndText( function ( $namespace, $text ) use ( $titles ) {
if ( isset( $titles[$namespace][$text] ) ) {
return $titles[$namespace][$text];
}
return Title::makeTitleSafe( $namespace, $text );
} );
};
}
/**
* @dataProvider provider_updateRow
*/
public function testUpdateRow( $message, $expected, $input, $callable = null ) {
$gen = new EchoSuppressionRowUpdateGenerator;
if ( $callable ) {
call_user_func( $callable, $this, $gen );
}
$update = $gen->update( (object)$input );
$this->assertEquals( $expected, $update, $message );
}
}