WebformComponentsTestCase.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Webform module component tests.
  4. */
  5. class WebformComponentsTestCase extends WebformTestCase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => t('Webform components'),
  12. 'description' => t('Add and remove components from a webform.'),
  13. 'group' => t('Webform'),
  14. );
  15. }
  16. /**
  17. * Webform module component tests.
  18. */
  19. public function testWebformComponents() {
  20. // Test webform_component_list().
  21. // Create a form consisting of three textfields separated by pagebreaks.
  22. $test_components = $this->webformComponents();
  23. $textfield = $test_components['textfield']['component'];
  24. // Page 1 textfield.
  25. $textfield['page_num'] = 1;
  26. $textfield['name'] = $this->randomName();
  27. $textfield['form_key'] = $this->randomName();
  28. $components[1] = $textfield;
  29. // Page 2 break.
  30. $components[2] = array(
  31. 'type' => 'pagebreak',
  32. 'form_key' => 'pagebreak_' . $this->randomName(),
  33. 'pid' => 0,
  34. 'name' => $this->randomName(),
  35. 'page_num' => 2,
  36. );
  37. // Page 2 textfield.
  38. $textfield['name'] = $this->randomName();
  39. $textfield['form_key'] = $this->randomName();
  40. $textfield['page_num'] = 2;
  41. $components[3] = $textfield;
  42. // Page 3 break.
  43. $components[4] = array(
  44. 'type' => 'pagebreak',
  45. 'form_key' => 'pagebreak_' . $this->randomName(),
  46. 'pid' => 0,
  47. // Name is the cid of another component. Should get a space added when it
  48. // is used as a key in the value returned from webform_component_list().
  49. 'name' => '1',
  50. 'page_num' => 3,
  51. );
  52. // Page 3 textfield.
  53. $textfield['name'] = $this->randomName();
  54. $textfield['form_key'] = $this->randomName();
  55. $textfield['page_num'] = 3;
  56. $components[5] = $textfield;
  57. // Page 4 break.
  58. $components[6] = array(
  59. 'type' => 'pagebreak',
  60. 'form_key' => 'pagebreak_' . $this->randomName(),
  61. 'pid' => 0,
  62. // Name is the same as Page 3 break. Tests that name is made unique.
  63. 'name' => '1',
  64. 'page_num' => 4,
  65. );
  66. // Page 4 textfield.
  67. $textfield['name'] = $this->randomName();
  68. $textfield['form_key'] = $this->randomName();
  69. $textfield['page_num'] = 4;
  70. $components[7] = $textfield;
  71. // Generate a component list.
  72. $node = new stdClass();
  73. $node->webform['components'] = $components;
  74. $webform_component_list = webform_component_list($node, NULL, TRUE, TRUE);
  75. // Test return value.
  76. $test_list = array(
  77. 1 => $components[1]['name'],
  78. $components[2]['name'] => array(2 => $components[2]['name'], 3 => $components[3]['name']),
  79. $components[4]['name'] . ' ' => array(4 => $components[4]['name'], 5 => $components[5]['name']),
  80. $components[6]['name'] . '_2' => array(6 => $components[6]['name'], 7 => $components[7]['name']),
  81. );
  82. $this->assertIdentical($webform_component_list, $test_list, 'webform_component_list() returns expected value.');
  83. // Test webform_component_parent_keys().
  84. $components = array(
  85. 1 => array(
  86. 'form_key' => $this->randomName(),
  87. 'name' => $this->randomName(),
  88. 'pid' => 0,
  89. ),
  90. 2 => array(
  91. 'form_key' => $this->randomName(),
  92. 'name' => $this->randomName(),
  93. 'pid' => 1,
  94. ),
  95. 3 => array(
  96. 'form_key' => $this->randomName(),
  97. 'name' => $this->randomName(),
  98. 'pid' => 2,
  99. ),
  100. );
  101. $node = new stdClass();
  102. $node->webform['components'] = $components;
  103. $parents = webform_component_parent_keys($node, $components[3]);
  104. $test_parents = array($components[1]['form_key'], $components[2]['form_key'], $components[3]['form_key']);
  105. $this->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected form_keys.');
  106. $parents = webform_component_parent_keys($node, $components[3], 'name');
  107. $test_parents = array($components[1]['name'], $components[2]['name'], $components[3]['name']);
  108. $this->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected names.');
  109. $parents = webform_component_parent_keys($node, $components[3], TRUE);
  110. $test_parents = array($components[1], $components[2], $components[3]);
  111. $this->assertIdentical($parents, $test_parents, 'webform_component_parent_keys() returns expected component arrays.');
  112. // Test webform_get_cid().
  113. $settings = array(
  114. 'title' => 'Test webform with multiple instances of a Form Key',
  115. 'type' => 'webform',
  116. );
  117. $node = $this->drupalCreateNode($settings);
  118. // Add a new textarea component.
  119. $components = $this->webformComponents();
  120. $textarea = $components['textarea'];
  121. $textarea['type'] = 'textarea';
  122. $textarea['form_key'] = 'testing_key';
  123. $textarea['cid'] = 1;
  124. $textarea['pid'] = 0;
  125. $textarea = array_merge(webform_component_invoke('textarea', 'defaults'), $textarea);
  126. $node->webform['components'][1] = $textarea;
  127. // Add a new fieldset component.
  128. $fieldset = array(
  129. 'cid' => 2,
  130. 'pid' => 0,
  131. 'form_key' => 'another_key',
  132. 'name' => 'Fieldset',
  133. 'type' => 'fieldset',
  134. 'value' => '',
  135. 'required' => '0',
  136. 'weight' => '0',
  137. );
  138. $node->webform['components'][2] = $fieldset;
  139. // Add a new textfield component as child of the fieldset.
  140. $textfield = $components['textfield'];
  141. $textfield['type'] = 'textfield';
  142. $textfield['form_key'] = 'testing_key';
  143. $textfield['cid'] = 3;
  144. $textfield['pid'] = 2;
  145. $textfield = array_merge(webform_component_invoke('textarea', 'defaults'), $textfield);
  146. $node->webform['components'][3] = $textfield;
  147. // Add another textfield component.
  148. $textfield = $components['textfield'];
  149. $textfield['type'] = 'textfield';
  150. $textfield['form_key'] = 'dummy_key';
  151. $textfield['cid'] = 4;
  152. $textfield['pid'] = 0;
  153. $textfield = array_merge(webform_component_invoke('textarea', 'defaults'), $textfield);
  154. $node->webform['components'][4] = $textfield;
  155. node_save($node);
  156. // Test webform_get_cid() with providing a parent cid.
  157. $this->assertTrue(webform_get_cid($node, 'testing_key', 2) == 3, t('Returned expected Webform component id for a given form_key and parent (pid).'));
  158. // Test webform_get_cid() without providing a parent cid.
  159. $this->assertTrue(webform_get_cid($node, 'testing_key') == array(1, 3), t('Returned expected Webform component ids array for a given form_key.'));
  160. // Create and visit a new Webform test node.
  161. $node = $this->webformForm();
  162. $this->drupalGet('node/' . $node->nid);
  163. // Check th elements have the scope attribute.
  164. $this->assertRaw('<th class="checkbox webform-grid-option" scope="col">', 'Grid <code>th</code> elements have @scope of "col".');
  165. // Check that each label @for points to an element.
  166. $labels = $this->xpath('//label/@for');
  167. foreach ($labels as $label) {
  168. $for = $this->xpath("//*[@id=':id']", array(':id' => $label['for']));
  169. $this->assertTrue($for, 'Label with @for "' . $label['for'] . '" points to an element.');
  170. }
  171. // Test grid headers.
  172. $grid_headers = $this->xpath('//th[@class="webform-grid-question"]');
  173. $this->assertIdentical(count($grid_headers), 3, 'There are three table headers with class "webform-grid-question".');
  174. $grid_headers = $this->xpath('//th[@class="webform-grid-question"]/text()');
  175. $this->assertIdentical(count($grid_headers), 2, 'There are two non-empty table headers with class "webform-grid-question".');
  176. $this->assertIdentical((string) $grid_headers[0], 'Grid Keyed', 'The value of the left non-empty header is "Grid Keyed".');
  177. $this->assertIdentical((string) $grid_headers[1], 'Grid Keyed', 'The value of the right non-empty header is the same as the left.');
  178. }
  179. }