Skip to content

Commit dde9ae2

Browse files
committed
add more tests:
- add special check for OnBeforePageDisplay() method
1 parent 074cca4 commit dde9ae2

File tree

2 files changed

+45
-53
lines changed

2 files changed

+45
-53
lines changed

src/HidePrefix.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,18 @@ public static function onBeforePageDisplay( &$out, &$sk ) {
7474

7575
// result example 'prefix:title', split it to use title
7676
$titleWithPrefix = $title->getPrefixedText();
77-
$titleWithoutPrefix = explode( ':', $titleWithPrefix );
77+
$titleParts = explode( ':', $titleWithPrefix );
7878

79-
// double check $pageTitle from $out - should contains title of given page
79+
// double check $pageTitle from $out - should contain title of given page
8080
$pageTitle = trim( $out->getPageTitle() );
81-
if ( ( $pageTitle === trim( $titleWithoutPrefix[1] ) ) ||
82-
( strpos( $pageTitle, trim( $titleWithoutPrefix[1] ) ) ) ) {
81+
if ( count( $titleParts ) > 1 ) {
82+
$titleWithoutPrefix = trim( $titleParts[1] );
83+
} else {
84+
$titleWithoutPrefix = trim( $titleParts[0] );
85+
}
86+
87+
if ( ( $pageTitle === $titleWithoutPrefix ) ||
88+
( strpos( $pageTitle, $titleWithoutPrefix ) ) ) {
8389
$out->setPageTitle( $title->getText() );
8490
}
8591
}

tests/phpunit/Unit/HidePrefixTest.php

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -102,55 +102,6 @@ public function testOnHtmlPageLinkRendererBeginTitlesMatch() {
102102
$this->assertSame( $text, $mockTitle->getPrefixedText() );
103103
}
104104

105-
/**
106-
* @covers HidePrefix::onBeforePageDisplay
107-
*/
108-
public function testOnBeforePageDisplay() {
109-
// Create mock OutputPage and Skin objects
110-
$outMock = $this->getMockBuilder( 'OutputPage' )
111-
->disableOriginalConstructor()
112-
->getMock();
113-
114-
// Create mock Skin object
115-
$skMock = $this->getMockBuilder( 'Skin' )
116-
->disableOriginalConstructor()
117-
->getMock();
118-
119-
// Example page title with prefix
120-
$pageTitleWithPrefix = 'Prefix:Example_Title';
121-
$pageTitleWithoutPrefix = 'Example_Title';
122-
123-
// Create a mock Title object
124-
$titleMock = $this->createMock( Title::class );
125-
$titleMock->method( 'getPrefixedText' )->willReturn( $pageTitleWithPrefix );
126-
$titleMock->method( 'getText' )->willReturn( $pageTitleWithoutPrefix );
127-
128-
$outMock->expects( $this->any() )
129-
->method( 'getTitle' )
130-
->willReturn( $titleMock );
131-
132-
$title = $outMock->getTitle();
133-
134-
$outMock->expects( $this->any() )
135-
->method( 'getPageTitle' )
136-
->willReturn( $pageTitleWithPrefix );
137-
138-
// Assert that pageTitle is with prefix
139-
$this->assertSame( $title->getPrefixedText(), $outMock->getPageTitle() );
140-
141-
$outMock->expects( $this->any() )
142-
->method( 'setPageTitle' )
143-
->willReturn( $pageTitleWithoutPrefix );
144-
$pageTitle = $outMock->setPageTitle( $pageTitleWithoutPrefix );
145-
146-
// Get the full title with prefix and split it
147-
$titleWithPrefix = $title->getPrefixedText();
148-
$titleWithoutPrefix = explode( ':', $titleWithPrefix, 2 );
149-
150-
// Assert that pageTitle is without prefix
151-
$this->assertSame( $titleWithoutPrefix[1], $pageTitle );
152-
}
153-
154105
/**
155106
* @covers HidePrefix::onBeforePageDisplay
156107
*/
@@ -179,4 +130,39 @@ public function testOnBeforePageDisplayWhenTitleIsMissing() {
179130

180131
$this->assertSame( $title->getPrefixedText(), $outMock->getPageTitle() );
181132
}
133+
134+
public function newHooksInstance() {
135+
return new Hooks(
136+
$this->getServiceContainer()->getMainConfig(),
137+
$this->getServiceContainer()->getSpecialPageFactory(),
138+
$this->getServiceContainer()->getUserOptionsLookup(),
139+
null
140+
);
141+
}
142+
143+
public static function provideOnBeforePageDisplay() {
144+
return [
145+
'no prefix' => [ 'Main Page', 'Main Page', 'Main Page' ],
146+
'with prefix' => [ 'Category:Main Page', 'Main Page', 'Main Page' ],
147+
'special with prefix' => [ 'Special:ListFiles', 'ListFiles', 'ListFiles' ],
148+
'special no prefix' => [ 'Special:Watchlist', 'Watchlist', 'Watchlist' ],
149+
];
150+
}
151+
152+
/**
153+
* @dataProvider provideOnBeforePageDisplay
154+
* @covers HidePrefix::onBeforePageDisplay
155+
*/
156+
public function testOnBeforePageDisplay( $pagename, $expectedTitle, $pageTitle ) {
157+
$t = Title::newFromText( $pagename );
158+
$t->setContentModel( CONTENT_MODEL_WIKITEXT );
159+
$skin = new SkinTemplate();
160+
$output = $this->createMock( OutputPage::class );
161+
$output->method( 'getTitle' )->willReturn( $t );
162+
$output->method( 'isArticle' )->willReturn( true );
163+
$output->method( 'getPageTitle' )->willReturn( $pageTitle );
164+
$output->expects( $this->once() )->method( 'setPageTitle' )->with( $expectedTitle );
165+
166+
HidePrefix::onBeforePageDisplay( $output, $skin );
167+
}
182168
}

0 commit comments

Comments
 (0)