X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fnode%2Ftests%2Fsrc%2FFunctional%2FNodeTestBase.php;fp=web%2Fcore%2Fmodules%2Fnode%2Ftests%2Fsrc%2FFunctional%2FNodeTestBase.php;h=2ef0d73c50c3ad1107beab8251df62f177cabfa7;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/node/tests/src/Functional/NodeTestBase.php b/web/core/modules/node/tests/src/Functional/NodeTestBase.php new file mode 100644 index 000000000..2ef0d73c5 --- /dev/null +++ b/web/core/modules/node/tests/src/Functional/NodeTestBase.php @@ -0,0 +1,110 @@ +profile != 'standard') { + $this->drupalCreateContentType([ + 'type' => 'page', + 'name' => 'Basic page', + 'display_submitted' => FALSE, + ]); + $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); + } + $this->accessHandler = \Drupal::entityManager()->getAccessControlHandler('node'); + } + + /** + * Asserts that node access correctly grants or denies access. + * + * @param array $ops + * An associative array of the expected node access grants for the node + * and account, with each key as the name of an operation (e.g. 'view', + * 'delete') and each value a Boolean indicating whether access to that + * operation should be granted. + * @param \Drupal\node\NodeInterface $node + * The node object to check. + * @param \Drupal\Core\Session\AccountInterface $account + * The user account for which to check access. + */ + public function assertNodeAccess(array $ops, NodeInterface $node, AccountInterface $account) { + foreach ($ops as $op => $result) { + $this->assertEqual($result, $this->accessHandler->access($node, $op, $account), $this->nodeAccessAssertMessage($op, $result, $node->language()->getId())); + } + } + + /** + * Asserts that node create access correctly grants or denies access. + * + * @param string $bundle + * The node bundle to check access to. + * @param bool $result + * Whether access should be granted or not. + * @param \Drupal\Core\Session\AccountInterface $account + * The user account for which to check access. + * @param string|null $langcode + * (optional) The language code indicating which translation of the node + * to check. If NULL, the untranslated (fallback) access is checked. + */ + public function assertNodeCreateAccess($bundle, $result, AccountInterface $account, $langcode = NULL) { + $this->assertEqual($result, $this->accessHandler->createAccess($bundle, $account, [ + 'langcode' => $langcode, + ]), $this->nodeAccessAssertMessage('create', $result, $langcode)); + } + + /** + * Constructs an assert message to display which node access was tested. + * + * @param string $operation + * The operation to check access for. + * @param bool $result + * Whether access should be granted or not. + * @param string|null $langcode + * (optional) The language code indicating which translation of the node + * to check. If NULL, the untranslated (fallback) access is checked. + * + * @return string + * An assert message string which contains information in plain English + * about the node access permission test that was performed. + */ + public function nodeAccessAssertMessage($operation, $result, $langcode = NULL) { + return format_string( + 'Node access returns @result with operation %op, language code %langcode.', + [ + '@result' => $result ? 'true' : 'false', + '%op' => $operation, + '%langcode' => !empty($langcode) ? $langcode : 'empty' + ] + ); + } + +}