You are here

trait ThunderTestTrait in Thunder 8.4

Same name and namespace in other branches
  1. 8.5 tests/src/Traits/ThunderTestTrait.php \Drupal\Tests\thunder\Traits\ThunderTestTrait
  2. 8.2 tests/src/Traits/ThunderTestTrait.php \Drupal\Tests\thunder\Traits\ThunderTestTrait
  3. 8.3 tests/src/Traits/ThunderTestTrait.php \Drupal\Tests\thunder\Traits\ThunderTestTrait
  4. 6.2.x tests/src/Traits/ThunderTestTrait.php \Drupal\Tests\thunder\Traits\ThunderTestTrait
  5. 6.0.x tests/src/Traits/ThunderTestTrait.php \Drupal\Tests\thunder\Traits\ThunderTestTrait
  6. 6.1.x tests/src/Traits/ThunderTestTrait.php \Drupal\Tests\thunder\Traits\ThunderTestTrait

Use this trait to reuse an existing database.

Hierarchy

2 files declare their use of ThunderTestTrait
ThunderJavascriptTestBase.php in tests/src/FunctionalJavascript/ThunderJavascriptTestBase.php
ThunderTestBase.php in tests/src/Functional/ThunderTestBase.php

File

tests/src/Traits/ThunderTestTrait.php, line 18

Namespace

Drupal\Tests\thunder\Traits
View source
trait ThunderTestTrait {

  /**
   * {@inheritdoc}
   */
  protected function installParameters() {
    $parameters = parent::installParameters();
    $parameters['forms']['thunder_module_configure_form'] = [
      'install_modules_thunder_demo' => NULL,
    ];
    return $parameters;
  }

  /**
   * {@inheritdoc}
   */
  public function installDrupal() {
    $this
      ->initUserSession();
    $this
      ->prepareSettings();
    $this
      ->doInstall();
    $this
      ->initSettings();
    $request = Request::createFromGlobals();
    $container = $this
      ->initKernel($request);
    $this
      ->initConfig($container);

    // Add the config directories to settings.php.
    $sync_directory = Settings::get('config_sync_directory');
    \Drupal::service('file_system')
      ->prepareDirectory($sync_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);

    // Ensure the default temp directory exist and is writable. The configured
    // temp directory may be removed during update.
    \Drupal::service('file_system')
      ->prepareDirectory($this->tempFilesDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    $this
      ->installDefaultThemeFromClassProperty($container);
    $this
      ->installModulesFromClassProperty($container);
    $this
      ->rebuildAll();
    $this
      ->replaceUser1();
  }

  /**
   * Replace User 1 with the user created here.
   */
  protected function replaceUser1() {

    /** @var \Drupal\user\UserInterface $account */

    // @todo: Saving the account before the update is problematic.
    // https://www.drupal.org/node/2560237
    $account = User::load(1);
    $account
      ->setPassword($this->rootUser->pass_raw);
    $account
      ->setEmail($this->rootUser
      ->getEmail());
    $account
      ->setUsername($this->rootUser
      ->getAccountName());
    $account
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  protected function prepareSettings() {
    parent::prepareSettings();

    // Remember the profile which was used.
    $settings['settings']['install_profile'] = (object) [
      'value' => $this->profile,
      'required' => TRUE,
    ];

    // Generate a hash salt.
    $settings['settings']['hash_salt'] = (object) [
      'value' => Crypt::randomBytesBase64(55),
      'required' => TRUE,
    ];

    // Since the installer isn't run, add the database settings here too.
    $settings['databases']['default'] = (object) [
      'value' => Database::getConnectionInfo(),
      'required' => TRUE,
    ];

    // Force every update hook to only run one entity per batch.
    $settings['entity_update_batch_size'] = (object) [
      'value' => 1,
      'required' => TRUE,
    ];

    // Set up sync directory.
    $settings['settings']['config_sync_directory'] = (object) [
      'value' => $this->publicFilesDirectory . '/config_sync',
      'required' => TRUE,
    ];
    $this
      ->writeSettings($settings);
  }

  /**
   * {@inheritdoc}
   */
  protected function doInstall() {
    if (!empty($_SERVER['thunderDumpFile']) && file_exists($_SERVER['thunderDumpFile'])) {
      $file = $_SERVER['thunderDumpFile'];

      // Load the database.
      if (substr($file, -3) == '.gz') {
        $file = "compress.zlib://{$file}";
      }
      require $file;
    }
    else {
      parent::doInstall();
    }
  }

  /**
   * LogIn with defined role assigned to user.
   *
   * @param string $role
   *   Role name that will be assigned to user.
   */
  protected function logWithRole($role) {
    $editor = $this
      ->drupalCreateUser();
    $editor
      ->addRole($role);
    $editor
      ->save();
    $this
      ->drupalLogin($editor);
    return $editor;
  }

  /**
   * {@inheritdoc}
   */
  protected function tearDown() {

    /** @var \Drupal\Core\Database\Query\SelectInterface $query */
    $query = \Drupal::database()
      ->select('watchdog', 'w')
      ->fields('w', [
      'message',
      'variables',
    ]);
    $andGroup = $query
      ->andConditionGroup()
      ->condition('severity', 5, '<')
      ->condition('type', 'php');
    $group = $query
      ->orConditionGroup()
      ->condition('severity', 4, '<')
      ->condition($andGroup);
    $query
      ->condition($group);
    $query
      ->groupBy('w.message');
    $query
      ->groupBy('w.variables');
    $controller = DbLogController::create($this->container);

    // Check that there are no warnings in the log after installation.
    // $this->assertEqual($query->countQuery()->execute()->fetchField(), 0);.
    if ($query
      ->countQuery()
      ->execute()
      ->fetchField()) {

      // Output all errors for modules tested.
      $errors = [];
      foreach ($query
        ->execute()
        ->fetchAll() as $row) {
        $errors[] = Unicode::truncate(Html::decodeEntities(strip_tags($controller
          ->formatMessage($row))), 256, TRUE, TRUE);
      }
      throw new \Exception(print_r($errors, TRUE));
    }
    parent::tearDown();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThunderTestTrait::doInstall protected function
ThunderTestTrait::installDrupal public function
ThunderTestTrait::installParameters protected function
ThunderTestTrait::logWithRole protected function LogIn with defined role assigned to user.
ThunderTestTrait::prepareSettings protected function
ThunderTestTrait::replaceUser1 protected function Replace User 1 with the user created here.
ThunderTestTrait::tearDown protected function