643684cde43c02cc984980c0efba8abc0d5106a1
[yaffs-website] / web / core / modules / node / src / Tests / NodeTestBase.php
1 <?php
2
3 namespace Drupal\node\Tests;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\node\NodeInterface;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Sets up page and article content types.
11  *
12  * @deprecated Scheduled for removal in Drupal 9.0.0.
13  *   Use \Drupal\Tests\node\Functional\NodeTestBase instead.
14  */
15 abstract class NodeTestBase extends WebTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['node', 'datetime'];
23
24   /**
25    * The node access control handler.
26    *
27    * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
28    */
29   protected $accessHandler;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36
37     // Create Basic page and Article node types.
38     if ($this->profile != 'standard') {
39       $this->drupalCreateContentType([
40         'type' => 'page',
41         'name' => 'Basic page',
42         'display_submitted' => FALSE,
43       ]);
44       $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
45     }
46     $this->accessHandler = \Drupal::entityManager()->getAccessControlHandler('node');
47   }
48
49   /**
50    * Asserts that node access correctly grants or denies access.
51    *
52    * @param array $ops
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.
61    */
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()));
65     }
66   }
67
68   /**
69    * Asserts that node create access correctly grants or denies access.
70    *
71    * @param string $bundle
72    *   The node bundle to check access to.
73    * @param bool $result
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.
80    */
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));
85   }
86
87   /**
88    * Constructs an assert message to display which node access was tested.
89    *
90    * @param string $operation
91    *   The operation to check access for.
92    * @param bool $result
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.
97    *
98    * @return string
99    *   An assert message string which contains information in plain English
100    *   about the node access permission test that was performed.
101    */
102   public function nodeAccessAssertMessage($operation, $result, $langcode = NULL) {
103     return format_string(
104       'Node access returns @result with operation %op, language code %langcode.',
105       [
106         '@result' => $result ? 'true' : 'false',
107         '%op' => $operation,
108         '%langcode' => !empty($langcode) ? $langcode : 'empty',
109       ]
110     );
111   }
112
113 }