/[drupal]/contributions/modules/simpletest/switch/simpletest_switch.module
ViewVC logotype

Diff of /contributions/modules/simpletest/switch/simpletest_switch.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Tue Nov 10 02:30:08 2009 UTC revision 1.1.2.1, Tue Nov 10 02:30:08 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file
6     * Provides an easy method to switch between 1.x and 2.x testing APIs.
7     */
8    
9    /**
10     * Implement hook_menu().
11     */
12    function simpletest_switch_menu() {
13      $items = array();
14    
15      $items['admin/config/development/testing/switch'] = array(
16        'title callback' => 'simpletest_switch_title',
17        'page callback' => 'simpletest_switch_invoke',
18        'page arguments' => array(TRUE),
19        'access arguments' => array('administer unit tests'),
20        'type' => MENU_LOCAL_TASK,
21        'weight' => 10,
22      );
23      $items['admin/config/development/testing/switch-second'] = array(
24        'page callback' => 'simpletest_switch_invoke',
25        'access arguments' => array('administer unit tests'),
26        'type' => MENU_CALLBACK,
27      );
28    
29      return $items;
30    }
31    
32    /**
33     * Get the current testing API version.
34     *
35     * @return Current testing API version, either: '1.x' or '2.x'.
36     */
37    function simpletest_switch_current() {
38      return variable_get('testing_api', '2.x');
39    }
40    
41    /**
42     * Get the switch callback title.
43     *
44     * @return unknown
45     */
46    function simpletest_switch_title() {
47      return t('Switch to @version', array('@version' => simpletest_switch_current() == '2.x' ? '1.x' : '2.x'));
48    }
49    
50    /**
51     * Clear Drupal caches and change active testing API version.
52     *
53     * Due to the complexity of the switch this function must be called twice. The
54     * first time simply to change the testing API version and the second to
55     * complete the switch.
56     *
57     * @param boolean $switch Switch the active testing API version.
58     */
59    function simpletest_switch_invoke($switch = FALSE) {
60      // If specified switch test API version.
61      if ($switch) {
62        variable_set('testing_api', simpletest_switch_current() == '2.x' ? '1.x' : '2.x');
63      }
64    
65      // Use the built in system form to ensure that any addon modules are cleared.
66      module_load_include('admin.inc', 'system');
67      $form_state = array();
68      $form_state['values'] = array('op' => t('Clear all caches'));
69      drupal_form_submit('system_performance_settings', $form_state);
70    
71      // Hide the cache clearing messages.
72      drupal_get_messages();
73    
74      // Last iteration so add a display message.
75      if (!$switch) {
76        drupal_set_message(t('Testing API switched to @version.', array('@version' => simpletest_switch_current())));
77      }
78    
79      // First time: goto second menu callback, second time: goto testing list.
80      drupal_goto($switch ? 'admin/config/development/testing/switch-second': 'admin/config/development/testing');
81    }
82    
83    /**
84     * Implement hook_registry_files_alter().
85     */
86    function simpletest_switch_registry_files_alter(&$files, $modules) {
87      if (simpletest_switch_current() == '1.x') {
88        foreach ($files as $key => $file) {
89          $key_new = preg_replace('/.*?\/simpletest\//', 'modules/simpletest/', $key);
90    
91          // If the string replace had an effect then remove the old file and copy
92          // its definition to the new key.
93          if ($key_new != $key) {
94            if (file_exists($key_new)) {
95              $files[$key_new] = $file;
96            }
97            unset($files[$key]);
98          }
99        }
100      }
101    }
102    
103    /**
104     * Implement hook_system_modules_alter().
105     */
106    function simpletest_switch_system_modules_alter(&$modules) {
107      if (simpletest_switch_current() == '1.x') {
108        $modules['simpletest']->uri = 'modules/simpletest/simpletest.module';
109      }
110    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2