Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\system\Tests\Form\TriggeringElementProgrammedUnitTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\Component\Utility\String;
use Drupal\Core\Form\FormInterface;
Dries Buytaert
committed
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Tests detection of triggering_element for programmed form submissions.
*
* @group Form
Angie Byron
committed
*/
class TriggeringElementProgrammedUnitTest extends DrupalUnitTestBase implements FormInterface {
public static $modules = array('system');
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'triggering_element_programmed_form';
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
Angie Byron
committed
$form['one'] = array(
'#type' => 'textfield',
'#title' => 'One',
'#required' => TRUE,
);
$form['two'] = array(
'#type' => 'textfield',
'#title' => 'Two',
'#required' => TRUE,
);
$form['actions'] = array('#type' => 'actions');
Angie Byron
committed
$user_input = $form_state->getUserInput();
Angie Byron
committed
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
'#limit_validation_errors' => array(
Angie Byron
committed
array($user_input['section']),
Angie Byron
committed
),
// Required for #limit_validation_errors.
'#submit' => array(array($this, 'submitForm')),
);
return $form;
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function validateForm(array &$form, FormStateInterface $form_state) {
Angie Byron
committed
// Verify that the only submit button was recognized as triggering_element.
$this->assertEqual($form['actions']['submit']['#array_parents'], $form_state['triggering_element']['#array_parents']);
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Angie Byron
committed
}
/**
* Tests that #limit_validation_errors of the only submit button takes effect.
*/
function testLimitValidationErrors() {
// Programmatically submit the form.
Dries Buytaert
committed
$form_state = new FormState();
Alex Pott
committed
$form_state->setValue('section', 'one');
Angie Byron
committed
$form_builder = $this->container->get('form_builder');
$form_builder->submitForm($this, $form_state);
// Verify that only the specified section was validated.
$errors = $form_state->getErrors();
Angie Byron
committed
$this->assertTrue(isset($errors['one']), "Section 'one' was validated.");
$this->assertFalse(isset($errors['two']), "Section 'two' was not validated.");
// Verify that there are only values for the specified section.
Alex Pott
committed
$this->assertTrue($form_state->hasValue('one'), "Values for section 'one' found.");
$this->assertFalse($form_state->hasValue('two'), "Values for section 'two' not found.");