3 namespace Drupal\Tests\node\Functional;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\node\NodeInterface;
7 use Drupal\Tests\BrowserTestBase;
10 * Sets up page and article content types.
12 abstract class NodeTestBase extends BrowserTestBase {
19 public static $modules = ['node', 'datetime'];
22 * The node access control handler.
24 * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
26 protected $accessHandler;
31 protected function setUp() {
34 // Create Basic page and Article node types.
35 if ($this->profile != 'standard') {
36 $this->drupalCreateContentType([
38 'name' => 'Basic page',
39 'display_submitted' => FALSE,
41 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
43 $this->accessHandler = \Drupal::entityManager()->getAccessControlHandler('node');
47 * Asserts that node access correctly grants or denies access.
50 * An associative array of the expected node access grants for the node
51 * and account, with each key as the name of an operation (e.g. 'view',
52 * 'delete') and each value a Boolean indicating whether access to that
53 * operation should be granted.
54 * @param \Drupal\node\NodeInterface $node
55 * The node object to check.
56 * @param \Drupal\Core\Session\AccountInterface $account
57 * The user account for which to check access.
59 public 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()));
66 * Asserts that node create access correctly grants or denies access.
68 * @param string $bundle
69 * The node bundle to check access to.
71 * Whether access should be granted or not.
72 * @param \Drupal\Core\Session\AccountInterface $account
73 * The user account for which to check access.
74 * @param string|null $langcode
75 * (optional) The language code indicating which translation of the node
76 * to check. If NULL, the untranslated (fallback) access is checked.
78 public function assertNodeCreateAccess($bundle, $result, AccountInterface $account, $langcode = NULL) {
79 $this->assertEqual($result, $this->accessHandler->createAccess($bundle, $account, [
80 'langcode' => $langcode,
81 ]), $this->nodeAccessAssertMessage('create', $result, $langcode));
85 * Constructs an assert message to display which node access was tested.
87 * @param string $operation
88 * The operation to check access for.
90 * Whether access should be granted or not.
91 * @param string|null $langcode
92 * (optional) The language code indicating which translation of the node
93 * to check. If NULL, the untranslated (fallback) access is checked.
96 * An assert message string which contains information in plain English
97 * about the node access permission test that was performed.
99 public function nodeAccessAssertMessage($operation, $result, $langcode = NULL) {
100 return format_string(
101 'Node access returns @result with operation %op, language code %langcode.',
103 '@result' => $result ? 'true' : 'false',
105 '%langcode' => !empty($langcode) ? $langcode : 'empty'