update_test.module

  1. drupal
    1. 7 modules/update/tests/update_test.module
    2. 8 core/modules/update/tests/modules/update_test/update_test.module

Classes

NameDescription
UpdateTestFileTransferMock FileTransfer object to test the settings form functionality.

Functions & methods

NameDescription
update_callback_service_unavailableReturn an Error 503 (Service unavailable) page.
update_test_archiver_infoImplement hook_archiver_info().
update_test_filetransfer_infoImplements hook_filetransfer_info().
update_test_menuImplements hook_menu().
update_test_mock_pagePage callback, prints mock XML for the update module.
update_test_system_info_alterImplements hook_system_info_alter().
update_test_system_theme_infoImplements hook_system_theme_info().
update_test_update_status_alterImplements hook_update_status_alter().

File

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