You are here

NodeAccessFieldTest.php in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 core/modules/node/src/Tests/NodeAccessFieldTest.php

Namespace

Drupal\node\Tests

File

core/modules/node/src/Tests/NodeAccessFieldTest.php
View source
<?php

/**
 * @file
 * Contains \Drupal\node\Tests\NodeAccessFieldTest.
 */
namespace Drupal\node\Tests;

use Drupal\Component\Utility\Unicode;

/**
 * Tests the interaction of the node access system with fields.
 *
 * @group node
 */
class NodeAccessFieldTest extends NodeTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'node_access_test',
    'field_ui',
  );

  /**
   * A user with permission to bypass access content.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * A user with permission to manage content types and fields.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $contentAdminUser;

  /**
   * The name of the created field.
   *
   * @var string
   */
  protected $fieldName;
  protected function setUp() {
    parent::setUp();
    node_access_rebuild();

    // Create some users.
    $this->adminUser = $this
      ->drupalCreateUser(array(
      'access content',
      'bypass node access',
    ));
    $this->contentAdminUser = $this
      ->drupalCreateUser(array(
      'access content',
      'administer content types',
      'administer node fields',
    ));

    // Add a custom field to the page content type.
    $this->fieldName = Unicode::strtolower($this
      ->randomMachineName() . '_field_name');
    entity_create('field_storage_config', array(
      'field_name' => $this->fieldName,
      'entity_type' => 'node',
      'type' => 'text',
    ))
      ->save();
    entity_create('field_config', array(
      'field_name' => $this->fieldName,
      'entity_type' => 'node',
      'bundle' => 'page',
    ))
      ->save();
    entity_get_display('node', 'page', 'default')
      ->setComponent($this->fieldName)
      ->save();
    entity_get_form_display('node', 'page', 'default')
      ->setComponent($this->fieldName)
      ->save();
  }

  /**
   * Tests administering fields when node access is restricted.
   */
  function testNodeAccessAdministerField() {

    // Create a page node.
    $fieldData = array();
    $value = $fieldData[0]['value'] = $this
      ->randomMachineName();
    $node = $this
      ->drupalCreateNode(array(
      $this->fieldName => $fieldData,
    ));

    // Log in as the administrator and confirm that the field value is present.
    $this
      ->drupalLogin($this->adminUser);
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertText($value, 'The saved field value is visible to an administrator.');

    // Log in as the content admin and try to view the node.
    $this
      ->drupalLogin($this->contentAdminUser);
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertText('Access denied', 'Access is denied for the content admin.');

    // Modify the field default as the content admin.
    $edit = array();
    $default = 'Sometimes words have two meanings';
    $edit["default_value_input[{$this->fieldName}][0][value]"] = $default;
    $this
      ->drupalPostForm("admin/structure/types/manage/page/fields/node.page.{$this->fieldName}", $edit, t('Save settings'));

    // Log in as the administrator.
    $this
      ->drupalLogin($this->adminUser);

    // Confirm that the existing node still has the correct field value.
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertText($value, 'The original field value is visible to an administrator.');

    // Confirm that the new default value appears when creating a new node.
    $this
      ->drupalGet('node/add/page');
    $this
      ->assertRaw($default, 'The updated default value is displayed when creating a new node.');
  }

}

Classes

Namesort descending Description
NodeAccessFieldTest Tests the interaction of the node access system with fields.