View source
<?php
module_load_include('inc', 'coder_upgrade', 'coder_upgrade');
class CoderUpgradeUnitTestCase extends DrupalUnitTestCase {
protected $test_directory, $site_directory;
public static function getInfo1($subdir) {
$settings = l('here', 'admin/config/development/coder/upgrade/settings');
$msg1 = '';
$msg2 = '';
$msg3 = " NOTICE: During this test the \"Preserve array formatting\" option will enabled for this module. Afterward, this setting may be disabled {$settings}.";
$msg = "WARNING: Do not run this test if any of these options is checked for this module: \"Replace files,\" \"Enable debug output from coder upgrade,\" or \"Enable debug output from grammar parser.\" Change these setting {$settings}.";
$dir = variable_get('coder_upgrade_dir_new', DEADWOOD_OLD);
return array(
'name' => "Run interface ({$subdir})",
'description' => "Test the output from the upgrade routines on the files in the files/{$dir}/{$subdir} directory.{$msg}{$msg1}{$msg2}{$msg3}",
'group' => 'Coder Upgrade',
);
}
protected function setUp() {
if (!variable_get('coder_upgrade_preserve_array_format', FALSE)) {
variable_set('coder_upgrade_preserve_array_format', TRUE);
}
file_put_contents('output.html', '');
file_put_contents('output.html', __METHOD__ . "\n", FILE_APPEND);
$this
->captureThemeInfo();
file_put_contents('output.html', "after captureThemeInfo\n", FILE_APPEND);
$this->site_directory = DRUPAL_ROOT . '/' . coder_upgrade_directory_path('new');
file_put_contents('output.html', $this->site_directory . "\n", FILE_APPEND);
parent::setUp('grammar_parser', 'coder_upgrade');
module_load_include('install', 'coder_upgrade');
coder_upgrade_install();
file_put_contents('output.html', "after install\n", FILE_APPEND);
global $_coder_upgrade_debug;
$_coder_upgrade_debug = TRUE;
}
protected function captureThemeInfo() {
if (variable_get('coder_upgrade_replace_files', FALSE)) {
return;
}
$module_dirname = DRUPAL_ROOT . '/' . drupal_get_path('module', 'grammar_parser');
require_once $module_dirname . '/engine/grammar_parser.parser.inc';
require_once $module_dirname . '/engine/grammar_parser.reader.inc';
$module_dirname = DRUPAL_ROOT . '/' . drupal_get_path('module', 'coder_upgrade');
require_once $module_dirname . '/includes/main.inc';
require_once $module_dirname . '/conversions/begin.inc';
require_once $module_dirname . '/conversions/function.inc';
coder_upgrade_debug_print("module = {$module_dirname}");
coder_upgrade_path_clear('memory');
coder_upgrade_memory_print('load code');
$in_dirname = $module_dirname . '/tests/old/';
$out_dirname = DRUPAL_ROOT . '/' . coder_upgrade_directory_path('new');
$directories = array(
$this->test_directory => 1,
);
foreach ($directories as $key => $directory) {
$items[] = array(
'name' => $key,
'old_dir' => $in_dirname . $key,
'new_dir' => $out_dirname . $key,
);
}
coder_upgrade_upgrade_begin_alter($items[0]);
}
protected function testRunInterface() {
file_put_contents('output.html', __METHOD__ . "\n", FILE_APPEND);
$_coder_upgrade_replace_files = variable_get('coder_upgrade_replace_files', FALSE);
if ($_coder_upgrade_replace_files) {
$settings = l('here', 'admin/config/development/coder/upgrade/settings');
$msg = "WARNING: Do not run this test if the \"Replace files\" option is checked for this module. Change this setting {$settings}.";
$this
->assertFalse($_coder_upgrade_replace_files, $msg, 'Settings');
return;
}
file_put_contents('output.html', __METHOD__ . "\n", FILE_APPEND);
$module_dirname = DRUPAL_ROOT . '/' . drupal_get_path('module', 'coder_upgrade');
$in_dirname = $module_dirname . '/tests/old/';
$expected_dirname = $module_dirname . '/tests/new/';
$out_dirname = DRUPAL_ROOT . '/' . coder_upgrade_directory_path('new');
file_put_contents('output.html', "{$out_dirname}\n", FILE_APPEND);
$upgrades = coder_upgrade_upgrade_info();
$extensions = array(
'inc' => TRUE,
'info' => TRUE,
'install' => TRUE,
'module' => TRUE,
'php' => FALSE,
'profile' => FALSE,
'test' => FALSE,
'theme' => FALSE,
);
$directories = array(
$this->test_directory => 1,
);
foreach ($directories as $key => $directory) {
$items[] = array(
'name' => $key,
'old_dir' => $in_dirname . $key,
'new_dir' => $out_dirname . $key,
);
}
global $_coder_upgrade_is_test;
$_coder_upgrade_is_test = TRUE;
module_load_include('inc', 'coder_upgrade', 'includes/main');
if (coder_upgrade_start($upgrades, $extensions, $items)) {
}
$ignore = array(
'.',
'..',
'CVS',
'.svn',
);
$filenames = scandir($in_dirname . $key . '/');
cdp($filenames);
foreach ($filenames as $filename) {
if (in_array($filename, $ignore)) {
continue;
}
$expected_filename = $expected_dirname . $key . '/' . $filename;
$out_filename = $out_dirname . $key . '/' . $filename;
coder_upgrade_debug_print("exp = {$expected_filename}");
coder_upgrade_debug_print("out = {$out_filename}");
$this
->compareFiles($expected_filename, $out_filename);
}
}
function compareFiles($expected_filename, $out_filename) {
$name = pathinfo($out_filename, PATHINFO_BASENAME);
$b1 = file_exists($expected_filename);
$this
->assertTrue($b1, 'Expected file exists', $name);
$b2 = file_exists($out_filename);
$this
->assertTrue($b2, 'Output file exists', $name);
copy($out_filename, $this->site_directory . $name);
if ($b1 && $b2) {
$expected = file_get_contents($expected_filename);
$actual = file_get_contents($out_filename);
$this
->assertEqual($expected, $actual, 'Contents of output file match that of expected file', $name);
}
else {
$this
->assertEqual('expected', 'actual', 'Contents of output file match that of expected file', $name);
}
}
}
class CoderUpgradeUnitTestCase1 extends CoderUpgradeUnitTestCase {
public static function getInfo() {
return CoderUpgradeUnitTestCase::getInfo1('test');
}
protected function setUp() {
parent::setUp();
$this->test_directory = 'test';
}
}
class CoderUpgradeUnitTestCase2 extends CoderUpgradeUnitTestCase {
public static function getInfo() {
return CoderUpgradeUnitTestCase::getInfo1('samples');
}
protected function setUp() {
parent::setUp();
$this->test_directory = 'samples';
}
}
class CoderUpgradeWebTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Settings interface',
'description' => 'Test the administration interface for Coder Upgrade.',
'group' => 'Coder Upgrade',
);
}
function setUp() {
parent::setUp('coder_upgrade');
$this->admin_user = $this
->drupalCreateUser(array(
'access administration pages',
'administer site configuration',
'administer code conversions',
));
$this
->drupalLogin($this->admin_user);
}
function testAdminInterface() {
module_load_include('inc', 'coder_upgrade', 'coder_upgrade');
$this
->checkSettingsInterface();
$this
->checkRunInterface();
}
function checkSettingsInterface() {
$this
->drupalGet('admin/config/development/coder_upgrade/settings');
$edit = array();
$edit['coder_upgrade_dir_old'] = $this
->randomName();
$edit['coder_upgrade_dir_new'] = $this
->randomName();
$edit['coder_upgrade_dir_patch'] = $this
->randomName();
$this
->drupalPost(NULL, $edit, t('Save configuration'));
$this
->assertRaw(t('The configuration options have been saved.'), t('Settings updated successfully'));
$settings = variable_get('coder_upgrade_dir_old', '');
$this
->assertEqual($settings, $edit['coder_upgrade_dir_old'], t('Old directory name found in variable.'));
$settings = variable_get('coder_upgrade_dir_new', '');
$this
->assertEqual($settings, $edit['coder_upgrade_dir_new'], t('New directory name found in variable.'));
$settings = variable_get('coder_upgrade_dir_patch', '');
$this
->assertEqual($settings, $edit['coder_upgrade_dir_patch'], t('Patch directory name found in variable.'));
$edit = array();
$edit['coder_upgrade_dir_old'] = DEADWOOD_OLD;
$edit['coder_upgrade_dir_new'] = DEADWOOD_NEW;
$edit['coder_upgrade_dir_patch'] = DEADWOOD_PATCH;
$this
->drupalPost(NULL, $edit, t('Save configuration'));
$this
->assertRaw(t('The configuration options have been saved.'), t('Settings updated successfully'));
$settings = variable_get('coder_upgrade_dir_old', '');
$this
->assertEqual($settings, $edit['coder_upgrade_dir_old'], t('Old directory name was cleared.'));
$settings = variable_get('coder_upgrade_dir_new', '');
$this
->assertEqual($settings, $edit['coder_upgrade_dir_new'], t('New directory name was cleared.'));
$settings = variable_get('coder_upgrade_dir_patch', '');
$this
->assertEqual($settings, $edit['coder_upgrade_dir_patch'], t('Patch directory name was cleared.'));
}
function checkRunInterface() {
}
}