示例#1
0
 /**
  * Tests exporting variables.
  *
  * @dataProvider providerTestExport
  * @covers ::export
  *
  * @param string $expected
  *   The expected exported variable.
  * @param mixed $variable
  *   The variable to be exported.
  */
 public function testExport($expected, $variable)
 {
     $this->assertEquals($expected, Variable::export($variable));
 }
示例#2
0
    /**
     * {@inheritdoc}
     */
    protected function setUp()
    {
        $this->keyValueFactory = new KeyValueMemoryFactory();
        // Back up settings from TestBase::prepareEnvironment().
        $settings = Settings::getAll();
        // Allow for test-specific overrides.
        $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
        $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
        $container_yamls = [];
        if (file_exists($settings_services_file)) {
            // Copy the testing-specific service overrides in place.
            $testing_services_file = $directory . '/services.yml';
            copy($settings_services_file, $testing_services_file);
            $container_yamls[] = $testing_services_file;
        }
        $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php';
        if (file_exists($settings_testing_file)) {
            // Copy the testing-specific settings.php overrides in place.
            copy($settings_testing_file, $directory . '/settings.testing.php');
        }
        if (file_exists($directory . '/settings.testing.php')) {
            // Add the name of the testing class to settings.php and include the
            // testing specific overrides
            $hash_salt = Settings::getHashSalt();
            $test_class = get_class($this);
            $container_yamls_export = Variable::export($container_yamls);
            $php = <<<EOD
<?php

\$settings['hash_salt'] = '{$hash_salt}';
\$settings['container_yamls'] = {$container_yamls_export};

\$test_class = '{$test_class}';
include DRUPAL_ROOT . '/' . \$site_path . '/settings.testing.php';
EOD;
            file_put_contents($directory . '/settings.php', $php);
        }
        // Add this test class as a service provider.
        // @todo Remove the indirection; implement ServiceProviderInterface instead.
        $GLOBALS['conf']['container_service_providers']['TestServiceProvider'] = 'Drupal\\simpletest\\TestServiceProvider';
        // Bootstrap a new kernel.
        $class_loader = (require DRUPAL_ROOT . '/autoload.php');
        $this->kernel = new DrupalKernel('testing', $class_loader, FALSE);
        $request = Request::create('/');
        $site_path = DrupalKernel::findSitePath($request);
        $this->kernel->setSitePath($site_path);
        if (file_exists($directory . '/settings.testing.php')) {
            Settings::initialize(DRUPAL_ROOT, $site_path, $class_loader);
        }
        $this->kernel->boot();
        // Ensure database install tasks have been run.
        require_once __DIR__ . '/../../../includes/install.inc';
        $connection = Database::getConnection();
        $errors = db_installer_object($connection->driver())->runTasks();
        if (!empty($errors)) {
            $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
        }
        // Reboot the kernel because the container might contain a connection to the
        // database that has been closed during the database install tasks. This
        // prevents any services created during the first boot from having stale
        // database connections, for example, \Drupal\Core\Config\DatabaseStorage.
        $this->kernel->shutdown();
        $this->kernel->boot();
        // Save the original site directory path, so that extensions in the
        // site-specific directory can still be discovered in the test site
        // environment.
        // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
        $settings['test_parent_site'] = $this->originalSite;
        // Restore and merge settings.
        // DrupalKernel::boot() initializes new Settings, and the containerBuild()
        // method sets additional settings.
        new Settings($settings + Settings::getAll());
        // Create and set new configuration directories.
        $this->prepareConfigDirectories();
        // Set the request scope.
        $this->container = $this->kernel->getContainer();
        $this->container->get('request_stack')->push($request);
        // Re-inject extension file listings into state, unless the key/value
        // service was overridden (in which case its storage does not exist yet).
        if ($this->container->get('keyvalue') instanceof KeyValueMemoryFactory) {
            $this->container->get('state')->set('system.module.files', $this->moduleFiles);
            $this->container->get('state')->set('system.theme.files', $this->themeFiles);
        }
        // Create a minimal core.extension configuration object so that the list of
        // enabled modules can be maintained allowing
        // \Drupal\Core\Config\ConfigInstaller::installDefaultConfig() to work.
        // Write directly to active storage to avoid early instantiation of
        // the event dispatcher which can prevent modules from registering events.
        \Drupal::service('config.storage')->write('core.extension', array('module' => array(), 'theme' => array()));
        // Collect and set a fixed module list.
        $class = get_class($this);
        $modules = array();
        while ($class) {
            if (property_exists($class, 'modules')) {
                // Only add the modules, if the $modules property was not inherited.
                $rp = new \ReflectionProperty($class, 'modules');
                if ($rp->class == $class) {
                    $modules[$class] = $class::$modules;
                }
            }
            $class = get_parent_class($class);
        }
        // Modules have been collected in reverse class hierarchy order; modules
        // defined by base classes should be sorted first. Then, merge the results
        // together.
        $modules = array_reverse($modules);
        $modules = call_user_func_array('array_merge_recursive', $modules);
        if ($modules) {
            $this->enableModules($modules);
        }
        // Tests based on this class are entitled to use Drupal's File and
        // StreamWrapper APIs.
        // @todo Move StreamWrapper management into DrupalKernel.
        // @see https://www.drupal.org/node/2028109
        file_prepare_directory($this->publicFilesDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
        $this->settingsSet('file_public_path', $this->publicFilesDirectory);
        $this->streamWrappers = array();
        $this->registerStreamWrapper('public', 'Drupal\\Core\\StreamWrapper\\PublicStream');
        // The temporary stream wrapper is able to operate both with and without
        // configuration.
        $this->registerStreamWrapper('temporary', 'Drupal\\Core\\StreamWrapper\\TemporaryStream');
    }
示例#3
0
 /**
  * The part of the script for each table.
  *
  * @param string $table
  *   Table name.
  * @param array $schema
  *   Drupal schema definition.
  * @param array $data
  *   Data for the table.
  *
  * @return string
  *   The table create statement, and if there is data, the insert command.
  */
 protected function getTableScript($table, array $schema, array $data)
 {
     $output = '';
     $output .= "\$connection->schema()->createTable('" . $table . "', " . Variable::export($schema) . ");\n\n";
     if (!empty($data)) {
         $insert = '';
         foreach ($data as $record) {
             $insert .= "->values(" . Variable::export($record) . ")\n";
         }
         $output .= "\$connection->insert('" . $table . "')\n" . "->fields(" . Variable::export(array_keys($schema['fields'])) . ")\n" . $insert . "->execute();\n\n";
     }
     return $output;
 }
 public static function debug($variable, $name = NULL)
 {
     print "\n" . ($name ? $name . ' >> ' : '') . \Drupal\Component\Utility\Variable::export($variable) . "\n";
 }
 /**
  * {@inheritdoc}
  */
 public function export($input, $name = NULL)
 {
     $name = $name ? $name . ' => ' : '';
     $dump = '<pre>' . $name . Variable::export($input) . '</pre>';
     return $this->setSafeMarkup($dump);
 }