/**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     \Drupal::unsetContainer();
     parent::setUp();
     $this->installSchema('system', ['router', 'url_alias']);
     \Drupal::service('router.builder')->rebuild();
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Ensure that an instantiated container in the global state of \Drupal from
     // a previous test does not leak into this test.
     \Drupal::unsetContainer();
     $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Store the previous container.
     $this->previousContainer = $this->container;
     $this->container = NULL;
     \Drupal::unsetContainer();
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Ensure that an instantiated container in the global state of \Drupal from
     // a previous test does not leak into this test.
     \Drupal::unsetContainer();
     // Ensure that the NullFileCache implementation is used for the FileCache as
     // unit tests should not be relying on caches implicitly.
     FileCacheFactory::setConfiguration(['default' => ['class' => '\\Drupal\\Component\\FileCache\\NullFileCache']]);
     $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Ensure that an instantiated container in the global state of \Drupal from
     // a previous test does not leak into this test.
     \Drupal::unsetContainer();
     // Ensure that the NullFileCache implementation is used for the FileCache as
     // unit tests should not be relying on caches implicitly.
     FileCacheFactory::setConfiguration([FileCacheFactory::DISABLE_CACHE => TRUE]);
     // Ensure that FileCacheFactory has a prefix.
     FileCacheFactory::setPrefix('prefix');
     $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Ensure that an instantiated container in the global state of \Drupal from
     // a previous test does not leak into this test.
     \Drupal::unsetContainer();
     // Ensure that the NullFileCache implementation is used for the FileCache as
     // unit tests should not be relying on caches implicitly.
     FileCacheFactory::setConfiguration(['default' => ['class' => '\\Drupal\\Component\\FileCache\\NullFileCache']]);
     $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
     // Reset the static list of SafeStrings to prevent bleeding between tests.
     $reflected_class = new \ReflectionClass('\\Drupal\\Component\\Utility\\SafeMarkup');
     $reflected_property = $reflected_class->getProperty('safeStrings');
     $reflected_property->setAccessible(true);
     $reflected_property->setValue([]);
 }
Example #7
0
 /**
  * Returns all supported database driver installer objects.
  *
  * This wraps drupal_get_database_types() for use without a current container.
  *
  * @return \Drupal\Core\Database\Install\Tasks[]
  *   An array of available database driver installer objects.
  */
 protected function getDatabaseTypes()
 {
     \Drupal::setContainer($this->originalContainer);
     $database_types = drupal_get_database_types();
     \Drupal::unsetContainer();
     return $database_types;
 }
 /**
  * {@inheritdoc}
  */
 public function tearDown()
 {
     \Drupal::unsetContainer();
 }
Example #9
0
 /**
  * Prepares the current environment for running the test.
  *
  * Also sets up new resources for the testing environment, such as the public
  * filesystem and configuration directories.
  *
  * This method is private as it must only be called once by
  * BrowserTestBase::setUp() (multiple invocations for the same test would have
  * unpredictable consequences) and it must not be callable or overridable by
  * test classes.
  */
 protected function prepareEnvironment()
 {
     // Bootstrap Drupal so we can use Drupal's built in functions.
     $this->classLoader = (require __DIR__ . '/../../../../autoload.php');
     $request = Request::createFromGlobals();
     $kernel = TestRunnerKernel::createFromRequest($request, $this->classLoader);
     // TestRunnerKernel expects the working directory to be DRUPAL_ROOT.
     chdir(DRUPAL_ROOT);
     $kernel->prepareLegacyRequest($request);
     $this->prepareDatabasePrefix();
     $this->originalSiteDirectory = $kernel->findSitePath($request);
     // Create test directory ahead of installation so fatal errors and debug
     // information can be logged during installation process.
     file_prepare_directory($this->siteDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     // Prepare filesystem directory paths.
     $this->publicFilesDirectory = $this->siteDirectory . '/files';
     $this->privateFilesDirectory = $this->siteDirectory . '/private';
     $this->tempFilesDirectory = $this->siteDirectory . '/temp';
     $this->translationFilesDirectory = $this->siteDirectory . '/translations';
     // Ensure the configImporter is refreshed for each test.
     $this->configImporter = NULL;
     // Unregister all custom stream wrappers of the parent site.
     $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::ALL);
     foreach ($wrappers as $scheme => $info) {
         stream_wrapper_unregister($scheme);
     }
     // Reset statics.
     drupal_static_reset();
     // Ensure there is no service container.
     $this->container = NULL;
     \Drupal::unsetContainer();
     // Unset globals.
     unset($GLOBALS['config_directories']);
     unset($GLOBALS['config']);
     unset($GLOBALS['conf']);
     // Log fatal errors.
     ini_set('log_errors', 1);
     ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
     // Change the database prefix.
     $this->changeDatabasePrefix();
     // After preparing the environment and changing the database prefix, we are
     // in a valid test environment.
     drupal_valid_test_ua($this->databasePrefix);
     // Reset settings.
     new Settings(array('hash_salt' => $this->databasePrefix));
     drupal_set_time_limit($this->timeLimit);
 }
Example #10
0
 /**
  * Prepares the current environment for running the test.
  *
  * Backups various current environment variables and resets them, so they do
  * not interfere with the Drupal site installation in which tests are executed
  * and can be restored in TestBase::restoreEnvironment().
  *
  * Also sets up new resources for the testing environment, such as the public
  * filesystem and configuration directories.
  *
  * This method is private as it must only be called once by TestBase::run()
  * (multiple invocations for the same test would have unpredictable
  * consequences) and it must not be callable or overridable by test classes.
  *
  * @see TestBase::beforePrepareEnvironment()
  */
 private function prepareEnvironment()
 {
     $user = \Drupal::currentUser();
     // Allow (base) test classes to backup global state information.
     $this->beforePrepareEnvironment();
     // Create the database prefix for this test.
     $this->prepareDatabasePrefix();
     $language_interface = \Drupal::languageManager()->getCurrentLanguage();
     // When running the test runner within a test, back up the original database
     // prefix.
     if (DRUPAL_TEST_IN_CHILD_SITE) {
         $this->originalPrefix = drupal_valid_test_ua();
     }
     // Backup current in-memory configuration.
     $site_path = \Drupal::service('site.path');
     $this->originalSite = $site_path;
     $this->originalSettings = Settings::getAll();
     $this->originalConfig = $GLOBALS['config'];
     // @todo Remove all remnants of $GLOBALS['conf'].
     // @see https://www.drupal.org/node/2183323
     $this->originalConf = isset($GLOBALS['conf']) ? $GLOBALS['conf'] : NULL;
     // Backup statics and globals.
     $this->originalContainer = clone \Drupal::getContainer();
     $this->originalLanguage = $language_interface;
     $this->originalConfigDirectories = $GLOBALS['config_directories'];
     // Save further contextual information.
     // Use the original files directory to avoid nesting it within an existing
     // simpletest directory if a test is executed within a test.
     $this->originalFileDirectory = Settings::get('file_public_path', $site_path . '/files');
     $this->originalProfile = drupal_get_profile();
     $this->originalUser = isset($user) ? clone $user : NULL;
     // Prevent that session data is leaked into the UI test runner by closing
     // the session and then setting the session-name (i.e. the name of the
     // session cookie) to a random value. If a test starts a new session, then
     // it will be associated with a different session-name. After the test-run
     // it can be safely destroyed.
     // @see TestBase::restoreEnvironment()
     if (PHP_SAPI !== 'cli' && session_status() === PHP_SESSION_ACTIVE) {
         session_write_close();
     }
     $this->originalSessionName = session_name();
     session_name('SIMPLETEST' . Crypt::randomBytesBase64());
     // Save and clean the shutdown callbacks array because it is static cached
     // and will be changed by the test run. Otherwise it will contain callbacks
     // from both environments and the testing environment will try to call the
     // handlers defined by the original one.
     $callbacks =& drupal_register_shutdown_function();
     $this->originalShutdownCallbacks = $callbacks;
     $callbacks = array();
     // Create test directory ahead of installation so fatal errors and debug
     // information can be logged during installation process.
     file_prepare_directory($this->siteDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     // Prepare filesystem directory paths.
     $this->publicFilesDirectory = $this->siteDirectory . '/files';
     $this->privateFilesDirectory = $this->siteDirectory . '/private';
     $this->tempFilesDirectory = $this->siteDirectory . '/temp';
     $this->translationFilesDirectory = $this->siteDirectory . '/translations';
     $this->generatedTestFiles = FALSE;
     // Ensure the configImporter is refreshed for each test.
     $this->configImporter = NULL;
     // Unregister all custom stream wrappers of the parent site.
     // Availability of Drupal stream wrappers varies by test base class:
     // - KernelTestBase supports and maintains stream wrappers in a custom
     //   way.
     // - WebTestBase re-initializes Drupal stream wrappers after installation.
     // The original stream wrappers are restored after the test run.
     // @see TestBase::restoreEnvironment()
     $this->originalContainer->get('stream_wrapper_manager')->unregister();
     // Reset statics.
     drupal_static_reset();
     // Ensure there is no service container.
     $this->container = NULL;
     \Drupal::unsetContainer();
     // Unset globals.
     unset($GLOBALS['config_directories']);
     unset($GLOBALS['config']);
     unset($GLOBALS['conf']);
     // Log fatal errors.
     ini_set('log_errors', 1);
     ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
     // Change the database prefix.
     $this->changeDatabasePrefix();
     // After preparing the environment and changing the database prefix, we are
     // in a valid test environment.
     drupal_valid_test_ua($this->databasePrefix);
     // Reset settings.
     new Settings(array('hash_salt' => $this->databasePrefix, 'container_yamls' => []));
     drupal_set_time_limit($this->timeLimit);
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 protected function tearDown()
 {
     // Destroy the testing kernel.
     if (isset($this->kernel)) {
         $this->kernel->shutdown();
     }
     // Remove all prefixed tables.
     $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
     $original_prefix = $original_connection_info['default']['prefix']['default'];
     $test_connection_info = Database::getConnectionInfo('default');
     $test_prefix = $test_connection_info['default']['prefix']['default'];
     if ($original_prefix != $test_prefix) {
         $tables = Database::getConnection()->schema()->findTables('%');
         foreach ($tables as $table) {
             if (Database::getConnection()->schema()->dropTable($table)) {
                 unset($tables[$table]);
             }
         }
     }
     // Free up memory: Own properties.
     $this->classLoader = NULL;
     $this->vfsRoot = NULL;
     $this->configImporter = NULL;
     // Free up memory: Custom test class properties.
     // Note: Private properties cannot be cleaned up.
     $rc = new \ReflectionClass(__CLASS__);
     $blacklist = array();
     foreach ($rc->getProperties() as $property) {
         $blacklist[$property->name] = $property->getDeclaringClass()->name;
     }
     $rc = new \ReflectionClass($this);
     foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $property) {
         if (!$property->isStatic() && !isset($blacklist[$property->name])) {
             $this->{$property->name} = NULL;
         }
     }
     // Clean FileCache cache.
     FileCache::reset();
     // Clean up statics, container, and settings.
     if (function_exists('drupal_static_reset')) {
         drupal_static_reset();
     }
     \Drupal::unsetContainer();
     $this->container = NULL;
     new Settings(array());
     parent::tearDown();
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     \Drupal::unsetContainer();
     parent::setUp();
     \Drupal::service('router.builder')->rebuild();
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 protected function prepareConfigDirectories()
 {
     \Drupal::setContainer($this->originalContainer);
     parent::prepareConfigDirectories();
     \Drupal::unsetContainer();
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 protected function tearDown()
 {
     // Destroy the testing kernel.
     if (isset($this->kernel)) {
         $this->kernel->shutdown();
     }
     // Free up memory: Own properties.
     $this->classLoader = NULL;
     $this->vfsRoot = NULL;
     $this->configImporter = NULL;
     // Free up memory: Custom test class properties.
     // Note: Private properties cannot be cleaned up.
     $rc = new \ReflectionClass(__CLASS__);
     $blacklist = array();
     foreach ($rc->getProperties() as $property) {
         $blacklist[$property->name] = $property->getDeclaringClass()->name;
     }
     $rc = new \ReflectionClass($this);
     foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED) as $property) {
         if (!$property->isStatic() && !isset($blacklist[$property->name])) {
             $this->{$property->name} = NULL;
         }
     }
     // Clean up statics, container, and settings.
     if (function_exists('drupal_static_reset')) {
         drupal_static_reset();
     }
     \Drupal::unsetContainer();
     $this->container = NULL;
     new Settings(array());
     // Destroy the database connection, which for example removes the memory
     // from sqlite in memory.
     foreach (Database::getAllConnectionInfo() as $key => $targets) {
         Database::removeConnection($key);
     }
     parent::tearDown();
 }