Newer
Older

Dries Buytaert
committed
<?php

Dries Buytaert
committed
// $Id$

Dries Buytaert
committed
class RegistryParseFileTestCase extends DrupalWebTestCase {

Angie Byron
committed
public static function getInfo() {

Dries Buytaert
committed
return array(
'name' => 'Registry parse file test',
'description' => 'Parse a simple file and check that its resources are saved to the database.',
'group' => 'System'

Dries Buytaert
committed
);
}
function setUp() {
$this->fileName = 'registry_test_' . md5(rand());

Angie Byron
committed
$this->functionName = 'registry_test_function' . md5(rand());

Dries Buytaert
committed
$this->className = 'registry_test_class' . md5(rand());
$this->interfaceName = 'registry_test_interface' . md5(rand());
parent::setUp();
}
/**

Dries Buytaert
committed
* testRegistryParseFile

Dries Buytaert
committed
*/
function testRegistryParseFile() {
_registry_parse_file($this->fileName, $this->getFileContents());

Angie Byron
committed
foreach (array('functionName', 'className', 'interfaceName') as $resource) {

Dries Buytaert
committed
$foundName = db_query('SELECT name FROM {registry} WHERE name = :name', array(':name' => $this->$resource))->fetchField();

Dries Buytaert
committed
$this->assertTrue($this->$resource == $foundName, t('Resource "@resource" found.', array('@resource' => $this->$resource)));
}
}
/**

Dries Buytaert
committed
* getFileContents

Dries Buytaert
committed
*/
function getFileContents() {
$file_contents = <<<CONTENTS
<?php

Angie Byron
committed
function {$this->functionName}() {}

Dries Buytaert
committed
class {$this->className} {}
interface {$this->interfaceName} {}
CONTENTS;
return $file_contents;
}
}
class RegistryParseFilesTestCase extends DrupalWebTestCase {
protected $fileTypes = array('new', 'existing_changed');

Angie Byron
committed
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) {
$this->$fileType = new stdClass();
$this->$fileType->fileName = file_directory_path() . '/registry_test_' . md5(rand());

Angie Byron
committed
$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') {

Dries Buytaert
committed
db_insert('registry_file')
->fields(array(

Dries Buytaert
committed
'filectime' => rand(1, 1000000),
'filemtime' => rand(1, 1000000),

Dries Buytaert
committed
'filename' => $this->$fileType->fileName,
))
->execute();
// Insert some fake resource records.

Angie Byron
committed
foreach (array('function', 'class', 'interface') as $type) {

Dries Buytaert
committed
db_insert('registry')
->fields(array(
'name' => $type . md5(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.

Angie Byron
committed
foreach (array('functionName', 'className', 'interfaceName') as $resource) {

Dries Buytaert
committed
$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)));
}

Dries Buytaert
committed
// Test that we have the right file creation and modification dates.
$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)));
}
}
/**
* getFiles
*/
function getFiles() {
$files = array();
foreach ($this->fileTypes as $fileType) {

Dries Buytaert
committed
$files[$this->$fileType->fileName] = array('module' => '', 'weight' => 0);
if ($fileType == 'existing_changed') {

Dries Buytaert
committed
$files[$this->$fileType->fileName]['filectime'] = rand(1, 1000000);
$files[$this->$fileType->fileName]['filemtime'] = rand(1, 1000000);
}
}
return $files;
}
/**
* getFileContents
*/
function getFileContents($fileType) {
$file_contents = <<<CONTENTS
<?php

Angie Byron
committed
function {$this->$fileType->functionName}() {}
class {$this->$fileType->className} {}
interface {$this->$fileType->interfaceName} {}
CONTENTS;
return $file_contents;
}
}

Angie Byron
committed
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
class RegistrySkipBodyTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Skip function body test',
'description' => 'Tokenize a simple function and check that the body is skipped right',
'group' => 'System',
);
}
function testRegistrySkipBody() {
// This string contains all three kinds of opening braces.
$function = '<?php function foo () { $x = "{$y}"; $x = "${y}"; }';
$tokens = token_get_all($function);
require_once DRUPAL_ROOT . '/includes/registry.inc';
_registry_skip_body($tokens);
// Consume the last }
each($tokens);
$this->assertIdentical(each($tokens), FALSE, t('Tokens skipped'));
// Check workaround for PHP < 5.2.3 regarding tokenization of strings
// containing variables. The { contained in the string should not be
// treated as a separate token.
$function = '<?php function foo() { $x = "$y {"; $x = `$y {`; $x = ' . "<<<EOD\n\$y {\nEOD\n; } function bar() {}";
$tokens = token_get_all($function);
_registry_skip_body($tokens);
$this->assertTrue(each($tokens), t('Tokens not skipped to end of file.'));
}
}