View source
<?php
namespace Drupal\Tests\system\Unit\Transliteration;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\Transliteration\PhpTransliteration;
use Drupal\system\MachineNameController;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class MachineNameControllerTest extends UnitTestCase {
protected $machineNameController;
protected $tokenGenerator;
protected function setUp() {
parent::setUp();
$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());
}
public function providerTestMachineNameController() {
$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"',
],
[
[
'text' => 'B?!"@\\/-ob@e',
'langcode' => 'en',
'lowercase' => TRUE,
'replace' => '_',
'replace_pattern' => '[^a-z0-9_.]+',
],
'"b_ob_e"',
],
[
[
'text' => 'Bob@e\\0',
'langcode' => 'en',
'lowercase' => TRUE,
'replace' => '_',
'replace_pattern' => '[^a-z0-9_.@]+',
],
'"bob@e_0"',
],
[
[
'text' => 'Bob',
'langcode' => 'en',
'lowercase' => TRUE,
'replace' => 'fail()',
'replace_pattern' => ".*@e\0",
],
'"bob"',
],
[
[
'text' => 'Bob@e',
'langcode' => 'en',
'lowercase' => TRUE,
'replace' => 'fail()',
'replace_pattern' => ".*@e\0",
],
'"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;
}
public function testMachineNameController(array $request_params, $expected_content) {
$request = Request::create('', 'GET', $request_params);
$json = $this->machineNameController
->transliterate($request);
$this
->assertEquals($expected_content, $json
->getContent());
}
public function testMachineNameControllerWithInvalidReplacePattern() {
$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);
}
public function testMachineNameControllerWithMissingToken() {
$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);
}
}