class RenderElementTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php \Drupal\Tests\Core\Render\Element\RenderElementTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php \Drupal\Tests\Core\Render\Element\RenderElementTest
  3. 10 core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php \Drupal\Tests\Core\Render\Element\RenderElementTest

@coversDefaultClass \Drupal\Core\Render\Element\RenderElementBase
@group Render

Hierarchy

Expanded class hierarchy of RenderElementTest

File

core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php, line 19

Namespace

Drupal\Tests\Core\Render\Element
View source
class RenderElementTest extends UnitTestCase {
  
  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;
  
  /**
   * The container.
   *
   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
   */
  protected $container;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->requestStack = new RequestStack();
    $this->container = new ContainerBuilder();
    $this->container
      ->set('request_stack', $this->requestStack);
    \Drupal::setContainer($this->container);
  }
  
  /**
   * @covers ::preRenderAjaxForm
   */
  public function testPreRenderAjaxForm() : void {
    $request = Request::create('/test');
    $request->query
      ->set('foo', 'bar');
    $this->requestStack
      ->push($request);
    $prophecy = $this->prophesize('Drupal\\Core\\Routing\\UrlGeneratorInterface');
    $url = '/test?foo=bar&ajax_form=1';
    $prophecy->generateFromRoute('<current>', [], [
      'query' => [
        'foo' => 'bar',
        FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
      ],
    ], TRUE)
      ->willReturn((new GeneratedUrl())->setCacheContexts([
      'route',
    ])
      ->setGeneratedUrl($url));
    $url_generator = $prophecy->reveal();
    $this->container
      ->set('url_generator', $url_generator);
    $element = [
      '#type' => 'select',
      '#id' => 'test',
      '#ajax' => [
        'wrapper' => 'foo',
        'callback' => 'test-callback',
      ],
    ];
    $element = RenderElementBase::preRenderAjaxForm($element);
    $this->assertTrue($element['#ajax_processed']);
    $this->assertEquals($url, $element['#attached']['drupalSettings']['ajax']['test']['url']);
  }
  
  /**
   * @covers ::preRenderAjaxForm
   */
  public function testPreRenderAjaxFormWithQueryOptions() : void {
    $request = Request::create('/test');
    $request->query
      ->set('foo', 'bar');
    $this->requestStack
      ->push($request);
    $prophecy = $this->prophesize('Drupal\\Core\\Routing\\UrlGeneratorInterface');
    $url = '/test?foo=bar&other=query&ajax_form=1';
    $prophecy->generateFromRoute('<current>', [], [
      'query' => [
        'foo' => 'bar',
        'other' => 'query',
        FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
      ],
    ], TRUE)
      ->willReturn((new GeneratedUrl())->setCacheContexts([
      'route',
    ])
      ->setGeneratedUrl($url));
    $url_generator = $prophecy->reveal();
    $this->container
      ->set('url_generator', $url_generator);
    $element = [
      '#type' => 'select',
      '#id' => 'test',
      '#ajax' => [
        'wrapper' => 'foo',
        'callback' => 'test-callback',
        'options' => [
          'query' => [
            'other' => 'query',
          ],
        ],
      ],
    ];
    $element = RenderElementBase::preRenderAjaxForm($element);
    $this->assertTrue($element['#ajax_processed']);
    $this->assertEquals($url, $element['#attached']['drupalSettings']['ajax']['test']['url']);
  }
  
  /**
   * @covers ::setAttributes
   *
   * @dataProvider providerTestSetAttributes
   */
  public function testSetAttributes(array $element, array $class, array $expected) : void {
    RenderElementBase::setAttributes($element, $class);
    $this->assertSame($expected, $element);
  }
  
  /**
   * Provides test data for testSetAttributes().
   */
  public static function providerTestSetAttributes() : array {
    return [
      'No-op' => [
        'element' => [
          '#type' => 'textfield',
        ],
        'class' => [],
        'expected' => [
          '#type' => 'textfield',
        ],
      ],
      'Add first class' => [
        'element' => [
          '#type' => 'textfield',
        ],
        'class' => [
          'foo',
          'bar',
        ],
        'expected' => [
          '#type' => 'textfield',
          '#attributes' => [
            'class' => [
              'foo',
              'bar',
            ],
          ],
        ],
      ],
      'Append classes' => [
        'element' => [
          '#type' => 'textfield',
          '#attributes' => [
            'class' => [
              'foo',
              'bar',
            ],
          ],
        ],
        'class' => [
          'baz',
        ],
        'expected' => [
          '#type' => 'textfield',
          '#attributes' => [
            'class' => [
              'foo',
              'bar',
              'baz',
            ],
          ],
        ],
      ],
      'Required' => [
        'element' => [
          '#type' => 'textfield',
          '#required' => TRUE,
        ],
        'class' => [],
        'expected' => [
          '#type' => 'textfield',
          '#required' => TRUE,
          '#attributes' => [
            'class' => [
              'required',
            ],
            'required' => 'required',
          ],
        ],
      ],
      'Parent with error' => [
        'element' => [
          '#type' => 'textfield',
          '#parents' => [
            'dummy_parent',
          ],
          '#errors' => 'invalid',
          '#validated' => TRUE,
        ],
        'class' => [],
        'expected' => [
          '#type' => 'textfield',
          '#parents' => [
            'dummy_parent',
          ],
          '#errors' => 'invalid',
          '#validated' => TRUE,
          '#attributes' => [
            'class' => [
              'error',
            ],
            'aria-invalid' => 'true',
          ],
        ],
      ],
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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.
RenderElementTest::$container protected property The container.
RenderElementTest::$requestStack protected property The request stack.
RenderElementTest::providerTestSetAttributes public static function Provides test data for testSetAttributes().
RenderElementTest::setUp protected function Overrides UnitTestCase::setUp
RenderElementTest::testPreRenderAjaxForm public function @covers ::preRenderAjaxForm[[api-linebreak]]
RenderElementTest::testPreRenderAjaxFormWithQueryOptions public function @covers ::preRenderAjaxForm[[api-linebreak]]
RenderElementTest::testSetAttributes public function @covers ::setAttributes[[api-linebreak]]
UnitTestCase::$root protected property The app root.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.