assertNodeAccess example

$web_user = $this->drupalCreateUser(['access content']);

    $expected_node_access = ['view' => TRUE, 'update' => FALSE, 'delete' => FALSE];
    $expected_node_access_no_access = ['view' => FALSE, 'update' => FALSE, 'delete' => FALSE];

    // Creating a public node with langcode Hungarian, will be saved as the     // fallback in node access table.     $node_public_hu = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'hu', 'private' => FALSE]);
    $this->assertSame('hu', $node_public_hu->language()->getId(), 'Node created as Hungarian.');

    // Tests the default access is provided for the public Hungarian node.     $this->assertNodeAccess($expected_node_access$node_public_hu$web_user);

    // Tests that Hungarian provided specifically results in the same.     $this->assertNodeAccess($expected_node_access$node_public_hu->getTranslation('hu')$web_user);

    // Creating a public node with no special langcode, like when no language     // module enabled.     $node_public_no_language = $this->drupalCreateNode([
      'private' => FALSE,
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $this->assertSame(LanguageInterface::LANGCODE_NOT_SPECIFIED, $node_public_no_language->language()->getId(), 'Node created with not specified language.');

    
/** * Tests node access and node access queries with multiple node languages. */
  public function testNodeAccessLanguageAwareCombination() {

    $expected_node_access = ['view' => TRUE, 'update' => FALSE, 'delete' => FALSE];
    $expected_node_access_no_access = ['view' => FALSE, 'update' => FALSE, 'delete' => FALSE];

    // When the node and both translations are public, access should always be     // granted.     $this->assertNodeAccess($expected_node_access$this->nodes['public_both_public']$this->webUser);
    $this->assertNodeAccess($expected_node_access$this->nodes['public_both_public']->getTranslation('hu')$this->webUser);
    $this->assertNodeAccess($expected_node_access$this->nodes['public_both_public']->getTranslation('ca')$this->webUser);

    // If the node is marked private but both existing translations are not,     // access should still be granted, because the grants are additive.     $this->assertNodeAccess($expected_node_access$this->nodes['private_both_public']$this->webUser);
    $this->assertNodeAccess($expected_node_access$this->nodes['private_both_public']->getTranslation('hu')$this->webUser);
    $this->assertNodeAccess($expected_node_access$this->nodes['private_both_public']->getTranslation('ca')$this->webUser);

    // If the node is marked private, but an existing translation is public,     // access should only be granted for the public translation. With the
/** * Tests node access and node access queries with multiple node languages. */
  public function testNodeAccessLanguageAware() {
    // The node_access_test_language module only grants view access.     $expected_node_access = ['view' => TRUE, 'update' => FALSE, 'delete' => FALSE];
    $expected_node_access_no_access = ['view' => FALSE, 'update' => FALSE, 'delete' => FALSE];

    // When both Hungarian and Catalan are marked as public, access to the     // Hungarian translation should be granted with the default entity object or     // when the Hungarian translation is specified explicitly.     $this->assertNodeAccess($expected_node_access$this->nodes['both_public']$this->webUser);
    $this->assertNodeAccess($expected_node_access$this->nodes['both_public']->getTranslation('hu')$this->webUser);
    // Access to the Catalan translation should also be granted.     $this->assertNodeAccess($expected_node_access$this->nodes['both_public']->getTranslation('ca')$this->webUser);

    // When Hungarian is marked as private, access to the Hungarian translation     // should be denied with the default entity object or when the Hungarian     // translation is specified explicitly.     $this->assertNodeAccess($expected_node_access_no_access$this->nodes['hu_private']$this->webUser);
    $this->assertNodeAccess($expected_node_access_no_access$this->nodes['hu_private']->getTranslation('hu')$this->webUser);
    // Access to the Catalan translation should be granted.     $this->assertNodeAccess($expected_node_access$this->nodes['hu_private']->getTranslation('ca')$this->webUser);

    

  public function testNodeAccess() {
    // Ensures user without 'access content' permission can do nothing.     $web_user1 = $this->drupalCreateUser([
      'create page content',
      'edit any page content',
      'delete any page content',
    ]);
    $node1 = $this->drupalCreateNode(['type' => 'page']);
    $this->assertNodeCreateAccess($node1->bundle(), FALSE, $web_user1);
    $this->assertNodeAccess([
      'view' => FALSE,
      'update' => FALSE,
      'delete' => FALSE,
    ]$node1$web_user1);

    // Ensures user with 'bypass node access' permission can do everything.     $web_user2 = $this->drupalCreateUser(['bypass node access']);
    $node2 = $this->drupalCreateNode(['type' => 'page']);
    $this->assertNodeCreateAccess($node2->bundle(), TRUE, $web_user2);
    $this->assertNodeAccess([
      'view' => TRUE,
      
Home | Imprint | This part of the site doesn't use cookies.