You are here

public function FormBuilderTest::testFormTokenCacheability in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testFormTokenCacheability()
  2. 10 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testFormTokenCacheability()

@covers ::prepareForm

@dataProvider providerTestFormTokenCacheability

File

core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php, line 875
Contains \Drupal\Tests\Core\Form\FormBuilderTest.

Class

FormBuilderTest
@coversDefaultClass \Drupal\Core\Form\FormBuilder @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testFormTokenCacheability($token, $is_authenticated, $expected_form_cacheability, $expected_token_cacheability, $method) {
  $user = $this
    ->prophesize(AccountProxyInterface::class);
  $user
    ->isAuthenticated()
    ->willReturn($is_authenticated);
  $this->container
    ->set('current_user', $user
    ->reveal());
  \Drupal::setContainer($this->container);
  $form_id = 'test_form_id';
  $form = $form_id();
  $form['#method'] = $method;
  if (isset($token)) {
    $form['#token'] = $token;
  }
  $form_arg = $this
    ->createMock('Drupal\\Core\\Form\\FormInterface');
  $form_arg
    ->expects($this
    ->once())
    ->method('getFormId')
    ->will($this
    ->returnValue($form_id));
  $form_arg
    ->expects($this
    ->once())
    ->method('buildForm')
    ->will($this
    ->returnValue($form));
  $form_state = new FormState();
  $built_form = $this->formBuilder
    ->buildForm($form_arg, $form_state);
  if (!isset($expected_form_cacheability) || $method == 'get' && !is_string($token)) {
    $this
      ->assertFalse(isset($built_form['#cache']));
  }
  else {
    $this
      ->assertTrue(isset($built_form['#cache']));
    $this
      ->assertEquals($expected_form_cacheability, $built_form['#cache']);
  }
  if (!isset($expected_token_cacheability)) {
    $this
      ->assertFalse(isset($built_form['form_token']));
  }
  else {
    $this
      ->assertTrue(isset($built_form['form_token']));
    $this
      ->assertEquals($expected_token_cacheability, $built_form['form_token']['#cache']);
  }
}