features.test 9.8 KB

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