Commit 127689c5 authored by Jonathan Hedstrom's avatar Jonathan Hedstrom Committed by Stefan Borchert
Browse files

Issue #3257879 by finagler, kreynen, jhedstrom, stBorchert: add cache context for user roles

parent bea70a54
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ class ShsTermCacheDependency implements CacheableDependencyInterface {
   * {@inheritdoc}
   */
  public function __construct($tags = []) {
    $this->contexts = ['languages:language_interface'];
    $this->contexts = ['languages:language_interface', 'user.roles'];
    $this->tags = Cache::mergeTags(['taxonomy_term_values'], $tags);
    $this->maxAge = Cache::PERMANENT;
  }
+41 −0
Original line number Diff line number Diff line
@@ -61,4 +61,45 @@ class ShsTermTest extends ShsTestBase {

  }

  /**
   * Tests caching of responses.
   */
  public function testRoleCache():void {
    $user_with_all_access = $this->drupalCreateUser(['administer taxonomy']);
    $user_with_view_access = $this->drupalCreateUser(['access content']);

    // Unpublish one of the terms.
    /** @var \Drupal\taxonomy\TermInterface $term */
    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($this->termIds['aaa 1']);
    $term->setUnpublished();
    $term->save();

    // The unpublished term should be in the json for the administrator.
    $this->drupalLogin($user_with_all_access);

    $field_name = 'shs-' . strtr($this->fieldName, ['_' => '-']);
    $request_url = "shs-term-data/{$field_name}/{$this->vocabulary->id()}/0";

    $data = $this->drupalGetJson($request_url);

    $this->assertCount(4, $data);

    $names = array_map(function ($a) {
      return $a['name'];
    }, $data);
    $this->assertContains('aaa 1', $names);

    // The other user should not see the unpublished term.
    $this->drupalLogin($user_with_view_access);

    $data = $this->drupalGetJson($request_url);

    $this->assertCount(3, $data);

    $names = array_map(function ($a) {
      return $a['name'];
    }, $data);
    $this->assertNotContains('aaa 1', $names);
  }

}