Commit feab614d authored by Dries's avatar Dries

Issue #1991078 by alexpott: Convert MachineNameControllerTest to PHPUnit.

parent 4f6113f5
......@@ -8,6 +8,7 @@
namespace Drupal\system;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\ControllerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
......@@ -63,7 +64,7 @@ public function transliterate(Request $request) {
$transliterated = $this->transliteration->transliterate($text, $langcode, '_');
if($lowercase) {
$transliterated = drupal_strtolower($transliterated);
$transliterated = Unicode::strtolower($transliterated);
}
if(isset($replace_pattern) && isset($replace)) {
$transliterated = preg_replace('@' . $replace_pattern . '@', $replace, $transliterated);
......
......@@ -7,16 +7,18 @@
namespace Drupal\system\Tests\Transliteration;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Component\Transliteration\PHPTransliteration;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\system\MachineNameController;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests machine name controller's transliteration functionality.
*
* @group System
*/
class MachineNameControllerTest extends DrupalUnitTestBase {
class MachineNameControllerTest extends UnitTestCase {
/**
* The machine name controller.
......@@ -39,36 +41,43 @@ public function setUp() {
$this->machineNameController = new MachineNameController(new PHPTransliteration());
}
/**
* Data provider for testMachineNameController().
*
* @see testMachineNameController()
*
* @return array
* An array containing:
* - An array of request parameters.
* - The expected content of the JSONresponse.
*/
public function providerTestMachineNameController() {
return array(
array(array('text' => 'Bob', 'langcode' => 'en'), '"Bob"'),
array(array('text' => 'Bob', 'langcode' => 'en', 'lowercase' => TRUE), '"bob"'),
array(array('text' => 'Bob', 'langcode' => 'en', 'replace' => 'Alice', 'replace_pattern' => 'Bob'), '"Alice"'),
array(array('text' => 'Bob', 'langcode' => 'en', 'replace' => 'Alice', 'replace_pattern' => 'Tom'), '"Bob"'),
array(array('text' => 'Äwesome', 'langcode' => 'en', 'lowercase' => TRUE), '"awesome"'),
array(array('text' => 'Äwesome', 'langcode' => 'de', 'lowercase' => TRUE), '"aewesome"'),
);
}
/**
* 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() {
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en'));
public function testMachineNameController(array $request_params, $expected_content) {
$request = Request::create('', 'GET', $request_params);
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"Bob"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en', 'lowercase' => TRUE));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"bob"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en', 'replace' => 'Alice', 'replace_pattern' => 'Bob'));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"Alice"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en', 'replace' => 'Alice', 'replace_pattern' => 'Tom'));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"Bob"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Äwesome', 'langcode' => 'en', 'lowercase' => TRUE));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"awesome"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Äwesome', 'langcode' => 'de', 'lowercase' => TRUE));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"aewesome"', $json->getContent());
$this->assertEquals($expected_content, $json->getContent());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment