You are here

class MachineNameControllerTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 system

Hierarchy

Expanded class hierarchy of MachineNameControllerTest

File

core/modules/system/tests/src/Unit/Transliteration/MachineNameControllerTest.php, line 20
Contains \Drupal\Tests\system\Unit\Transliteration\MachineNameControllerTest.

Namespace

Drupal\Tests\system\Unit\Transliteration
View source
class MachineNameControllerTest extends UnitTestCase {

  /**
   * The machine name controller.
   *
   * @var \Drupal\system\MachineNameController
   */
  protected $machineNameController;
  protected function setUp() {
    parent::setUp();

    // Create the machine name controller.
    $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 special characters replacement in the input text.
      array(
        array(
          'text' => 'B?!"@\\/-ob@e',
          'langcode' => 'en',
          'lowercase' => TRUE,
          'replace' => '_',
          'replace_pattern' => '[^a-z0-9_.]+',
        ),
        '"b_ob_e"',
      ),
      // Tests @ character in the replace_pattern regex.
      array(
        array(
          '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.
      array(
        array(
          'text' => 'Bob',
          'langcode' => 'en',
          'lowercase' => TRUE,
          'replace' => 'fail()',
          'replace_pattern' => ".*@e\0",
        ),
        '"bob"',
      ),
      array(
        array(
          'text' => 'Bob@e',
          'langcode' => 'en',
          'lowercase' => TRUE,
          'replace' => 'fail()',
          'replace_pattern' => ".*@e\0",
        ),
        '"fail()"',
      ),
    );
  }

  /**
   * 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) {
    $request = Request::create('', 'GET', $request_params);
    $json = $this->machineNameController
      ->transliterate($request);
    $this
      ->assertEquals($expected_content, $json
      ->getContent());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MachineNameControllerTest::$machineNameController protected property The machine name controller.
MachineNameControllerTest::providerTestMachineNameController public function Data provider for testMachineNameController().
MachineNameControllerTest::setUp protected function Overrides UnitTestCase::setUp
MachineNameControllerTest::testMachineNameController public function Tests machine name controller's transliteration functionality.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.