RefreshVariablesTrait.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace Drupal\Core\Test;
  3. use Drupal\Core\Cache\Cache;
  4. /**
  5. * Provides a method to refresh in-memory configuration and state information.
  6. */
  7. trait RefreshVariablesTrait {
  8. /**
  9. * Refreshes in-memory configuration and state information.
  10. *
  11. * Useful after a page request is made that changes configuration or state in
  12. * a different thread.
  13. *
  14. * In other words calling a settings page with $this->drupalPostForm() with a
  15. * changed value would update configuration to reflect that change, but in the
  16. * thread that made the call (thread running the test) the changed values
  17. * would not be picked up.
  18. *
  19. * This method clears the cache and loads a fresh copy.
  20. */
  21. protected function refreshVariables() {
  22. // Clear the tag cache.
  23. \Drupal::service('cache_tags.invalidator')->resetChecksums();
  24. foreach (Cache::getBins() as $backend) {
  25. if (is_callable([$backend, 'reset'])) {
  26. $backend->reset();
  27. }
  28. }
  29. \Drupal::service('config.factory')->reset();
  30. \Drupal::service('state')->resetCache();
  31. }
  32. }