features.test
8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* User permission component tests for Features
*/
class FeaturesUserTestCase extends DrupalWebTestCase {
protected $profile = 'testing';
/**
* Test info.
*/
public static function getInfo() {
return array(
'name' => t('Component tests'),
'description' => t('Run tests for components of Features.') ,
'group' => t('Features'),
);
}
/**
* Set up test.
*/
public function setUp() {
parent::setUp(array(
'field',
'filter',
'image',
'taxonomy',
'views',
'features',
'features_test'
));
// Run a features rebuild to ensure our feature is fully installed.
features_rebuild();
$admin_user = $this->drupalCreateUser(array('administer features'));
$this->drupalLogin($admin_user);
}
/**
* Run test.
*/
public function test() {
module_load_include('inc', 'features', 'features.export');
$components = array_filter(array(
'field_instance' => 'field',
'filter' => 'filter',
'image' => 'image',
'node' => 'node',
'user_permission' => 'user',
'views_view' => 'views',
), 'module_exists');
foreach (array_keys($components) as $component) {
$callback = "_test_{$component}";
// Ensure that the component/default is properly available.
$object = $this->$callback('load');
$this->assertTrue(!empty($object), t('@component present.', array('@component' => $component)));
// Ensure that the component is defaulted.
$states = features_get_component_states(array('features_test'), FALSE, TRUE);
$this->assertTrue($states['features_test'][$component] === FEATURES_DEFAULT, t('@component state: Default.', array('@component' => $component)));
// Override component and test that Features detects the override.
$this->$callback('override', $this);
$states = features_get_component_states(array('features_test'), FALSE, TRUE);
$this->assertTrue($states['features_test'][$component] === FEATURES_OVERRIDDEN, t('@component state: Overridden.', array('@component' => $component)));
}
// Revert component and ensure that component has reverted.
// Do this in separate loops so we only have to run
// drupal_flush_all_caches() once.
foreach (array_keys($components) as $component) {
features_revert(array('features_test' => array($component)));
}
drupal_flush_all_caches();
foreach (array_keys($components) as $component) {
// Reload so things like Views can clear it's cache
$this->$callback('load');
$states = features_get_component_states(array('features_test'), FALSE, TRUE);
$this->assertTrue($states['features_test'][$component] === FEATURES_DEFAULT, t('@component reverted.', array('@component' => $component)));
}
}
protected function _test_field_instance($op = 'load') {
switch ($op) {
case 'load':
return field_info_instance('node', 'field_features_test', 'features_test');
case 'override':
$field_instance = field_info_instance('node', 'field_features_test', 'features_test');
$field_instance['label'] = 'Foo bar';
field_update_instance($field_instance);
break;
}
}
protected function _test_filter($op = 'load') {
// So... relying on our own API functions to test is pretty lame.
// But these modules don't have APIs either. So might as well use
// the ones we've written for them...
features_include();
switch ($op) {
case 'load':
return features_filter_format_load('features_test');
case 'override':
$format = features_filter_format_load('features_test');
unset($format->filters['filter_url']);
filter_format_save($format);
break;
}
}
protected function _test_image($op = 'load') {
switch ($op) {
case 'load':
return image_style_load('features_test');
case 'override':
$style = image_style_load('features_test');
$style = image_style_save($style);
foreach ($style['effects'] as $effect) {
$effect['data']['width'] = '120';
image_effect_save($effect);
}
break;
}
}
protected function _test_node($op = 'load') {
switch ($op) {
case 'load':
return node_type_get_type('features_test');
case 'override':
$type = node_type_get_type('features_test');
$type->description = 'Foo bar baz.';
$type->modified = TRUE;
node_type_save($type);
break;
}
}
protected function _test_views_view($op = 'load') {
switch ($op) {
case 'load':
return views_get_view('features_test', TRUE);
case 'override':
$view = views_get_view('features_test', TRUE);
$view->set_display('default');
$view->display_handler->override_option('title', 'Foo bar');
$view->save();
// Clear the load cache from above
views_get_view('features_test', TRUE);
break;
}
}
protected function _test_user_permission($op = 'load') {
switch ($op) {
case 'load':
$permissions = user_role_permissions(array(DRUPAL_AUTHENTICATED_RID => 'authenticated user'));
return !empty($permissions[DRUPAL_AUTHENTICATED_RID]['create features_test content']);
case 'override':
user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array('create features_test content' => 0));
break;
}
}
}
/**
* Tests enabling of feature modules.
*/
class FeaturesEnableTestCase extends DrupalWebTestCase {
protected $profile = 'testing';
/**
* Test info.
*/
public static function getInfo() {
return array(
'name' => t('Features enable tests'),
'description' => t('Run tests for enabling of features.') ,
'group' => t('Features'),
);
}
/**
* Run test for features_get_components on enable.
*/
public function testFeaturesGetComponents() {
// Testing that features_get_components returns correct after enable.
$modules = array(
'features',
'taxonomy',
'features_test',
);
// Make sure features_get_components is cached if features already enabled.
if (!module_exists('features')) {
drupal_load('module', 'features');
}
features_get_components();
module_enable($modules);
// Make sure correct information for enabled modules is now cached.
$components = features_get_components();
$taxonomy_component_info = taxonomy_features_api();
$this->assertTrue(!empty($components['taxonomy']) && $components['taxonomy'] == $taxonomy_component_info['taxonomy'], 'features_get_components returns correct taxonomy information on enable');
features_rebuild();
$this->assertNotNull(taxonomy_vocabulary_machine_name_load('taxonomy_features_test'), 'Taxonomy vocabulary correctly enabled on enable.');
}
}
/**
* Tests intergration of ctools for features.
*/
class FeaturesCtoolsIntegrationTest extends DrupalWebTestCase {
protected $profile = 'testing';
/**
* Test info.
*/
public static function getInfo() {
return array(
'name' => t('Features Chaos Tools integration'),
'description' => t('Run tests for ctool integration of features.') ,
'group' => t('Features'),
);
}
/**
* Set up test.
*/
public function setUp() {
parent::setUp(array(
'features',
'ctools',
));
}
/**
* Run test.
*/
public function testModuleEnable() {
$try = array(
'strongarm',
'views',
);
// Trigger the first includes and the static to be set.
features_include();
$function_ends = array(
'features_export',
'features_export_options',
'features_export_render',
'features_revert',
);
foreach ($try as $module) {
$function = $module . '_features_api';
$this->assertFalse(function_exists($function), 'Chaos tools functions for ' . $module . ' do not exist while it is disabled.');
// Module enable will trigger declaring the new functions.
module_enable(array($module));
}
// CTools hooks only created when there is an actual feature exportable
// enabled.
module_enable(array('features_test'));
foreach ($try as $module) {
if (module_exists($module)) {
$function_exists = function_exists($function);
if ($function_exists) {
foreach ($function() as $component_type => $component_info) {
foreach ($function_ends as $function_end) {
$function_exists = $function_exists && function_exists($component_type . '_' . $function_end);
}
}
}
$this->assertTrue($function_exists, 'Chaos tools functions for ' . $module . ' exist when it is enabled.');
}
}
}
}