drupalCreateNode example


  public function testDifferentPermissions() {
    $this->drupalLogin($this->editorUser);

    // Create three nodes in the following order:     // - An article, which should be user-editable.     // - A page, which should not be user-editable.     // - A second article, which should also be user-editable.     $node1 = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $node2 = $this->drupalCreateNode(['type' => 'page', 'promote' => 1]);
    $node3 = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);

    // Now, on the front page, all article nodes should have contextual links     // placeholders, as should the view that contains them.     $ids = [
      'node:node=' . $node1->id() . ':changed=' . $node1->getChangedTime() . '&langcode=en',
      'node:node=' . $node2->id() . ':changed=' . $node2->getChangedTime() . '&langcode=en',
      'node:node=' . $node3->id() . ':changed=' . $node3->getChangedTime() . '&langcode=en',
      'entity.view.edit_form:view=frontpage:location=page&name=frontpage&display_id=page_1&langcode=en',
    ];

    
// Create a user account and login.     $web_user = $this->drupalCreateUser();
    $this->drupalLogin($web_user);

    // Create some nodes with different authored-on dates.     // Two dates in PST (winter time):     $date1 = '2007-03-09 21:00:00 -0800';
    $date2 = '2007-03-11 01:00:00 -0800';
    // One date in PDT (summer time):     $date3 = '2007-03-20 21:00:00 -0700';
    $this->drupalCreateContentType(['type' => 'article']);
    $node1 = $this->drupalCreateNode(['created' => strtotime($date1), 'type' => 'article']);
    $node2 = $this->drupalCreateNode(['created' => strtotime($date2), 'type' => 'article']);
    $node3 = $this->drupalCreateNode(['created' => strtotime($date3), 'type' => 'article']);

    // Confirm date format and time zone.     $this->drupalGet('node/' . $node1->id());
    // Date should be PST.     $this->assertSession()->pageTextContains('2007-03-09 21:00 0');
    $this->drupalGet('node/' . $node2->id());
    // Date should be PST.     $this->assertSession()->pageTextContains('2007-03-11 01:00 0');
    $this->drupalGet('node/' . $node3->id());
    
$this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save();
    $user_storage = $this->container->get('entity_type.manager')->getStorage('user');

    // Create a user.     $account = $this->drupalCreateUser([]);
    $this->drupalLogin($account);
    // Load a real user object.     $user_storage->resetCache([$account->id()]);
    $account = $user_storage->load($account->id());

    // Create a node.     $node = $this->drupalCreateNode(['uid' => $account->id()]);

    // Attempt to cancel account.     $this->drupalGet('user/' . $account->id() . '/edit');
    $this->assertSession()->pageTextNotContains("Cancel account");

    // Attempt bogus account cancellation request confirmation.     $timestamp = $account->getLastLoginTime();
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account$timestamp));
    $this->assertSession()->statusCodeEquals(403);
    $user_storage->resetCache([$account->id()]);
    $account = $user_storage->load($account->id());
    
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests if template_preprocess_node() generates the correct suggestions. */
  public function testNodeThemeHookSuggestions() {
    // Create node to be rendered.     $node = $this->drupalCreateNode();
    $view_mode = 'full';

    // Simulate theming of the node.     $build = \Drupal::entityTypeManager()->getViewBuilder('node')->view($node$view_mode);

    $variables['elements'] = $build;
    $suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_node', [$variables]);

    $this->assertEquals(['node__full', 'node__page', 'node__page__full', 'node__' . $node->id(), 'node__' . $node->id() . '__full']$suggestions, 'Found expected node suggestions.');

    // Change the view mode.
class NodeViewTest extends NodeTestBase {

  /** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests the html head links. */
  public function testHtmlHeadLinks() {
    $node = $this->drupalCreateNode();

    $this->drupalGet($node->toUrl());

    $element = $this->assertSession()->elementExists('css', 'link[rel="canonical"]');
    $this->assertEquals($node->toUrl()->setAbsolute()->toString()$element->getAttribute('href'));

    $element = $this->assertSession()->elementExists('css', 'link[rel="shortlink"]');
    $this->assertEquals($node->toUrl('canonical', ['alias' => TRUE])->setAbsolute()->toString()$element->getAttribute('href'));
  }

  /** * Tests the Link header. */
'administer nodes',
      'access site reports',
    ]);
    $this->drupalLogin($this->testUser);

    foreach ($this->numbers as $num) {
      $info = [
        'body' => [['value' => $num]],
        'type' => 'page',
        'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ];
      $this->nodes[] = $this->drupalCreateNode($info);
    }

    // Run cron to ensure the content is indexed.     $this->cronRun();
    $this->drupalGet('admin/reports/dblog');
    $this->assertSession()->pageTextContains('Cron run completed');
  }

  /** * Tests that all the numbers can be searched. */
  
    $links = $this->xpath('//*/td[contains(text(),:menu_label)]/following::a[normalize-space()=:link_label]', [':menu_label' => (string) $this->menu->label(), ':link_label' => 'Edit menu']);
    $links[0]->click();

    // Test the 'Add link' local action.     $this->clickLink('Add link');
    $link_title = $this->randomString();
    $this->submitForm(['link[0][uri]' => '/', 'title[0][value]' => $link_title], 'Save');
    $this->assertSession()->linkExists($link_title);
    $this->assertSession()->addressEquals(Url::fromRoute('entity.menu.edit_form', ['menu' => $menu_name]));

    // Add nodes to use as links for menu links.     $node1 = $this->drupalCreateNode(['type' => 'article']);
    $node2 = $this->drupalCreateNode(['type' => 'article']);
    $node3 = $this->drupalCreateNode(['type' => 'article']);
    $node4 = $this->drupalCreateNode(['type' => 'article']);
    // Create a node with an alias.     $node5 = $this->drupalCreateNode([
      'type' => 'article',
      'path' => [
        'alias' => '/node5',
      ],
    ]);

    
public function testCommentCount() {
    $this->drupalGet('test-comment-count');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertCount(2, $this->cssSelect('.views-row'));
    $comment_count_with_comment = $this->cssSelect(".views-field-comment-count span:contains('1')");
    $this->assertCount(1, $comment_count_with_comment);
    $comment_count_without_comment = $this->cssSelect(".views-field-comment-count span:contains('0')");
    $this->assertCount(1, $comment_count_without_comment);

    // Create a content type with no comment field, and add a node.     $this->drupalCreateContentType(['type' => 'no_comment', 'name' => 'No comment page']);
    $this->nodeUserPosted = $this->drupalCreateNode(['type' => 'no_comment']);
    $this->drupalGet('test-comment-count');

    // Test that the node with no comment field is also shown.     $this->assertSession()->statusCodeEquals(200);
    $this->assertCount(3, $this->cssSelect('.views-row'));
    $comment_count_with_comment = $this->cssSelect(".views-field-comment-count span:contains('1')");
    $this->assertCount(1, $comment_count_with_comment);
    $comment_count_without_comment = $this->cssSelect(".views-field-comment-count span:contains('0')");
    $this->assertCount(2, $comment_count_without_comment);
  }

}
$administratorUser = $this->drupalCreateUser([
      'skip comment approval',
      'post comments',
      'access comments',
      'access content',
      'administer nodes',
      'administer comments',
      'bypass node access',
    ]);
    $this->drupalLogin($administratorUser);

    $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
    $this->comment = $this->postComment($this->node, $this->randomMachineName()$this->randomMachineName());
    $this->assertInstanceOf(Comment::class$this->comment);

    $this->node2 = $this->drupalCreateNode([
      'title' => $this->randomMachineName(),
      'type' => 'article',
    ]);
  }

  /** * Tests that comments are correctly saved as entity references. */

  public function testSearchIntegration() {
    // Create a content type.     $type = $this->drupalCreateContentType();

    // Add three nodes, one containing the word "pizza", one containing     // "sandwich", and one containing "cola is good with pizza". Make the     // second node link to the first.     $node['title'] = 'pizza';
    $node['body'] = [['value' => 'pizza']];
    $node['type'] = $type->id();
    $this->drupalCreateNode($node);

    $this->drupalGet('node/1');
    $node_url = $this->getUrl();

    $node['title'] = 'sandwich';
    $node['body'] = [['value' => 'sandwich with a <a href="' . $node_url . '">link to first node</a>']];
    $this->drupalCreateNode($node);

    $node['title'] = 'cola';
    $node['body'] = [['value' => 'cola is good with pizza']];
    $node['type'] = $type->id();
    
    // node to be marked private (not viewable by normal users).
    // Create six nodes:     // 1. Four Hungarian nodes with Catalan translations     // - One with neither language marked as private.     // - One with only the Hungarian translation private.     // - One with only the Catalan translation private.     // - One with both the Hungarian and Catalan translations private.     // 2. Two nodes with no language specified.     // - One public.     // - One private.     $this->nodes['both_public'] = $node = $this->drupalCreateNode([
      'body' => [[]],
      'langcode' => 'hu',
      'field_private' => [['value' => 0]],
    ]);
    $translation = $node->addTranslation('ca');
    $translation->title->value = $this->randomString();
    $translation->field_private->value = 0;
    $node->save();

    $this->nodes['ca_private'] = $node = $this->drupalCreateNode([
      'body' => [[]],
      
public function testCommentThreading() {
    // Set comments to have a subject with preview disabled.     $this->drupalLogin($this->adminUser);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
    $this->drupalLogout();

    // Create a node.     $this->drupalLogin($this->webUser);
    $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);

    // Post comment #1.     $this->drupalLogin($this->webUser);
    $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();

    $comment1 = $this->postComment($this->node, $comment_text$subject_text, TRUE);
    // Confirm that the comment was created and has the correct threading.     $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
    $this->assertEquals('01/', $comment1->getThread());
    // Confirm that there is no reference to a parent comment.
// After the third node, we don't care what the settings are. But we       // need to have at least 5 to make sure the throttling is working       // correctly. So, let's make 8 total.       [],
      [],
      [],
      [],
      [],
    ];
    $this->searchableNodes = [];
    foreach ($nodes as $setting) {
      $this->searchableNodes[] = $this->drupalCreateNode($setting);
    }

    // Add a single translation to the second node.     $translation = $this->searchableNodes[1]->addTranslation('hu', ['title' => 'Second node hu']);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[1]->save();

    // Add two translations to the third node.     $translation = $this->searchableNodes[2]->addTranslation('hu', ['title' => 'Third node this is the Hungarian title']);
    $translation->body->value = $this->randomMachineName(32);
    $translation = $this->searchableNodes[2]->addTranslation('sv', ['title' => 'Third node sv']);
    

  public function testModeratedContentPage() {
    $assert_session = $this->assertSession();
    $this->drupalLogin($this->adminUser);

    // Use an explicit changed time to ensure the expected order in the content     // admin listing. We want these to appear in the table in the same order as     // they appear in the following code, and the 'moderated_content' view has a     // table style configuration with a default sort on the 'changed' field     // descending.     $time = \Drupal::time()->getRequestTime();
    $excluded_nodes['published_page'] = $this->drupalCreateNode(['type' => 'page', 'changed' => $time--, 'moderation_state' => 'published']);
    $excluded_nodes['published_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published']);

    $excluded_nodes['unmoderated_type'] = $this->drupalCreateNode(['type' => 'unmoderated_type', 'changed' => $time--]);
    $excluded_nodes['unmoderated_type']->setNewRevision(TRUE);
    $excluded_nodes['unmoderated_type']->isDefaultRevision(FALSE);
    $excluded_nodes['unmoderated_type']->changed->value = $time--;
    $excluded_nodes['unmoderated_type']->save();

    $nodes['published_then_draft_article'] = $this->drupalCreateNode(['type' => 'article', 'changed' => $time--, 'moderation_state' => 'published', 'title' => 'first article - published']);
    $nodes['published_then_draft_article']->setNewRevision(TRUE);
    $nodes['published_then_draft_article']->setTitle('first article - draft');
    
protected function setUp($import_test_views = TRUE, $modules = []): void {
    parent::setUp($import_test_views$modules);

    // Create a hierarchy 3 deep. Note the parent setup function creates two     // top-level terms w/o children.     $first = $this->createTerm(['name' => 'First']);
    $second = $this->createTerm(['name' => 'Second', 'parent' => $first->id()]);
    $third = $this->createTerm(['name' => 'Third', 'parent' => $second->id()]);

    // Create a node w/o any terms.     $settings = ['type' => 'article'];
    $this->nodes[] = $this->drupalCreateNode($settings);

    // Create a node with only the top level term.     $settings['field_views_testing_tags'][0]['target_id'] = $first->id();
    $this->nodes[] = $this->drupalCreateNode($settings);

    // Create a node with only the third level term.     $settings['field_views_testing_tags'][0]['target_id'] = $third->id();
    $this->nodes[] = $this->drupalCreateNode($settings);

    $this->terms[0] = $first;
    $this->terms[1] = $second;
    
Home | Imprint | This part of the site doesn't use cookies.