annotate core/modules/node/tests/src/Functional/NodeAccessFieldTest.php @ 19:fa3358dc1485 tip

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