wiki.techinc.nl/tests/phpunit/integration/includes/block/UnblockUserTest.php
Thiemo Kreuz 68ec2661d2 Use native array_column() in tests instead of loops
It's just a nice, lesser known convenience function. Exists since
PHP 5.5.

The changes to SerializationTestTrait ended being a little different.
Original I tried to use array_column() as well. But it drops the
array keys, which is relevant here.

We found that calling the two methods getTestInstances(AndAssertions)
multiple times is quite expensive and should be avoided. I changed
the code slightly so it's much less likely this is done unintentionally.

Change-Id: Ifaba3c370871a7c97b4d81ec21ff6ec134433fc0
2022-08-16 15:41:14 +02:00

81 lines
1.8 KiB
PHP

<?php
namespace MediaWiki\Tests\Block;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Block\UnblockUserFactory;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWikiIntegrationTestCase;
use User;
/**
* @group Blocking
* @group Database
*/
class UnblockUserTest extends MediaWikiIntegrationTestCase {
use MockAuthorityTrait;
/**
* @var User
*/
private $user;
/**
* @var UnblockUserFactory
*/
private $unblockUserFactory;
protected function setUp(): void {
parent::setUp();
// Prepare users
$this->user = $this->getTestUser()->getUser();
// Prepare factory
$this->unblockUserFactory = $this->getServiceContainer()->getUnblockUserFactory();
}
/**
* @covers \MediaWiki\Block\UnblockUser::unblock
*/
public function testValidUnblock() {
$performer = $this->mockAnonUltimateAuthority();
$block = new DatabaseBlock( [
'address' => $this->user->getName(),
'by' => $performer->getUser()
] );
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
$this->assertInstanceOf( DatabaseBlock::class, $this->user->getBlock() );
$status = $this->unblockUserFactory->newUnblockUser(
$this->user,
$performer,
'test'
)->unblock();
$this->assertStatusOK( $status );
$this->assertNotInstanceOf(
DatabaseBlock::class,
User::newFromName(
$this->user->getName()
)
->getBlock()
);
}
/**
* @covers \MediaWiki\Block\UnblockUser::unblockUnsafe
*/
public function testNotBlocked() {
$this->user = User::newFromName( $this->user->getName() ); // Reload the user object
$status = $this->unblockUserFactory->newUnblockUser(
$this->user,
$this->mockRegisteredUltimateAuthority(),
'test'
)->unblock();
$this->assertStatusNotOK( $status );
$this->assertContains(
'ipb_cant_unblock',
array_column( $status->getErrorsArray(), 0 )
);
}
}