drupalGetNodeByTitle example


  public function testModerationWithExistingContent() {
    // Create a published article in English.     $edit = [
      'title[0][value]' => 'Published English node',
      'langcode[0][value]' => 'en',
    ];
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('Article Published English node has been created.');
    $english_node = $this->drupalGetNodeByTitle('Published English node');

    // Add a French translation.     $this->drupalGet('node/' . $english_node->id() . '/translations');
    $this->clickLink('Add');
    $edit = [
      'title[0][value]' => 'Published French node',
    ];
    $this->submitForm($edit, 'Save (this translation)');
    $this->assertSession()->pageTextContains('Article Published French node has been updated.');

    // Install content moderation and enable moderation on Article node type.

  public function testActiveForumTopicsBlock() {
    $this->drupalLogin($this->adminUser);

    // Create 10 forum topics.     $topics = $this->createForumTopics(10);

    // Comment on the first 5 topics.     $date = new DrupalDateTime();
    for ($index = 0; $index < 5; $index++) {
      // Get the node from the topic title.       $node = $this->drupalGetNodeByTitle($topics[$index]);
      $date->modify('+1 minute');
      $comment = Comment::create([
        'entity_id' => $node->id(),
        'field_name' => 'comment_forum',
        'entity_type' => 'node',
        'node_type' => 'node_type_' . $node->bundle(),
        'subject' => $this->randomString(20),
        'comment_body' => $this->randomString(256),
        'created' => $date->getTimestamp(),
      ]);
      $comment->save();
    }

    $this->submitForm($edit, 'Save settings');
    // Add a node of our new type and upload a file to it.     $file = current($this->drupalGetTestFiles('text'));
    $title = $this->randomString();
    $edit = [
      'title[0][value]' => $title,
      'files[field_' . $field_name . '_0]' => \Drupal::service('file_system')->realpath($file->uri),
    ];
    $this->drupalGet('node/add/' . $type_name);
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($title);
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()->pageTextContains('The description may be used as the label of the link to the file.');
  }

  /** * Tests description display of File Field. */
  public function testDescriptionDefaultFileFieldDisplay() {
    $field_name = strtolower($this->randomMachineName());
    $type_name = 'article';
    $field_storage_settings = [
      
$term2 = $this->createTerm($this->vocabulary);

    // Post an article.     $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $edit['body[0][value]'] = $this->randomMachineName();
    $edit[$this->field->getName() . '[]'] = $term1->id();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');

    // Check that the term is displayed when the node is viewed.     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->drupalGet('node/' . $node->id());
    $this->assertSession()->pageTextContains($term1->getName());

    $this->clickLink('Edit');
    $this->assertSession()->pageTextContains($term1->getName());
    $this->submitForm([], 'Save');
    $this->assertSession()->pageTextContains($term1->getName());

    // Edit the node with a different term.     $edit[$this->field->getName() . '[]'] = $term2->id();
    $this->drupalGet('node/' . $node->id() . '/edit');
    
// Post an article.     $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $edit['body[0][value]'] = $this->randomMachineName();
    $edit["{$this->fieldName1}[]"] = $term_1->id();
    $edit["{$this->fieldName2}[]"] = $term_1->id();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');

    // Check that the term is indexed, and only once.     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $connection = Database::getConnection();
    $index_count = $connection->select('taxonomy_index')
      ->condition('nid', $node->id())
      ->condition('tid', $term_1->id())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEquals(1, $index_count, 'Term 1 is indexed once.');

    // Update the article to change one term.     $edit["{$this->fieldName1}[]"] = $term_2->id();
    

  public function testFormSaveDestination() {
    // Create new moderated content in draft.     $this->drupalGet('node/add/moderated_content');
    $this->submitForm([
      'title[0][value]' => 'Some moderated content',
      'body[0][value]' => 'First version of the content.',
      'moderation_state[0][state]' => 'draft',
    ], 'Save');

    $node = $this->drupalGetNodeByTitle('Some moderated content');
    $edit_path = sprintf('node/%d/edit', $node->id());

    // After saving, we should be at the canonical URL and viewing the first     // revision.     $this->assertSession()->addressEquals(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
    $this->assertSession()->pageTextContains('First version of the content.');

    // Create a new draft; after saving, we should still be on the canonical     // URL, but viewing the second revision.     $this->drupalGet($edit_path);
    $this->submitForm([
      
$body_key = 'body[0][value]';
    $body_value = $this->randomMachineName(16);

    // Create node to edit.     $edit = [];
    $edit[$title_key] = $title_value;
    $edit[$body_key] = $body_value;
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');

    // Check that the node exists in the database.     $node = $this->drupalGetNodeByTitle($edit[$title_key]);
    $this->assertNotEmpty($node, 'Node found in database.');
    $this->assertSame($langcode$node->language()->getId());
    $this->assertSame($body_value$node->body->value);

    // Change node language.     $langcode = 'it';
    $this->drupalGet("node/{$node->id()}/edit");
    $edit = [
      $title_key => $this->randomMachineName(8),
      'langcode[0][value]' => $langcode,
    ];
    
// Assert the content is kept when reloading the page.     $this->drupalGet('node/add/page', ['query' => ['uuid' => $uuid]]);
    $this->assertSession()->fieldValueEquals($title_key$edit[$title_key]);
    $this->assertSession()->fieldValueEquals($body_key$edit[$body_key]);
    $this->assertSession()->fieldValueEquals($term_key$edit[$term_key]);

    // Save the node - this is a new POST, so we need to upload the image.     $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Upload');
    $this->submitForm(['field_image[0][alt]' => 'Picture of llamas'], 'Save');
    $node = $this->drupalGetNodeByTitle($edit[$title_key]);

    // Check the term was displayed on the saved node.     $this->drupalGet('node/' . $node->id());
    $this->assertSession()->pageTextContains($edit[$term_key]);

    // Check the term appears again on the edit form.     $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()->fieldValueEquals($term_key$edit[$term_key] . ' (' . $this->term->id() . ')');

    // Check with two new terms on the edit form, additionally to the existing     // one.

    $this->drupalLogin($web_user);

    // Create a node with language LanguageInterface::LANGCODE_NOT_SPECIFIED.     $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName(8);
    $edit['body[0][value]'] = $this->randomMachineName(16);
    $this->drupalGet('node/add/page');
    $form_langcode = \Drupal::state()->get('entity_test.form_langcode');
    $this->submitForm($edit, 'Save');

    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);

    $this->assertSame($form_langcode$node->language()->getId(), 'Form language is the same as the entity language.');

    // Edit the node and test the form language.     $this->drupalGet($this->langcodes[0] . '/node/' . $node->id() . '/edit');
    $form_langcode = \Drupal::state()->get('entity_test.form_langcode');
    $this->assertSame($form_langcode$node->language()->getId(), 'Form language is the same as the entity language.');

    // Explicitly set form langcode.     $langcode = $this->langcodes[0];
    $form_state_additions['langcode'] = $langcode;
    
    $this->drupalGet('node/add/article');
    $this->submitForm([
      'title[0][value]' => 'First article - published',
      'moderation_state[0][state]' => 'published',
    ], 'Save');
    $this->drupalGet('node/add/article');
    $this->submitForm([
      'title[0][value]' => 'Second article - draft',
      'moderation_state[0][state]' => 'draft',
    ], 'Save');

    $first_article = $this->drupalGetNodeByTitle('First article - published', TRUE);
    $this->assertEquals('published', $first_article->moderation_state->value);

    $second_article = $this->drupalGetNodeByTitle('Second article - draft', TRUE);
    $this->assertEquals('draft', $second_article->moderation_state->value);

    // Check that neither of them are visible in Live.     $this->switchToLive();
    $this->drupalGet('<front>');
    $this->assertSession()->pageTextNotContains('First article');
    $this->assertSession()->pageTextNotContains('Second article');

    

    $this->translator = $this->drupalCreateUser($permissions);
  }

  /** * Tests that a contextual link is available for translating a node. */
  public function testContentTranslationContextualLinks() {
    // Create a node.     $title = $this->randomString();
    $this->drupalCreateNode(['type' => $this->bundle, 'title' => $title, 'langcode' => 'en']);
    $node = $this->drupalGetNodeByTitle($title);

    // Use a UI form submission to make the node type and field translatable.     // This tests that caches are properly invalidated.     $this->drupalLogin($this->rootUser);
    $edit = [
      'entity_types[node]' => TRUE,
      'settings[node][' . $this->bundle . '][settings][language][language_alterable]' => TRUE,
      'settings[node][' . $this->bundle . '][translatable]' => TRUE,
      'settings[node][' . $this->bundle . '][fields][field_test_text]' => TRUE,
    ];
    $this->drupalGet('admin/config/regional/content-language');
    
// Verify that no menu settings are displayed and nodes can be created.     $this->drupalGet('node/add/page');
    $this->assertSession()->pageTextContains('Create Basic page');
    $this->assertSession()->pageTextNotContains('Menu settings');
    $node_title = $this->randomMachineName();
    $edit = [
      'title[0][value]' => $node_title,
      'body[0][value]' => $this->randomString(),
    ];
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($node_title);
    $this->assertEquals($edit['title[0][value]']$node->getTitle());

    // Test that we cannot set a menu item from a menu that is not set as     // available.     $edit = [
      'menu_options[tools]' => 1,
      'menu_parent' => 'main:',
    ];
    $this->drupalGet('admin/structure/types/manage/page');
    $this->submitForm($edit, 'Save content type');
    $this->assertSession()->pageTextContains('The selected menu link is not under one of the selected menus.');
    
'title[0][value]' => $title,
      'body[0][value]' => $this->randomMachineName(200),
    ];

    // Create the forum topic, preselecting the forum ID via a URL parameter.     $this->drupalGet("forum/$tid");
    $this->clickLink('Add new Forum topic');
    $this->assertSession()->addressEquals("node/add/forum?forum_id=$tid");
    $this->submitForm($edit, 'Save');

    // Check that the node exists in the database.     $node = $this->drupalGetNodeByTitle($title);
    $this->assertNotEmpty($node, 'New forum node found in database.');

    // Create a child forum.     $edit = [
      'name[0][value]' => $this->randomMachineName(20),
      'description[0][value]' => $this->randomMachineName(200),
      'parent[0]' => $tid,
    ];
    $this->drupalGet('admin/structure/forum/add/forum');
    $this->submitForm($edit, 'Save');
    $this->assertSession()->linkExists('edit forum');

    

    $this->drupalLogin($web_user);

    // Create a node.     $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName(8);
    $edit['body[0][value]'] = 'Data that should appear only in the body for the node.';
    $edit['body[0][summary]'] = 'Extra data that should appear only in the teaser for the node.';
    $this->drupalGet('node/add/page');
    $this->submitForm($edit, 'Save');

    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);

    // Set the flag to alter the view mode and view the node.     \Drupal::state()->set('node_test_change_view_mode', 'teaser');
    Cache::invalidateTags(['rendered']);
    $this->drupalGet('node/' . $node->id());

    // Check that teaser mode is viewed.     $this->assertSession()->pageTextContains('Extra data that should appear only in the teaser for the node.');
    // Make sure body text is not present.     $this->assertSession()->pageTextNotContains('Data that should appear only in the body for the node.');

    
/** * Tests language field visibility features. */
  public function testLanguageFieldVisibility() {
    // Creates a node to test Language field visibility feature.     $edit = [
      'title[0][value]' => $this->randomMachineName(8),
      'body[0][value]' => $this->randomMachineName(16),
    ];
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertNotEmpty($node, 'Node found in database.');

    // Loads node page and check if Language field is hidden by default.     $this->drupalGet('node/' . $node->id());
    $this->assertSession()->elementNotExists('xpath', '//div[@id="field-language-display"]/div');

    // Configures Language field formatter and check if it is saved.     $edit = [
      'fields[langcode][type]' => 'language',
      'fields[langcode][region]' => 'content',
    ];
    
Home | Imprint | This part of the site doesn't use cookies.