class contextCase

  1. 8.0.x tests/contextTest.php contextCase
  2. 6.x tests/contextTest.php contextCase
  3. 7.x tests/contextTest.php contextCase
  4. 4.x tests/contextTest.php contextCase
  5. 5.x tests/contextTest.php contextCase
  6. master tests/contextTest.php contextCase

Hierarchy

Expanded class hierarchy of contextCase

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
contextCase::ConfigSearchPaths function Assure that all possible config files get loaded.
contextCase::ConfigVersionSpecific function Assure that matching version-specific config files are loaded and others are ignored.
contextCase::ContextHierarchy function Assure that options are loaded into right context and hierarchy is respected by drush_get_option().
contextCase::setUp function Try to write a tiny drushrc.php to each place that drush checks. Also write a sites/dev/aliases.drushrc.php file to the sandbox.
contextCase::setUpPaths function
contextCase::testContext function These should be different tests but I could not work out how to do that without calling setUp() twice. setUpBeforeClass() did not work out (for MW).
Drush_CommandTestCase::drush function Invoke drush in via execute().
Drush_CommandTestCase::drush_major_version function
Drush_CommandTestCase::execute function Actually runs the command. Does not trap the error stream output as this need PHP 4.3+.
Drush_CommandTestCase::EXIT_ERROR constant
Drush_CommandTestCase::EXIT_SUCCESS constant
Drush_TestCase::bit_bucket function Borrowed from Drush. Checks operating system and returns supported bit bucket folder.
Drush_TestCase::convert_path function Converts a Windows path (dir1\dir2\dir3) into a Unix path (dir1/dir2/dir3). Also converts a cygwin "drive emulation" path (/cygdrive/c/dir1) into a proper drive path, still with Unix slashes (c:/dir1).
Drush_TestCase::db_driver function
Drush_TestCase::db_url function
Drush_TestCase::directory_cache function
Drush_TestCase::drupalSitewideDirectory function
Drush_TestCase::escapeshellarg public static function
Drush_TestCase::fetchInstallDrupal function
Drush_TestCase::getOutput function Accessor for the last output.
Drush_TestCase::getOutputAsList function Accessor for the last output.
Drush_TestCase::getOutputFromJSON function Accessor for the last output, decoded from json.
Drush_TestCase::get_tar_executable public static function
Drush_TestCase::is_windows public static function
Drush_TestCase::log function Print a log message to the console.
Drush_TestCase::log_level function
Drush_TestCase::randomString public function Helper function to generate a random string of arbitrary length.
Drush_TestCase::setUpBeforeClass public static function Assure that each class starts with an empty sandbox directory and a clean environment - http://drupal.org/node/1103568.
Drush_TestCase::setUpDrupal function
Drush_TestCase::setUpFreshSandBox public static function Remove any pre-existing sandbox, then create a new one.
Drush_TestCase::tearDownAfterClass public static function Runs after all tests in a class are run. Remove sandbox directory.
Drush_TestCase::webroot function
Drush_TestCase::_escapeshellarg_windows public static function
Drush_TestCase::__construct function

File

tests/contextTest.php, line 13
Assure that context API behaves as designed. Mostly implicitly tested, but we do have some edges that need explicit testing. *

View source
class contextCase extends Drush_CommandTestCase {

  function setUpPaths() {
    $this->log("webroot: " . $this->webroot() . "\n");
    $this->env = key($this->sites);
    $this->site = $this->webroot() . '/sites/' . $this->env;
    $this->home = UNISH_SANDBOX . '/home';
    $this->paths = array(
      'custom' => UNISH_SANDBOX,
      'site' => $this->site,
      'drupal' => $this->webroot() . '/sites/all/drush',
      'user' => $this->home,
      'home.drush' => $this->home . '/.drush',
      'system' => UNISH_SANDBOX . '/etc/drush',
      // We don't want to write a file into drush dir since it is not in the sandbox. // 'drush' => dirname(realpath(UNISH_DRUSH)),
    );
    // Run each path through realpath() since the paths we'll compare against
    // will have already run through drush_load_config_file().
    foreach ($this->paths as $key => $path) {
      $this->paths[$key] = realpath($path);
    };
  }

  /**
   * Try to write a tiny drushrc.php to each place that drush checks. Also
   * write a sites/dev/aliases.drushrc.php file to the sandbox.
   */
  function setUp() {
    parent::setUp();

    $this->setUpDrupal();
    $this->setUpPaths();

    // These files are only written to sandbox so get automatically cleaned up.
    foreach ($this->paths as $key => $path) {
      $contents = <<<EOD
<?php
// Written by Drush's contextCase::setUp(). This file is safe to delete.

\$options['contextConfig'] = '$key';
\$command_specific['unit-eval']['contextConfig'] = '$key-specific';

EOD;
      $path .= $key == 'user' ? '/.drushrc.php' : '/drushrc.php';
      if (file_put_contents($path, $contents)) {
        $this->written[] = $this->convert_path($path);
      }
    }

    // Also write a site alias so we can test its supremacy in context hierarchy.
    $path = $this->site . '/aliases.drushrc.php';
    $aliases['contextAlias'] = array(
      'contextConfig' => 'alias1',
      'command-specific' => array(
        'unit-eval' => array(
          'contextConfig' => 'alias-specific',
        ),
      ),
    );
    $contents = unish_file_aliases($aliases);
    $return = file_put_contents($path, $contents);
  }

  /**
   * These should be different tests but I could not work out how to do that
   * without calling setUp() twice. setUpBeforeClass() did not work out (for MW).
   */
  function testContext() {
    $this->ConfigSearchPaths();
    $this->ConfigVersionSpecific();
    $this->ContextHierarchy();
  }

  /**
   * Assure that all possible config files get loaded.
   */
  function ConfigSearchPaths() {
    $options = array(
      'pipe' => NULL,
      'config' => UNISH_SANDBOX,
      'root' => $this->webroot(),
      'uri' => $this->env,
    );
    $this->drush('core-status', array('Drush configuration'), $options);
    $loaded = $this->getOutputFromJSON('drush-conf');
    $loaded = array_map(array(&$this, 'convert_path'), $loaded);
    $this->assertSame($this->written, $loaded);
  }

  /**
   * Assure that matching version-specific config files are loaded and others are ignored.
   */
  function ConfigVersionSpecific() {
    $major = $this->drush_major_version();
    // Arbitrarily choose the system search path.
    $path = realpath(UNISH_SANDBOX . '/etc/drush');
    $contents = <<<EOD
<?php
// Written by Unish. This file is safe to delete.
\$options['unish_foo'] = 'bar';
EOD;

    // Write matched and unmatched files to the system search path.
    $files = array(
      $path . '/drush' . $major . 'rc.php',
      $path . '/drush999' . 'rc.php',
    );
    mkdir($path . '/drush' . $major);
    mkdir($path . '/drush999');
    foreach ($files as $file) {
      file_put_contents($file, $contents);
    }

    $this->drush('core-status', array('Drush configuration'), array('pipe' => NULL));
    $loaded = $this->getOutputFromJSON('drush-conf');
    // Next 2 lines needed for Windows compatibility.
    $loaded = array_map(array(&$this, 'convert_path'), $loaded);
    $files = array_map(array(&$this, 'convert_path'), $files);
    $this->assertTrue(in_array($files[0], $loaded), 'Loaded a version-specific config file.');
    $this->assertFalse(in_array($files[1], $loaded), 'Did not load a mismatched version-specific config file.');
  }

  /**
   * Assure that options are loaded into right context and hierarchy is
   * respected by drush_get_option().
   *
   * Stdin context not exercised here. See backendCase::testTarget().
   */
  function ContextHierarchy() {
    // The 'custom' config file has higher priority than cli and regular config files.
    $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
    $eval .= '$cli1 = drush_get_option("cli1");';
    $eval .= 'print json_encode(get_defined_vars());';
    $config = UNISH_SANDBOX . '/drushrc.php';
    $options = array(
      'cli1' => NULL,
      'config' => $config,
      'root' => $this->webroot(),
      'uri' => $this->env,
    );
    $this->drush('php-eval', array($eval), $options);
    $output = $this->getOutput();
    $actuals = json_decode(trim($output));
    $this->assertEquals('custom', $actuals->contextConfig);
    $this->assertTrue($actuals->cli1);

    // Site alias trumps 'custom'.
    $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
    $eval .= 'print json_encode(get_defined_vars());';
    $options = array(
      'config' => $config,
      'root' => $this->webroot(),
      'uri' => $this->env,
    );
    $this->drush('php-eval', array($eval), $options, '@contextAlias');
    $output = $this->getOutput();
    $actuals = json_decode(trim($output));
    $this->assertEquals('alias1', $actuals->contextConfig);

    // Command specific wins over non-specific. If it did not, $expected would
    // be 'site'. Note we call unit-eval command in order not to purturb
    // php-eval with options in config file.
    $eval = '$contextConfig = drush_get_option("contextConfig", "n/a");';
    $eval .= 'print json_encode(get_defined_vars());';
    $options = array(
      'root' => $this->webroot(),
      'uri' => $this->env,
      'include' => dirname(__FILE__), // Find unit.drush.inc commandfile.
    );
    $this->drush('unit-eval', array($eval), $options);
    $output = $this->getOutput();
    $actuals = json_decode(trim($output));
    $this->assertEquals('site-specific', $actuals->contextConfig);
  }
}