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