Commit cbaa67d9 authored by catch's avatar catch

Issue #1598578 by aspilicious: Convert form.test to PSR-0.

parent d66a6777
<?php
/**
* @file
* Definition of Drupal\system\Tests\Form\AlterTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Test form alter hooks.
*/
class AlterTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Form alter hooks',
'description' => 'Tests hook_form_alter() and hook_form_FORM_ID_alter().',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp(array('block', 'form_test'));
}
/**
* Tests execution order of hook_form_alter() and hook_form_FORM_ID_alter().
*/
function testExecutionOrder() {
$this->drupalGet('form-test/alter');
// Ensure that the order is first by module, then for a given module, the
// id-specific one after the generic one.
$expected = array(
'block_form_form_test_alter_form_alter() executed.',
'form_test_form_alter() executed.',
'form_test_form_form_test_alter_form_alter() executed.',
'system_form_form_test_alter_form_alter() executed.',
);
$content = preg_replace('/\s+/', ' ', filter_xss($this->content, array()));
$this->assert(strpos($content, implode(' ', $expected)) !== FALSE, t('Form alter hooks executed in the expected order.'));
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Form\ArbitraryRebuildTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Tests rebuilding of arbitrary forms by altering them.
*/
class ArbitraryRebuildTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Rebuild arbitrary forms',
'description' => 'Tests altering forms to be rebuilt so there are multiple steps.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
// Auto-create a field for testing.
$field = array(
'field_name' => 'test_multiple',
'type' => 'text',
'cardinality' => -1,
'translatable' => FALSE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'node',
'field_name' => 'test_multiple',
'bundle' => 'page',
'label' => 'Test a multiple valued field',
'widget' => array(
'type' => 'text_textfield',
'weight' => 0,
),
);
field_create_instance($instance);
variable_set('user_register', USER_REGISTER_VISITORS);
}
/**
* Tests a basic rebuild with the user registration form.
*/
function testUserRegistrationRebuild() {
$edit = array(
'name' => 'foo',
'mail' => 'bar@example.com',
);
$this->drupalPost('user/register', $edit, 'Rebuild');
$this->assertText('Form rebuilt.');
$this->assertFieldByName('name', 'foo', 'Entered user name has been kept.');
$this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.');
}
/**
* Tests a rebuild caused by a multiple value field.
*/
function testUserRegistrationMultipleField() {
$edit = array(
'name' => 'foo',
'mail' => 'bar@example.com',
);
$this->drupalPost('user/register', $edit, t('Add another item'), array('query' => array('field' => TRUE)));
$this->assertText('Test a multiple valued field', 'Form has been rebuilt.');
$this->assertFieldByName('name', 'foo', 'Entered user name has been kept.');
$this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.');
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Form\CheckboxTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Tests checkbox element.
*/
class CheckboxTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Form API checkbox',
'description' => 'Tests form API checkbox handling of various combinations of #default_value and #return_value.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
function testFormCheckbox() {
// Ensure that the checked state is determined and rendered correctly for
// tricky combinations of default and return values.
foreach (array(FALSE, NULL, TRUE, 0, '0', '', 1, '1', 'foobar', '1foobar') as $default_value) {
// Only values that can be used for array indeces are supported for
// #return_value, with the exception of integer 0, which is not supported.
// @see form_process_checkbox().
foreach (array('0', '', 1, '1', 'foobar', '1foobar') as $return_value) {
$form_array = drupal_get_form('form_test_checkbox_type_juggling', $default_value, $return_value);
$form = drupal_render($form_array);
if ($default_value === TRUE) {
$checked = TRUE;
}
elseif ($return_value === '0') {
$checked = ($default_value === '0');
}
elseif ($return_value === '') {
$checked = ($default_value === '');
}
elseif ($return_value === 1 || $return_value === '1') {
$checked = ($default_value === 1 || $default_value === '1');
}
elseif ($return_value === 'foobar') {
$checked = ($default_value === 'foobar');
}
elseif ($return_value === '1foobar') {
$checked = ($default_value === '1foobar');
}
$checked_in_html = strpos($form, 'checked') !== FALSE;
$message = t('#default_value is %default_value #return_value is %return_value.', array('%default_value' => var_export($default_value, TRUE), '%return_value' => var_export($return_value, TRUE)));
$this->assertIdentical($checked, $checked_in_html, $message);
}
}
// Ensure that $form_state['values'] is populated correctly for a checkboxes
// group that includes a 0-indexed array of options.
$results = json_decode($this->drupalPost('form-test/checkboxes-zero', array(), 'Save'));
$this->assertIdentical($results->checkbox_off, array(0, 0, 0), t('All three in checkbox_off are zeroes: off.'));
$this->assertIdentical($results->checkbox_zero_default, array('0', 0, 0), t('The first choice is on in checkbox_zero_default'));
$this->assertIdentical($results->checkbox_string_zero_default, array('0', 0, 0), t('The first choice is on in checkbox_string_zero_default'));
$edit = array('checkbox_off[0]' => '0');
$results = json_decode($this->drupalPost('form-test/checkboxes-zero', $edit, 'Save'));
$this->assertIdentical($results->checkbox_off, array('0', 0, 0), t('The first choice is on in checkbox_off but the rest is not'));
// Ensure that each checkbox is rendered correctly for a checkboxes group
// that includes a 0-indexed array of options.
$this->drupalPost('form-test/checkboxes-zero/0', array(), 'Save');
$checkboxes = $this->xpath('//input[@type="checkbox"]');
foreach ($checkboxes as $checkbox) {
$checked = isset($checkbox['checked']);
$name = (string) $checkbox['name'];
$this->assertIdentical($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', t('Checkbox %name correctly checked', array('%name' => $name)));
}
$edit = array('checkbox_off[0]' => '0');
$this->drupalPost('form-test/checkboxes-zero/0', $edit, 'Save');
$checkboxes = $this->xpath('//input[@type="checkbox"]');
foreach ($checkboxes as $checkbox) {
$checked = isset($checkbox['checked']);
$name = (string) $checkbox['name'];
$this->assertIdentical($checked, $name == 'checkbox_off[0]' || $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', t('Checkbox %name correctly checked', array('%name' => $name)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Form\ElementTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Tests building and processing of core form elements.
*/
class ElementTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Element processing',
'description' => 'Tests building and processing of core form elements.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp(array('form_test'));
}
/**
* Tests placeholder text for elements that support placeholders.
*/
function testPlaceHolderText() {
$this->drupalGet('form-test/placeholder-text');
$expected = 'placeholder-text';
// Test to make sure non-textarea elements have the proper placeholder text.
foreach (array('textfield', 'tel', 'url', 'password', 'email', 'number') as $type) {
$element = $this->xpath('//input[@id=:id and @placeholder=:expected]', array(
':id' => 'edit-' . $type,
':expected' => $expected,
));
$this->assertTrue(!empty($element), t('Placeholder text placed in @type.', array('@type' => $type)));
}
// Test to make sure textarea has the proper placeholder text.
$element = $this->xpath('//textarea[@id=:id and @placeholder=:expected]', array(
':id' => 'edit-textarea',
':expected' => $expected,
));
$this->assertTrue(!empty($element), t('Placeholder text placed in textarea.'));
}
/**
* Tests expansion of #options for #type checkboxes and radios.
*/
function testOptions() {
$this->drupalGet('form-test/checkboxes-radios');
// Verify that all options appear in their defined order.
foreach (array('checkbox', 'radio') as $type) {
$elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
$expected_values = array('0', 'foo', '1', 'bar', '>');
foreach ($elements as $element) {
$expected = array_shift($expected_values);
$this->assertIdentical((string) $element['value'], $expected);
}
}
// Enable customized option sub-elements.
$this->drupalGet('form-test/checkboxes-radios/customize');
// Verify that all options appear in their defined order, taking a custom
// #weight into account.
foreach (array('checkbox', 'radio') as $type) {
$elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
$expected_values = array('0', 'foo', 'bar', '>', '1');
foreach ($elements as $element) {
$expected = array_shift($expected_values);
$this->assertIdentical((string) $element['value'], $expected);
}
}
// Verify that custom #description properties are output.
foreach (array('checkboxes', 'radios') as $type) {
$elements = $this->xpath('//input[@id=:id]/following-sibling::div[@class=:class]', array(
':id' => 'edit-' . $type . '-foo',
':class' => 'description',
));
$this->assertTrue(count($elements), t('Custom %type option description found.', array(
'%type' => $type,
)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Form\ElementsLabelsTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Test form element labels, required markers and associated output.
*/
class ElementsLabelsTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Form element and label output test',
'description' => 'Test form element labels, required markers and associated output.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Test form elements, labels, title attibutes and required marks output
* correctly and have the correct label option class if needed.
*/
function testFormLabels() {
$this->drupalGet('form_test/form-labels');
// Check that the checkbox/radio processing is not interfering with
// basic placement.
$elements = $this->xpath('//input[@id="edit-form-checkboxes-test-third-checkbox"]/following-sibling::label[@for="edit-form-checkboxes-test-third-checkbox" and @class="option"]');
$this->assertTrue(isset($elements[0]), t("Label follows field and label option class correct for regular checkboxes."));
// Make sure the label is rendered for checkboxes.
$elements = $this->xpath('//input[@id="edit-form-checkboxes-test-0"]/following-sibling::label[@for="edit-form-checkboxes-test-0" and @class="option"]');
$this->assertTrue(isset($elements[0]), t("Label 0 found checkbox."));
$elements = $this->xpath('//input[@id="edit-form-radios-test-second-radio"]/following-sibling::label[@for="edit-form-radios-test-second-radio" and @class="option"]');
$this->assertTrue(isset($elements[0]), t("Label follows field and label option class correct for regular radios."));
// Make sure the label is rendered for radios.
$elements = $this->xpath('//input[@id="edit-form-radios-test-0"]/following-sibling::label[@for="edit-form-radios-test-0" and @class="option"]');
$this->assertTrue(isset($elements[0]), t("Label 0 found radios."));
// Exercise various defaults for checkboxes and modifications to ensure
// appropriate override and correct behavior.
$elements = $this->xpath('//input[@id="edit-form-checkbox-test"]/following-sibling::label[@for="edit-form-checkbox-test" and @class="option"]');
$this->assertTrue(isset($elements[0]), t("Label follows field and label option class correct for a checkbox by default."));
// Exercise various defaults for textboxes and modifications to ensure
// appropriate override and correct behavior.
$elements = $this->xpath('//label[@for="edit-form-textfield-test-title-and-required"]/child::abbr[@class="form-required"]/parent::*/following-sibling::input[@id="edit-form-textfield-test-title-and-required"]');
$this->assertTrue(isset($elements[0]), t("Label precedes textfield, with required marker inside label."));
$elements = $this->xpath('//input[@id="edit-form-textfield-test-no-title-required"]/preceding-sibling::label[@for="edit-form-textfield-test-no-title-required"]/abbr[@class="form-required"]');
$this->assertTrue(isset($elements[0]), t("Label tag with required marker precedes required textfield with no title."));
$elements = $this->xpath('//input[@id="edit-form-textfield-test-title-invisible"]/preceding-sibling::label[@for="edit-form-textfield-test-title-invisible" and @class="element-invisible"]');
$this->assertTrue(isset($elements[0]), t("Label preceding field and label class is element-invisible."));
$elements = $this->xpath('//input[@id="edit-form-textfield-test-title"]/preceding-sibling::abbr[@class="form-required"]');
$this->assertFalse(isset($elements[0]), t("No required marker on non-required field."));
$elements = $this->xpath('//input[@id="edit-form-textfield-test-title-after"]/following-sibling::label[@for="edit-form-textfield-test-title-after" and @class="option"]');
$this->assertTrue(isset($elements[0]), t("Label after field and label option class correct for text field."));
$elements = $this->xpath('//label[@for="edit-form-textfield-test-title-no-show"]');
$this->assertFalse(isset($elements[0]), t("No label tag when title set not to display."));
// Check #field_prefix and #field_suffix placement.
$elements = $this->xpath('//span[@class="field-prefix"]/following-sibling::div[@id="edit-form-radios-test"]');
$this->assertTrue(isset($elements[0]), t("Properly placed the #field_prefix element after the label and before the field."));
$elements = $this->xpath('//span[@class="field-suffix"]/preceding-sibling::div[@id="edit-form-radios-test"]');
$this->assertTrue(isset($elements[0]), t("Properly places the #field_suffix element immediately after the form field."));
// Check #prefix and #suffix placement.
$elements = $this->xpath('//div[@id="form-test-textfield-title-prefix"]/following-sibling::div[contains(@class, \'form-item-form-textfield-test-title\')]');
$this->assertTrue(isset($elements[0]), t("Properly places the #prefix element before the form item."));
$elements = $this->xpath('//div[@id="form-test-textfield-title-suffix"]/preceding-sibling::div[contains(@class, \'form-item-form-textfield-test-title\')]');
$this->assertTrue(isset($elements[0]), t("Properly places the #suffix element before the form item."));
// Check title attribute for radios and checkboxes.
$elements = $this->xpath('//div[@id="edit-form-checkboxes-title-attribute"]');
$this->assertEqual($elements[0]['title'], 'Checkboxes test' . ' (' . t('Required') . ')', 'Title attribute found.');
$elements = $this->xpath('//div[@id="edit-form-radios-title-attribute"]');
$this->assertEqual($elements[0]['title'], 'Radios test' . ' (' . t('Required') . ')', 'Title attribute found.');
}
}
<?php
/**
* @file
* Definition of Drupal\system\Tests\Form\ElementsTableSelectTest.
*/
namespace Drupal\system\Tests\Form;
use Drupal\simpletest\WebTestBase;
/**
* Test the tableselect form element for expected behavior.
*/
class ElementsTableSelectTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Tableselect form element type test',
'description' => 'Test the tableselect element for expected behavior',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Test the display of checkboxes when #multiple is TRUE.
*/
function testMultipleTrue() {
$this->drupalGet('form_test/tableselect/multiple-true');
$this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.'));
// Test for the presence of the Select all rows tableheader.
$this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Presence of the "Select all" checkbox.'));
$rows = array('row1', 'row2', 'row3');
foreach ($rows as $row) {
$this->assertFieldByXPath('//input[@type="checkbox"]', $row, t('Checkbox for value @row.', array('@row' => $row)));
}
}
/**
* Test the display of radios when #multiple is FALSE.
*/
function testMultipleFalse() {
$this->drupalGet('form_test/tableselect/multiple-false');
$this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.'));
// Test for the absence of the Select all rows tableheader.
$this->assertNoFieldByXPath('//th[@class="select-all"]', '', t('Absence of the "Select all" checkbox.'));
$rows = array('row1', 'row2', 'row3');
foreach ($rows as $row) {
$this->assertFieldByXPath('//input[@type="radio"]', $row, t('Radio button for value @row.', array('@row' => $row)));
}
}
/**
* Tests the display when #colspan is set.
*/
function testTableselectColSpan() {
$this->drupalGet('form_test/tableselect/colspan');
$this->assertText(t('Three'), 'Presence of the third column');
$this->assertNoText(t('Four'), 'Absence of a fourth column');
// There should be three labeled column headers and 1 for the input.
$table_head = $this->xpath('//thead');
$this->assertEqual(count($table_head[0]->tr->th), 4, 'There are four column headers');
$table_body = $this->xpath('//tbody');
// The first two body rows should each have 5 table cells: One for the
// radio, one cell in the first column, one cell in the the second column,
// and two cells in the third column which has colspan 2.
for ( $i = 0; $i <= 1; $i++) {
$this->assertEqual(count($table_body[0]->tr[$i]->td), 5, format_string('There are five cells in row @row.', array('@row' => $i)));
}
// The third row should have 3 cells, one for the radio, one spanning the
// first and second column, and a third in column 3 (which has colspan 3).
$this->assertEqual(count($table_body[0]->tr[2]->td), 3, 'There are three cells in row 3.');
}
/**
* Test the display of the #empty text when #options is an empty array.
*/
function testEmptyText() {
$this->drupalGet('form_test/tableselect/empty-text');
$this->assertText(t('Empty text.'), t('Empty text should be displayed.'));
}
/**
* Test the submission of single and multiple values when #multiple is TRUE.
*/
function testMultipleTrueSubmit() {
// Test a submission with one checkbox checked.
$edit = array();
$edit['tableselect[row1]'] = TRUE;
$this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit');
$this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1'));
$this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.'));
$this->assertText(t('Submitted: row3 = 0'), t('Unchecked checkbox row3.'));
// Test a submission with multiple checkboxes checked.
$edit['tableselect[row1]'] = TRUE;
$edit['tableselect[row3]'] = TRUE;
$this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit');
$this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1.'));
$this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.'));
$this->assertText(t('Submitted: row3 = row3'), t('Checked checkbox row3.'));
}
/**
* Test submission of values when #multiple is FALSE.
*/
function testMultipleFalseSubmit() {
$edit['tableselect'] = 'row1';
$this->drupalPost('form_test/tableselect/multiple-false', $edit, 'Submit');
$this->assertText(t('Submitted: row1'), t('Selected radio button'));
}
/**
* Test the #js_select property.
*/
function testAdvancedSelect() {
// When #multiple = TRUE a Select all checkbox should be displayed by default.
$this->drupalGet('form_test/tableselect/advanced-select/multiple-true-default');
$this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Display a "Select all" checkbox by default when #multiple is TRUE.'));
// When #js_select is set to FALSE, a "Select all" checkbox should not be displayed.
$this->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select');
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #js_select is FALSE.'));
// A "Select all" checkbox never makes sense when #multiple = FALSE, regardless of the value of #js_select.
$this->drupalGet('form_test/tableselect/advanced-select/multiple-false-default');
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE.'));
$this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE, even when #js_select is TRUE.'));
}
/**
* Test the whether the option checker gives an error on invalid tableselect values for checkboxes.
*/
function testMultipleTrueOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
);
// Test with a valid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => array('row1' => 'row1')));
$this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for checkboxes.'));
// Test with an invalid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => array('non_existing_value' => 'non_existing_value')));
$this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for checkboxes.'));
}
/**
* Test the whether the option checker gives an error on invalid tableselect values for radios.
*/
function testMultipleFalseOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE,
);
// Test with a valid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1'));
$this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for radio buttons.'));
// Test with an invalid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value'));
$this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for radio buttons.'));
}