You are here

public function EntityReferenceSelectionAccessTest::testNodeHandler in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\system\Tests\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testNodeHandler()

Test the node-specific overrides of the entity handler.

File

core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php, line 75
Contains \Drupal\system\Tests\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest.

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\system\Tests\Entity\EntityReferenceSelection

Code

public function testNodeHandler() {
  $selection_options = array(
    'target_type' => 'node',
    'handler' => 'default',
    'handler_settings' => array(
      'target_bundles' => NULL,
    ),
  );

  // Build a set of test data.
  // Titles contain HTML-special characters to test escaping.
  $node_values = array(
    'published1' => array(
      'type' => 'article',
      'status' => NODE_PUBLISHED,
      'title' => 'Node published1 (<&>)',
      'uid' => 1,
    ),
    'published2' => array(
      'type' => 'article',
      'status' => NODE_PUBLISHED,
      'title' => 'Node published2 (<&>)',
      'uid' => 1,
    ),
    'unpublished' => array(
      'type' => 'article',
      'status' => NODE_NOT_PUBLISHED,
      'title' => 'Node unpublished (<&>)',
      'uid' => 1,
    ),
  );
  $nodes = array();
  $node_labels = array();
  foreach ($node_values as $key => $values) {
    $node = entity_create('node', $values);
    $node
      ->save();
    $nodes[$key] = $node;
    $node_labels[$key] = Html::escape($node
      ->label());
  }

  // Test as a non-admin.
  $normal_user = $this
    ->drupalCreateUser(array(
    'access content',
  ));
  \Drupal::currentUser()
    ->setAccount($normal_user);
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published1']
            ->id() => $node_labels['published1'],
          $nodes['published2']
            ->id() => $node_labels['published2'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'published1',
          'CONTAINS',
        ),
        array(
          'Published1',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published1']
            ->id() => $node_labels['published1'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'published2',
          'CONTAINS',
        ),
        array(
          'Published2',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published2']
            ->id() => $node_labels['published2'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'invalid node',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
    array(
      'arguments' => array(
        array(
          'Node unpublished',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
  );
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'Node handler');

  // Test as an admin.
  $admin_user = $this
    ->drupalCreateUser(array(
    'access content',
    'bypass node access',
  ));
  \Drupal::currentUser()
    ->setAccount($admin_user);
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['published1']
            ->id() => $node_labels['published1'],
          $nodes['published2']
            ->id() => $node_labels['published2'],
          $nodes['unpublished']
            ->id() => $node_labels['unpublished'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'Node unpublished',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'article' => array(
          $nodes['unpublished']
            ->id() => $node_labels['unpublished'],
        ),
      ),
    ),
  );
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'Node handler (admin)');
}