NodeTestBase.php 3.7 KB

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