class BanAdminTest

Same name in other branches
  1. 11.x core/modules/ban/tests/src/Unit/BanAdminTest.php \Drupal\Tests\ban\Unit\BanAdminTest

Tests the BanAdmin form.

@coversDefaultClass \Drupal\ban\Form\BanAdmin @group ban

Hierarchy

Expanded class hierarchy of BanAdminTest

File

core/modules/ban/tests/src/Unit/BanAdminTest.php, line 21

Namespace

Drupal\Tests\ban\Unit
View source
class BanAdminTest extends UnitTestCase {
    
    /**
     * Tests various user input to confirm correct validation.
     *
     * @covers ::validateForm
     * @dataProvider providerIpValidation
     */
    public function testIpValidation(string $ip, bool $isBanned, ?string $error) : void {
        $manager = $this->getIpManagerMock();
        $manager->expects($this->once())
            ->method('isBanned')
            ->with($ip)
            ->willReturn($isBanned);
        $formObject = new BanAdmin($manager);
        $formObject->setStringTranslation($this->getStringTranslationStub());
        $formObject->setRequestStack($this->getRequestStackMock());
        $formState = $this->createMock(FormStateInterface::class);
        $formState->expects($this->any())
            ->method('getValue')
            ->with('ip')
            ->willReturn($ip);
        if ($error === NULL) {
            $formState->expects($this->never())
                ->method('setErrorByName');
        }
        else {
            $formState->expects($this->once())
                ->method('setErrorByName')
                ->with('ip', $error);
        }
        $form = [];
        $formObject->validateForm($form, $formState);
    }
    
    /**
     * Test form submission.
     */
    public function testSubmit() : void {
        $ip = '1.2.3.4';
        $manager = $this->getIpManagerMock();
        $manager->expects($this->once())
            ->method('banIp')
            ->with($ip);
        $messenger = $this->createMock(MessengerInterface::class);
        $messenger->expects($this->once())
            ->method('addStatus');
        $formObject = new BanAdmin($manager);
        $formObject->setStringTranslation($this->getStringTranslationStub());
        $formObject->setMessenger($messenger);
        $formState = $this->createMock(FormStateInterface::class);
        $formState->expects($this->any())
            ->method('getValue')
            ->with('ip')
            ->willReturn($ip);
        $form = [];
        $formObject->submitForm($form, $formState);
    }
    
    /**
     * Test passing an IP address as a route parameter.
     *
     * @covers ::buildForm
     */
    public function testRouteParameter() : void {
        $ip = '1.2.3.4';
        $formObject = new BanAdmin($this->getIpManagerMock());
        $formObject->setStringTranslation($this->getStringTranslationStub());
        $formState = $this->createMock(FormStateInterface::class);
        $form = $formObject->buildForm([], $formState, $ip);
        $this->assertSame($ip, $form['ip']['#default_value']);
    }
    
    /**
     * Data provider for testIpValidation().
     */
    public static function providerIpValidation() : array {
        return [
            'valid ip' => [
                '1.2.3.3',
                FALSE,
                NULL,
            ],
            'already blocked' => [
                '1.2.3.3',
                TRUE,
                'This IP address is already banned.',
            ],
            'reserved ip' => [
                '255.255.255.255',
                FALSE,
                'Enter a valid IP address.',
            ],
            'fqdn' => [
                'test.example.com',
                FALSE,
                'Enter a valid IP address.',
            ],
            'empty' => [
                '',
                FALSE,
                'Enter a valid IP address.',
            ],
            'client ip' => [
                '127.0.0.1',
                FALSE,
                'You may not ban your own IP address.',
            ],
        ];
    }
    
    /**
     * Get a request stack with a dummy IP.
     */
    protected function getRequestStackMock() : RequestStack {
        $request = $this->createMock(Request::class);
        $request->expects($this->any())
            ->method('getClientIp')
            ->willReturn('127.0.0.1');
        $requestStack = $this->createMock(RequestStack::class);
        $requestStack->expects($this->any())
            ->method('getCurrentRequest')
            ->willReturn($request);
        return $requestStack;
    }
    
    /**
     * Get the mocked IP manager service.
     */
    protected function getIpManagerMock() : BanIpManagerInterface {
        $manager = $this->createMock(BanIpManagerInterface::class);
        $manager->expects($this->any())
            ->method('findAll')
            ->willReturn([]);
        return $manager;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
BanAdminTest::getIpManagerMock protected function Get the mocked IP manager service.
BanAdminTest::getRequestStackMock protected function Get a request stack with a dummy IP.
BanAdminTest::providerIpValidation public static function Data provider for testIpValidation().
BanAdminTest::testIpValidation public function Tests various user input to confirm correct validation.
BanAdminTest::testRouteParameter public function Test passing an IP address as a route parameter.
BanAdminTest::testSubmit public function Test form submission.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.