update_test.module 6.1 KB

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