diff core/lib/Drupal/Core/Test/RefreshVariablesTrait.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/lib/Drupal/Core/Test/RefreshVariablesTrait.php	Thu Feb 28 13:21:36 2019 +0000
@@ -0,0 +1,38 @@
+<?php
+
+namespace Drupal\Core\Test;
+
+use Drupal\Core\Cache\Cache;
+
+/**
+ * Provides a method to refresh in-memory configuration and state information.
+ */
+trait RefreshVariablesTrait {
+
+  /**
+   * Refreshes in-memory configuration and state information.
+   *
+   * Useful after a page request is made that changes configuration or state in
+   * a different thread.
+   *
+   * In other words calling a settings page with $this->drupalPostForm() with a
+   * changed value would update configuration to reflect that change, but in the
+   * thread that made the call (thread running the test) the changed values
+   * would not be picked up.
+   *
+   * This method clears the cache and loads a fresh copy.
+   */
+  protected function refreshVariables() {
+    // Clear the tag cache.
+    \Drupal::service('cache_tags.invalidator')->resetChecksums();
+    foreach (Cache::getBins() as $backend) {
+      if (is_callable([$backend, 'reset'])) {
+        $backend->reset();
+      }
+    }
+
+    \Drupal::service('config.factory')->reset();
+    \Drupal::service('state')->resetCache();
+  }
+
+}