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' => 'field', 'filter' => 'filter', 'image' => 'image', 'node' => 'node', 'user_permission' => 'user_permission', 'views' => 'views', ), 'module_exists'); foreach (array_keys($components) as $component) { $callback = "_test_{$component}"; // Ensure that the component/default is properly available. $object = $this->$callback('load', $id); $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 ($components as $component) { features_revert(array('features_test' => array($component))); } drupal_flush_all_caches(); foreach ($components as $component) { $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($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', &$test) { 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($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(); break; } } protected function _test_user_permission($op = 'load') { switch ($op) { case 'load': $permissions = user_role_permissions(array(DRUPAL_AUTHENTICATED_RID)); 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; } } }