features.test 9.9 KB

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