update_test.module 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Module for testing Update Manager functionality.
  5. */
  6. /**
  7. * Implements hook_system_theme_info().
  8. */
  9. function update_test_system_theme_info() {
  10. $themes['update_test_basetheme'] = drupal_get_path('module', 'update_test') . '/themes/update_test_basetheme/update_test_basetheme.info';
  11. $themes['update_test_subtheme'] = drupal_get_path('module', 'update_test') . '/themes/update_test_subtheme/update_test_subtheme.info';
  12. return $themes;
  13. }
  14. /**
  15. * Implements hook_menu().
  16. */
  17. function update_test_menu() {
  18. $items = array();
  19. $items['update-test'] = array(
  20. 'title' => t('Update test'),
  21. 'page callback' => 'update_test_mock_page',
  22. 'access callback' => TRUE,
  23. 'type' => MENU_CALLBACK,
  24. );
  25. $items['503-error'] = array(
  26. 'title' => t('503 Service unavailable'),
  27. 'page callback' => 'update_callback_service_unavailable',
  28. 'access callback' => TRUE,
  29. 'type' => MENU_CALLBACK,
  30. );
  31. return $items;
  32. }
  33. /**
  34. * Implements hook_system_info_alter().
  35. *
  36. * Checks the 'update_test_system_info' variable and sees if we need to alter
  37. * the system info for the given $file based on the setting. The setting is
  38. * expected to be a nested associative array. If the key '#all' is defined, its
  39. * subarray will include .info keys and values for all modules and themes on the
  40. * system. Otherwise, the settings array is keyed by the module or theme short
  41. * name ($file->name) and the subarrays contain settings just for that module or
  42. * theme.
  43. */
  44. function update_test_system_info_alter(&$info, $file) {
  45. $setting = variable_get('update_test_system_info', array());
  46. foreach (array('#all', $file->name) as $id) {
  47. if (!empty($setting[$id])) {
  48. foreach ($setting[$id] as $key => $value) {
  49. $info[$key] = $value;
  50. }
  51. }
  52. }
  53. }
  54. /**
  55. * Implements hook_update_status_alter().
  56. *
  57. * Checks the 'update_test_update_status' variable and sees if we need to alter
  58. * the update status for the given project based on the setting. The setting is
  59. * expected to be a nested associative array. If the key '#all' is defined, its
  60. * subarray will include .info keys and values for all modules and themes on the
  61. * system. Otherwise, the settings array is keyed by the module or theme short
  62. * name and the subarrays contain settings just for that module or theme.
  63. */
  64. function update_test_update_status_alter(&$projects) {
  65. $setting = variable_get('update_test_update_status', array());
  66. if (!empty($setting)) {
  67. foreach ($projects as $project_name => &$project) {
  68. foreach (array('#all', $project_name) as $id) {
  69. if (!empty($setting[$id])) {
  70. foreach ($setting[$id] as $key => $value) {
  71. $project[$key] = $value;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * Page callback: Prints mock XML for the Update Manager module.
  80. *
  81. * The specific XML file to print depends on two things: the project we're
  82. * trying to fetch data for, and the desired "availability scenario" for that
  83. * project which we're trying to test. Before attempting to fetch this data (by
  84. * checking for updates on the available updates report), callers need to define
  85. * the 'update_test_xml_map' variable as an array, keyed by project name,
  86. * indicating which availability scenario to use for that project.
  87. *
  88. * @param $project_name
  89. * The project short name the update manager is trying to fetch data for (the
  90. * fetch URLs are of the form: [base_url]/[project_name]/[core_version]).
  91. *
  92. * @see update_test_menu()
  93. */
  94. function update_test_mock_page($project_name) {
  95. $xml_map = variable_get('update_test_xml_map', FALSE);
  96. if (isset($xml_map[$project_name])) {
  97. $availability_scenario = $xml_map[$project_name];
  98. }
  99. elseif (isset($xml_map['#all'])) {
  100. $availability_scenario = $xml_map['#all'];
  101. }
  102. else {
  103. // The test didn't specify (for example, the webroot has other modules and
  104. // themes installed but they're disabled by the version of the site
  105. // running the test. So, we default to a file we know won't exist, so at
  106. // least we'll get an empty page from readfile instead of a bunch of
  107. // Drupal page output.
  108. $availability_scenario = '#broken#';
  109. }
  110. $path = drupal_get_path('module', 'update_test');
  111. readfile("$path/$project_name.$availability_scenario.xml");
  112. }
  113. /**
  114. * Implements hook_archiver_info().
  115. */
  116. function update_test_archiver_info() {
  117. return array(
  118. 'update_test_archiver' => array(
  119. // This is bogus, we only care about the extensions for now.
  120. 'class' => 'ArchiverUpdateTest',
  121. 'extensions' => array('update-test-extension'),
  122. ),
  123. );
  124. }
  125. /**
  126. * Implements hook_filetransfer_info().
  127. */
  128. function update_test_filetransfer_info() {
  129. // Define a mock file transfer method, to ensure that there will always be
  130. // at least one method available in the user interface (regardless of the
  131. // environment in which the update manager tests are run).
  132. return array(
  133. 'system_test' => array(
  134. 'title' => t('Update Test FileTransfer'),
  135. // This should be in an .inc file, but for testing purposes, it is OK to
  136. // leave it in the main module file.
  137. 'file' => 'update_test.module',
  138. 'class' => 'UpdateTestFileTransfer',
  139. 'weight' => -20,
  140. ),
  141. );
  142. }
  143. /**
  144. * Mocks a FileTransfer object to test the settings form functionality.
  145. */
  146. class UpdateTestFileTransfer {
  147. /**
  148. * Returns an UpdateTestFileTransfer object.
  149. *
  150. * @return
  151. * A new UpdateTestFileTransfer object.
  152. */
  153. public static function factory() {
  154. return new UpdateTestFileTransfer;
  155. }
  156. /**
  157. * Returns a settings form with a text field to input a username.
  158. */
  159. public function getSettingsForm() {
  160. $form = array();
  161. $form['udpate_test_username'] = array(
  162. '#type' => 'textfield',
  163. '#title' => t('Update Test Username'),
  164. );
  165. return $form;
  166. }
  167. }
  168. /**
  169. * Page callback: Displays an Error 503 (Service unavailable) page.
  170. *
  171. * @see update_test_menu()
  172. */
  173. function update_callback_service_unavailable() {
  174. drupal_add_http_header('Status', '503 Service unavailable');
  175. print "503 Service Temporarily Unavailable";
  176. }