setFormState example


  public function testHandleFormSubmissionWithResponses($class$form_state_key) {
    $response = $this->getMockBuilder($class)
      ->disableOriginalConstructor()
      ->getMock();
    $response->expects($this->any())
      ->method('prepare')
      ->willReturn($response);

    $form_state = (new FormState())
      ->setSubmitted()
      ->setFormState([$form_state_key => $response]);

    $form_submitter = $this->getFormSubmitter();
    $form = [];
    $return = $form_submitter->doSubmitForm($form$form_state);

    $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $return);
  }

  public function providerTestHandleFormSubmissionWithResponses() {
    return [
      ['Symfony\Component\HttpFoundation\Response', 'response'],
      [

class FormStateTest extends UnitTestCase {

  /** * Tests the getRedirect() method. * * @covers ::getRedirect * * @dataProvider providerTestGetRedirect */
  public function testGetRedirect($form_state_additions$expected) {
    $form_state = (new FormState())->setFormState($form_state_additions);
    $redirect = $form_state->getRedirect();
    $this->assertEquals($expected$redirect);
  }

  /** * Provides test data for testing the getRedirect() method. * * @return array * Returns some test data. */
  public function providerTestGetRedirect() {
    
      // allowed to survive here:       // - always_process: Does not make sense in conjunction with form caching       // in the first place, since passing form_build_id as a GET parameter is       // not desired.       // - temporary: Any assigned data is expected to survives within the same       // page request.       if ($check_cache) {
        $cache_form_state = $form_state->getCacheableArray();
        $cache_form_state['always_process'] = $form_state->getAlwaysProcess();
        $cache_form_state['temporary'] = $form_state->getTemporary();
        $form_state = $form_state_before_retrieval;
        $form_state->setFormState($cache_form_state);
      }
    }

    // If this form is an AJAX request, disable all form redirects.     if ($ajax_form_request = $request->query->has(static::AJAX_FORM_REQUEST)) {
      $form_state->disableRedirect();
    }

    // Now that we have a constructed form, process it. This is where:     // - Element #process functions get called to further refine $form.     // - User input, if any, gets incorporated in the #value property of the
$form_id = 'test_form_id';
    $expected_form = $form_id();

    $response = $this->getMockBuilder($class)
      ->disableOriginalConstructor()
      ->getMock();

    $form_arg = $this->getMockForm($form_id$expected_form);
    $form_arg->expects($this->any())
      ->method('submitForm')
      ->willReturnCallback(function D$form, FormStateInterface $form_state) use ($response$form_state_key) {
        $form_state->setFormState([$form_state_key => $response]);
      });

    $form_state = new FormState();
    try {
      $input['form_id'] = $form_id;
      $form_state->setUserInput($input);
      $this->simulateFormSubmission($form_id$form_arg$form_state, FALSE);
      $this->fail('EnforcedResponseException was not thrown.');
    }
    catch (EnforcedResponseException $e) {
      $this->assertSame($response$e->getResponse());
    }
$this->entityTypeManager = $entity_type_manager;
    $this->formBuilder = $form_builder;
  }

  /** * {@inheritdoc} */
  public function getForm(EntityInterface $entity$operation = 'default', array $form_state_additions = []) {
    $form_object = $this->entityTypeManager->getFormObject($entity->getEntityTypeId()$operation);
    $form_object->setEntity($entity);

    $form_state = (new FormState())->setFormState($form_state_additions);
    return $this->formBuilder->buildForm($form_object$form_state);
  }

}

  }

  /** * @covers ::setFormState */
  public function testSetFormState() {
    $form_state_additions = [
      'foo' => 'bar',
    ];

    $this->decoratedFormState->setFormState($form_state_additions)
      ->shouldBeCalled();

    $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setFormState($form_state_additions));
  }

  /** * @covers ::setAlwaysProcess * * @dataProvider providerSingleBooleanArgument */
  public function testSetAlwaysProcess($always_process) {
    
/** * The decorated form state. * * @var \Drupal\Core\Form\FormStateInterface */
  protected $decoratedFormState;

  /** * {@inheritdoc} */
  public function setFormState(array $form_state_additions) {
    $this->decoratedFormState->setFormState($form_state_additions);

    return $this;
  }

  /** * {@inheritdoc} */
  public function setAlwaysProcess($always_process = TRUE) {
    $this->decoratedFormState->setAlwaysProcess($always_process);

    return $this;
  }
/** * Loads the cached form state. * * @param string $form_build_id * The unique form build ID. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
  protected function loadCachedFormState($form_build_id, FormStateInterface $form_state) {
    if ($stored_form_state = $this->keyValueExpirableFactory->get('form_state')->get($form_build_id)) {
      // Re-populate $form_state for subsequent rebuilds.       $form_state->setFormState($stored_form_state);

      // If the original form is contained in include files, load the files.       // @see \Drupal\Core\Form\FormStateInterface::loadInclude()       $build_info = $form_state->getBuildInfo();
      $build_info += ['files' => []];
      foreach ($build_info['files'] as $file) {
        if (is_array($file)) {
          $file += ['type' => 'inc', 'name' => $file['module']];
          $this->moduleHandler->loadInclude($file['module']$file['type']$file['name']);
        }
        elseif (file_exists($file)) {
          
Home | Imprint | This part of the site doesn't use cookies.