You are here

abstract class QuickStartTestBase in Automatic Updates 8

Helper methods for using the quickstart feature of Drupal.

Hierarchy

Expanded class hierarchy of QuickStartTestBase

2 files declare their use of QuickStartTestBase
InPlaceUpdateTest.php in tests/src/Build/InPlaceUpdateTest.php
ModifiedFilesTest.php in tests/src/Build/ModifiedFilesTest.php

File

tests/src/Build/QuickStart/QuickStartTestBase.php, line 16

Namespace

Drupal\Tests\automatic_updates\Build\QuickStart
View source
abstract class QuickStartTestBase extends BuildTestBase {

  /**
   * User name of the admin account generated during install.
   *
   * @var string
   */
  protected $adminUsername;

  /**
   * Password of the admin account generated during install.
   *
   * @var string
   */
  protected $adminPassword;

  /**
   * Install a Drupal site using the quick start feature.
   *
   * @param string $profile
   *   Drupal profile to install.
   * @param string $working_dir
   *   (optional) A working directory relative to the workspace, within which to
   *   execute the command. Defaults to the workspace directory.
   */
  public function installQuickStart($profile, $working_dir = NULL) {
    $finder = new PhpExecutableFinder();
    $process = $this
      ->executeCommand($finder
      ->find() . ' ./core/scripts/drupal install ' . $profile, $working_dir);
    $this
      ->assertCommandSuccessful();
    $this
      ->assertCommandOutputContains('Username:');
    preg_match('/Username: (.+)\\vPassword: (.+)/', $process
      ->getOutput(), $matches);
    $this
      ->assertNotEmpty($this->adminUsername = $matches[1]);
    $this
      ->assertNotEmpty($this->adminPassword = $matches[2]);
  }

  /**
   * Prepare core for testing.
   *
   * @param string $starting_version
   *   The starting version.
   */
  protected function installCore($starting_version) {

    // Get tarball of drupal core.
    $drupal_tarball = "drupal-{$starting_version}.zip";
    $destination = DrupalFileSystem::getOsTemporaryDirectory() . DIRECTORY_SEPARATOR . 'drupal-' . random_int(10000, 99999) . microtime(TRUE);
    $fs = new SymfonyFilesystem();
    $fs
      ->mkdir($destination);
    $http_client = new Client();
    $http_client
      ->get("https://ftp.drupal.org/files/projects/{$drupal_tarball}", [
      'sink' => $destination . DIRECTORY_SEPARATOR . $drupal_tarball,
    ]);
    $zip = new Zip($destination . DIRECTORY_SEPARATOR . $drupal_tarball);
    $zip
      ->extract($destination);

    // Move the tarball codebase over to the test workspace.
    $finder = new Finder();
    $finder
      ->files()
      ->ignoreUnreadableDirs()
      ->ignoreDotFiles(FALSE)
      ->in("{$destination}/drupal-{$starting_version}");
    $options = [
      'override' => TRUE,
      'delete' => FALSE,
    ];
    $fs
      ->mirror("{$destination}/drupal-{$starting_version}", $this
      ->getWorkingPath(), $finder
      ->getIterator(), $options);
    $fs
      ->remove("{$destination}/drupal-{$starting_version}");

    // Copy in this module from the original code base.
    $finder = new Finder();
    $finder
      ->files()
      ->ignoreUnreadableDirs()
      ->in($this
      ->getDrupalRoot())
      ->path('automatic_updates');
    $this
      ->copyCodebase($finder
      ->getIterator());
    $fs
      ->chmod($this
      ->getWorkspaceDirectory() . '/sites/default', 0700);
    $this
      ->installQuickStart('minimal');

    // Currently, this test has to use extension_discovery_scan_tests so we can
    // install test modules.
    $settings_php = $this
      ->getWorkspaceDirectory() . '/sites/default/settings.php';
    $fs
      ->chmod($this
      ->getWorkspaceDirectory() . '/sites/default', 0755);
    $fs
      ->chmod($settings_php, 0640);
    $fs
      ->appendToFile($settings_php, '$settings[\'extension_discovery_scan_tests\'] = TRUE;' . PHP_EOL);

    // Log in so that we can install modules.
    $this
      ->formLogin($this->adminUsername, $this->adminPassword);
    $this
      ->moduleInstall('update');
    $this
      ->moduleInstall('automatic_updates');
    $this
      ->moduleInstall('test_automatic_updates');

    // Confirm we are running correct Drupal version.
    $finder = new Finder();
    $finder
      ->files()
      ->in($this
      ->getWorkspaceDirectory())
      ->path('core/lib/Drupal.php');
    $finder
      ->contains("/const VERSION = '{$starting_version}'/");
    $this
      ->assertTrue($finder
      ->hasResults(), "Expected version {$starting_version} does not exist in {$this->getWorkspaceDirectory()}/core/lib/Drupal.php");

    // Assert that the site is functional after install.
    $this
      ->visit();
    $this
      ->assertDrupalVisit();
  }

  /**
   * Helper that uses Drupal's user/login form to log in.
   *
   * @param string $username
   *   Username.
   * @param string $password
   *   Password.
   * @param string $working_dir
   *   (optional) A working directory within which to login. Defaults to the
   *   workspace directory.
   */
  public function formLogin($username, $password, $working_dir = NULL) {
    $mink = $this
      ->visit('/user/login', $working_dir);
    $this
      ->assertEquals(200, $mink
      ->getSession()
      ->getStatusCode());
    $assert = $mink
      ->assertSession();
    $assert
      ->fieldExists('edit-name')
      ->setValue($username);
    $assert
      ->fieldExists('edit-pass')
      ->setValue($password);
    $mink
      ->getSession()
      ->getPage()
      ->findButton('Log in')
      ->submit();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BuildTestBase::$commandProcess private property The most recent command process.
BuildTestBase::$destroyBuild protected property Default to destroying build artifacts after a test finishes.
BuildTestBase::$hostName private static property Our native host name, used by PHP when it starts up the server.
BuildTestBase::$hostPort private property Port that will be tested.
BuildTestBase::$mink private property The Mink session manager.
BuildTestBase::$portLocks private property A list of ports used by the test.
BuildTestBase::$serverDocroot private property The docroot for the server process.
BuildTestBase::$serverProcess private property The process that's running the HTTP server.
BuildTestBase::$workspaceDir private property The working directory where this test will manipulate files.
BuildTestBase::assertCommandExitCode public function Asserts that the last command returned the specified exit code.
BuildTestBase::assertCommandOutputContains public function Assert that text is present in the output of the most recent command.
BuildTestBase::assertCommandSuccessful public function Asserts that the last command ran without error.
BuildTestBase::assertDrupalVisit public function Helper function to assert that the last visit was a Drupal site.
BuildTestBase::assertErrorOutputContains public function Assert that text is present in the error output of the most recent command.
BuildTestBase::checkPortIsAvailable protected function Checks whether a port is available.
BuildTestBase::copyCodebase public function Copy the current working codebase into a workspace.
BuildTestBase::executeCommand public function Run a command.
BuildTestBase::findAvailablePort protected function Discover an available port number.
BuildTestBase::getCodebaseFinder public function Get a default Finder object for a Drupal codebase.
BuildTestBase::getDrupalRoot protected function Get the root path of this Drupal codebase.
BuildTestBase::getMink public function Get the Mink instance.
BuildTestBase::getPortNumber protected function Get the port number for requests.
BuildTestBase::getWorkingPath protected function Get the working directory within the workspace, creating if necessary.
BuildTestBase::getWorkspaceDirectory public function Full path to the workspace where this test can build.
BuildTestBase::initMink protected function Set up the Mink session manager.
BuildTestBase::instantiateServer protected function Do the work of making a server process.
BuildTestBase::setUp protected function
BuildTestBase::setUpBeforeClass public static function
BuildTestBase::standUpServer protected function Makes a local test server using PHP's internal HTTP server.
BuildTestBase::stopServer protected function Stop the HTTP server, zero out all necessary variables.
BuildTestBase::tearDown protected function
BuildTestBase::visit public function Visit a URI on the HTTP server.
ExternalCommandRequirementsTrait::$existingCommands private static property A list of existing external commands we've already discovered.
ExternalCommandRequirementsTrait::checkClassCommandRequirements private static function Checks whether required external commands are available per test class.
ExternalCommandRequirementsTrait::checkExternalCommandRequirements private static function Checks missing external command requirements.
ExternalCommandRequirementsTrait::checkMethodCommandRequirements private static function Checks whether required external commands are available per method.
ExternalCommandRequirementsTrait::externalCommandIsAvailable private static function Determine if an external command is available. 3
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
QuickStartTestBase::$adminPassword protected property Password of the admin account generated during install.
QuickStartTestBase::$adminUsername protected property User name of the admin account generated during install.
QuickStartTestBase::formLogin public function Helper that uses Drupal's user/login form to log in.
QuickStartTestBase::installCore protected function Prepare core for testing.
QuickStartTestBase::installQuickStart public function Install a Drupal site using the quick start feature.