class MachineNameControllerTest

Same name in other branches
  1. 9 core/modules/system/tests/src/Unit/Transliteration/MachineNameControllerTest.php \Drupal\Tests\system\Unit\Transliteration\MachineNameControllerTest
  2. 8.9.x core/modules/system/tests/src/Unit/Transliteration/MachineNameControllerTest.php \Drupal\Tests\system\Unit\Transliteration\MachineNameControllerTest

Tests that the machine name controller can transliterate strings as expected.

@group legacy

Hierarchy

Expanded class hierarchy of MachineNameControllerTest

File

core/modules/system/tests/src/Unit/Transliteration/MachineNameControllerTest.php, line 22

Namespace

Drupal\Tests\system\Unit\Transliteration
View source
class MachineNameControllerTest extends UnitTestCase {
    
    /**
     * The machine name controller.
     *
     * @var \Drupal\system\MachineNameController
     */
    protected $machineNameController;
    
    /**
     * The CSRF token generator.
     *
     * @var \Drupal\Core\Access\CsrfTokenGenerator
     */
    protected $tokenGenerator;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        // Create the machine name controller.
        $this->tokenGenerator = $this->prophesize(CsrfTokenGenerator::class);
        $this->tokenGenerator
            ->validate(Argument::cetera())
            ->will(function ($args) {
            return $args[0] === 'token-' . $args[1];
        });
        $this->machineNameController = new MachineNameController(new PhpTransliteration(), $this->tokenGenerator
            ->reveal());
    }
    
    /**
     * Data provider for testMachineNameController().
     *
     * @see testMachineNameController()
     *
     * @return array
     *   An array containing:
     *     - An array of request parameters.
     *     - The expected content of the JSONresponse.
     */
    public static function providerTestMachineNameController() {
        // cspell:ignore äwesome
        $valid_data = [
            [
                [
                    'text' => 'Bob',
                    'langcode' => 'en',
                ],
                '"Bob"',
            ],
            [
                [
                    'text' => 'Bob',
                    'langcode' => 'en',
                    'lowercase' => TRUE,
                ],
                '"bob"',
            ],
            [
                [
                    'text' => 'Bob',
                    'langcode' => 'en',
                    'replace' => 'Alice',
                    'replace_pattern' => 'Bob',
                ],
                '"Alice"',
            ],
            [
                [
                    'text' => 'Bob',
                    'langcode' => 'en',
                    'replace' => 'Alice',
                    'replace_pattern' => 'Tom',
                ],
                '"Bob"',
            ],
            [
                [
                    'text' => 'Äwesome',
                    'langcode' => 'en',
                    'lowercase' => TRUE,
                ],
                '"awesome"',
            ],
            [
                [
                    'text' => 'Äwesome',
                    'langcode' => 'de',
                    'lowercase' => TRUE,
                ],
                '"aewesome"',
            ],
            // Tests special characters replacement in the input text.
[
                [
                    'text' => 'B?!"@\\/-ob@e',
                    'langcode' => 'en',
                    'lowercase' => TRUE,
                    'replace' => '_',
                    'replace_pattern' => '[^a-z0-9_.]+',
                ],
                '"b_ob_e"',
            ],
            // Tests @ character in the replace_pattern regex.
[
                [
                    'text' => 'Bob@e\\0',
                    'langcode' => 'en',
                    'lowercase' => TRUE,
                    'replace' => '_',
                    'replace_pattern' => '[^a-z0-9_.@]+',
                ],
                '"bob@e_0"',
            ],
            // Tests null byte in the replace_pattern regex.
[
                [
                    'text' => 'Bob',
                    'langcode' => 'en',
                    'lowercase' => TRUE,
                    'replace' => 'fail()',
                    'replace_pattern' => ".*@e\x00",
                ],
                '"bob"',
            ],
            [
                [
                    'text' => 'Bob@e',
                    'langcode' => 'en',
                    'lowercase' => TRUE,
                    'replace' => 'fail()',
                    'replace_pattern' => ".*@e\x00",
                ],
                '"fail()"',
            ],
        ];
        $valid_data = array_map(function ($data) {
            if (isset($data[0]['replace_pattern'])) {
                $data[0]['replace_token'] = 'token-' . $data[0]['replace_pattern'];
            }
            return $data;
        }, $valid_data);
        return $valid_data;
    }
    
    /**
     * Tests machine name controller's transliteration functionality.
     *
     * @param array $request_params
     *   An array of request parameters.
     * @param $expected_content
     *   The expected content of the JSONresponse.
     *
     * @see \Drupal\system\MachineNameController::transliterate()
     *
     * @dataProvider providerTestMachineNameController
     */
    public function testMachineNameController(array $request_params, $expected_content) : void {
        $request = Request::create('', 'GET', $request_params);
        $json = $this->machineNameController
            ->transliterate($request);
        $this->assertEquals($expected_content, $json->getContent());
    }
    
    /**
     * Tests the pattern validation.
     */
    public function testMachineNameControllerWithInvalidReplacePattern() : void {
        $request = Request::create('', 'GET', [
            'text' => 'Bob',
            'langcode' => 'en',
            'replace' => 'Alice',
            'replace_pattern' => 'Bob',
            'replace_token' => 'invalid',
        ]);
        $this->expectException(AccessDeniedHttpException::class);
        $this->expectExceptionMessage("Invalid 'replace_token' query parameter.");
        $this->machineNameController
            ->transliterate($request);
    }
    
    /**
     * Tests the pattern validation with a missing token.
     */
    public function testMachineNameControllerWithMissingToken() : void {
        $request = Request::create('', 'GET', [
            'text' => 'Bob',
            'langcode' => 'en',
            'replace' => 'Alice',
            'replace_pattern' => 'Bob',
        ]);
        $this->expectException(AccessDeniedHttpException::class);
        $this->expectExceptionMessage("Missing 'replace_token' query parameter.");
        $this->machineNameController
            ->transliterate($request);
    }
    
    /**
     * Tests deprecation of MachineNameController.
     */
    public function testMachineNameControllerDeprecation() : void {
        $request = Request::create('', 'GET', [
            'text' => 'Bob',
            'langcode' => 'en',
        ]);
        $this->expectDeprecation('Drupal\\system\\MachineNameController::transliterate() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3367037');
        $json = $this->machineNameController
            ->transliterate($request);
        $this->assertEquals('"Bob"', $json->getContent());
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
MachineNameControllerTest::$machineNameController protected property The machine name controller.
MachineNameControllerTest::$tokenGenerator protected property The CSRF token generator.
MachineNameControllerTest::providerTestMachineNameController public static function Data provider for testMachineNameController().
MachineNameControllerTest::setUp protected function Overrides UnitTestCase::setUp
MachineNameControllerTest::testMachineNameController public function Tests machine name controller's transliteration functionality.
MachineNameControllerTest::testMachineNameControllerDeprecation public function Tests deprecation of MachineNameController.
MachineNameControllerTest::testMachineNameControllerWithInvalidReplacePattern public function Tests the pattern validation.
MachineNameControllerTest::testMachineNameControllerWithMissingToken public function Tests the pattern validation with a missing token.
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::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.