annotate core/modules/node/src/Tests/NodeTestBase.php @ 0:c75dbcec494b

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