Commit c2bc0d52 authored by mxh's avatar mxh
Browse files

Issue #3257050 by rkoller, mxh: AssertionError: Failed to assert that...

Issue #3257050 by rkoller, mxh: AssertionError: Failed to assert that "context_stack:account:..." are valid cache contexts
parent e1d215d8
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ class ContextCollection implements ContextCollectionInterface {

  use DependencySerializationTrait {
    __sleep as goodNightServices;
    __wakeup as goodMorningServices;
  }

  /**
@@ -435,6 +436,19 @@ class ContextCollection implements ContextCollectionInterface {
    });
  }

  /**
   * Implements the magic wakeup method.
   */
  public function __wakeup() {
    if (!$this->contextStackCacheContextIsAvailable()) {
      // When the "context_stack" cache context is missing on the next process,
      // we cannot apply proper caching on this collection.
      $this->cacheContexts = [];
      $this->cacheMaxAge = 0;
    }
    $this->goodMorningServices();
  }

  /**
   * Check whether the service for "context_stack" cache contexts is available.
   *
@@ -442,7 +456,7 @@ class ContextCollection implements ContextCollectionInterface {
   *   Returns TRUE if available, FALSE otherwise.
   */
  protected function contextStackCacheContextIsAvailable(): bool {
    return in_array('context_stack', \Drupal::getContainer()->getParameter('cache_contexts'));
    return \Drupal::hasContainer() && in_array('context_stack', \Drupal::getContainer()->getParameter('cache_contexts'));
  }

}
+3 −2
Original line number Diff line number Diff line
@@ -175,7 +175,8 @@ class ContextStack extends ContextCollection implements ContextStackInterface {
   */
  public function getCacheContexts() {
    $cache_contexts = parent::getCacheContexts();
    if ($this->hasPurpose() && $this->contextStackCacheContextIsAvailable()) {
    $cache_context_is_avalable = $this->contextStackCacheContextIsAvailable();
    if ($this->hasPurpose() && $cache_context_is_avalable) {
      $cache_context_parameterized = 'context_stack:' . $this->getPurpose() . ':' . $this->generateChecksum();
      if (!in_array($cache_context_parameterized, $cache_contexts, TRUE)) {
        $cache_contexts[] = $cache_context_parameterized;
@@ -191,7 +192,7 @@ class ContextStack extends ContextCollection implements ContextStackInterface {
        break;
      }
    }
    if (!$has_cache_context) {
    if (!$has_cache_context && $cache_context_is_avalable) {
      $cache_contexts[] = 'context_stack';
    }

+1 −1
Original line number Diff line number Diff line
@@ -213,7 +213,7 @@ trait ContextStackTrait {
   *   Returns TRUE if available, FALSE otherwise.
   */
  protected function contextStackCacheContextIsAvailable(): bool {
    return in_array('context_stack', \Drupal::getContainer()->getParameter('cache_contexts'));
    return \Drupal::hasContainer() && in_array('context_stack', \Drupal::getContainer()->getParameter('cache_contexts'));
  }

}