NodeTestBase.php 3.56 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\node\Tests;

5
use Drupal\Core\Session\AccountInterface;
6
use Drupal\node\NodeInterface;
7 8
use Drupal\simpletest\WebTestBase;

9 10 11
/**
 * Sets up page and article content types.
 */
12
abstract class NodeTestBase extends WebTestBase {
13 14 15 16 17 18

  /**
   * Modules to enable.
   *
   * @var array
   */
19
  public static $modules = array('node', 'datetime');
20

21
  /**
22
   * The node access control handler.
23
   *
24
   * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
25
   */
26
  protected $accessHandler;
27

28 29 30
  /**
   * {@inheritdoc}
   */
31
  protected function setUp() {
32
    parent::setUp();
33 34 35

    // Create Basic page and Article node types.
    if ($this->profile != 'standard') {
36 37 38 39 40
      $this->drupalCreateContentType(array(
        'type' => 'page',
        'name' => 'Basic page',
        'display_submitted' => FALSE,
      ));
41 42
      $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
    }
43
    $this->accessHandler = \Drupal::entityManager()->getAccessControlHandler('node');
44
  }
45 46

  /**
47
   * Asserts that node access correctly grants or denies access.
48 49 50 51 52 53
   *
   * @param array $ops
   *   An associative array of the expected node access grants for the node
   *   and account, with each key as the name of an operation (e.g. 'view',
   *   'delete') and each value a Boolean indicating whether access to that
   *   operation should be granted.
54
   * @param \Drupal\node\NodeInterface $node
55
   *   The node object to check.
56
   * @param \Drupal\Core\Session\AccountInterface $account
57 58
   *   The user account for which to check access.
   */
59
  function assertNodeAccess(array $ops, NodeInterface $node, AccountInterface $account) {
60
    foreach ($ops as $op => $result) {
61
      $this->assertEqual($result, $this->accessHandler->access($node, $op, $account), $this->nodeAccessAssertMessage($op, $result, $node->language()->getId()));
62 63 64
    }
  }

65 66 67 68 69 70 71 72 73 74 75 76 77 78
  /**
   * Asserts that node create access correctly grants or denies access.
   *
   * @param string $bundle
   *   The node bundle to check access to.
   * @param bool $result
   *   Whether access should be granted or not.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The user account for which to check access.
   * @param string|null $langcode
   *   (optional) The language code indicating which translation of the node
   *   to check. If NULL, the untranslated (fallback) access is checked.
   */
  function assertNodeCreateAccess($bundle, $result, AccountInterface $account, $langcode = NULL) {
79
    $this->assertEqual($result, $this->accessHandler->createAccess($bundle, $account, array(
80 81 82 83 84
      'langcode' => $langcode,
    )), $this->nodeAccessAssertMessage('create', $result, $langcode));
  }

  /**
85
   * Constructs an assert message to display which node access was tested.
86 87 88 89 90 91 92 93 94 95
   *
   * @param string $operation
   *   The operation to check access for.
   * @param bool $result
   *   Whether access should be granted or not.
   * @param string|null $langcode
   *   (optional) The language code indicating which translation of the node
   *   to check. If NULL, the untranslated (fallback) access is checked.
   *
   * @return string
96 97
   *   An assert message string which contains information in plain English
   *   about the node access permission test that was performed.
98 99 100 101 102 103 104 105 106 107 108 109
   */
  function nodeAccessAssertMessage($operation, $result, $langcode = NULL) {
    return format_string(
      'Node access returns @result with operation %op, language code %langcode.',
      array(
        '@result' => $result ? 'true' : 'false',
        '%op' => $operation,
        '%langcode' => !empty($langcode) ? $langcode : 'empty'
      )
    );
  }

110
}