You are here

class RenameAdminPathsSettingsFormTest in Rename Admin Paths 8.2

@group tests

Hierarchy

Expanded class hierarchy of RenameAdminPathsSettingsFormTest

File

tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php, line 16

Namespace

Drupal\Tests\rename_admin_paths\Unit\Form
View source
class RenameAdminPathsSettingsFormTest extends UnitTestCase {
  public function testValidatePathWithoutValue() {
    $element = [];
    $this
      ->getForm()
      ->validate($element, $this
      ->getInvalidFormState());
  }

  /**
   * @dataProvider validValues
   *
   * @param string $value
   */
  public function testWithValidValue(string $value) {
    $element = [
      '#value' => $value,
    ];
    $this
      ->getForm()
      ->validate($element, $this
      ->getValidFormState());
  }

  /**
   * @dataProvider invalidValues
   *
   * @param string $value
   */
  public function testWithInvalidValue(string $value) {
    $element = [
      '#value' => $value,
    ];
    $this
      ->getForm()
      ->validate($element, $this
      ->getInvalidFormState());
  }

  /**
   * @return \Generator
   */
  public function validValues() {
    (yield [
      'backend',
    ]);
    (yield [
      'back-end',
    ]);
    (yield [
      'Backend',
    ]);
    (yield [
      'Back-End',
    ]);
    (yield [
      'Back_End',
    ]);
    (yield [
      'Back-End_123',
    ]);
    (yield [
      'admin2',
    ]);
    (yield [
      'user2',
    ]);
  }

  /**
   * @return \Generator
   */
  public function invalidValues() {
    (yield [
      'backend!',
    ]);
    (yield [
      'back@end',
    ]);
    (yield [
      '(Backend)',
    ]);
    (yield [
      'Back~End',
    ]);
    (yield [
      'Back=End',
    ]);
    (yield [
      'Back-End+123',
    ]);
    (yield [
      'admin',
    ]);
    (yield [
      'user',
    ]);
    (yield [
      'Admin',
    ]);
  }

  /**
   * @return RenameAdminPathsSettingsForm
   */
  private function getForm() {
    $config = $this
      ->createMock(Config::class);
    $routeBuilder = $this
      ->createMock(RouteBuilderInterface::class);
    $translator = $this
      ->createMock(TranslationInterface::class);
    $translator
      ->method('translateString')
      ->willReturn('Error');
    return new RenameAdminPathsSettingsForm($config, $routeBuilder, $translator);
  }

  /**
   * @return FormStateInterface
   */
  private function getValidFormState() {
    $formState = $this
      ->prophesize(FormStateInterface::class);
    $formState
      ->setError()
      ->shouldNotBeCalled();
    return $formState
      ->reveal();
  }

  /**
   * @return FormStateInterface
   */
  private function getInvalidFormState() {
    $formState = $this
      ->prophesize(FormStateInterface::class);
    $formState
      ->setError(Argument::any(), Argument::any())
      ->shouldBeCalled();
    return $formState
      ->reveal();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RenameAdminPathsSettingsFormTest::getForm private function
RenameAdminPathsSettingsFormTest::getInvalidFormState private function
RenameAdminPathsSettingsFormTest::getValidFormState private function
RenameAdminPathsSettingsFormTest::invalidValues public function
RenameAdminPathsSettingsFormTest::testValidatePathWithoutValue public function
RenameAdminPathsSettingsFormTest::testWithInvalidValue public function @dataProvider invalidValues
RenameAdminPathsSettingsFormTest::testWithValidValue public function @dataProvider validValues
RenameAdminPathsSettingsFormTest::validValues public function
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::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.
UnitTestCase::setUp protected function 340