NodeAccessFieldTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\field\Entity\FieldConfig;
  4. use Drupal\field\Entity\FieldStorageConfig;
  5. /**
  6. * Tests the interaction of the node access system with fields.
  7. *
  8. * @group node
  9. */
  10. class NodeAccessFieldTest extends NodeTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = ['node_access_test', 'field_ui'];
  17. /**
  18. * A user with permission to bypass access content.
  19. *
  20. * @var \Drupal\user\UserInterface
  21. */
  22. protected $adminUser;
  23. /**
  24. * A user with permission to manage content types and fields.
  25. *
  26. * @var \Drupal\user\UserInterface
  27. */
  28. protected $contentAdminUser;
  29. /**
  30. * The name of the created field.
  31. *
  32. * @var string
  33. */
  34. protected $fieldName;
  35. protected function setUp() {
  36. parent::setUp();
  37. node_access_rebuild();
  38. // Create some users.
  39. $this->adminUser = $this->drupalCreateUser(['access content', 'bypass node access']);
  40. $this->contentAdminUser = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields']);
  41. // Add a custom field to the page content type.
  42. $this->fieldName = mb_strtolower($this->randomMachineName() . '_field_name');
  43. FieldStorageConfig::create([
  44. 'field_name' => $this->fieldName,
  45. 'entity_type' => 'node',
  46. 'type' => 'text',
  47. ])->save();
  48. FieldConfig::create([
  49. 'field_name' => $this->fieldName,
  50. 'entity_type' => 'node',
  51. 'bundle' => 'page',
  52. ])->save();
  53. entity_get_display('node', 'page', 'default')
  54. ->setComponent($this->fieldName)
  55. ->save();
  56. entity_get_form_display('node', 'page', 'default')
  57. ->setComponent($this->fieldName)
  58. ->save();
  59. }
  60. /**
  61. * Tests administering fields when node access is restricted.
  62. */
  63. public function testNodeAccessAdministerField() {
  64. // Create a page node.
  65. $fieldData = [];
  66. $value = $fieldData[0]['value'] = $this->randomMachineName();
  67. $node = $this->drupalCreateNode([$this->fieldName => $fieldData]);
  68. // Log in as the administrator and confirm that the field value is present.
  69. $this->drupalLogin($this->adminUser);
  70. $this->drupalGet('node/' . $node->id());
  71. $this->assertText($value, 'The saved field value is visible to an administrator.');
  72. // Log in as the content admin and try to view the node.
  73. $this->drupalLogin($this->contentAdminUser);
  74. $this->drupalGet('node/' . $node->id());
  75. $this->assertText('Access denied', 'Access is denied for the content admin.');
  76. // Modify the field default as the content admin.
  77. $edit = [];
  78. $default = 'Sometimes words have two meanings';
  79. $edit["default_value_input[{$this->fieldName}][0][value]"] = $default;
  80. $this->drupalPostForm(
  81. "admin/structure/types/manage/page/fields/node.page.{$this->fieldName}",
  82. $edit,
  83. t('Save settings')
  84. );
  85. // Log in as the administrator.
  86. $this->drupalLogin($this->adminUser);
  87. // Confirm that the existing node still has the correct field value.
  88. $this->drupalGet('node/' . $node->id());
  89. $this->assertText($value, 'The original field value is visible to an administrator.');
  90. // Confirm that the new default value appears when creating a new node.
  91. $this->drupalGet('node/add/page');
  92. $this->assertRaw($default, 'The updated default value is displayed when creating a new node.');
  93. }
  94. }