field_group.display.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. $first_tab_id = 'node_article_full_' . $first_tab->group_name;
  184. $data = array(
  185. 'label' => 'Tab 2',
  186. 'weight' => '1',
  187. 'children' => array(
  188. 0 => 'field_test_2',
  189. ),
  190. 'format_type' => 'htab',
  191. 'format_settings' => array(
  192. 'label' => 'Tab 1',
  193. 'instance_settings' => array(
  194. 'classes' => 'test-class-2',
  195. 'description' => 'description of second tab',
  196. ),
  197. 'formatter' => 'closed',
  198. ),
  199. );
  200. $second_tab = $this->createGroup('default', $data);
  201. $second_tab_id = 'node_article_full_' . $first_tab->group_name;
  202. $data = array(
  203. 'label' => 'Tabs',
  204. 'weight' => '1',
  205. 'children' => array(
  206. 0 => $first_tab->group_name,
  207. 1 => $second_tab->group_name,
  208. ),
  209. 'format_type' => 'htabs',
  210. 'format_settings' => array(
  211. 'label' => 'Tab 1',
  212. 'instance_settings' => array(
  213. 'classes' => 'test-class-wrapper',
  214. ),
  215. ),
  216. );
  217. $tabs = $this->createGroup('default', $data);
  218. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  219. $this->drupalGet('node/' . $this->node->nid);
  220. // Test properties.
  221. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
  222. $this->assertFieldByXPath("//fieldset[contains(@class, 'test-class-2')]", NULL, t('Test class set on second tab'));
  223. $this->assertRaw('<div class="fieldset-description">description of second tab</div>', t('Description of tab is shown'));
  224. $this->assertRaw('class="collapsible collapsed test-class-2', t('Second tab is default collapsed'));
  225. // Test if correctly nested
  226. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$first_tab_id')]", NULL, 'First tab is displayed as child of the wrapper.');
  227. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$second_tab_id')]", NULL, 'Second tab is displayed as child of the wrapper.');
  228. }
  229. /**
  230. * Test the vertical tabs formatter.
  231. */
  232. function testVerticalTabs() {
  233. $data = array(
  234. 'label' => 'Tab 1',
  235. 'weight' => '1',
  236. 'children' => array(
  237. 0 => 'field_test',
  238. ),
  239. 'format_type' => 'tab',
  240. 'format_settings' => array(
  241. 'label' => 'Tab 1',
  242. 'instance_settings' => array(
  243. 'classes' => 'test-class',
  244. 'description' => '',
  245. ),
  246. 'formatter' => 'open',
  247. ),
  248. );
  249. $first_tab = $this->createGroup('default', $data);
  250. $first_tab_id = 'node_article_full_' . $first_tab->group_name;
  251. $data = array(
  252. 'label' => 'Tab 2',
  253. 'weight' => '1',
  254. 'children' => array(
  255. 0 => 'field_test_2',
  256. ),
  257. 'format_type' => 'tab',
  258. 'format_settings' => array(
  259. 'label' => 'Tab 1',
  260. 'instance_settings' => array(
  261. 'classes' => 'test-class-2',
  262. 'description' => 'description of second tab',
  263. ),
  264. 'formatter' => 'closed',
  265. ),
  266. );
  267. $second_tab = $this->createGroup('default', $data);
  268. $second_tab_id = 'node_article_full_' . $first_tab->group_name;
  269. $data = array(
  270. 'label' => 'Tabs',
  271. 'weight' => '1',
  272. 'children' => array(
  273. 0 => $first_tab->group_name,
  274. 1 => $second_tab->group_name,
  275. ),
  276. 'format_type' => 'tabs',
  277. 'format_settings' => array(
  278. 'label' => 'Tab 1',
  279. 'instance_settings' => array(
  280. 'classes' => 'test-class-wrapper',
  281. ),
  282. ),
  283. );
  284. $tabs = $this->createGroup('default', $data);
  285. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  286. $this->drupalGet('node/' . $this->node->nid);
  287. // Test properties.
  288. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
  289. $this->assertFieldByXPath("//fieldset[contains(@class, 'test-class-2')]", NULL, t('Test class set on second tab'));
  290. $this->assertRaw('<div class="fieldset-description">description of second tab</div>', t('Description of tab is shown'));
  291. $this->assertRaw('class="collapsible collapsed test-class-2', t('Second tab is default collapsed'));
  292. // Test if correctly nested
  293. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$first_tab_id')]", NULL, 'First tab is displayed as child of the wrapper.');
  294. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//fieldset[contains(@id, '$second_tab_id')]", NULL, 'Second tab is displayed as child of the wrapper.');
  295. }
  296. /**
  297. * Test the accordion formatter.
  298. */
  299. function testAccordion() {
  300. $data = array(
  301. 'label' => 'Accordion item 1',
  302. 'weight' => '1',
  303. 'children' => array(
  304. 0 => 'field_test',
  305. ),
  306. 'format_type' => 'accordion-item',
  307. 'format_settings' => array(
  308. 'label' => 'Accordion item 1',
  309. 'instance_settings' => array(
  310. 'classes' => 'test-class',
  311. ),
  312. 'formatter' => 'closed',
  313. ),
  314. );
  315. $first_item = $this->createGroup('default', $data);
  316. $first_item_id = 'node_article_full_' . $first_item->group_name;
  317. $data = array(
  318. 'label' => 'Accordion item 2',
  319. 'weight' => '1',
  320. 'children' => array(
  321. 0 => 'field_test_2',
  322. ),
  323. 'format_type' => 'accordion-item',
  324. 'format_settings' => array(
  325. 'label' => 'Tab 2',
  326. 'instance_settings' => array(
  327. 'classes' => 'test-class-2',
  328. ),
  329. 'formatter' => 'open',
  330. ),
  331. );
  332. $second_item = $this->createGroup('default', $data);
  333. $second_item_id = 'node_article_full_' . $second_item->group_name;
  334. $data = array(
  335. 'label' => 'Accordion',
  336. 'weight' => '1',
  337. 'children' => array(
  338. 0 => $first_item->group_name,
  339. 1 => $second_item->group_name,
  340. ),
  341. 'format_type' => 'accordion',
  342. 'format_settings' => array(
  343. 'label' => 'Tab 1',
  344. 'instance_settings' => array(
  345. 'classes' => 'test-class-wrapper',
  346. 'effect' => 'bounceslide'
  347. ),
  348. ),
  349. );
  350. $accordion = $this->createGroup('default', $data);
  351. $groups = field_group_info_groups('node', 'article', 'default', TRUE);
  352. $this->drupalGet('node/' . $this->node->nid);
  353. // Test properties.
  354. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]", NULL, t('Test class set on tabs wrapper'));
  355. $this->assertFieldByXPath("//div[contains(@class, 'effect-bounceslide')]", NULL, t('Correct effect is set on the accordion'));
  356. $this->assertFieldByXPath("//div[contains(@class, 'test-class')]", NULL, t('Accordion item with test-class is shown'));
  357. $this->assertFieldByXPath("//div[contains(@class, 'test-class-2')]", NULL, t('Accordion item with test-class-2 is shown'));
  358. $this->assertFieldByXPath("//h3[contains(@class, 'field-group-accordion-active')]", NULL, t('Accordion item 2 was set active'));
  359. // Test if correctly nested
  360. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//div[contains(@class, 'test-class')]", NULL, 'First item is displayed as child of the wrapper.');
  361. $this->assertFieldByXPath("//div[contains(@class, 'test-class-wrapper')]//div[contains(@class, 'test-class-2')]", NULL, 'Second item is displayed as child of the wrapper.');
  362. }
  363. }