2017-07-12 15:12:40 +00:00
|
|
|
<?php
|
2017-07-14 23:56:16 +00:00
|
|
|
|
2022-05-03 18:11:54 +00:00
|
|
|
namespace MediaWiki\Minerva;
|
2017-07-14 23:56:16 +00:00
|
|
|
|
2020-05-01 01:16:53 +00:00
|
|
|
use MediaWiki\Minerva\Skins\SkinUserPageHelper;
|
2023-07-17 17:40:07 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2021-10-11 22:53:10 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2017-07-14 23:56:16 +00:00
|
|
|
use Title;
|
2023-07-17 17:40:07 +00:00
|
|
|
use User;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-14 23:56:16 +00:00
|
|
|
* @group MinervaNeue
|
2020-05-01 01:16:53 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Minerva\Skins\SkinUserPageHelper
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
2021-10-11 22:53:10 +00:00
|
|
|
class SkinUserPageHelperTest extends MediaWikiIntegrationTestCase {
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isUserPage
|
|
|
|
* @covers ::fetchData
|
|
|
|
* @covers ::__construct
|
|
|
|
*/
|
|
|
|
public function testTitleNotInUserNamespace() {
|
2022-03-10 21:39:37 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, 'Test_Page' );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$title
|
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertFalse( $helper->isUserPage() );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 23:42:00 +00:00
|
|
|
/**
|
|
|
|
* @covers ::isUserPage
|
|
|
|
* @covers ::fetchData
|
|
|
|
* @covers ::__construct
|
|
|
|
*/
|
|
|
|
public function testTitleIsNull() {
|
|
|
|
$title = null;
|
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$title
|
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertNull( $helper->getPageUser() );
|
|
|
|
$this->assertFalse( $helper->isUserPage() );
|
2019-05-24 23:42:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* @covers ::isUserPage
|
|
|
|
* @covers ::fetchData
|
|
|
|
*/
|
|
|
|
public function testTitleisASubpage() {
|
2022-03-10 21:39:37 +00:00
|
|
|
$title = Title::makeTitle( NS_USER, 'TestUser/subpage' );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$title
|
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertFalse( $helper->isUserPage() );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:55:21 +00:00
|
|
|
/**
|
|
|
|
* @covers ::isUserPage
|
|
|
|
* @covers ::fetchData
|
|
|
|
* @covers ::buildPageUserObject
|
|
|
|
*/
|
|
|
|
public function testTitleisAnIP() {
|
2022-03-10 21:39:37 +00:00
|
|
|
$title = Title::makeTitle( NS_USER, '127.0.0.1' );
|
2019-05-23 09:55:21 +00:00
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$title
|
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertTrue( $helper->isUserPage() );
|
2019-05-23 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isUserPage
|
|
|
|
* @covers ::fetchData
|
|
|
|
* @covers ::buildPageUserObject
|
|
|
|
*/
|
|
|
|
public function testTitleIsIPRange() {
|
2022-03-10 21:39:37 +00:00
|
|
|
$title = Title::makeTitle( NS_USER, '127.0.0.1/24' );
|
2019-05-23 09:55:21 +00:00
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
|
|
$this->getServiceContainer()->getUserFactory(),
|
|
|
|
$title
|
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertFalse( $helper->isUserPage() );
|
2019-05-23 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isUserPage
|
|
|
|
* @covers ::fetchData
|
|
|
|
* @covers ::buildPageUserObject
|
|
|
|
*/
|
|
|
|
public function testTitleIsFakeUserPage() {
|
2023-08-01 18:04:24 +00:00
|
|
|
$origUserFactory = $this->getServiceContainer()->getUserFactory();
|
|
|
|
$userFactory = $this->createMock( UserFactory::class );
|
|
|
|
$userFactory->method( 'newFromName' )
|
|
|
|
->willReturnCallback( static function () use ( $origUserFactory ) {
|
|
|
|
$user = $origUserFactory->newFromName( ...func_get_args() );
|
|
|
|
$user->setId( 0 );
|
|
|
|
$user->setItemLoaded( 'id' );
|
|
|
|
return $user;
|
|
|
|
} );
|
2022-03-10 21:39:37 +00:00
|
|
|
$title = Title::makeTitle( NS_USER, 'Fake_user' );
|
2019-05-23 09:55:21 +00:00
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
2023-08-01 18:04:24 +00:00
|
|
|
$userFactory,
|
2022-12-21 18:57:45 +00:00
|
|
|
$title
|
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertFalse( $helper->isUserPage() );
|
2019-05-23 09:55:21 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* @covers ::fetchData
|
|
|
|
*/
|
|
|
|
public function testTitleProcessingIsCached() {
|
|
|
|
$titleMock = $this->getMockBuilder( Title::class )
|
2020-03-13 14:00:40 +00:00
|
|
|
->disableOriginalConstructor()
|
2017-07-12 15:12:40 +00:00
|
|
|
->getMock();
|
|
|
|
$titleMock->expects( $this->once() )
|
|
|
|
->method( 'inNamespace' )
|
|
|
|
->with( NS_USER )
|
|
|
|
->willReturn( true );
|
|
|
|
|
|
|
|
$titleMock->expects( $this->once() )
|
|
|
|
->method( 'isSubpage' )
|
|
|
|
->willReturn( false );
|
|
|
|
|
|
|
|
$titleMock->expects( $this->once() )
|
|
|
|
->method( 'getText' )
|
|
|
|
->willReturn( 'Test' );
|
|
|
|
|
2023-08-01 18:04:24 +00:00
|
|
|
$origUserFactory = $this->getServiceContainer()->getUserFactory();
|
|
|
|
$userFactory = $this->createMock( UserFactory::class );
|
|
|
|
$userFactory->method( 'newFromName' )
|
|
|
|
->willReturnCallback( static function () use ( $origUserFactory ) {
|
|
|
|
$user = $origUserFactory->newFromName( ...func_get_args() );
|
|
|
|
$user->setId( 0 );
|
|
|
|
$user->setItemLoaded( 'id' );
|
|
|
|
return $user;
|
|
|
|
} );
|
|
|
|
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
2023-08-01 18:04:24 +00:00
|
|
|
$userFactory,
|
2022-12-21 18:57:45 +00:00
|
|
|
$titleMock
|
|
|
|
);
|
2017-07-12 15:12:40 +00:00
|
|
|
$helper->isUserPage();
|
|
|
|
$helper->isUserPage();
|
|
|
|
$helper->getPageUser();
|
|
|
|
$helper->getPageUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::fetchData
|
|
|
|
* @covers ::getPageUser
|
|
|
|
* @covers ::isUserPage
|
|
|
|
*/
|
|
|
|
public function testGetPageUserWhenOnUserPage() {
|
2023-07-17 17:40:07 +00:00
|
|
|
$user = $this->createMock( User::class );
|
|
|
|
$user->method( 'getId' )->willReturn( 42 );
|
|
|
|
$user->method( 'getName' )->willReturn( __METHOD__ );
|
|
|
|
$user->method( 'isRegistered' )->willReturn( true );
|
|
|
|
$title = Title::makeTitle( NS_USER, $user->getName() );
|
|
|
|
|
|
|
|
$userFactory = $this->createMock( UserFactory::class );
|
|
|
|
$userFactory->method( 'newFromName' )
|
|
|
|
->with( $user->getName() )
|
|
|
|
->willReturn( $user );
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
2023-07-17 17:40:07 +00:00
|
|
|
$userFactory,
|
2022-12-21 18:57:45 +00:00
|
|
|
$title
|
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertTrue( $helper->isUserPage() );
|
2023-07-17 17:40:07 +00:00
|
|
|
$this->assertEquals( $user->getId(), $helper->getPageUser()->getId() );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::fetchData
|
|
|
|
* @covers ::getPageUser
|
|
|
|
* @covers ::isUserPage
|
|
|
|
*/
|
|
|
|
public function testGetPageUserWhenOnUserPageReturnsCorrectUser() {
|
2023-07-17 17:40:07 +00:00
|
|
|
$user = $this->createMock( User::class );
|
|
|
|
$user->method( 'getId' )->willReturn( 42 );
|
|
|
|
$user->method( 'getName' )->willReturn( __METHOD__ );
|
|
|
|
$user->method( 'isRegistered' )->willReturn( true );
|
|
|
|
$userTitle = Title::makeTitle( NS_USER, $user->getName() );
|
|
|
|
|
|
|
|
$secondUser = $this->createMock( User::class );
|
|
|
|
$secondUser->method( 'getId' )->willReturn( $user->getId() + 1 );
|
|
|
|
$secondUser->method( 'getName' )->willReturn( $user->getName() . 'other' );
|
|
|
|
$secondUser->method( 'isRegistered' )->willReturn( true );
|
|
|
|
$secondUserTitle = Title::makeTitle( NS_USER, $secondUser->getName() );
|
|
|
|
|
|
|
|
$userFactory = $this->createMock( UserFactory::class );
|
|
|
|
$userFactory->method( 'newFromName' )
|
|
|
|
->with( $secondUser->getName() )
|
|
|
|
->willReturn( $secondUser );
|
2022-12-21 18:57:45 +00:00
|
|
|
$helper = new SkinUserPageHelper(
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
2023-07-17 17:40:07 +00:00
|
|
|
$userFactory,
|
|
|
|
$secondUserTitle
|
2022-12-21 18:57:45 +00:00
|
|
|
);
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertTrue( $helper->isUserPage() );
|
2023-07-17 17:40:07 +00:00
|
|
|
$this->assertNotEquals( $user->getId(), $helper->getPageUser()->getId() );
|
|
|
|
$this->assertNotEquals( $helper->getPageUser()->getUserPage(), $userTitle );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|