NodeAccessAutoBubblingTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\Core\Url;
  4. use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
  5. /**
  6. * Tests the node access automatic cacheability bubbling logic.
  7. *
  8. * @group node
  9. * @group Cache
  10. * @group cacheability_safeguards
  11. */
  12. class NodeAccessAutoBubblingTest extends NodeTestBase {
  13. use AssertPageCacheContextsAndTagsTrait;
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['node_access_test', 'node_access_test_auto_bubbling'];
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function setUp() {
  24. parent::setUp();
  25. node_access_rebuild();
  26. // Create some content.
  27. $this->drupalCreateNode();
  28. $this->drupalCreateNode();
  29. $this->drupalCreateNode();
  30. $this->drupalCreateNode();
  31. }
  32. /**
  33. * Tests that the node grants cache context is auto-added, only when needed.
  34. *
  35. * @see node_query_node_access_alter()
  36. */
  37. public function testNodeAccessCacheabilitySafeguard() {
  38. $this->dumpHeaders = TRUE;
  39. // The node grants cache context should be added automatically.
  40. $this->drupalGet(new Url('node_access_test_auto_bubbling'));
  41. $this->assertCacheContext('user.node_grants:view');
  42. // The root user has the 'bypass node access' permission, which means the
  43. // node grants cache context is not necessary.
  44. $this->drupalLogin($this->rootUser);
  45. $this->drupalGet(new Url('node_access_test_auto_bubbling'));
  46. $this->assertNoCacheContext('user.node_grants:view');
  47. $this->drupalLogout();
  48. // Uninstall the module with the only hook_node_grants() implementation.
  49. $this->container->get('module_installer')->uninstall(['node_access_test']);
  50. $this->rebuildContainer();
  51. // Because there are no node grants defined, there also is no need for the
  52. // node grants cache context to be bubbled.
  53. $this->drupalGet(new Url('node_access_test_auto_bubbling'));
  54. $this->assertNoCacheContext('user.node_grants:view');
  55. }
  56. }