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
|
<?php
class RegistryParseFileTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Registry parse file test',
'description' => 'Parse a simple file and check that its resources are saved to the database.',
'group' => 'System'
);
}
function setUp() {
$chrs = hash('sha256', microtime() . mt_rand());
$this->fileName = 'registry_test_' . substr($chrs, 0, 16);
$this->className = 'registry_test_class' . substr($chrs, 16, 16);
$this->interfaceName = 'registry_test_interface' . substr($chrs, 32, 16);
parent::setUp();
}
/**
* testRegistryParseFile
*/
function testRegistryParseFile() {
_registry_parse_file($this->fileName, $this->getFileContents());
foreach (array('className', 'interfaceName') as $resource) {
$foundName = db_query('SELECT name FROM {registry} WHERE name = :name', array(':name' => $this->$resource))->fetchField();
$this->assertTrue($this->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$resource)));
}
}
/**
* getFileContents
*/
function getFileContents() {
$file_contents = <<<CONTENTS
<?php
class {$this->className} {}
interface {$this->interfaceName} {}
CONTENTS;
return $file_contents;
}
}
class RegistryParseFilesTestCase extends DrupalWebTestCase {
protected $fileTypes = array('new', 'existing_changed');
public static function getInfo() {
return array(
'name' => 'Registry parse files test',
'description' => 'Read two a simple files from disc, and check that their resources are saved to the database.',
'group' => 'System'
);
}
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) {
$chrs = hash('sha256', microtime() . mt_rand());
$this->$fileType = new stdClass();
$this->$fileType->fileName = 'public://registry_test_' . substr($chrs, 0, 16);
$this->$fileType->className = 'registry_test_class' . substr($chrs, 16, 16);
$this->$fileType->interfaceName = 'registry_test_interface' . substr($chrs, 32, 16);
$this->$fileType->contents = $this->getFileContents($fileType);
file_save_data($this->$fileType->contents, $this->$fileType->fileName);
if ($fileType == 'existing_changed') {
// Add a record with an incorrect hash.
$this->$fileType->fakeHash = hash('sha256', mt_rand());
db_insert('registry_file')
->fields(array(
'hash' => $this->$fileType->fakeHash,
'filename' => $this->$fileType->fileName,
))
->execute();
// Insert some fake resource records.
foreach (array('class', 'interface') as $type) {
db_insert('registry')
->fields(array(
'name' => $type . hash('sha256', microtime() . mt_rand()),
'type' => $type,
'filename' => $this->$fileType->fileName,
))
->execute();
}
}
}
}
/**
* testRegistryParseFiles
*/
function testRegistryParseFiles() {
_registry_parse_files($this->getFiles());
foreach ($this->fileTypes as $fileType) {
// Test that we have all the right resources.
foreach (array('className', 'interfaceName') as $resource) {
$foundName = db_query('SELECT name FROM {registry} WHERE name = :name', array(':name' => $this->$fileType->$resource))->fetchField();
$this->assertTrue($this->$fileType->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$fileType->$resource)));
}
// Test that we have the right hash.
$hash = db_query('SELECT hash FROM {registry_file} WHERE filename = :filename', array(':filename' => $this->$fileType->fileName))->fetchField();
$this->assertTrue(hash('sha256', $this->$fileType->contents) == $hash, t('sha-256 for "@filename" matched.' . $fileType . $hash, array('@filename' => $this->$fileType->fileName)));
}
}
/**
* getFiles
*/
function getFiles() {
$files = array();
foreach ($this->fileTypes as $fileType) {
$files[$this->$fileType->fileName] = array('module' => '', 'weight' => 0);
if ($fileType == 'existing_changed') {
$files[$this->$fileType->fileName]['hash'] = $this->$fileType->fakeHash;
}
}
return $files;
}
/**
* getFileContents
*/
function getFileContents($fileType) {
$file_contents = <<<CONTENTS
<?php
class {$this->$fileType->className} {}
interface {$this->$fileType->interfaceName} {}
CONTENTS;
return $file_contents;
}
}
|