RequiredByRoleCommonWebTestCase.test in Required by role 7
Same filename and directory in other branches
Tests for the common cases.
File
tests/required_by_role_test/RequiredByRoleCommonWebTestCase.testView source
<?php
/**
* @file
* Tests for the common cases.
*/
class RequiredByRoleCommonWebTestCase extends RequiredByRoleBasic {
/**
* Field name.
*/
protected $fieldName = NULL;
/**
* Field label.
*/
protected $fieldLabel = NULL;
/**
* Field label.
*/
protected $optionsFieldCardinality = 0;
/**
* Getinfo method.
*/
public static function getInfo() {
return array(
'name' => 'Required by role: Common',
'description' => 'Tests for common behaviors.',
'group' => 'Required by role',
);
}
/**
* Setup method.
*/
protected function setUp() {
parent::setUp();
$rid = (int) $this->required_fields_rid;
$settings = array(
"instance[settings][required_by_role][{$rid}]" => $rid,
);
$this
->drupalLogin($this
->getRequiredUser());
$this
->setFieldLabel($this
->randomName(4));
$this
->setFieldName('text_test');
$this
->createTextField(NULL, $settings);
$this
->createOptionsField();
}
/**
* Helper function to set the field name.
*/
protected function setFieldName($value) {
$this->fieldName = $value . '_' . strtolower($this
->getFieldLabel());
}
/**
* Helper function to set the field name.
*/
protected function setFieldLabel($value) {
$this->fieldLabel = $value;
}
/**
* Helper function to get the field name.
*/
protected function getFieldName() {
return $this->fieldName;
}
/**
* Helper function to get the field machine name.
*/
protected function getFieldMachineName() {
return 'field_' . $this
->getFieldName();
}
/**
* Helper function to get the field name.
*/
protected function getFieldLabel() {
return $this->fieldLabel;
}
/**
* Helper function to create an email field.
*/
protected function createTextField($bundle = NULL, $settings = array()) {
$node_type = $bundle ? $bundle : $this->entity_bundle;
$label = $this
->getFieldLabel();
$name = $this
->getFieldName();
$edit = array(
'fields[_add_new_field][label]' => $label,
'fields[_add_new_field][field_name]' => $name,
'fields[_add_new_field][type]' => 'text',
'fields[_add_new_field][widget_type]' => 'text_textfield',
);
$field_name = $this
->getFieldMachineName();
$this
->drupalPost('admin/structure/types/manage/' . $node_type . '/fields', $edit, t('Save'));
$this
->drupalPost(NULL, array(), t('Save field settings'));
$this
->drupalPost(NULL, $settings, t('Save settings'));
// Is field created?
$this
->assertRaw(t('Saved %label configuration', array(
'%label' => $label,
)), 'Field added');
node_types_rebuild();
menu_rebuild();
return $field_name;
}
protected function createOptionsField($bundle = NULL) {
$node_type = $bundle ? $bundle : $this->entity_bundle;
// Field with cardinality 0.
$this->options_field = array(
'field_name' => 'options_field',
'type' => 'list_integer',
'cardinality' => $this->optionsFieldCardinality,
'settings' => array(
'allowed_values' => array(
1 => 'One',
2 => 'Two',
),
),
);
$this->options_field = field_create_field($this->options_field);
$rid = (int) $this->required_fields_rid;
// Create an instance of the 'single value' field.
$instance = array(
'field_name' => $this->options_field['field_name'],
'entity_type' => $this->entity_type,
'bundle' => $node_type,
'settings' => array(
'required_by_role' => array(
$rid => $rid,
),
),
'widget' => array(
'type' => 'options_select',
),
);
$instance = field_create_instance($instance);
}
/**
* Helper function to set the required property.
*/
protected function setFieldRequiredProperty($settings) {
$node_type = $this->entity_bundle;
$url = "admin/structure/types/manage/" . $node_type . "/fields/" . $this
->getFieldMachineName();
$this
->drupalPost($url, $settings, t('Save settings'));
}
/**
* Helper function check the settings.
*/
protected function checkFieldRequiredProperty($ids) {
$node_type = $this->entity_bundle;
$url = "admin/structure/types/manage/" . $node_type . "/fields/" . $this
->getFieldMachineName();
$this
->drupalGet($url);
foreach ($ids as $id => $active) {
if ($active) {
$this
->assertFieldChecked($id);
}
else {
$this
->assertNoFieldChecked($id);
}
}
}
/**
* Group the tests for better performance.
*/
public function testCommonBehaviors() {
$this
->DotestDefaultValueIsNotRequired();
$this
->DotestNoneRemovedInOptionsWhenFieldIsRequired();
$this
->DotestChangeToDefaultBehavior();
$this
->DotestChangeToRequiredByRole();
}
/**
* Check if the default_value_widget is always NOT required.
*/
public function DotestDefaultValueIsNotRequired() {
$field_name = $this
->getFieldMachineName();
$label = $this
->getFieldLabel();
$edit = array();
$this
->drupalPost('admin/structure/types/manage/' . $this->entity_bundle . '/fields/' . $field_name, $edit, t('Save settings'));
// We shouldn´t get a field mandatory error message.
$expected_error = t('@label field is required.', array(
'@label' => $label,
));
$this
->assertNoText($expected_error, 'Default value widget is not required.');
// We should get a success message.
$expected_success = t('Saved @label configuration.', array(
'@label' => $label,
));
$this
->assertText($expected_success, 'Default value widget is not required.');
}
/**
* Checks that the _none option is not present in options fields when the
* field is required_by_role.
*/
public function DotestNoneRemovedInOptionsWhenFieldIsRequired() {
// Display form.
$this
->drupalGet('node/add/' . $this->entity_bundle);
$langcode = LANGUAGE_NONE;
$xpath = '//select[@id=:id]//option[@value="_none" and text()=:label]';
$id = drupal_html_id('edit-options_field-' . $langcode);
$arguments = array(
':id' => $id,
':label' => t('- Select -'),
);
$element = $this
->xpath($xpath, $arguments);
$this
->assertFalse($element, 'A required select list should not has a "- Select -" option.');
$arguments = array(
':id' => $id,
':label' => t('- None -'),
);
$element = $this
->xpath($xpath, $arguments);
$this
->assertTrue($element, 'A required select list should not has a "- None -" option.');
}
/**
* Test changing the required property to the default behavior.
*/
public function DotestChangeToDefaultBehavior() {
$rid = (int) $this->required_fields_rid;
$settings = array(
"instance[required]" => 1,
"instance[settings][required_by_role][{$rid}]" => FALSE,
);
$this
->setFieldRequiredProperty($settings);
$ids = array(
'edit-instance-required' => 1,
'edit-instance-settings-required-by-role-' . $rid => 0,
);
$this
->checkFieldRequiredProperty($ids);
}
/**
* Test changing the required property to required by role.
*/
public function DotestChangeToRequiredByRole() {
$rid = (int) $this->required_fields_rid;
$settings = array(
"instance[required]" => FALSE,
"instance[settings][required_by_role][{$rid}]" => $rid,
);
$this
->setFieldRequiredProperty($settings);
$ids = array(
'edit-instance-settings-required-by-role-' . $rid => 1,
'edit-instance-required' => 0,
);
$this
->checkFieldRequiredProperty($ids);
}
}
Classes
Name | Description |
---|---|
RequiredByRoleCommonWebTestCase | @file Tests for the common cases. |