You are here

class UpdatePathTestInstallHelper in Style Switcher 3.0.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/Update/UpdatePathTestInstallHelper.php \Drupal\Tests\styleswitcher\Functional\Update\UpdatePathTestInstallHelper

Helps install database additions for a testing of update paths.

Hierarchy

Expanded class hierarchy of UpdatePathTestInstallHelper

1 file declares its use of UpdatePathTestInstallHelper
drupal-8.8.0.styleswitcher.php in tests/fixtures/update/8201/drupal-8.8.0.styleswitcher.php
DB additions for the update path testing of styleswitcher_update_8201().

File

tests/src/Functional/Update/UpdatePathTestInstallHelper.php, line 12

Namespace

Drupal\Tests\styleswitcher\Functional\Update
View source
class UpdatePathTestInstallHelper {

  /**
   * The DB connection object.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected static $connection;

  /**
   * Gets the DB connection object.
   *
   * @return \Drupal\Core\Database\Connection
   *   The DB connection object.
   */
  protected static function getConnection() : Connection {
    if (!isset(static::$connection)) {
      static::$connection = Database::getConnection();
    }
    return static::$connection;
  }

  /**
   * Enables a module or theme by adding it to the core.extension config.
   *
   * @param string $name
   *   Extension name.
   * @param int $weight
   *   (optional) Extension weight.
   * @param string $type
   *   (optional) Extension type (module/theme).
   */
  public static function enableExtension(string $name, int $weight = 0, string $type = 'module') {
    $extensions = static::getConnection()
      ->select('config')
      ->fields('config', [
      'data',
    ])
      ->condition('collection', '')
      ->condition('name', 'core.extension')
      ->execute()
      ->fetchField();
    $extensions = unserialize($extensions);
    $extensions[$type][$name] = $weight;
    $extensions = serialize($extensions);
    static::getConnection()
      ->update('config')
      ->fields([
      'data' => $extensions,
    ])
      ->condition('collection', '')
      ->condition('name', 'core.extension')
      ->execute();
  }

  /**
   * Sets a schema version for a module or profile.
   *
   * @param string $name
   *   Extension name.
   * @param int $version
   *   Version to set, e.g. 8000.
   */
  public static function setSchemaVersion(string $name, int $version) {
    static::getConnection()
      ->merge('key_value')
      ->condition('collection', 'system.schema')
      ->condition('name', $name)
      ->fields([
      'collection' => 'system.schema',
      'name' => $name,
      'value' => serialize($version),
    ])
      ->execute();
  }

  /**
   * Imports a config from a YAML file.
   *
   * @param string $name
   *   Config name.
   * @param string $filename
   *   Path and name of the YAML file to import.
   */
  public static function addConfigYml(string $name, string $filename) {
    $config = Yaml::decode(file_get_contents($filename));
    static::getConnection()
      ->insert('config')
      ->fields([
      'collection' => '',
      'name' => $name,
      'data' => serialize($config),
    ])
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UpdatePathTestInstallHelper::$connection protected static property The DB connection object.
UpdatePathTestInstallHelper::addConfigYml public static function Imports a config from a YAML file.
UpdatePathTestInstallHelper::enableExtension public static function Enables a module or theme by adding it to the core.extension config.
UpdatePathTestInstallHelper::getConnection protected static function Gets the DB connection object.
UpdatePathTestInstallHelper::setSchemaVersion public static function Sets a schema version for a module or profile.