node_access_test_add_field example

/** * {@inheritdoc} */
  protected function setUp($import_test_views = TRUE, $modules = ['node_test_views']): void {
    parent::setUp($import_test_views$modules);

    // Create Article node type.     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);

    $this->accessHandler = \Drupal::entityTypeManager()->getAccessControlHandler('node');

    node_access_test_add_field(NodeType::load('article'));

    // After enabling a node access module, the access table has to be rebuild.     node_access_rebuild();

    // Enable the private node feature of the node_access_test module.     \Drupal::state()->set('node_access_test.private', TRUE);
  }

  /** * Tests if nodes that may not be edited, can not be edited in bulk. */
  
$this->assertFalse($comment->isPublished(), 'Comment of the user has been unpublished.');
  }

  /** * Tests nodes are unpublished even if inaccessible to cancelling user. */
  public function testUserBlockUnpublishNodeAccess() {
    \Drupal::service('module_installer')->install(['node_access_test', 'user_form_test']);

    // Setup node access     node_access_rebuild();
    node_access_test_add_field(NodeType::load('page'));
    \Drupal::state()->set('node_access_test.private', TRUE);

    $this->config('user.settings')->set('cancel_method', 'user_cancel_block_unpublish')->save();

    // Create a user.     $user_storage = $this->container->get('entity_type.manager')->getStorage('user');
    $account = $this->drupalCreateUser(['cancel account']);
    // Load a real user object.     $user_storage->resetCache([$account->id()]);
    $account = $user_storage->load($account->id());

    

  protected $adminUser;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    node_access_test_add_field(NodeType::load('page'));

    // Create the 'private' field, which allows the node to be marked as private     // (restricted access) in a given translation.     $field_storage = FieldStorageConfig::create([
      'field_name' => 'field_private',
      'entity_type' => 'node',
      'type' => 'boolean',
      'cardinality' => 1,
    ]);
    $field_storage->save();

    

    ];
    $this->createEntityReferenceField('node', 'article', 'field_' . $vocabulary->id(), 'Tags', 'taxonomy_term', 'default', $handler_settings);

    \Drupal::service('entity_display.repository')
      ->getFormDisplay('node', 'article')
      ->setComponent('field_' . $vocabulary->id())
      ->save();

    // Setup node access testing.     node_access_rebuild();
    node_access_test_add_field(NodeType::load('article'));
    \Drupal::state()->set('node_access_test.private', TRUE);

  }

  /** * Runs the field CRUD tests. * * In order to act on the same fields, and not create the fields over and over * again the following tests create, update and delete the same fields. */
  public function testCRUDFields() {
    
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $admin_user = $this->drupalCreateUser(['administer modules']);
    $this->drupalLogin($admin_user);

    $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
    node_access_rebuild();
    node_access_test_add_field(NodeType::load('article'));
    \Drupal::state()->set('node_access_test.private', TRUE);

    // Create 10 nodes.     for ($i = 1; $i <= 5; $i++) {
      $this->nodes[] = $this->drupalCreateNode(['type' => 'page']);
      // These 5 articles are inaccessible to the admin user doing the uninstalling.       $this->nodes[] = $this->drupalCreateNode(['type' => 'article', 'uid' => 0, 'private' => TRUE]);
    }

    // Create 3 top-level taxonomy terms, each with 11 children.     $vocabulary = $this->createVocabulary();
    
$this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $entity_type_manager = $this->container->get('entity_type.manager');
    $entity_type_manager
      ->getStorage('entity_form_display')
      ->load('node.article.default')
      ->setComponent($field_name[
        'type' => 'entity_reference_autocomplete_tags',
        'weight' => -4,
      ])
      ->save();

    node_access_test_add_field(NodeType::load('article'));

    node_access_rebuild();
    \Drupal::state()->set('node_access_test.private', TRUE);
  }

  /** * Tests the "private" node access functionality. * * - Create 2 users with "access content" and "create article" permissions. * - Each user creates one private and one not private article. * * - Test that each user can view the other user's non-private article. * - Test that each user cannot view the other user's private article. * - Test that each user finds only appropriate (non-private + own private) * in taxonomy listing. * - Create another user with 'view any private content'. * - Test that user 4 can view all content created above. * - Test that user 4 can view all content on taxonomy listing. */

  public static $testViews = ['test_filter_node_access'];

  /** * {@inheritdoc} */
  protected function setUp($import_test_views = TRUE, $modules = ['node_test_views']): void {
    parent::setUp($import_test_views$modules);

    $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);

    node_access_test_add_field(NodeType::load('article'));

    node_access_rebuild();
    \Drupal::state()->set('node_access_test.private', TRUE);

    $num_simple_users = 2;
    $this->users = [];

    for ($i = 0; $i < $num_simple_users$i++) {
      $this->users[$i] = $this->drupalCreateUser([
        'access content',
        'create article content',
      ]);
/** * {@inheritdoc} */
  protected static $modules = ['language', 'node_access_test'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    node_access_test_add_field(NodeType::load('page'));

    // After enabling a node access module, the access table has to be rebuild.     node_access_rebuild();

    // Enable the private node feature of the node_access_test module.     \Drupal::state()->set('node_access_test.private', TRUE);

    // Add Hungarian, Catalan and Croatian.     ConfigurableLanguage::createFromLangcode('hu')->save();
    ConfigurableLanguage::createFromLangcode('ca')->save();
    ConfigurableLanguage::createFromLangcode('hr')->save();
  }

  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    node_access_rebuild();
    $this->drupalCreateContentType(['type' => 'page']);
    node_access_test_add_field(NodeType::load('page'));
    $this->addDefaultCommentField('node', 'page', 'comment', CommentItemInterface::OPEN);
    \Drupal::state()->set('node_access_test.private', TRUE);
  }

  /** * Ensure that tracker_cron is not access sensitive. */
  public function testTrackerNodeAccessIndexing() {
    // The node is private and not authored by the anonymous user, so any entity     // queries run for the anonymous user will miss it.     $author = $this->drupalCreateUser();
    
$this->drupalLogin($this->adminUser);

    $this->webUser = $this->drupalCreateUser();
  }

  /** * Tests rebuilding the node access permissions table with content. */
  public function testNodeAccessRebuildNodeGrants() {
    \Drupal::service('module_installer')->install(['node_access_test']);
    \Drupal::state()->set('node_access_test.private', TRUE);
    node_access_test_add_field(NodeType::load('page'));
    $this->resetAll();

    // Create 30 nodes so that _node_access_rebuild_batch_operation() has to run     // more than once.     for ($i = 0; $i < 30; $i++) {
      $nodes[] = $this->drupalCreateNode([
        'uid' => $this->webUser->id(),
        'private' => [['value' => 1]],
      ]);
    }

    
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->drupalLogin($this->adminUser);
    $this->createContentTypeFromUi('Moderated content', 'moderated_content', FALSE);
    $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');

    // Add the private field to the node type.     node_access_test_add_field(NodeType::load('moderated_content'));

    // Rebuild permissions because hook_node_grants() is implemented by the     // node_access_test_empty module.     node_access_rebuild();
  }

  /** * Verifies that a non-admin user can still access the appropriate pages. */
  public function testPageAccess() {
    // Initially disable access grant records in
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    node_access_test_add_field(NodeType::load('article'));
    node_access_rebuild();
    \Drupal::state()->set('node_access_test.private', TRUE);
    // This test expects unused managed files to be marked as a temporary file.     $this->config('file.settings')
      ->set('make_unused_managed_files_temporary', TRUE)
      ->save();
  }

  /** * Tests file access for file uploaded to a private node. */
  
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    node_access_rebuild();
    node_access_test_add_field(NodeType::load('forum'));
    \Drupal::state()->set('node_access_test.private', TRUE);
  }

  /** * Creates some users and creates a public node and a private node. * * Adds both active forum topics and new forum topics blocks to the sidebar. * Tests to ensure private node/public node access is respected on blocks. */
  public function testForumNodeAccess() {
    // Create some users.
Home | Imprint | This part of the site doesn't use cookies.