You are here

class CoderUpgradeUnitTestCase in Coder 7

Same name and namespace in other branches
  1. 7.2 coder_upgrade/coder_upgrade.test \CoderUpgradeUnitTestCase

Unit tests for the upgrade routines.

Hierarchy

Expanded class hierarchy of CoderUpgradeUnitTestCase

File

coder_upgrade/coder_upgrade.test, line 12

View source
class CoderUpgradeUnitTestCase extends DrupalUnitTestCase {
  protected $test_directory, $site_directory;
  public static function getInfo1($subdir) {

    // Always display these warnings since this function is only called when cache is cleared.
    $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.";
    $msg1 = '';

    //     if (variable_get('coder_upgrade_enable_debug_output', FALSE)) {
    //       $msg1 = " WARNING: Do not run this test if the \"Enable debug output from coder upgrade\" option is checked for this module."; // Change this setting $settings.";
    //     }
    $msg2 = '';

    //     if (variable_get('coder_upgrade_enable_parser_debug_output', FALSE)) {
    //       $msg2 = " WARNING: Do not run this test if the \"Enable debug output from grammar parser\" option is checked for this module."; // Change this setting $settings.";
    //     }
    //     $msg3 = '';
    //     if (!variable_get('coder_upgrade_preserve_array_format', FALSE)) {
    $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() {

    /*
     * When running a unit test case, we do not have access to a database once
     * the parent::setUp() method is called. If there are any pre-processing
     * tasks that require database access, we need to do them first.
     */
    if (!variable_get('coder_upgrade_preserve_array_format', FALSE)) {

      // Expected files were created with this setting enabled.
      variable_set('coder_upgrade_preserve_array_format', TRUE);
    }
    file_put_contents('output.html', '');
    file_put_contents('output.html', __METHOD__ . "\n", FILE_APPEND);

    // Build theme registry cache.
    $this
      ->captureThemeInfo();
    file_put_contents('output.html', "after captureThemeInfo\n", FILE_APPEND);

    // Save the live site files directory path.
    $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');

    // Create output file directories.
    module_load_include('install', 'coder_upgrade');
    coder_upgrade_install();
    file_put_contents('output.html', "after install\n", FILE_APPEND);

    // Enable debug printing.
    global $_coder_upgrade_debug;
    $_coder_upgrade_debug = TRUE;
  }

  /**
   * Stores the theme registry for core modules and the modules being upgraded.
   */
  protected function captureThemeInfo() {
    if (variable_get('coder_upgrade_replace_files', FALSE)) {
      return;
    }

    // Include necessary files.
    $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';

    // TODO Move the debug_print method to .inc file??? Then avoid this load at this time.
    $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,
      );
    }

    // Build theme registry cache.
    coder_upgrade_upgrade_begin_alter($items[0]);
  }

  /**
   * Tests the upgrade routines (outside of the user interface).
   */
  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,
      );
    }

    // If Drupal won't commit patch to DrupalUnitTestCase.php, then use a global.
    global $_coder_upgrade_is_test;
    $_coder_upgrade_is_test = TRUE;

    // Apply upgrade routines.
    module_load_include('inc', 'coder_upgrade', 'includes/main');
    if (coder_upgrade_start($upgrades, $extensions, $items)) {
    }
    $ignore = array(
      '.',
      '..',
      'CVS',
      '.svn',
    );

    // Loop on files.
    // TODO This needs to recurse if there are subdirectories.
    $filenames = scandir($in_dirname . $key . '/');
    cdp($filenames);
    foreach ($filenames as $filename) {
      if (in_array($filename, $ignore)) {
        continue;
      }

      // Set file names to be compared.
      $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}");

      // Compare upgraded file to expected file.
      $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);

    // Save a copy outside of the simpletest directory that will be deleted.
    // TODO This copy may have name clashes.
    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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CoderUpgradeUnitTestCase::$test_directory protected property
CoderUpgradeUnitTestCase::captureThemeInfo protected function Stores the theme registry for core modules and the modules being upgraded.
CoderUpgradeUnitTestCase::compareFiles function
CoderUpgradeUnitTestCase::getInfo1 public static function
CoderUpgradeUnitTestCase::setUp protected function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp 2
CoderUpgradeUnitTestCase::testRunInterface protected function Tests the upgrade routines (outside of the user interface).
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct