Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\node\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
6 use Drupal\node\NodeInterface;
|
Chris@0
|
7 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Sets up page and article content types.
|
Chris@0
|
11 */
|
Chris@0
|
12 abstract class NodeTestBase extends BrowserTestBase {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Modules to enable.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var array
|
Chris@0
|
18 */
|
Chris@0
|
19 public static $modules = ['node', 'datetime'];
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The node access control handler.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $accessHandler;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * {@inheritdoc}
|
Chris@0
|
30 */
|
Chris@0
|
31 protected function setUp() {
|
Chris@0
|
32 parent::setUp();
|
Chris@0
|
33
|
Chris@0
|
34 // Create Basic page and Article node types.
|
Chris@0
|
35 if ($this->profile != 'standard') {
|
Chris@0
|
36 $this->drupalCreateContentType([
|
Chris@0
|
37 'type' => 'page',
|
Chris@0
|
38 'name' => 'Basic page',
|
Chris@0
|
39 'display_submitted' => FALSE,
|
Chris@0
|
40 ]);
|
Chris@0
|
41 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
|
Chris@0
|
42 }
|
Chris@0
|
43 $this->accessHandler = \Drupal::entityManager()->getAccessControlHandler('node');
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Asserts that node access correctly grants or denies access.
|
Chris@0
|
48 *
|
Chris@0
|
49 * @param array $ops
|
Chris@0
|
50 * An associative array of the expected node access grants for the node
|
Chris@0
|
51 * and account, with each key as the name of an operation (e.g. 'view',
|
Chris@0
|
52 * 'delete') and each value a Boolean indicating whether access to that
|
Chris@0
|
53 * operation should be granted.
|
Chris@0
|
54 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
55 * The node object to check.
|
Chris@0
|
56 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
57 * The user account for which to check access.
|
Chris@0
|
58 */
|
Chris@0
|
59 public function assertNodeAccess(array $ops, NodeInterface $node, AccountInterface $account) {
|
Chris@0
|
60 foreach ($ops as $op => $result) {
|
Chris@0
|
61 $this->assertEqual($result, $this->accessHandler->access($node, $op, $account), $this->nodeAccessAssertMessage($op, $result, $node->language()->getId()));
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Asserts that node create access correctly grants or denies access.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @param string $bundle
|
Chris@0
|
69 * The node bundle to check access to.
|
Chris@0
|
70 * @param bool $result
|
Chris@0
|
71 * Whether access should be granted or not.
|
Chris@0
|
72 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
73 * The user account for which to check access.
|
Chris@0
|
74 * @param string|null $langcode
|
Chris@0
|
75 * (optional) The language code indicating which translation of the node
|
Chris@0
|
76 * to check. If NULL, the untranslated (fallback) access is checked.
|
Chris@0
|
77 */
|
Chris@0
|
78 public function assertNodeCreateAccess($bundle, $result, AccountInterface $account, $langcode = NULL) {
|
Chris@0
|
79 $this->assertEqual($result, $this->accessHandler->createAccess($bundle, $account, [
|
Chris@0
|
80 'langcode' => $langcode,
|
Chris@0
|
81 ]), $this->nodeAccessAssertMessage('create', $result, $langcode));
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Constructs an assert message to display which node access was tested.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @param string $operation
|
Chris@0
|
88 * The operation to check access for.
|
Chris@0
|
89 * @param bool $result
|
Chris@0
|
90 * Whether access should be granted or not.
|
Chris@0
|
91 * @param string|null $langcode
|
Chris@0
|
92 * (optional) The language code indicating which translation of the node
|
Chris@0
|
93 * to check. If NULL, the untranslated (fallback) access is checked.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return string
|
Chris@0
|
96 * An assert message string which contains information in plain English
|
Chris@0
|
97 * about the node access permission test that was performed.
|
Chris@0
|
98 */
|
Chris@0
|
99 public function nodeAccessAssertMessage($operation, $result, $langcode = NULL) {
|
Chris@0
|
100 return format_string(
|
Chris@0
|
101 'Node access returns @result with operation %op, language code %langcode.',
|
Chris@0
|
102 [
|
Chris@0
|
103 '@result' => $result ? 'true' : 'false',
|
Chris@0
|
104 '%op' => $operation,
|
Chris@17
|
105 '%langcode' => !empty($langcode) ? $langcode : 'empty',
|
Chris@0
|
106 ]
|
Chris@0
|
107 );
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 }
|