NodeTestBase.php 3.6 KB

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