summaryrefslogtreecommitdiff
path: root/modules/simpletest/simpletest.install
blob: 4287b2a5dc75476db0fa27a87949f2763a1c8c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
// $Id$

/**
 * Implementation of hook_install().
 */
function simpletest_install() {
  // Check for files directory.
  $path = file_directory_path() . '/simpletest';
  if (file_check_directory($path, FILE_CREATE_DIRECTORY)) {
    // Generate binary and text test files.
    $generated = FALSE;
    if (simpletest_get_file_count($path, 'binary') == 0) {
      $lines = array(64, 1024);
      foreach ($lines as $line) {
        simpletest_generate_file('binary', 64, $line, 'binary');
      }
      $generated = TRUE;
    }

    if (simpletest_get_file_count($path, 'text') == 0) {
      $lines = array(16, 256, 1024, 2048, 20480);
      foreach ($lines as $line) {
        simpletest_generate_file('text', 64, $line);
      }
      $generated = TRUE;
    }

    // Copy other test files for consistency.
    $files = file_scan_directory($path, '(html|image|javascript|php|sql)-.*');
    if (count($files) == 0) {
      $original = drupal_get_path('module', 'simpletest') .'/files';
      $files = file_scan_directory($original, '(html|image|javascript|php|sql)-.*');
      foreach ($files as $file) {
        file_copy($file->filename, $path .'/'. $file->basename);
      }
      $generated = TRUE;
    }

    if ($generated) {
      drupal_set_message('Extra test files generated.');
    }
  }
}

/**
 * Generate test file.
 */
function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
  $size = $width * $lines - $lines;

  // Generate random text
  $text = '';
  for ($i = 0; $i < $size; $i++) {
    switch ($type) {
      case 'text':
        $text .= chr(rand(32, 126));
        break;
      case 'binary':
        $text .= chr(rand(0, 31));
        break;
      case 'binary-text':
      default:
        $text .= rand(0, 1);
        break;
    }
  }
  $text = wordwrap($text, $width - 1, "\n", TRUE) ."\n"; // Add \n for symetrical file.

  // Create filename.
  $path = file_directory_path() . '/simpletest/';
  $count = simpletest_get_file_count($path, $filename);
  file_put_contents($path . $filename .'-'. ($count + 1) .'.txt', $text);
}

/**
 * Get the number of files that have the specified filename base.
 */
function simpletest_get_file_count($directory, $filename) {
  $files = scandir($directory);
  $count = 0;
  foreach ($files as $file) {
    if (preg_match('/'. $filename .'.*?/', $file)) {
      $count++;
    }
  }
  return $count;
}

/**
 * Implementation of hook_uninstall().
 */
function simpletest_uninstall() {
  variable_del('simpletest_httpauth');
  variable_del('simpletest_httpauth_username');
  variable_del('simpletest_httpauth_pass');
  variable_del('simpletest_devel');
}