1 | <?php |
---|
2 | |
---|
3 | module_load_include('test', 'content', 'tests/content.crud'); |
---|
4 | |
---|
5 | class FieldgroupCRUDTest extends ContentCrudTestCase { |
---|
6 | var $last_group = NULL; |
---|
7 | var $next_group_n = 1; |
---|
8 | |
---|
9 | /** |
---|
10 | * Enable fieldgroup module. |
---|
11 | */ |
---|
12 | function setUp() { |
---|
13 | $args = func_get_args(); |
---|
14 | $modules = array_merge(array('fieldgroup'), $args); |
---|
15 | call_user_func_array(array('parent','setUp'), $modules); |
---|
16 | } |
---|
17 | |
---|
18 | /** |
---|
19 | * Creates a fieldgroup with a predictable name. Also makes all future calls to functions |
---|
20 | * which take an optional fieldgroup use this one as the default. |
---|
21 | * @param $settings Array to be passed to fieldgroup_save_group. |
---|
22 | * @param $content_type Either a content type object, or the index of an acquired content type |
---|
23 | * @return The newly created fieldgroup. |
---|
24 | */ |
---|
25 | function createGroup($settings, $fields = array(), $content_type = 0) { |
---|
26 | if (is_numeric($content_type) && isset($this->content_types[$content_type])) { |
---|
27 | $content_type = $this->content_types[$content_type]; |
---|
28 | } |
---|
29 | $type_name = $content_type->type; |
---|
30 | $group_name = 'simpletest_g'. $this->next_group_n++; |
---|
31 | |
---|
32 | $defaults = array( |
---|
33 | 'group_name' => $group_name, |
---|
34 | 'group_type' => 'standard', |
---|
35 | 'parent' => '', |
---|
36 | 'label' => $group_name, |
---|
37 | 'weight' => -3, |
---|
38 | 'settings' => array( |
---|
39 | 'form' => array( |
---|
40 | 'style' => 'fieldset', |
---|
41 | 'description' => '', |
---|
42 | ), |
---|
43 | 'display' => array( |
---|
44 | 'weight' => -3, |
---|
45 | 'parent' => '', |
---|
46 | 'label' => 'above', |
---|
47 | 'description' => '', |
---|
48 | 'teaser' => array( |
---|
49 | 'format' => 'fieldset', |
---|
50 | 'exclude' => 0, |
---|
51 | ), |
---|
52 | 'full' => array( |
---|
53 | 'format' => 'fieldset', |
---|
54 | 'exclude' => 0, |
---|
55 | ), |
---|
56 | ), |
---|
57 | ), |
---|
58 | ); |
---|
59 | $group = $settings + $defaults; |
---|
60 | fieldgroup_save_group($type_name, $group); |
---|
61 | $this->last_group = $group_name; |
---|
62 | |
---|
63 | foreach ($fields as $field_name) { |
---|
64 | $form_values = array( |
---|
65 | 'field_name' => $field_name, |
---|
66 | 'group' => $group_name, |
---|
67 | 'type_name' => $type_name, |
---|
68 | ); |
---|
69 | fieldgroup_update_fields($form_values); |
---|
70 | } |
---|
71 | cache_clear_all('fieldgroup_data:', content_cache_tablename(), TRUE); |
---|
72 | return $this->last_group; |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
77 | class testFieldgroupFields extends FieldgroupCRUDTest { |
---|
78 | public static function getInfo() { |
---|
79 | return array( |
---|
80 | 'name' => t('Fieldgroups'), |
---|
81 | 'description' => t('Test creating fields in fieldgroups.'), |
---|
82 | 'group' => t('CCK'), |
---|
83 | ); |
---|
84 | } |
---|
85 | |
---|
86 | function setUp() { |
---|
87 | parent::setUp(); |
---|
88 | $this->loginWithPermissions(); |
---|
89 | $this->acquireContentTypes(1); |
---|
90 | } |
---|
91 | |
---|
92 | function testFieldgroup() { |
---|
93 | |
---|
94 | // Create a content type with a multivalue text field. |
---|
95 | $type = $this->content_types[0]; |
---|
96 | $type_name = $type->type; |
---|
97 | $type_url = str_replace('_', '-', $type_name); |
---|
98 | $admin_type_url = 'admin/content/node-type/'. $type_url; |
---|
99 | $field = $this->createFieldText(array('text_processing' => 0, 'multiple' => 1, 'weight' => -4)); |
---|
100 | $field_name = $field['field_name']; |
---|
101 | $field2 = $this->createFieldText(array('text_processing' => 0, 'multiple' => 1, 'weight' => -3)); |
---|
102 | $field_name2 = $field2['field_name']; |
---|
103 | $fields = array($field_name, $field_name2); |
---|
104 | |
---|
105 | // Create a fieldgroup and nest the fields in it. |
---|
106 | $settings = array( |
---|
107 | 'type_name' => $type_name, |
---|
108 | 'group_type' => 'standard', |
---|
109 | 'parent' => '', |
---|
110 | ); |
---|
111 | $group_name = $this->createGroup($settings, $fields); |
---|
112 | |
---|
113 | // Check that our new group shows up on the administration pages. |
---|
114 | $this->drupalGet($admin_type_url .'/groups/'. $group_name); |
---|
115 | $this->drupalGet($admin_type_url .'/fields'); |
---|
116 | $this->assertText($group_name, 'Group name displayed'); |
---|
117 | $this->drupalGet($admin_type_url .'/display'); |
---|
118 | $this->assertText($group_name, 'Group name displayed'); |
---|
119 | |
---|
120 | // Create a node with 2 values set in each of 2 fields. |
---|
121 | $value1 = $this->randomName(5); |
---|
122 | $value2 = $this->randomName(5); |
---|
123 | $value3 = $this->randomName(5); |
---|
124 | $value4 = $this->randomName(5); |
---|
125 | $edit = array( |
---|
126 | 'title' => $this->randomName(20), |
---|
127 | 'body' => $this->randomName(20), |
---|
128 | 'type' => $type_name, |
---|
129 | ); |
---|
130 | $edit[$field_name][0]['value'] = $value1; |
---|
131 | $edit[$field_name][1]['value'] = $value2; |
---|
132 | $edit[$field_name2][0]['value'] = $value3; |
---|
133 | $edit[$field_name2][1]['value'] = $value4; |
---|
134 | |
---|
135 | $node = $this->drupalCreateNode($edit); |
---|
136 | |
---|
137 | $this->drupalGet('node/'. $node->nid); |
---|
138 | $this->assertText($value1, 'First value displayed'); |
---|
139 | $this->assertText($value2, 'Second value displayed'); |
---|
140 | $this->assertText($value3, 'Third value displayed'); |
---|
141 | $this->assertText($value4, 'Fourth value displayed'); |
---|
142 | $this->assertText($group_name, 'Group name displayed'); |
---|
143 | |
---|
144 | // Create a second fieldgroup and nest the first fieldgroup inside the second one. |
---|
145 | $settings = array( |
---|
146 | 'type_name' => $type_name, |
---|
147 | 'group_type' => 'standard', |
---|
148 | 'parent' => '', |
---|
149 | ); |
---|
150 | $group_name2 = $this->createGroup($settings, array()); |
---|
151 | |
---|
152 | // Check that our new group shows up on the administration pages. |
---|
153 | $this->drupalGet($admin_type_url .'/groups/'. $group_name2); |
---|
154 | $this->drupalGet($admin_type_url .'/fields'); |
---|
155 | $this->assertText($group_name2, 'Group name 2 displayed'); |
---|
156 | $this->drupalGet($admin_type_url .'/display'); |
---|
157 | $this->assertText($group_name2, 'Group name 2 displayed'); |
---|
158 | |
---|
159 | // Set the first group to use the second group as a parent. |
---|
160 | $groups = fieldgroup_groups($type->name); |
---|
161 | $settings = $groups[$group_name]; |
---|
162 | $settings['parent'] = $group_name2; |
---|
163 | $group_name = $this->createGroup($settings, $fields); |
---|
164 | |
---|
165 | // Clear the content cache to re-generate the node display. |
---|
166 | cache_clear_all('content:', content_cache_tablename(), TRUE); |
---|
167 | |
---|
168 | // Checking that the outside group is displayed is sufficient to tell us they got nested, |
---|
169 | // because it has no fields and empty groups are not displayed. |
---|
170 | |
---|
171 | $this->drupalGet('node/'. $node->nid); |
---|
172 | $this->assertText($value1, 'First value displayed'); |
---|
173 | $this->assertText($value2, 'Second value displayed'); |
---|
174 | $this->assertText($value3, 'Third value displayed'); |
---|
175 | $this->assertText($value4, 'Fourth value displayed'); |
---|
176 | $this->assertText($group_name, 'Group name displayed'); |
---|
177 | $this->assertText($group_name2, 'Group name 2 displayed'); |
---|
178 | |
---|
179 | } |
---|
180 | } |
---|