/[drupal]/drupal/modules/simpletest/simpletest.install
ViewVC logotype

Contents of /drupal/modules/simpletest/simpletest.install

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


Revision 1.27 - (show annotations) (download) (as text)
Thu Sep 10 06:38:19 2009 UTC (2 months, 2 weeks ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, DRUPAL-7-0-UNSTABLE-9, HEAD
Changes since 1.26: +1 -5 lines
File MIME type: text/x-php
- Patch #306151 by agentrickard, David_Rothstein, Dave Reid, dbabbage, moshe weitzman: automatically install/uninstall schema.
1 <?php
2 // $Id: simpletest.install,v 1.26 2009/08/17 19:14:41 webchick Exp $
3
4 /**
5 * @file
6 * Install, update and uninstall functions for the simpletest module.
7 */
8
9 /**
10 * Implement hook_install().
11 */
12 function simpletest_install() {
13 // Check for files directory.
14 $path = 'public://simpletest';
15 if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
16 // Generate binary and text test files.
17 $generated = FALSE;
18 if (simpletest_get_file_count($path, 'binary') == 0) {
19 $lines = array(64, 1024);
20 foreach ($lines as $line) {
21 simpletest_generate_file('binary', 64, $line, 'binary');
22 }
23 $generated = TRUE;
24 }
25
26 if (simpletest_get_file_count($path, 'text') == 0) {
27 $lines = array(16, 256, 1024, 2048, 20480);
28 foreach ($lines as $line) {
29 simpletest_generate_file('text', 64, $line);
30 }
31 $generated = TRUE;
32 }
33
34 // Copy other test files for consistency.
35 $original = drupal_get_path('module', 'simpletest') . '/files';
36 $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
37
38 // If there are more files in SimpleTest's files directory than the site's
39 // files directory, restore all the files. This situation might occur when
40 // an errant test deletes one or more files from the site's files
41 // directory. It serves a convenience to developers so that they can get
42 // the test files back easily.
43 if (count($files) > count(file_scan_directory($path, '/(html|image|javascript|php|sql)-.*/'))) {
44 foreach ($files as $file) {
45 file_unmanaged_copy($file->uri, $path, FILE_EXISTS_REPLACE);
46 }
47 $generated = TRUE;
48 }
49
50 if ($generated) {
51 drupal_set_message('Extra test files generated/copied.');
52 }
53 }
54 }
55
56 /**
57 * Generate test file.
58 */
59 function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
60 $size = $width * $lines - $lines;
61
62 // Generate random text
63 $text = '';
64 for ($i = 0; $i < $size; $i++) {
65 switch ($type) {
66 case 'text':
67 $text .= chr(rand(32, 126));
68 break;
69 case 'binary':
70 $text .= chr(rand(0, 31));
71 break;
72 case 'binary-text':
73 default:
74 $text .= rand(0, 1);
75 break;
76 }
77 }
78 $text = wordwrap($text, $width - 1, "\n", TRUE) . "\n"; // Add \n for symetrical file.
79
80 // Create filename.
81 $path = file_directory_path() . '/simpletest/';
82 $count = simpletest_get_file_count($path, $filename);
83 file_put_contents($path . $filename . '-' . ($count + 1) . '.txt', $text);
84 }
85
86 /**
87 * Get the number of files that have the specified filename base.
88 */
89 function simpletest_get_file_count($directory, $filename) {
90 $files = scandir($directory);
91 $count = 0;
92 foreach ($files as $file) {
93 if (preg_match('/' . $filename . '.*?/', $file)) {
94 $count++;
95 }
96 }
97 return $count;
98 }
99
100 /**
101 * Implement hook_uninstall().
102 */
103 function simpletest_uninstall() {
104 simpletest_clean_environment();
105
106 // Remove settings variables.
107 variable_del('simpletest_username');
108 variable_del('simpletest_password');
109 variable_del('simpletest_clear_results');
110 variable_del('simpletest_verbose');
111
112 // Remove generated files.
113 $path = file_directory_path() . '/simpletest';
114 $files = file_scan_directory($path, '/.*/');
115 foreach ($files as $file) {
116 file_unmanaged_delete($file->uri);
117 }
118 rmdir($path);
119 }
120
121 /**
122 * Check that the cURL extension exists for PHP.
123 */
124 function simpletest_requirements($phase) {
125 $requirements = array();
126 $t = get_t();
127
128 $has_curl = function_exists('curl_init');
129 $has_hash = function_exists('hash_hmac');
130 $has_domdocument = class_exists('DOMDocument');
131
132 $requirements['curl'] = array(
133 'title' => $t('cURL'),
134 'value' => $has_curl ? $t('Enabled') : $t('Not found'),
135 );
136 if (!$has_curl) {
137 $requirements['curl']['severity'] = REQUIREMENT_ERROR;
138 $requirements['curl']['description'] = $t('Simpletest could not be installed because the PHP <a href="@curl_url">cURL</a> library is not available.', array('@curl_url' => 'http://php.net/manual/en/curl.setup.php'));
139 }
140 $requirements['hash'] = array(
141 'title' => $t('hash'),
142 'value' => $has_hash ? $t('Enabled') : $t('Not found'),
143 );
144 if (!$has_hash) {
145 $requirements['hash']['severity'] = REQUIREMENT_ERROR;
146 $requirements['hash']['description'] = $t('Simpletest could not be installed because the PHP <a href="@hash_url">hash</a> extension is disabled.', array('@hash_url' => 'http://php.net/manual/en/book.hash.php'));
147 }
148
149 $requirements['php_domdocument'] = array(
150 'title' => $t('PHP DOMDocument class'),
151 'value' => $has_domdocument ? $t('Enabled') : $t('Not found'),
152 );
153 if (!$has_domdocument) {
154 $requirements['php_domdocument']['severity'] = REQUIREMENT_ERROR;
155 $requirements['php_domdocument']['description'] =t('SimpleTest requires the DOMDocument class to be available. Please check the configure command at the <a href="@link-phpinfo">PHP info page</a>.', array('@link-phpinfo' => url('admin/reports/status/php')));
156 }
157
158 return $requirements;
159 }
160
161 function simpletest_schema() {
162 $schema['simpletest'] = array(
163 'description' => 'Stores simpletest messages',
164 'fields' => array(
165 'message_id' => array(
166 'type' => 'serial',
167 'not null' => TRUE,
168 'description' => 'Primary Key: Unique simpletest message ID.',
169 ),
170 'test_id' => array(
171 'type' => 'int',
172 'not null' => TRUE,
173 'default' => 0,
174 'description' => 'Test ID, messages belonging to the same ID are reported together',
175 ),
176 'test_class' => array(
177 'type' => 'varchar',
178 'length' => 255,
179 'not null' => TRUE,
180 'default' => '',
181 'description' => 'The name of the class that created this message.',
182 ),
183 'status' => array(
184 'type' => 'varchar',
185 'length' => 9,
186 'not null' => TRUE,
187 'default' => '',
188 'description' => 'Message status. Core understands pass, fail, exception.',
189 ),
190 'message' => array(
191 'type' => 'text',
192 'not null' => TRUE,
193 'description' => 'The message itself.',
194 ),
195 'message_group' => array(
196 'type' => 'varchar',
197 'length' => 255,
198 'not null' => TRUE,
199 'default' => '',
200 'description' => 'The message group this message belongs to. For example: warning, browser, user.',
201 ),
202 'function' => array(
203 'type' => 'varchar',
204 'length' => 255,
205 'not null' => TRUE,
206 'default' => '',
207 'description' => 'Name of the assertion function or method that created this message.',
208 ),
209 'line' => array(
210 'type' => 'int',
211 'not null' => TRUE,
212 'default' => 0,
213 'description' => 'Line number on which the function is called.',
214 ),
215 'file' => array(
216 'type' => 'varchar',
217 'length' => 255,
218 'not null' => TRUE,
219 'default' => '',
220 'description' => 'Name of the file where the function is called.',
221 ),
222 ),
223 'primary key' => array('message_id'),
224 'indexes' => array(
225 'reporter' => array('test_class', 'message_id'),
226 ),
227 );
228 $schema['simpletest_test_id'] = array(
229 'description' => 'Stores simpletest test IDs, used to auto-incrament the test ID so that a fresh test ID is used.',
230 'fields' => array(
231 'test_id' => array(
232 'type' => 'serial',
233 'not null' => TRUE,
234 'description' => 'Primary Key: Unique simpletest ID used to group test results together. Each time a set of tests
235 are run a new test ID is used.',
236 ),
237 'last_prefix' => array(
238 'type' => 'varchar',
239 'length' => 60,
240 'not null' => FALSE,
241 'default' => '',
242 'description' => 'The last database prefix used during testing.',
243 ),
244 ),
245 'primary key' => array('test_id'),
246 );
247 return $schema;
248 }

  ViewVC Help
Powered by ViewVC 1.1.2