Skip to content

Issue #29: Get tests working. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 1.x-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion conditional_fields.module
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ function conditional_fields_menu() {
'page callback' => 'conditional_fields_dependencies_overview_page',
'access arguments' => array('administer dependencies'),
'file' => 'includes/conditional_fields.admin.inc',
'type' => MENU_LOCAL_TASK,
);

$items['admin/structure/dependencies/overview'] = array(
Expand Down
114 changes: 90 additions & 24 deletions tests/conditional_fields.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,31 @@ class ConditionalFieldsTestCase extends BackdropWebTestCase {
parent::setUp('conditional_fields_test', 'list', 'text', 'number', 'taxonomy', 'image');

// Create and log in user.
$web_user = $this->backdropCreateUser(array('create conditional_fields_test content', 'edit any conditional_fields_test content'));
$web_user = $this->backdropCreateUser(array(
'create conditional_fields_test content',
'edit any conditional_fields_test content',
));
$this->backdropLogin($web_user);

// Create a vocabulary and two terms to test the term reference field.
$vocabulary = new stdClass();
$vocabulary->name = $this->randomName();
$vocabulary->machine_name = backdrop_strtolower($this->randomName());
taxonomy_vocabulary_save($vocabulary);
$term = new stdClass();
$term->name = 'Foo';
$term->vid = $vocabulary->vid;
$vocabulary = new TaxonomyVocabulary(array(
'machine_name' => backdrop_strtolower($this->randomName()),
'name' => $this->randomName(),
));
$vocabulary->save();
$term = array(
'name' => 'Foo',
'vocabulary' => $vocabulary->machine_name,
'parent' => array(0),
);
$term = new TaxonomyTerm($term);
taxonomy_term_save($term);
$term = new stdClass();
$term->name = 'Bar';
$term->vid = $vocabulary->vid;
$term = array(
'name' => 'Bar',
'vocabulary' => $vocabulary->machine_name,
'parent' => array(0),
);
$term = new TaxonomyTerm($term);
taxonomy_term_save($term);

// Create dependee.
Expand All @@ -47,7 +57,9 @@ class ConditionalFieldsTestCase extends BackdropWebTestCase {
'field_name' => 'dependee',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array($this->dependee_on_value => t('Dependent fields are visible'), $this->dependee_off_value => t('Dependent fields are invisible')),
'allowed_values' => array(
$this->dependee_on_value => t('Dependent fields are visible'),
$this->dependee_off_value => t('Dependent fields are invisible')),
),
);
field_create_field($dependee);
Expand Down Expand Up @@ -140,14 +152,28 @@ class ConditionalFieldsTestCase extends BackdropWebTestCase {
field_create_field($dependent['field']);
field_create_instance($dependent['instance']);
$dependent_instance = field_info_instance('node', $dependent['field']['field_name'], 'conditional_fields_test');
conditional_fields_dependency_insert($dependee_instance['id'], $dependent_instance['id'], $dependency_options);
$dependency = array(
'dependee' => $dependee_instance['field_name'],
'dependent' => $dependent_instance['field_name'],
'options' => $dependency_options,
'entity_type' => 'node',
'bundle' => 'conditional_fields_test',
);
conditional_fields_dependency_insert($dependency);
}
}

/**
* Tests field dependencies on a node.
*/
public function testNodeDependencies() {
// Temporarily force re-login.
$web_user = $this->backdropCreateUser(array(
'create conditional_fields_test content',
'edit any conditional_fields_test content',
));
$this->backdropLogin($web_user);

// Try to submit a node with triggered dependencies.
// The submit should fail because the dependent fields are required.
$langcode = LANGUAGE_NONE;
Expand All @@ -162,14 +188,39 @@ class ConditionalFieldsTestCase extends BackdropWebTestCase {
continue;
}
// Multiple value textfields are dumb and can't find their own name.
if ($dependent['field']['cardinality'] == -1 && in_array($dependent['widget_type'], array('number', 'text_textfield', 'text_textarea', 'text_textarea_with_summary'))) {
if ($dependent['field']['cardinality'] == -1 && in_array($dependent['widget_type'], array(
'number',
'text_textfield',
'text_textarea',
'text_textarea_with_summary',
))) {
$name = '';
}
else {
$name = $dependent['widget_type'] . '_' . $dependent['field_type'] . '_label';
$name .= $dependent['field']['cardinality'] == -1 ? '_multiple' : '';
}
$this->assertRaw(t('!name field is required.', array('!name' => $name)), 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value required ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' and no value fails validation');
$cardinality = $dependent['field']['cardinality'] == -1 ? 'multiple' : 'single';
$trigger_description = 'Triggered ' . $cardinality . ' value required ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' and no value fails validation (name: ' . $name . ')';
// Need to adjust the error message for date fields in particular, below.
$is_date = in_array($dependent['field_type'], array('date', 'datetime', 'datestamp'));
if ($is_date) {
if ($dependent['widget_type'] == 'date_text' || $dependent['widget_type'] == 'date_popup' || $dependent['widget_type'] == 'date_select') {
$assert_text = t('A valid date is required for %name.', array('%name' => $name));
}
elseif ($dependent['widget_type'] == 'date_html5') {
if ($cardinality == 'multiple') {
$assert_text = t('A start date for value #1 is required in %name.', array('%name' => $name));
}
else {
$assert_text = t('A start date is required in %name.', array('%name' => $name));
}
}
}
else {
$assert_text = t('!name field is required.', array('!name' => $name));
}
$this->assertRaw($assert_text, $trigger_description);
}

// Fill the dependents with values and save the node.
Expand Down Expand Up @@ -207,7 +258,10 @@ class ConditionalFieldsTestCase extends BackdropWebTestCase {
}
}
$this->backdropPost('node/add/conditional-fields-test', $edit, t('Save'));
$this->assertRaw(t('@type %title has been created.', array('@type' => 'Conditional Fields Test Node Type', '%title' => '')), 'Node was created with triggered dependencies.');
$this->assertRaw(t('@type %title has been created.', array(
'@type' => 'Conditional Fields Test Node Type',
'%title' => '',
)), 'Node was created with triggered dependencies.');
// Verify that the fields are visible on node view.
foreach ($this->dependents as $dependent) {
$this->assertText($dependent['instance']['label'] . ':', 'Triggered ' . ($dependent['field']['cardinality'] == -1 ? 'multiple' : 'single') . ' value ' . $dependent['field_type'] . ' dependent with widget ' . $dependent['widget_type'] . ' is visible on node view');
Expand Down Expand Up @@ -240,15 +294,22 @@ class ConditionalFieldsUITestCase extends backdropWebTestCase {
parent::setUp('conditional_fields_test', 'list', 'text');

// Create and log in user.
$web_user = $this->backdropCreateUser(array('access administration pages', 'administer content types', 'administer dependencies'));
$web_user = $this->backdropCreateUser(array(
'access administration pages',
'administer content types',
'administer dependencies',
));
$this->backdropLogin($web_user);

// Create a field that will be used as a dependee.
$dependee = array(
'field_name' => 'dependee',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array('on' => t('Dependent is visible'), 'off' => t('Dependent is invisible')),
'allowed_values' => array(
'on' => t('Dependent is visible'),
'off' => t('Dependent is invisible'),
),
),
);
field_create_field($dependee);
Expand Down Expand Up @@ -280,13 +341,14 @@ class ConditionalFieldsUITestCase extends backdropWebTestCase {
*/
public function testDependenciesOverview() {
$dependee = field_read_instance('node', 'dependee', 'conditional_fields_test');
$dependent = field_read_instance('node', 'dependent', 'conditional_fields_test');
$this->backdropGet('admin/structure');
$this->clickLink(t('Field dependencies'));
$this->clickLink(t('Node'));
$this->backdropGet('admin/structure/types/manage/conditional-fields-test');
$this->clickLink(t('Manage dependencies'));
$this->assertFieldByName('dependee', $dependee['id'], 'The dependee selection field is in the content type dependencies page.');
$this->assertFieldByName('dependent', $dependee['id'], 'The dependent selection field is in the content type dependencies page.');
$this->assertFieldByName('dependee', $dependee['field_name'], 'The dependee selection field is in the content type dependencies page.');
$this->assertFieldByName('dependent', '', 'The dependent selection field is in the content type dependencies page.');
}

/**
Expand All @@ -297,8 +359,8 @@ class ConditionalFieldsUITestCase extends backdropWebTestCase {
$dependee = field_read_instance('node', 'dependee', 'conditional_fields_test');
$dependent = field_read_instance('node', 'dependent', 'conditional_fields_test');
$edit = array(
'dependee' => $dependee['id'],
'dependent' => $dependent['id'],
'dependee' => $dependee['field_name'],
'dependent' => $dependent['field_name'],
);
$this->backdropPost('admin/structure/types/manage/conditional-fields-test/dependencies', $edit, t('Add dependency'), array(), array(), 'conditional-fields-dependency-add-form-node-conditional-fields-test');

Expand All @@ -316,8 +378,12 @@ class ConditionalFieldsUITestCase extends backdropWebTestCase {
)), 'The dependency was updated correctly.');

// Delete dependency.
$this->clickLink(t('delete'));
$this->backdropPost(NULL, array(), t('Delete dependency'), array('query' => array('destination' => 'admin/structure/types/manage/conditional-fields-test/dependencies')));
$this->clickLink(t('Delete'));
$this->backdropPost(NULL, array(), t('Delete dependency'), array(
'query' => array(
'destination' => 'admin/structure/types/manage/conditional-fields-test/dependencies',
),
));
$this->assertText(t('The dependency has been deleted.'));
}
}
20 changes: 0 additions & 20 deletions tests/conditional_fields_test.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,3 @@
* Test Conditional Fields functionality.
*/

/**
* Implements hook_node_info().
*/
function conditional_fields_test_node_info() {
return array(
'conditional_fields_test' => array(
'name' => t('Conditional Fields Test Node Type'),
'module' => 'conditional_fields_test',
'base' => 'conditional_fields_test',
),
);
}

/**
* Implements hook_autoload_info().
*/
function conditional_fields_test_autoload_info() {
return array(
);
}
50 changes: 50 additions & 0 deletions tests/config/node.type.conditional_fields_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"_config_name": "node.type.conditional_fields_test",
"type": "conditional_fields_test",
"name": "Conditional Fields Test Node Type",
"base": "node_content",
"module": "node",
"node_preview": "1",
"description": "",
"help": "",
"modified": true,
"disabled": false,
"has_title": true,
"title_label": "Title",
"settings": {
"comment_enabled": false,
"comment_default": "1",
"comment_per_page": "50",
"comment_mode": 1,
"comment_anonymous": 0,
"comment_title_options": "auto",
"comment_user_picture": true,
"comment_form_location": 1,
"comment_preview": "1",
"comment_close_enabled": 0,
"comment_close_days": "14",
"menu_default": 0,
"menu_options": {
"user-menu": 0,
"management": 0,
"main-menu": 0
},
"menu_parent": "main-menu:0",
"status_default": 1,
"scheduling_enabled": 1,
"promote_enabled": 1,
"promote_default": 0,
"sticky_enabled": 1,
"sticky_default": 0,
"revision_enabled": 1,
"revision_default": 0,
"node_preview": "1",
"node_submitted": 1,
"node_submitted_format": "[node:created:medium] by [node:author]",
"node_user_picture": true,
"hidden_path": 0,
"language": 0,
"node_permissions": true
},
"orig_type": "conditional_fields_test"
}