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