View source
<?php
class WebformComponentsTestCase extends WebformTestCase {
public static function getInfo() {
return array(
'name' => t('Webform components'),
'description' => t('Add and remove components from a webform.'),
'group' => t('Webform'),
);
}
public function testWebformComponents() {
$test_components = $this
->webformComponents();
$textfield = $test_components['textfield']['component'];
$textfield['page_num'] = 1;
$textfield['name'] = $this
->randomName();
$textfield['form_key'] = $this
->randomName();
$components[1] = $textfield;
$components[2] = array(
'type' => 'pagebreak',
'form_key' => 'pagebreak_' . $this
->randomName(),
'pid' => 0,
'name' => $this
->randomName(),
'page_num' => 2,
);
$textfield['name'] = $this
->randomName();
$textfield['form_key'] = $this
->randomName();
$textfield['page_num'] = 2;
$components[3] = $textfield;
$components[4] = array(
'type' => 'pagebreak',
'form_key' => 'pagebreak_' . $this
->randomName(),
'pid' => 0,
'name' => '1',
'page_num' => 3,
);
$textfield['name'] = $this
->randomName();
$textfield['form_key'] = $this
->randomName();
$textfield['page_num'] = 3;
$components[5] = $textfield;
$components[6] = array(
'type' => 'pagebreak',
'form_key' => 'pagebreak_' . $this
->randomName(),
'pid' => 0,
'name' => '1',
'page_num' => 4,
);
$textfield['name'] = $this
->randomName();
$textfield['form_key'] = $this
->randomName();
$textfield['page_num'] = 4;
$components[7] = $textfield;
$node = new stdClass();
$node->webform['components'] = $components;
$webform_component_list = webform_component_list($node, NULL, TRUE, TRUE);
$test_list = array(
1 => $components[1]['name'],
$components[2]['name'] => array(
2 => $components[2]['name'],
3 => $components[3]['name'],
),
$components[4]['name'] . ' ' => array(
4 => $components[4]['name'],
5 => $components[5]['name'],
),
$components[6]['name'] . '_2' => array(
6 => $components[6]['name'],
7 => $components[7]['name'],
),
);
$this
->assertIdentical($webform_component_list, $test_list, 'webform_component_list() returns expected value.');
$components = array(
1 => array(
'form_key' => $this
->randomName(),
'name' => $this
->randomName(),
'pid' => 0,
),
2 => array(
'form_key' => $this
->randomName(),
'name' => $this
->randomName(),
'pid' => 1,
),
3 => array(
'form_key' => $this
->randomName(),
'name' => $this
->randomName(),
'pid' => 2,
),
);
$node = new stdClass();
$node->webform['components'] = $components;
$parents = webform_component_parent_keys($node, $components[3]);
$test_parents = array(
$components[1]['form_key'],
$components[2]['form_key'],
$components[3]['form_key'],
);
$this
->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected form_keys.');
$parents = webform_component_parent_keys($node, $components[3], 'name');
$test_parents = array(
$components[1]['name'],
$components[2]['name'],
$components[3]['name'],
);
$this
->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected names.');
$parents = webform_component_parent_keys($node, $components[3], TRUE);
$test_parents = array(
$components[1],
$components[2],
$components[3],
);
$this
->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected component arrays.');
$settings = array(
'title' => 'Test webform with multiple instances of a Form Key',
'type' => 'webform',
);
$node = $this
->drupalCreateNode($settings);
$components = $this
->webformComponents();
$textarea = $components['textarea'];
$textarea['type'] = 'textarea';
$textarea['form_key'] = 'testing_key';
$textarea['cid'] = 1;
$textarea['pid'] = 0;
$textarea = array_merge(webform_component_invoke('textarea', 'defaults'), $textarea);
$node->webform['components'][1] = $textarea;
$fieldset = array(
'cid' => 2,
'pid' => 0,
'form_key' => 'another_key',
'name' => 'Fieldset',
'type' => 'fieldset',
'value' => '',
'required' => '0',
'weight' => '0',
);
$node->webform['components'][2] = $fieldset;
$textfield = $components['textfield'];
$textfield['type'] = 'textfield';
$textfield['form_key'] = 'testing_key';
$textfield['cid'] = 3;
$textfield['pid'] = 2;
$textfield = array_merge(webform_component_invoke('textarea', 'defaults'), $textfield);
$node->webform['components'][3] = $textfield;
$textfield = $components['textfield'];
$textfield['type'] = 'textfield';
$textfield['form_key'] = 'dummy_key';
$textfield['cid'] = 4;
$textfield['pid'] = 0;
$textfield = array_merge(webform_component_invoke('textarea', 'defaults'), $textfield);
$node->webform['components'][4] = $textfield;
node_save($node);
$this
->assertTrue(webform_get_cid($node, 'testing_key', 2) == 3, t('Returned expected Webform component id for a given form_key and parent (pid).'));
$this
->assertTrue(webform_get_cid($node, 'testing_key') == array(
1,
3,
), t('Returned expected Webform component ids array for a given form_key.'));
$node = $this
->webformForm();
$this
->drupalGet('node/' . $node->nid);
$this
->assertRaw('<th class="checkbox webform-grid-option" scope="col">', 'Grid <code>th</code> elements have @scope of "col".');
$labels = $this
->xpath('//label/@for');
foreach ($labels as $label) {
$for = $this
->xpath("//*[@id=':id']", array(
':id' => $label['for'],
));
$this
->assertTrue($for, 'Label with @for "' . $label['for'] . '" points to an element.');
}
$grid_headers = $this
->xpath('//th[@class="webform-grid-question"]');
$this
->assertIdentical(count($grid_headers), 3, 'There are three table headers with class "webform-grid-question".');
$grid_headers = $this
->xpath('//th[@class="webform-grid-question"]/text()');
$this
->assertIdentical(count($grid_headers), 2, 'There are two non-empty table headers with class "webform-grid-question".');
$this
->assertIdentical((string) $grid_headers[0], 'Grid Keyed', 'The value of the left non-empty header is "Grid Keyed".');
$this
->assertIdentical((string) $grid_headers[1], 'Grid Keyed', 'The value of the right non-empty header is the same as the left.');
}
}