field_group.display.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * @file
  4. * Test file for fieldgroup display.
  5. */
  6. /**
  7. * Group display tests
  8. */
  9. class GroupDisplayTestCase extends DrupalWebTestCase {
  10. protected $node;
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Display tests',
  14. 'description' => 'Test the field group display.',
  15. 'group' => 'Field Group',
  16. );
  17. }
  18. function setUp() {
  19. parent::setUp('field_test', 'field_group', 'field_group_test');
  20. $node = new stdClass();
  21. $node->type = 'article';
  22. $node->title = $this->randomName();
  23. $node->status = 1;
  24. // Create test fields.
  25. $test_fields = array('field_test', 'field_test_2', 'field_no_access');
  26. foreach ($test_fields as $field_name) {
  27. $field = array(
  28. 'field_name' => $field_name,
  29. 'type' => 'test_field',
  30. 'cardinality' => 1,
  31. );
  32. $instance = array(
  33. 'field_name' => $field_name,
  34. 'entity_type' => 'node',
  35. 'bundle' => 'article',
  36. 'label' => $this->randomName(),
  37. 'display' => array(
  38. 'default' => array(
  39. 'type' => 'field_test_default',
  40. 'settings' => array(
  41. 'test_formatter_setting' => $this->randomName(),
  42. ),
  43. ),
  44. 'teaser' => array(
  45. 'type' => 'field_test_default',
  46. 'settings' => array(
  47. 'test_formatter_setting' => $this->randomName(),
  48. ),
  49. ),
  50. ),
  51. );
  52. field_create_field($field);
  53. field_create_instance($instance);
  54. $node->{$field_name}[LANGUAGE_NONE][0]['value'] = mt_rand(1, 127);
  55. }
  56. node_save($node);
  57. $this->node = $node;
  58. }
  59. /**
  60. * Create a new group.
  61. * @param array $data
  62. * Data for the field group.
  63. */
  64. function createGroup($mode, array $data) {
  65. $group_name = 'group_' . drupal_strtolower($this->randomName(8));
  66. $identifier = $group_name . '|node|article|' . $mode;
  67. $field_group = new stdClass;
  68. $field_group->disabled = FALSE;
  69. $field_group->api_version = 1;
  70. $field_group->identifier = $identifier;
  71. $field_group->group_name = $group_name;
  72. $field_group->entity_type = 'node';
  73. $field_group->bundle = 'article';
  74. $field_group->mode = $mode;
  75. $field_group->parent_name = '';
  76. $field_group->children = $data['children'];
  77. $field_group->data = $data;
  78. drupal_write_record('field_group', $field_group);
  79. ctools_export_crud_enable('field_group', $field_group->identifier);
  80. return $field_group;
  81. }
  82. /**
  83. * Test if an empty formatter.
  84. */
  85. function testFieldAccess() {
  86. $data = array(
  87. 'label' => 'Wrapper',
  88. 'weight' => '1',
  89. 'children' => array(
  90. 0 => 'field_no_access',
  91. ),
  92. 'format_type' => 'div',
  93. 'format_settings' => array(
  94. 'label' => 'Link',
  95. 'instance_settings' => array(
  96. 'required_fields' => 0,
  97. 'id' => 'wrapper-id',
  98. 'classes' => 'test-class',
  99. 'description' => '',
  100. 'show_label' => FALSE,
  101. 'label_element' => 'h3',
  102. 'effect' => 'blink',
  103. 'speed' => 'fast',
  104. ),
  105. 'formatter' => 'open',
  106. ),
  107. );
  108. $group = $this->createGroup('default', $data);
  109. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  110. $this->drupalGet('node/' . $this->node->nid);
  111. // Test if group is not shown.
  112. $this->assertNoFieldByXPath("//div[contains(@id, 'wrapper-id')]", NULL, t('Div that contains fields with no access is not shown.'));
  113. }
  114. /**
  115. * Test the div formatter.
  116. */
  117. function testDiv() {
  118. $data = array(
  119. 'label' => 'Wrapper',
  120. 'weight' => '1',
  121. 'children' => array(
  122. 0 => 'field_test',
  123. ),
  124. 'format_type' => 'div',
  125. 'format_settings' => array(
  126. 'label' => 'Link',
  127. 'instance_settings' => array(
  128. 'required_fields' => 0,
  129. 'id' => 'wrapper-id',
  130. 'classes' => 'test-class',
  131. 'description' => '',
  132. 'show_label' => FALSE,
  133. 'label_element' => 'h3',
  134. 'effect' => 'blink',
  135. 'speed' => 'fast',
  136. ),
  137. 'formatter' => 'open',
  138. ),
  139. );
  140. $group = $this->createGroup('default', $data);
  141. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  142. $this->drupalGet('node/' . $this->node->nid);
  143. // Test group ids and classes.
  144. $this->assertFieldByXPath("//div[contains(@id, 'wrapper-id')]", NULL, t('Wrapper id set on wrapper div'));
  145. $this->assertFieldByXPath("//div[contains(@class, 'test-class')]", NULL, t('Test class set on wrapper div') . 'class="' . $group->group_name . ' test-class');
  146. // Test group label.
  147. $this->assertNoRaw('<h3><span>' . $data['label'] . '</span></h3>', t('Label is not shown'));
  148. // Set show label to true.
  149. $group->data['format_settings']['instance_settings']['show_label'] = TRUE;
  150. drupal_write_record('field_group', $group, array('identifier'));
  151. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  152. $this->drupalGet('node/' . $this->node->nid);
  153. $this->assertRaw('<h3><span>' . $data['label'] . '</span></h3>', t('Label is shown'));
  154. // Change to collapsible
  155. $group->data['format_settings']['formatter'] = 'collapsible';
  156. drupal_write_record('field_group', $group, array('identifier'));
  157. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  158. $this->drupalGet('node/' . $this->node->nid);
  159. $this->assertFieldByXPath("//div[contains(@class, 'speed-fast')]", NULL, t('Speed class is set'));
  160. $this->assertFieldByXPath("//div[contains(@class, 'effect-blink')]", NULL, t('Effect class is set'));
  161. }
  162. /**
  163. * Test the horizontal tabs formatter.
  164. */
  165. function testHorizontalTabs() {
  166. $data = array(
  167. 'label' => 'Tab 1',
  168. 'weight' => '1',
  169. 'children' => array(
  170. 0 => 'field_test',
  171. ),
  172. 'format_type' => 'htab',
  173. 'format_settings' => array(
  174. 'label' => 'Tab 1',
  175. 'instance_settings' => array(
  176. 'classes' => 'test-class',
  177. 'description' => '',
  178. ),
  179. 'formatter' => 'open',
  180. ),
  181. );
  182. $first_tab = $this->createGroup('default', $data);
  183. $data = array(
  184. 'label' => 'Tab 2',
  185. 'weight' => '1',
  186. 'children' => array(
  187. 0 => 'field_test_2',
  188. ),
  189. 'format_type' => 'htab',
  190. 'format_settings' => array(
  191. 'label' => 'Tab 1',
  192. 'instance_settings' => array(
  193. 'classes' => 'test-class-2',
  194. 'description' => 'description of second tab',
  195. ),
  196. 'formatter' => 'closed',
  197. ),
  198. );
  199. $second_tab = $this->createGroup('default', $data);
  200. $data = array(
  201. 'label' => 'Tabs',
  202. 'weight' => '1',
  203. 'children' => array(
  204. 0 => $first_tab->group_name,
  205. 1 => $second_tab->group_name,
  206. ),
  207. 'format_type' => 'htabs',
  208. 'format_settings' => array(
  209. 'label' => 'Tab 1',
  210. 'instance_settings' => array(
  211. 'classes' => 'test-class-wrapper',
  212. ),
  213. ),
  214. );
  215. $tabs = $this->createGroup('default', $data);
  216. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  217. $this->drupalGet('node/' . $this->node->nid);
  218. // Test properties.
  219. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
  220. $this->assertFieldByXPath("//fieldset[contains(@class, 'test-class-2')]", NULL, t('Test class set on second tab'));
  221. $this->assertRaw('<div class="fieldset-description">description of second tab</div>', t('Description of tab is shown'));
  222. $this->assertRaw('class="collapsible collapsed test-class-2', t('Second tab is default collapsed'));
  223. // Test if correctly nested
  224. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@class, 'test-class')]", NULL, 'First tab is displayed as child of the wrapper.');
  225. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@class, 'test-class-2')]", NULL, 'Second tab is displayed as child of the wrapper.');
  226. }
  227. /**
  228. * Test the vertical tabs formatter.
  229. */
  230. function testVerticalTabs() {
  231. $data = array(
  232. 'label' => 'Tab 1',
  233. 'weight' => '1',
  234. 'children' => array(
  235. 0 => 'field_test',
  236. ),
  237. 'format_type' => 'tab',
  238. 'format_settings' => array(
  239. 'label' => 'Tab 1',
  240. 'instance_settings' => array(
  241. 'classes' => 'test-class',
  242. 'description' => '',
  243. ),
  244. 'formatter' => 'open',
  245. ),
  246. );
  247. $first_tab = $this->createGroup('default', $data);
  248. $first_tab_id = 'edit-' . $first_tab->group_name;
  249. $data = array(
  250. 'label' => 'Tab 2',
  251. 'weight' => '1',
  252. 'children' => array(
  253. 0 => 'field_test_2',
  254. ),
  255. 'format_type' => 'tab',
  256. 'format_settings' => array(
  257. 'label' => 'Tab 1',
  258. 'instance_settings' => array(
  259. 'classes' => 'test-class-2',
  260. 'description' => 'description of second tab',
  261. ),
  262. 'formatter' => 'closed',
  263. ),
  264. );
  265. $second_tab = $this->createGroup('default', $data);
  266. $second_tab_id = 'edit-' . $second_tab->group_name;
  267. $data = array(
  268. 'label' => 'Tabs',
  269. 'weight' => '1',
  270. 'children' => array(
  271. 0 => $first_tab->group_name,
  272. 1 => $second_tab->group_name,
  273. ),
  274. 'format_type' => 'tabs',
  275. 'format_settings' => array(
  276. 'label' => 'Tab 1',
  277. 'instance_settings' => array(
  278. 'classes' => 'test-class-wrapper',
  279. ),
  280. ),
  281. );
  282. $tabs = $this->createGroup('default', $data);
  283. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  284. $this->drupalGet('node/' . $this->node->nid);
  285. // Test properties.
  286. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
  287. $this->assertFieldByXPath("//fieldset[contains(@class, 'test-class-2')]", NULL, t('Test class set on second tab'));
  288. $this->assertRaw('<div class="fieldset-description">description of second tab</div>', t('Description of tab is shown'));
  289. $this->assertRaw('class="collapsible collapsed test-class-2', t('Second tab is default collapsed'));
  290. // Test if correctly nested
  291. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$first_tab_id')]", NULL, 'First tab is displayed as child of the wrapper.');
  292. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$second_tab_id')]", NULL, 'Second tab is displayed as child of the wrapper.');
  293. }
  294. /**
  295. * Test the accordion formatter.
  296. */
  297. function testAccordion() {
  298. $data = array(
  299. 'label' => 'Accordion item 1',
  300. 'weight' => '1',
  301. 'children' => array(
  302. 0 => 'field_test',
  303. ),
  304. 'format_type' => 'accordion-item',
  305. 'format_settings' => array(
  306. 'label' => 'Accordion item 1',
  307. 'instance_settings' => array(
  308. 'classes' => 'test-class',
  309. ),
  310. 'formatter' => 'closed',
  311. ),
  312. );
  313. $first_item = $this->createGroup('default', $data);
  314. $first_item_id = 'node_article_full_' . $first_item->group_name;
  315. $data = array(
  316. 'label' => 'Accordion item 2',
  317. 'weight' => '1',
  318. 'children' => array(
  319. 0 => 'field_test_2',
  320. ),
  321. 'format_type' => 'accordion-item',
  322. 'format_settings' => array(
  323. 'label' => 'Tab 2',
  324. 'instance_settings' => array(
  325. 'classes' => 'test-class-2',
  326. ),
  327. 'formatter' => 'open',
  328. ),
  329. );
  330. $second_item = $this->createGroup('default', $data);
  331. $second_item_id = 'node_article_full_' . $second_item->group_name;
  332. $data = array(
  333. 'label' => 'Accordion',
  334. 'weight' => '1',
  335. 'children' => array(
  336. 0 => $first_item->group_name,
  337. 1 => $second_item->group_name,
  338. ),
  339. 'format_type' => 'accordion',
  340. 'format_settings' => array(
  341. 'label' => 'Tab 1',
  342. 'instance_settings' => array(
  343. 'classes' => 'test-class-wrapper',
  344. 'effect' => 'bounceslide'
  345. ),
  346. ),
  347. );
  348. $accordion = $this->createGroup('default', $data);
  349. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  350. $this->drupalGet('node/' . $this->node->nid);
  351. // Test properties.
  352. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
  353. $this->assertFieldByXPath("//div[contains(@class, 'effect-bounceslide')]", NULL, t('Correct effect is set on the accordion'));
  354. $this->assertFieldByXPath("//div[contains(@class, 'test-class')]", NULL, t('Accordion item with test-class is shown'));
  355. $this->assertFieldByXPath("//div[contains(@class, 'test-class-2')]", NULL, t('Accordion item with test-class-2 is shown'));
  356. $this->assertFieldByXPath("//h3[contains(@class, 'field-group-accordion-active')]", NULL, t('Accordion item 2 was set active'));
  357. // Test if correctly nested
  358. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//div[contains(@class, 'test-class')]", NULL, 'First item is displayed as child of the wrapper.');
  359. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//div[contains(@class, 'test-class-2')]", NULL, 'Second item is displayed as child of the wrapper.');
  360. }
  361. }