View source
<?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() {
$this->fileName = 'registry_test_' . md5(rand());
$this->className = 'registry_test_class' . md5(rand());
$this->interfaceName = 'registry_test_interface' . md5(rand());
parent::setUp();
}
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},
)));
}
}
function getFileContents() {
$file_contents = <<<CONTENTS
<?php
class {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">className</span>} {}
interface {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">interfaceName</span>} {}
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();
foreach ($this->fileTypes as $fileType) {
$this->{$fileType} = new stdClass();
$this->{$fileType}->fileName = file_directory_path() . '/registry_test_' . 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') {
db_insert('registry_file')
->fields(array(
'filectime' => rand(1, 1000000),
'filemtime' => rand(1, 1000000),
'filename' => $this->{$fileType}->fileName,
))
->execute();
foreach (array(
'class',
'interface',
) as $type) {
db_insert('registry')
->fields(array(
'name' => $type . md5(rand()),
'type' => $type,
'filename' => $this->{$fileType}->fileName,
))
->execute();
}
}
}
}
function testRegistryParseFiles() {
_registry_parse_files($this
->getFiles());
foreach ($this->fileTypes as $fileType) {
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},
)));
}
$dates = db_query('SELECT filectime, filemtime FROM {registry_file} WHERE filename = :filename', array(
':filename' => $this->{$fileType}->fileName,
))
->fetchObject();
$this
->assertEqual($dates->filectime, filectime($this->{$fileType}->fileName), t('File creation date matches for %filename.', array(
'%filename' => $this->{$fileType}->fileName,
)));
$this
->assertEqual($dates->filemtime, filemtime($this->{$fileType}->fileName), t('File modification date matches for %filename.', array(
'%filename' => $this->{$fileType}->fileName,
)));
}
}
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]['filectime'] = rand(1, 1000000);
$files[$this->{$fileType}->fileName]['filemtime'] = rand(1, 1000000);
}
}
return $files;
}
function getFileContents($fileType) {
$file_contents = <<<CONTENTS
<?php
class {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">{<span class="php-variable">$fileType</span>}</span>-><span class="php-function-or-constant property member-of-variable">className</span>} {}
interface {<span class="php-variable">$this</span>-><span class="php-function-or-constant property member-of-self">{<span class="php-variable">$fileType</span>}</span>-><span class="php-function-or-constant property member-of-variable">interfaceName</span>} {}
CONTENTS;
return $file_contents;
}
}