Skip to content

Commit

Permalink
(tests) Update tests to work with assertSame()
Browse files Browse the repository at this point in the history
Some tests inspect results of wfCgiToArray() and Database::selectRow().
Both these methods cast integers to strings, so the tests should
expect a string for $query['modid'], $row->mod_namespace, etc.

Arrays compared in ViewableEntryTest are internal to the test and are
ordered arbitrarily, so their order shouldn't be checked.
  • Loading branch information
edwardspec committed Jan 7, 2025
1 parent 348cc7f commit 49822e4
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 27 deletions.
4 changes: 2 additions & 2 deletions tests/phpunit/consequence/AddLogEntryConsequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Extension:Moderation - MediaWiki extension.
Copyright (C) 2020-2024 Edward Chernenko.
Copyright (C) 2020-2025 Edward Chernenko.
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
Expand Down Expand Up @@ -98,7 +98,7 @@ static function ( $logid, ManualLogEntry $logEntry ) use ( &$checkedLogId, &$che
$this->assertSame( $params, $logEntry->getParameters() );

if ( $runApproveHook ) {
$this->assertSame( $logid, $checkedLogId,
$this->assertSame( $logid, (string)$checkedLogId,
"logid passed to ApproveHook:checkLogEntry() doesn't match expected." );

$this->assertSame( 'moderation', $checkedLogEntry->getType() );
Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/consequence/ApiQueryModerationPreloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Extension:Moderation - MediaWiki extension.
Copyright (C) 2020-2024 Edward Chernenko.
Copyright (C) 2020-2025 Edward Chernenko.
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
Expand Down Expand Up @@ -101,7 +101,6 @@ public function testPreload( $notFound, array $extraParams ) {
if ( $notFound ) {
$expectedResult['missing'] = '';
} else {
$expectedResult['comment'] = $comment;
if ( ( $extraParams['mpmode'] ?? 'wikitext' ) === 'wikitext' ) {
$expectedResult['wikitext'] = $text;
} else {
Expand All @@ -121,6 +120,8 @@ public function testPreload( $notFound, array $extraParams ) {
'displaytitle' => $title->getFullText()
];
}

$expectedResult['comment'] = $comment;
}
$this->assertSame( $expectedResult, $actualResult, "API response doesn't match expected." );
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/consequence/EditFormOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Extension:Moderation - MediaWiki extension.
Copyright (C) 2020-2024 Edward Chernenko.
Copyright (C) 2020-2025 Edward Chernenko.
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
Expand Down Expand Up @@ -141,7 +141,7 @@ public function testEditFilterHook( $isWatch ) {
$editPage->attemptSave( $unusedResult );

$this->assertSame( $text, $opt->getSectionText(), 'getSectionText()' );
$this->assertSame( $section, $opt->getSection(), 'getSection()' );
$this->assertSame( (string)$section, $opt->getSection(), 'getSection()' );

$wrapper = TestingAccessWrapper::newFromObject( $opt );
$this->assertSame( $isWatch, $wrapper->watchthis, 'watchthis' );
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/consequence/EntryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ public function testLoadRowOptions( $testedMethod ) {
$anyWhere = [ 'mod_namespace >= 0' ];
$row = $factory->$testedMethod( $anyWhere, [ 'mod_namespace AS value' ], DB_PRIMARY,
[ 'ORDER BY' => 'mod_namespace' ] );
$this->assertSame( 4, $row->value );
$this->assertSame( '4', $row->value );

$row = $factory->$testedMethod( $anyWhere, [ 'mod_namespace AS value' ], DB_PRIMARY,
[ 'ORDER BY' => 'mod_namespace DESC' ] );
$this->assertSame( 10, $row->value );
$this->assertSame( '10', $row->value );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/consequence/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Extension:Moderation - MediaWiki extension.
Copyright (C) 2020-2022 Edward Chernenko.
Copyright (C) 2020-2025 Edward Chernenko.
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
Expand Down Expand Up @@ -283,7 +283,7 @@ public function testMergeFieldsInEditForm( $mergeID ) {

if ( $mergeID ) {
$this->assertNotNull( $mergeInput, 'wpMergeID field not found.' );
$this->assertSame( $mergeID, $mergeInput->getAttribute( 'value' ), 'wpMergeID.value' );
$this->assertSame( (string)$mergeID, $mergeInput->getAttribute( 'value' ), 'wpMergeID.value' );

$this->assertNotNull( $blankSummaryInput, 'wpIgnoreBlankSummary field not found.' );
$this->assertSame( "1", $blankSummaryInput->getAttribute( 'value' ),
Expand Down
23 changes: 14 additions & 9 deletions tests/phpunit/consequence/ModerationLogFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Extension:Moderation - MediaWiki extension.
Copyright (C) 2018-2024 Edward Chernenko.
Copyright (C) 2018-2025 Edward Chernenko.
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
Expand Down Expand Up @@ -134,7 +134,7 @@ public function dataProvider() {
'expectedParams' => [
3 => [
'text' => '(moderation-log-diff: 12345)',
'query' => [ 'title' => '{{TARGET}}', 'diff' => 12345 ],
'query' => [ 'title' => '{{TARGET}}', 'diff' => '12345' ],
'tooltip' => '(tooltip-moderation-approved-diff)'
]
]
Expand Down Expand Up @@ -176,7 +176,7 @@ public function dataProvider() {
'query' => [
'title' => 'Special:Moderation',
'modaction' => 'show',
'modid' => 678
'modid' => '678'
],
'tooltip' => '(tooltip-moderation-rejected-change)'
],
Expand All @@ -200,7 +200,7 @@ public function dataProvider() {
'query' => [
'title' => 'Special:Moderation',
'modaction' => 'show',
'modid' => 678
'modid' => '678'
],
'tooltip' => '(tooltip-moderation-rejected-change)'
],
Expand All @@ -218,7 +218,7 @@ public function dataProvider() {
'expectTargetUserlink' => true,
'expectedParams' => [
3 => [
'text' => 42
'text' => '42'
]
]

Expand All @@ -240,13 +240,13 @@ public function dataProvider() {
'query' => [
'title' => 'Special:Moderation',
'modaction' => 'show',
'modid' => 200
'modid' => '200'
],
'tooltip' => '(tooltip-moderation-rejected-change)'
],
4 => [
'text' => '(moderation-log-diff: 3000)',
'query' => [ 'title' => '{{TARGET}}', 'diff' => 3000 ],
'query' => [ 'title' => '{{TARGET}}', 'diff' => '3000' ],
'tooltip' => '(tooltip-moderation-approved-diff)'
]
]
Expand All @@ -261,7 +261,7 @@ public function dataProvider() {
'query' => [
'title' => 'Special:Moderation',
'modaction' => 'show',
'modid' => 12345
'modid' => '12345'
],
// This link currently doesn't have a custom tooltip.
'tooltip' => 'Special:Moderation'
Expand Down Expand Up @@ -315,7 +315,7 @@ protected function assertParamHtml( array $expectedParam, $paramHtml, $idx, Titl
// Expect a redlink
$expectedParam['query'] += [
'action' => 'edit',
'redlink' => 1
'redlink' => '1'
];

// Add "page does not exist" if this is NOT a customized tooltip
Expand All @@ -326,6 +326,11 @@ protected function assertParamHtml( array $expectedParam, $paramHtml, $idx, Titl
}

$param = $this->parseParam( $paramHtml );

// Order of keys doesn't matter.
ksort( $param );
ksort( $expectedParam );

$this->assertSame( $expectedParam, $param, "Incorrect HTML of parameter #$idx." );
}

Expand Down
10 changes: 5 additions & 5 deletions tests/phpunit/consequence/ModerationViewableEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Extension:Moderation - MediaWiki extension.
Copyright (C) 2020-2024 Edward Chernenko.
Copyright (C) 2020-2025 Edward Chernenko.
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
Expand Down Expand Up @@ -148,9 +148,9 @@ public function testImageURL( $isThumb ) {
$entry = $this->makeViewableEntry( [ 'id' => $modid ] );

$expectedResult = 'Some link ' . rand( 0, 100000 );
$expectedQuery = [ 'modaction' => 'showimg', 'modid' => $modid ];
$expectedQuery = [ 'modaction' => 'showimg', 'modid' => (string)$modid ];
if ( $isThumb ) {
$expectedQuery['thumb'] = 1;
$expectedQuery['thumb'] = '1';
}

// This hook will verify that Title::getLocalURL() was called with correct parameters,
Expand Down Expand Up @@ -261,8 +261,8 @@ public function testThumbImage() {
'title' =>
SpecialPage::getTitleFor( 'Moderation' )->fixSpecialName()->getPrefixedDBKey(),
'modaction' => 'showimg',
'modid' => $modid,
'thumb' => 1
'modid' => (string)$modid,
'thumb' => '1'
];
$this->assertSame( $expectedQuery, $query, 'Incorrect URL of <img> tag.' );
}
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/consequence/WatchOrUnwatchConsequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
Extension:Moderation - MediaWiki extension.
Copyright (C) 2020-2024 Edward Chernenko.
Copyright (C) 2020-2025 Edward Chernenko.
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
Expand Down Expand Up @@ -62,7 +62,7 @@ public function testWatchUnwatch( $watch, $noop ) {
function ( $userIdentity, $wikiPage ) use ( &$watchHookFired, $title, $user ) {
$watchHookFired = true;

$this->assertSame( $title, $wikiPage->getTitle()->getFullText() );
$this->assertSame( $title->getFullText(), $wikiPage->getTitle()->getFullText() );
$this->assertSame( $user->getName(), $userIdentity->getName() );

return true;
Expand All @@ -74,7 +74,7 @@ function ( $userIdentity, $wikiPage ) use ( &$watchHookFired, $title, $user ) {
function ( $userIdentity, $wikiPage ) use ( &$unwatchHookFired, $title, $user ) {
$unwatchHookFired = true;

$this->assertSame( $title, $wikiPage->getTitle()->getFullText() );
$this->assertSame( $title->getFullText(), $wikiPage->getTitle()->getFullText() );
$this->assertSame( $user->getName(), $userIdentity->getName() );

return true;
Expand Down

0 comments on commit 49822e4

Please sign in to comment.