annotate core/modules/node/tests/src/Functional/NodeAccessFieldTest.php @ 6:875880e46745

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