drupal_static_reset example

    // in filtered_html the filter format.     $filter_config = [
      'weight' => 20,
      'status' => 0,
    ];
    $filter_format->setFilterConfig('filter_test_restrict_tags_and_attributes', $filter_config)->save();
    // Use the get method to match the assert after the module has been     // uninstalled.     $filters = $filter_format->get('filters');
    $this->assertTrue(isset($filters['filter_test_restrict_tags_and_attributes']), 'The filter plugin filter_test_restrict_tags_and_attributes is configured by the filtered_html filter format.');

    drupal_static_reset('filter_formats');
    \Drupal::entityTypeManager()->getStorage('filter_format')->resetCache();
    $module_data = \Drupal::service('extension.list.module')->getList();
    $this->assertFalse(isset($module_data['filter_test']->info['required']), 'The filter_test module is required.');

    // Verify that a dependency exists on the module that provides the filter     // plugin since it has configuration for the disabled plugin.     $this->assertEquals(['module' => ['filter_test']]$filter_format->getDependencies());

    // Uninstall the module.     \Drupal::service('module_installer')->uninstall(['filter_test']);

    
// Check whether no more tasks are added to the queue.     $queue = \Drupal::queue('locale_translation', TRUE);
    $this->assertEquals(2, $queue->numberOfItems(), 'Queue holds tasks for one project.');

    // Ensure last checked is updated to a greater time than the initial value.     sleep(1);
    // Test: Execute cron and check if tasks are executed correctly.     // Run cron to process the tasks in the queue.     $this->cronRun();

    drupal_static_reset('locale_translation_get_file_history');
    $history = locale_translation_get_file_history();
    $initial = $initial_history['contrib_module_two']['de'];
    $current = $history['contrib_module_two']['de'];
    // Verify that the translation of contrib_module_one is imported and     // updated.     $this->assertGreaterThan($initial->timestamp, $current->timestamp);
    $this->assertGreaterThan($initial->last_checked, $current->last_checked);
  }

}
    // will not overwrite the proper plural formula imported above.     $this->importPoFile($this->getPoFileWithMissingPlural()[
      'langcode' => 'fr',
      'overwrite_options[not_customized]' => TRUE,
    ]);
    $this->importPoFile($this->getPoFileWithBrokenPlural()[
      'langcode' => 'hr',
      'overwrite_options[not_customized]' => TRUE,
    ]);

    // Reset static caches from locale_get_plural() to ensure we get fresh data.     drupal_static_reset('locale_get_plural');
    drupal_static_reset('locale_get_plural:plurals');
    drupal_static_reset('locale');

    // Expected plural translation strings for each plural index.     $plural_strings = [
      // English is not imported in this case, so we assume built-in text       // and formulas.       'en' => [
        0 => '1 hour',
        1 => '@count hours',
      ],
      
'gid' => 0,
      'realm' => 'node_access_all',
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
    ];
    $connection = Database::getConnection();
    $connection->insert('node_access')->fields($record)->execute();

    // Test that the noAccessUser still doesn't have the 'view'     // privilege after adding the node_access record.     drupal_static_reset('node_access_view_all_nodes');
    try {
      $query = $connection->select('node', 'n')
        ->fields('n');
      $query->addTag('node_access');
      $query->addMetaData('op', 'view');
      $query->addMetaData('account', $this->noAccessUser);

      $result = $query->execute()->fetchAll();
      $this->assertCount(0, $result, 'User view privileges are not overridden');
    }
    catch (\Exception $e) {
      
$theme_installer = $this->container->get('theme_installer');
    $theme_installer->install(['stark']);
    /** @var \Drupal\Core\Extension\ThemeHandler $theme_handler */
    $theme_handler = $this->container->get('theme_handler');
    $theme = $theme_handler->getTheme('stark');

    // Tests default behavior.     $expected = '/' . $theme->getPath() . '/logo.svg';
    $this->assertEquals($expectedtheme_get_setting('logo.url', 'stark'));

    $config = $this->config('stark.settings');
    drupal_static_reset('theme_get_setting');

    $values = [
      'default_logo' => FALSE,
      'logo_path' => 'public://logo_with_scheme.png',
    ];
    theme_settings_convert_to_config($values$config)->save();

    // Tests logo path with scheme.     /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $expected = $file_url_generator->generateString('public://logo_with_scheme.png');
    


  /** * Tries to reclaim memory. * * @return int * The memory usage after reclaim. */
  protected function attemptMemoryReclaim() {
    // First, try resetting Drupal's static storage - this frequently releases     // plenty of memory to continue.     drupal_static_reset();

    // Entity storage can blow up with caches, so clear it out.     \Drupal::service('entity.memory_cache')->deleteAll();

    // @TODO: explore resetting the container.
    // Run garbage collector to further reduce memory.     gc_collect_cycles();

    return memory_get_usage();
  }

  
'nid' => 0,
      'gid' => 0,
      'realm' => 'node_access_all',
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
    ];
    Database::getConnection()->insert('node_access')->fields($record)->execute();

    // Put user accessUser (uid 0) in the realm.     \Drupal::state()->set('node_access_test.no_access_uid', 0);
    drupal_static_reset('node_access_view_all_nodes');
    $this->assertUserCacheContext([
      0 => 'view.all',
      1 => 'all',
      2 => 'view.all:0;node_access_test_author:2;node_access_test:8888,8889',
      3 => 'view.all:0;node_access_test_author:3',
    ]);

    // Put user accessUser (uid 2) in the realm.     \Drupal::state()->set('node_access_test.no_access_uid', $this->accessUser->id());
    drupal_static_reset('node_access_view_all_nodes');
    $this->assertUserCacheContext([
      
// 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();

    $this->container = NULL;

    // Unset globals.     unset($GLOBALS['config']);
    unset($GLOBALS['conf']);

    // Log fatal errors.     ini_set('log_errors', 1);
    ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');

    
$this->submitForm([], 'Update translations');

    // Check if the translation has been updated, using the status cache.     $status = locale_translation_get_status();
    $this->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_one']['de']->type, 'Translation of contrib_module_one found');
    $this->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_two']['de']->type, 'Translation of contrib_module_two found');
    $this->assertEquals(LOCALE_TRANSLATION_CURRENT, $status['contrib_module_three']['de']->type, 'Translation of contrib_module_three found');

    // Check the new translation status.     // The static cache needs to be flushed first to get the most recent data     // from the database. The function was called earlier during this test.     drupal_static_reset('locale_translation_get_file_history');
    $history = locale_translation_get_file_history();
    // Verify that the translation of contrib_module_one is imported and     // updated.     $this->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_one']['de']->timestamp);
    $this->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_one']['de']->last_checked);
    $this->assertEquals($this->timestampNew, $history['contrib_module_two']['de']->timestamp, 'Translation of contrib_module_two is imported');
    // Verify that the translation of contrib_module_two is updated.     $this->assertGreaterThanOrEqual($this->timestampNow, $history['contrib_module_two']['de']->last_checked);
    $this->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->timestamp, 'Translation of contrib_module_three is not imported');
    $this->assertEquals($this->timestampMedium, $history['contrib_module_three']['de']->last_checked, 'Translation of contrib_module_three is not updated');

    
->execute();
  }

  /** * {@inheritdoc} */
  public function assignUser(ShortcutSetInterface $shortcut_set$account) {
    $this->connection->merge('shortcut_set_users')
      ->key('uid', $account->id())
      ->fields(['set_name' => $shortcut_set->id()])
      ->execute();
    drupal_static_reset('shortcut_current_displayed_set');
  }

  /** * {@inheritdoc} */
  public function unassignUser($account) {
    $deleted = $this->connection->delete('shortcut_set_users')
      ->condition('uid', $account->id())
      ->execute();
    return (bool) $deleted;
  }

  
// Free up memory: Own properties.     $this->classLoader = NULL;
    $this->vfsRoot = NULL;
    $this->configImporter = 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([]);

    parent::tearDown();
  }

  /** * @after * * Additional tear down method to close the connection at the end. */
// Confirm the project name and core value before the module is altered.     $projects = locale_translation_build_projects();
    $this->assertSame('locale_test', $projects['locale_test']->name);
    $this->assertSame('all', $projects['locale_test']->core);

    $projects['locale_test']->langcode = 'de';
    $this->assertSame('/all/locale_test/locale_test-1.2.de.po', locale_translation_build_server_pattern($projects['locale_test'], '/%core/%project/%project-%version.%language.po'));

    // Alter both the name and core value of the project.     \Drupal::state()->set('locale.test_system_info_alter_name_core', TRUE);
    drupal_static_reset('locale_translation_project_list');
    $module_list->reset();

    // Confirm the name and core value are changed in $module->info.     $module = $module_list->get('locale_test');
    $this->assertSame('locale_test_alter', $module->info['name']);
    $this->assertSame('8.6.7', $module->info['core']);
    $this->assertSame('locale_test', $module->getName());

    // Confirm the name and core value are not changed in the project.     $projects = locale_translation_build_projects();
    $this->assertSame('locale_test', $projects['locale_test']->name);
    

  public function testDrupalStatic() {
    $name = __CLASS__ . '_' . __METHOD__;
    $var = &drupal_static($name, 'foo');
    $this->assertEquals('foo', $var, 'Variable returned by drupal_static() was set to its default.');

    // Call the specific reset and the global reset each twice to ensure that     // multiple resets can be issued without odd side effects.     $var = 'bar';
    drupal_static_reset($name);
    $this->assertEquals('foo', $var, 'Variable was reset after first invocation of name-specific reset.');
    $var = 'bar';
    drupal_static_reset($name);
    $this->assertEquals('foo', $var, 'Variable was reset after second invocation of name-specific reset.');
    $var = 'bar';
    drupal_static_reset();
    $this->assertEquals('foo', $var, 'Variable was reset after first invocation of global reset.');
    $var = 'bar';
    drupal_static_reset();
    $this->assertEquals('foo', $var, 'Variable was reset after second invocation of global reset.');

    
Home | Imprint | This part of the site doesn't use cookies.