class CacheableMetadataCalculationTest

Same name in this branch
  1. 9 core/modules/views/tests/src/Kernel/CacheableMetadataCalculationTest.php \Drupal\Tests\views\Kernel\CacheableMetadataCalculationTest
Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Kernel/CacheableMetadataCalculationTest.php \Drupal\Tests\views\Kernel\CacheableMetadataCalculationTest
  2. 11.x core/modules/views/tests/modules/views_test_cacheable_metadata_calculation/src/Plugin/views/access/CacheableMetadataCalculationTest.php \Drupal\views_test_cacheable_metadata_calculation\Plugin\views\access\CacheableMetadataCalculationTest

Tests plugin that reports when cacheable metadata is being calculated.

Plugin annotation


@ViewsAccess(
  id = "test_cacheable_metadata_access",
  title = @Translation("Cacheable metadata calculation test access plugin"),
  help = @Translation("Provides a test access plugin that reports when cacheable metadata is being calculated.")
)

Hierarchy

Expanded class hierarchy of CacheableMetadataCalculationTest

File

core/modules/views/tests/modules/views_test_cacheable_metadata_calculation/src/Plugin/views/access/CacheableMetadataCalculationTest.php, line 22

Namespace

Drupal\views_test_cacheable_metadata_calculation\Plugin\views\access
View source
class CacheableMetadataCalculationTest extends AccessPluginBase implements CacheableDependencyInterface {
  
  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;
  
  /**
   * Constructs a CacheableMetadataCalculationTest access plugin.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->state = $state;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('state'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account) {
    return TRUE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function alterRouteDefinition(Route $route) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    $this->cacheableMetadataHasBeenAccessed();
    return [];
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $this->cacheableMetadataHasBeenAccessed();
    return [];
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    $this->cacheableMetadataHasBeenAccessed();
    return Cache::PERMANENT;
  }
  
  /**
   * Sets a flag to inform tests that cacheable metadata has been accessed.
   */
  protected function cacheableMetadataHasBeenAccessed() {
    $this->state
      ->set('views_test_cacheable_metadata_has_been_accessed', TRUE);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.