Пример #1
0
 /**
  * Test that invalid values are caught and marked as invalid.
  */
 function testInvalidField()
 {
     // Change the field to a datetime field.
     $this->fieldStorage->setSetting('datetime_type', 'datetime');
     $this->fieldStorage->save();
     $field_name = $this->fieldStorage->getName();
     // Display creation form.
     $this->drupalGet('entity_test/add');
     $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
     $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
     // Submit invalid dates and ensure they is not accepted.
     $date_value = '';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '12:00:00');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', 'Empty date value has been caught.');
     $date_value = 'aaaa-12-01';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', array('%date' => $date_value)));
     $date_value = '2012-75-01';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', array('%date' => $date_value)));
     $date_value = '2012-12-99';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', array('%date' => $date_value)));
     $date_value = '2012-12-01';
     $time_value = '';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value);
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', 'Empty time value has been caught.');
     $date_value = '2012-12-01';
     $time_value = '49:00:00';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value);
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', array('%time' => $time_value)));
     $date_value = '2012-12-01';
     $time_value = '12:99:00';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value);
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', array('%time' => $time_value)));
     $date_value = '2012-12-01';
     $time_value = '12:15:99';
     $edit = array("{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value);
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array('%time' => $time_value)));
 }
Пример #2
0
 /**
  * Tests the 'options_select' widget (multiple select).
  */
 function testSelectListMultiple()
 {
     // Create an instance of the 'multiple values' field.
     $field = entity_create('field_config', array('field_storage' => $this->card2, 'bundle' => 'entity_test'));
     $field->save();
     entity_get_form_display('entity_test', 'entity_test', 'default')->setComponent($this->card2->getName(), array('type' => 'options_select'))->save();
     // Create an entity.
     $entity = entity_create('entity_test', array('user_id' => 1, 'name' => $this->randomMachineName()));
     $entity->save();
     $entity_init = clone $entity;
     // Display form: with no field data, nothing is selected.
     $this->drupalGet('entity_test/manage/' . $entity->id());
     $this->assertOptionSelected("edit-card-2", '_none');
     $this->assertNoOptionSelected('edit-card-2', 0);
     $this->assertNoOptionSelected('edit-card-2', 1);
     $this->assertNoOptionSelected('edit-card-2', 2);
     $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
     // Submit form: select first and third options.
     $edit = array('card_2[]' => array(0 => 0, 2 => 2));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertFieldValues($entity_init, 'card_2', array(0, 2));
     // Display form: check that the right options are selected.
     $this->drupalGet('entity_test/manage/' . $entity->id());
     $this->assertOptionSelected('edit-card-2', 0);
     $this->assertNoOptionSelected('edit-card-2', 1);
     $this->assertOptionSelected('edit-card-2', 2);
     // Submit form: select only first option.
     $edit = array('card_2[]' => array(0 => 0));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertFieldValues($entity_init, 'card_2', array(0));
     // Display form: check that the right options are selected.
     $this->drupalGet('entity_test/manage/' . $entity->id());
     $this->assertOptionSelected('edit-card-2', 0);
     $this->assertNoOptionSelected('edit-card-2', 1);
     $this->assertNoOptionSelected('edit-card-2', 2);
     // Submit form: select the three options while the field accepts only 2.
     $edit = array('card_2[]' => array(0 => 0, 1 => 1, 2 => 2));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
     // Submit form: uncheck all options.
     $edit = array('card_2[]' => array());
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertFieldValues($entity_init, 'card_2', array());
     // Test the 'None' option.
     // Check that the 'none' option has no effect if actual options are selected
     // as well.
     $edit = array('card_2[]' => array('_none' => '_none', 0 => 0));
     $this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
     $this->assertFieldValues($entity_init, 'card_2', array(0));
     // Check that selecting the 'none' option empties the field.
     $edit = array('card_2[]' => array('_none' => '_none'));
     $this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
     $this->assertFieldValues($entity_init, 'card_2', array());
     // A required select list does not have an empty key.
     $field->setRequired(TRUE);
     $field->save();
     $this->drupalGet('entity_test/manage/' . $entity->id());
     $this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2')), 'A required select list does not have an empty key.');
     // We do not have to test that a required select list with one option is
     // auto-selected because the browser does it for us.
     // Test optgroups.
     // Use a callback function defining optgroups.
     $this->card2->setSetting('allowed_values', []);
     $this->card2->setSetting('allowed_values_function', 'options_test_allowed_values_callback');
     $this->card2->save();
     $field->setRequired(FALSE);
     $field->save();
     // Display form: with no field data, nothing is selected.
     $this->drupalGet('entity_test/manage/' . $entity->id());
     $this->assertNoOptionSelected('edit-card-2', 0);
     $this->assertNoOptionSelected('edit-card-2', 1);
     $this->assertNoOptionSelected('edit-card-2', 2);
     $this->assertRaw('Some dangerous & unescaped markup', 'Option text was properly filtered.');
     $this->assertRaw('More <script>dangerous</script> markup', 'Option group text was properly filtered.');
     $this->assertRaw('Group 1', 'Option groups are displayed.');
     // Submit form: select first option.
     $edit = array('card_2[]' => array(0 => 0));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertFieldValues($entity_init, 'card_2', array(0));
     // Display form: check that the right options are selected.
     $this->drupalGet('entity_test/manage/' . $entity->id());
     $this->assertOptionSelected('edit-card-2', 0);
     $this->assertNoOptionSelected('edit-card-2', 1);
     $this->assertNoOptionSelected('edit-card-2', 2);
     // Submit form: Unselect the option.
     $edit = array('card_2[]' => array('_none' => '_none'));
     $this->drupalPostForm('entity_test/manage/' . $entity->id(), $edit, t('Save'));
     $this->assertFieldValues($entity_init, 'card_2', array());
 }