Projet

Général

Profil

Paste
Télécharger (4,66 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / registry.test @ 76597ebf

1
<?php
2

    
3
class RegistryParseFileTestCase extends DrupalWebTestCase {
4
  public static function getInfo() {
5
    return array(
6
      'name' => 'Registry parse file test',
7
      'description' => 'Parse a simple file and check that its resources are saved to the database.',
8
      'group' => 'System'
9
    );
10
  }
11

    
12
  function setUp() {
13
    $chrs = hash('sha256', microtime() . mt_rand());
14
    $this->fileName = 'registry_test_' . substr($chrs, 0, 16);
15
    $this->className = 'registry_test_class' . substr($chrs, 16, 16);
16
    $this->interfaceName = 'registry_test_interface' . substr($chrs, 32, 16);
17
    parent::setUp();
18
  }
19

    
20
  /**
21
   * testRegistryParseFile
22
   */
23
  function testRegistryParseFile() {
24
    _registry_parse_file($this->fileName, $this->getFileContents());
25
    foreach (array('className', 'interfaceName') as $resource) {
26
      $foundName = db_query('SELECT name FROM {registry} WHERE name = :name', array(':name' => $this->$resource))->fetchField();
27
      $this->assertTrue($this->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$resource)));
28
    }
29
  }
30

    
31
  /**
32
   * getFileContents
33
   */
34
  function getFileContents() {
35
    $file_contents = <<<CONTENTS
36
<?php
37

    
38
class {$this->className} {}
39

    
40
interface {$this->interfaceName} {}
41

    
42
CONTENTS;
43
    return $file_contents;
44
  }
45

    
46
}
47

    
48
class RegistryParseFilesTestCase extends DrupalWebTestCase {
49
  protected $fileTypes = array('new', 'existing_changed');
50

    
51
  public static function getInfo() {
52
    return array(
53
      'name' => 'Registry parse files test',
54
      'description' => 'Read two a simple files from disc, and check that their resources are saved to the database.',
55
      'group' => 'System'
56
    );
57
  }
58

    
59
  function setUp() {
60
    parent::setUp();
61
    // Create files with some php to parse - one 'new', one 'existing' so
62
    // we test all the important code paths in _registry_parse_files.
63
    foreach ($this->fileTypes as $fileType) {
64
      $chrs = hash('sha256', microtime() . mt_rand());
65
      $this->$fileType = new stdClass();
66
      $this->$fileType->fileName = 'public://registry_test_' . substr($chrs, 0, 16);
67
      $this->$fileType->className = 'registry_test_class' . substr($chrs, 16, 16);
68
      $this->$fileType->interfaceName = 'registry_test_interface' . substr($chrs, 32, 16);
69
      $this->$fileType->contents = $this->getFileContents($fileType);
70
      file_save_data($this->$fileType->contents, $this->$fileType->fileName);
71

    
72
      if ($fileType == 'existing_changed') {
73
        // Add a record with an incorrect hash.
74
        $this->$fileType->fakeHash = hash('sha256', mt_rand());
75
        db_insert('registry_file')
76
          ->fields(array(
77
            'hash' => $this->$fileType->fakeHash,
78
            'filename' => $this->$fileType->fileName,
79
          ))
80
          ->execute();
81

    
82
        // Insert some fake resource records.
83
        foreach (array('class', 'interface') as $type) {
84
          db_insert('registry')
85
            ->fields(array(
86
              'name' => $type . hash('sha256', microtime() . mt_rand()),
87
              'type' => $type,
88
              'filename' => $this->$fileType->fileName,
89
            ))
90
            ->execute();
91
        }
92
      }
93
    }
94
  }
95

    
96
  /**
97
   * testRegistryParseFiles
98
   */
99
  function testRegistryParseFiles() {
100
    _registry_parse_files($this->getFiles());
101
    foreach ($this->fileTypes as $fileType) {
102
      // Test that we have all the right resources.
103
      foreach (array('className', 'interfaceName') as $resource) {
104
        $foundName = db_query('SELECT name FROM {registry} WHERE name = :name', array(':name' => $this->$fileType->$resource))->fetchField();
105
        $this->assertTrue($this->$fileType->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$fileType->$resource)));
106
      }
107
      // Test that we have the right hash.
108
      $hash = db_query('SELECT hash FROM {registry_file} WHERE filename = :filename', array(':filename' => $this->$fileType->fileName))->fetchField();
109
      $this->assertTrue(hash('sha256', $this->$fileType->contents) == $hash, t('sha-256 for "@filename" matched.' . $fileType . $hash, array('@filename' => $this->$fileType->fileName)));
110
    }
111
  }
112

    
113
  /**
114
   * getFiles
115
   */
116
  function getFiles() {
117
    $files = array();
118
    foreach ($this->fileTypes as $fileType) {
119
      $files[$this->$fileType->fileName] = array('module' => '', 'weight' => 0);
120
      if ($fileType == 'existing_changed') {
121
        $files[$this->$fileType->fileName]['hash'] = $this->$fileType->fakeHash;
122
      }
123
    }
124
    return $files;
125
  }
126

    
127
  /**
128
   * getFileContents
129
   */
130
  function getFileContents($fileType) {
131
    $file_contents = <<<CONTENTS
132
<?php
133

    
134
class {$this->$fileType->className} {}
135

    
136
interface {$this->$fileType->interfaceName} {}
137

    
138
CONTENTS;
139
    return $file_contents;
140
  }
141

    
142
}