You are here

coder_upgrade.test in Coder 7

Same filename and directory in other branches
  1. 7.2 coder_upgrade/coder_upgrade.test

File

coder_upgrade/coder_upgrade.test
View source
<?php

/**
 * @file
 * Provides tests.
 */
module_load_include('inc', 'coder_upgrade', 'coder_upgrade');

/**
 * Unit tests for the upgrade routines.
 */
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);
    }
  }

}

/**
 * Unit tests on a single file in files/coder_upgrade/old/test.
 *
 * Use this while developing on an upgrade routine.
 */
class CoderUpgradeUnitTestCase1 extends CoderUpgradeUnitTestCase {
  public static function getInfo() {
    return CoderUpgradeUnitTestCase::getInfo1('test');
  }
  protected function setUp() {
    parent::setUp();
    $this->test_directory = 'test';
  }

}

/**
 * Unit tests on a sample module in files/coder_upgrade/old/samples.
 */
class CoderUpgradeUnitTestCase2 extends CoderUpgradeUnitTestCase {
  public static function getInfo() {
    return CoderUpgradeUnitTestCase::getInfo1('samples');
  }
  protected function setUp() {
    parent::setUp();
    $this->test_directory = 'samples';
  }

}

/**
 * Functional tests for the admin interface (settings and execution).
 */
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);
  }

  /**
   * Call test methods (prefixed with 'check' to avoid setUp overhead).
   */
  function testAdminInterface() {

    // Include the admin file so we can call its functions.
    module_load_include('inc', 'coder_upgrade', 'coder_upgrade');
    $this
      ->checkSettingsInterface();
    $this
      ->checkRunInterface();
  }

  /**
   * Test the settings via the user interface.
   */
  function checkSettingsInterface() {

    // Visit the advanced menu settings page.
    $this
      ->drupalGet('admin/config/development/coder_upgrade/settings');

    // Update the 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'));

    // Verify the settings.
    $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.'));

    // There is no reset button visible?
    // Reset the settings.
    $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->drupalPost(NULL, $edit, t('Reset to defaults'));
    $this
      ->assertRaw(t('The configuration options have been saved.'), t('Settings updated successfully'));

    // Verify the settings.
    $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.'));
  }

  /**
   * Test the running via the user interface.
   */
  function checkRunInterface() {
  }

}

Classes

Namesort descending Description
CoderUpgradeUnitTestCase Unit tests for the upgrade routines.
CoderUpgradeUnitTestCase1 Unit tests on a single file in files/coder_upgrade/old/test.
CoderUpgradeUnitTestCase2 Unit tests on a sample module in files/coder_upgrade/old/samples.
CoderUpgradeWebTestCase Functional tests for the admin interface (settings and execution).