3 namespace Drupal\node\Tests;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\node\NodeInterface;
7 use Drupal\simpletest\WebTestBase;
10 * Sets up page and article content types.
12 * @deprecated Scheduled for removal in Drupal 9.0.0.
13 * Use \Drupal\Tests\node\Functional\NodeTestBase instead.
15 abstract class NodeTestBase extends WebTestBase {
22 public static $modules = ['node', 'datetime'];
25 * The node access control handler.
27 * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
29 protected $accessHandler;
34 protected function setUp() {
37 // Create Basic page and Article node types.
38 if ($this->profile != 'standard') {
39 $this->drupalCreateContentType([
41 'name' => 'Basic page',
42 'display_submitted' => FALSE,
44 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
46 $this->accessHandler = \Drupal::entityManager()->getAccessControlHandler('node');
50 * Asserts that node access correctly grants or denies access.
53 * An associative array of the expected node access grants for the node
54 * and account, with each key as the name of an operation (e.g. 'view',
55 * 'delete') and each value a Boolean indicating whether access to that
56 * operation should be granted.
57 * @param \Drupal\node\NodeInterface $node
58 * The node object to check.
59 * @param \Drupal\Core\Session\AccountInterface $account
60 * The user account for which to check access.
62 public function assertNodeAccess(array $ops, NodeInterface $node, AccountInterface $account) {
63 foreach ($ops as $op => $result) {
64 $this->assertEqual($result, $this->accessHandler->access($node, $op, $account), $this->nodeAccessAssertMessage($op, $result, $node->language()->getId()));
69 * Asserts that node create access correctly grants or denies access.
71 * @param string $bundle
72 * The node bundle to check access to.
74 * Whether access should be granted or not.
75 * @param \Drupal\Core\Session\AccountInterface $account
76 * The user account for which to check access.
77 * @param string|null $langcode
78 * (optional) The language code indicating which translation of the node
79 * to check. If NULL, the untranslated (fallback) access is checked.
81 public function assertNodeCreateAccess($bundle, $result, AccountInterface $account, $langcode = NULL) {
82 $this->assertEqual($result, $this->accessHandler->createAccess($bundle, $account, [
83 'langcode' => $langcode,
84 ]), $this->nodeAccessAssertMessage('create', $result, $langcode));
88 * Constructs an assert message to display which node access was tested.
90 * @param string $operation
91 * The operation to check access for.
93 * Whether access should be granted or not.
94 * @param string|null $langcode
95 * (optional) The language code indicating which translation of the node
96 * to check. If NULL, the untranslated (fallback) access is checked.
99 * An assert message string which contains information in plain English
100 * about the node access permission test that was performed.
102 public function nodeAccessAssertMessage($operation, $result, $langcode = NULL) {
103 return format_string(
104 'Node access returns @result with operation %op, language code %langcode.',
106 '@result' => $result ? 'true' : 'false',
108 '%langcode' => !empty($langcode) ? $langcode : 'empty',