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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
<?php
class RegistryParseFileTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Registry parse file test'),
'description' => t('Parse a simple file and check that its resources are saved to the database.'),
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
$this->fileName = 'registry_test_' . md5(rand());
$this->functionName = 'registry_test_function' . md5(rand());
$this->className = 'registry_test_class' . md5(rand());
$this->interfaceName = 'registry_test_interface' . md5(rand());
parent::setUp();
}
/**
* testRegistryParseFile
*/
function testRegistryParseFile() {
_registry_parse_file($this->fileName, $this->getFileContents());
foreach (array('functionName', 'className', 'interfaceName') as $resource) {
$foundName = db_result(db_query("SELECT name FROM {registry} WHERE name = '%s'", $this->$resource));
$this->assertTrue($this->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$resource)));
}
}
/**
* getFileContents
*/
function getFileContents() {
$file_contents = <<<CONTENTS
<?php
function {$this->functionName}() {}
class {$this->className} {}
interface {$this->interfaceName} {}
CONTENTS;
return $file_contents;
}
}
class RegistryParseFilesTestCase extends DrupalWebTestCase {
protected $fileTypes = array('new', 'existing_changed');
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Registry parse files test'),
'description' => t('Read two a simple files from disc, and check that their resources are saved to the database.'),
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp();
// Create files with some php to parse - one 'new', one 'existing' so
// we test all the important code paths in _registry_parse_files.
foreach ($this->fileTypes as $fileType) {
$this->$fileType = new StdClass();
$this->$fileType->fileName = file_directory_path() . '/registry_test_' . md5(rand());
$this->$fileType->functionName = 'registry_test_function' . md5(rand());
$this->$fileType->className = 'registry_test_class' . md5(rand());
$this->$fileType->interfaceName = 'registry_test_interface' . md5(rand());
$this->$fileType->contents = $this->getFileContents($fileType);
file_save_data($this->$fileType->contents, $this->$fileType->fileName);
if ($fileType == 'existing_changed') {
// Insert a record with a dodgy md5.
$this->$fileType->fakeMD5 = md5($this->$fileType->contents . rand());
db_query("INSERT INTO {registry_file} (md5, filename) VALUES ('%s', '%s')", $this->$fileType->fakeMD5, './' . $this->$fileType->fileName);
// Insert some fake resource records.
foreach (array('function', 'class', 'interface') as $type) {
db_query("INSERT INTO {registry} (name, type, filename) VALUES ('%s', '%s', '%s')", $type . md5(rand()), $type, './' . $this->$fileType->fileName);
}
}
}
}
/**
* testRegistryParseFiles
*/
function testRegistryParseFiles() {
_registry_parse_files($this->getFiles());
foreach ($this->fileTypes as $fileType) {
// Test that we have all the right resources.
foreach (array('functionName', 'className', 'interfaceName') as $resource) {
$foundName = db_result(db_query("SELECT name FROM {registry} WHERE name = '%s'", $this->$fileType->$resource));
$this->assertTrue($this->$fileType->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$fileType->$resource)));
}
// Test that we have the right md5.
$md5 = db_result(db_query("SELECT md5 FROM {registry_file} WHERE filename = '%s'", './' . $this->$fileType->fileName));
$this->assertTrue(md5($this->$fileType->contents) == $md5, t('MD5 for "@filename" matched.'.$fileType.$md5, array('@filename' => $this->$fileType->fileName)));
}
}
/**
* getFiles
*/
function getFiles() {
$files = array();
foreach ($this->fileTypes as $fileType) {
if ($fileType == 'existing_changed') {
$files['./' . $this->$fileType->fileName] = array('md5' => $this->$fileType->fakeMD5);
}
else {
$files['./' . $this->$fileType->fileName] = array();
}
}
return $files;
}
/**
* getFileContents
*/
function getFileContents($fileType) {
$file_contents = <<<CONTENTS
<?php
function {$this->$fileType->functionName}() {}
class {$this->$fileType->className} {}
interface {$this->$fileType->interfaceName} {}
CONTENTS;
return $file_contents;
}
}
class RegistryParseFilesTestCase extends DrupalWebTestCase {
protected $fileTypes = array('new', 'existing_changed');
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Registry parse files test'),
'description' => t('Read two a simple files from disc, and check that their resources are saved to the database.'),
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp();
// Create files with some php to parse - one 'new', one 'existing' so
// we test all the important code paths in _registry_parse_files.
foreach ($this->fileTypes as $fileType) {
$this->$fileType = new StdClass();
$this->$fileType->fileName = file_directory_path() . '/registry_test_' . md5(rand());
$this->$fileType->functionName = 'registry_test_function' . md5(rand());
$this->$fileType->className = 'registry_test_class' . md5(rand());
$this->$fileType->interfaceName = 'registry_test_interface' . md5(rand());
$this->$fileType->contents = $this->getFileContents($fileType);
file_save_data($this->$fileType->contents, $this->$fileType->fileName);
if ($fileType == 'existing_changed') {
// Insert a record with a dodgy md5.
$this->$fileType->fakeMD5 = md5($this->$fileType->contents . rand());
db_query("INSERT INTO {registry_file} (md5, filename) VALUES ('%s', '%s')", $this->$fileType->fakeMD5, './' . $this->$fileType->fileName);
// Insert some fake resource records.
foreach (array('function', 'class', 'interface') as $type) {
db_query("INSERT INTO {registry} (name, type, filename) VALUES ('%s', '%s', '%s')", $type . md5(rand()), $type, './' . $this->$fileType->fileName);
}
}
}
}
/**
* testRegistryParseFiles
*/
function testRegistryParseFiles() {
_registry_parse_files($this->getFiles());
foreach ($this->fileTypes as $fileType) {
// Test that we have all the right resources.
foreach (array('functionName', 'className', 'interfaceName') as $resource) {
$foundName = db_result(db_query("SELECT name FROM {registry} WHERE name = '%s'", $this->$fileType->$resource));
$this->assertTrue($this->$fileType->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$fileType->$resource)));
}
// Test that we have the right md5.
$md5 = db_result(db_query("SELECT md5 FROM {registry_file} WHERE filename = '%s'", './' . $this->$fileType->fileName));
$this->assertTrue(md5($this->$fileType->contents) == $md5, t('MD5 for "@filename" matched.'.$fileType.$md5, array('@filename' => $this->$fileType->fileName)));
}
}
/**
* getFiles
*/
function getFiles() {
$files = array();
foreach ($this->fileTypes as $fileType) {
if ($fileType == 'existing_changed') {
$files['./' . $this->$fileType->fileName] = array('md5' => $this->$fileType->fakeMD5);
}
else {
$files['./' . $this->$fileType->fileName] = array();
}
}
return $files;
}
/**
* getFileContents
*/
function getFileContents($fileType) {
$file_contents = <<<CONTENTS
<?php
function {$this->$fileType->functionName}() {}
class {$this->$fileType->className} {}
interface {$this->$fileType->interfaceName} {}
CONTENTS;
return $file_contents;
}
}
|