submitForm example

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

    $this->drupalLogin($this->rootUser);

    // Enable URL language detection and selection.     $edit = ['language_interface[enabled][language-url]' => 1];
    $this->drupalGet('admin/config/regional/language/detection');
    $this->submitForm($edit, 'Save settings');

    // Enable translation for article node.     $edit = [
      'entity_types[node]' => 1,
      'settings[node][article][translatable]' => 1,
      'settings[node][article][fields][path]' => 1,
      'settings[node][article][fields][body]' => 1,
      'settings[node][article][settings][language][language_alterable]' => 1,
    ];
    $this->drupalGet('admin/config/regional/content-language');
    $this->submitForm($edit, 'Save configuration');
    

  public function testFormatAdmin() {
    // Add text format.     $this->drupalGet('admin/config/content/formats');
    $this->clickLink('Add text format');
    $format_id = mb_strtolower($this->randomMachineName());
    $name = $this->randomMachineName();
    $edit = [
      'format' => $format_id,
      'name' => $name,
    ];
    $this->submitForm($edit, 'Save configuration');

    // Verify default weight of the text format.     $this->drupalGet('admin/config/content/formats');
    $this->assertSession()->fieldValueEquals("formats[$format_id][weight]", 0);

    // Change the weight of the text format.     $edit = [
      "formats[$format_id][weight]" => 5,
    ];
    $this->drupalGet('admin/config/content/formats');
    $this->submitForm($edit, 'Save');
    

  protected function createContentTypeFromUi($content_type_name$content_type_id$moderated = FALSE, $workflow_id = 'editorial') {
    $this->drupalGet('admin/structure/types');
    $this->clickLink('Add content type');

    $edit = [
      'name' => $content_type_name,
      'type' => $content_type_id,
    ];
    $this->submitForm($edit, 'Save content type');

    // Check the content type has been set to create new revisions.     $this->assertTrue(NodeType::load($content_type_id)->shouldCreateNewRevision());

    if ($moderated) {
      $this->enableModerationThroughUi($content_type_id$workflow_id);
    }
  }

  /** * Enable moderation for a specified content type, using the UI. * * @param string $content_type_id * Machine name. * @param string $workflow_id * The workflow to attach to the bundle. */

  protected $defaultTheme = 'stark';

  /** * Tests that #type 'url' fields are properly validated and trimmed. */
  public function testFormUrl() {
    $edit = [];
    $edit['url'] = 'http://';
    $edit['url_required'] = ' ';
    $this->drupalGet('form-test/url');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()->pageTextContains("The URL http:// is not valid.");
    $this->assertSession()->pageTextContains("Required URL field is required.");

    $edit = [];
    $edit['url'] = "\n";
    $edit['url_required'] = 'http://example.com/ ';
    $this->drupalGet('form-test/url');
    $this->submitForm($edit, 'Submit');
    $values = Json::decode($this->getSession()->getPage()->getContent());
    $this->assertSame('', $values['url']);
    $this->assertEquals('http://example.com/', $values['url_required']);

    
    foreach ($all_dependent_modules as $module) {
      $expected_required_list_items[$module] = $this->testModules[$module] . " (disabled)";
    }
    $this->assertUninstallableTheme($expected_required_list_items$theme_name);

    // Enable the first group of dependee modules.     $first_module_form_post = [];
    foreach ($first_modules as $module) {
      $first_module_form_post["modules[$module][enable]"] = 1;
    }
    $this->drupalGet('admin/modules');
    $this->submitForm($first_module_form_post, 'Install');
    $assert_module_enabled_message($first_modules);

    $this->drupalGet('admin/appearance');

    // Confirm the theme is still uninstallable due to a remaining module     // dependency.     // The modules that have already been enabled will no longer be listed as     // disabled.     foreach ($first_modules as $module) {
      $expected_required_list_items[$module] = $this->testModules[$module];
    }
    


    $node_search_plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
    // Update the search index.     $node_search_plugin->updateIndex();
    $search_index = \Drupal::service('search.index');
    assert($search_index instanceof SearchIndexInterface);

    // Search the node to verify it appears in search results     $edit = ['keys' => 'knights'];
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()->pageTextContains($node->label());

    // Update the node     $node->body->value = "We want a shrubbery!";
    $node->save();

    // Run indexer again     $node_search_plugin->updateIndex();

    // Search again to verify the new text appears in test results.     $edit = ['keys' => 'shrubbery'];
    
// Create the node.     $node = $this->drupalCreateNode();

    // Verify the checkbox is checked on the node edit form.     $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()->checkboxChecked('edit-revision');

    // Uncheck the create new revision checkbox and save the node.     $edit = ['revision' => FALSE];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');

    // Load the node again and check the revision is the same as before.     $node_storage->resetCache([$node->id()]);
    $node_revision = $node_storage->load($node->id(), TRUE);
    $this->assertEquals($node->getRevisionId()$node_revision->getRevisionId(), "After an existing node is saved with 'Create new revision' unchecked, a new revision is not created.");

    // Verify the checkbox is checked on the node edit form.     $this->drupalGet('node/' . $node->id() . '/edit');
    $this->assertSession()->checkboxChecked('edit-revision');

    // Submit the form without changing the checkbox.
->fields([
        'filename' => 'foo.txt',
        'uri' => 'public://foo.txt',
      ])
      ->execute();

    // Get valid credentials.     $edits = $this->translatePostValues($this->getCredentials());

    // Start the upgrade process.     $this->drupalGet('/upgrade');
    $this->submitForm([], 'Continue');
    $this->submitForm($edits, 'Review upgrade');
    $this->submitForm([], 'I acknowledge I may lose data. Continue anyway.');
    $this->useTestMailCollector();
    $this->submitForm([], 'Perform upgrade');

    // Tests the migration log contains an error message.     $migration = $this->getMigrationPluginManager()->createInstance('d7_file');
    $messages = $migration->getIdMap()->getMessages();

    $count = 0;
    foreach ($messages as $message) {
      
    $admin_user = $this->drupalCreateUser([
      'administer languages',
      'access administration pages',
    ]);
    $this->drupalLogin($admin_user);

    // Add custom language.     $edit = [
      'predefined_langcode' => 'custom',
    ];
    $this->drupalGet('admin/config/regional/language/add');
    $this->submitForm($edit, 'Add custom language');
    // Test validation on missing values.     $this->assertSession()->statusMessageContains('Language code field is required.', 'error');
    $this->assertSession()->statusMessageContains('Language name field is required.', 'error');
    $empty_language = new Language();
    $this->assertSession()->checkboxChecked('edit-direction-' . $empty_language->getDirection());
    $this->assertSession()->addressEquals(Url::fromRoute('language.add'));

    // Test validation of invalid values.     $edit = [
      'predefined_langcode' => 'custom',
      'langcode' => 'white space',
      

  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /** * Tests that fields validation messages are sorted in the fields order. */
  public function testValidationErrorMessagesSortedWithWeight() {
    $form_state = new FormState();
    $form_builder = $this->container->get('form_builder');
    $form_builder->submitForm($this$form_state);

    $messages = \Drupal::messenger()->all();
    \Drupal::messenger()->deleteAll();
    $this->assertTrue(isset($messages['error']));
    $error_messages = $messages['error'];
    $this->assertEquals('Three field is required.', $error_messages[0]);
    $this->assertEquals('Four field is required.', $error_messages[1]);
    $this->assertEquals('Two field is required.', $error_messages[2]);
    $this->assertEquals('One field is required.', $error_messages[3]);
  }

}
$this->assertUrlOutboundAlter("/user/$uid", "/user/$name");

    // Test that a path always uses its alias.     $this->createPathAlias("/user/$uid/test1", '/alias/test1');
    $this->rebuildContainer();
    $this->assertUrlInboundAlter('/alias/test1', "/user/$uid/test1");
    $this->assertUrlOutboundAlter("/user/$uid/test1", '/alias/test1');

    // Test adding an alias via the UI.     $edit = ['path[0][value]' => "/user/$uid/edit", 'alias[0][value]' => '/alias/test2'];
    $this->drupalGet('admin/config/search/path/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('The alias has been saved.');
    $this->drupalGet('alias/test2');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertUrlOutboundAlter("/user/$uid/edit", '/alias/test2');

    // Test a non-existent user is not altered.     $uid++;
    $this->assertUrlOutboundAlter("/user/$uid", "/user/$uid");

    // Test outbound query string altering.     $url = Url::fromRoute('user.login');
    
public function testViewAddWithNodeTitles() {
    $this->drupalCreateContentType(['type' => 'article']);

    $view = [];
    $view['label'] = $this->randomMachineName(16);
    $view['id'] = strtolower($this->randomMachineName(16));
    $view['description'] = $this->randomMachineName(16);
    $view['page[create]'] = FALSE;
    $view['show[wizard_key]'] = 'node';
    $view['page[style][row_plugin]'] = 'titles';
    $this->drupalGet('admin/structure/views/add');
    $this->submitForm($view, 'Save and edit');

    $view_storage_controller = \Drupal::entityTypeManager()->getStorage('view');
    /** @var \Drupal\views\Entity\View $view */
    $view = $view_storage_controller->load($view['id']);

    $display_options = $view->getDisplay('default')['display_options'];
    // Ensure that the 'entity_table' and 'entity_field' properties are set     // property.     $this->assertEquals('node', $display_options['fields']['title']['entity_type']);
    $this->assertEquals('title', $display_options['fields']['title']['entity_field']);

    
$this->assertSame('Aplicar', $view_config->get('display.default.display_options.exposed_form.options.submit_button'));
    $view_config = \Drupal::languageManager()->getLanguageConfigOverride('es', 'views.view.archive');
    $this->assertSame('Aplicar', $view_config->get('display.default.display_options.exposed_form.options.submit_button'));

    // Manually update the translation status so can re-run the import.     $status = locale_translation_get_status();
    $status['drupal']['es']->type = 'local';
    $status['drupal']['es']->files['local']->timestamp = time();
    \Drupal::keyValue('locale.translation_status')->set('drupal', $status['drupal']);
    // Run the translation import.     $this->drupalGet('admin/reports/translations');
    $this->submitForm([], 'Update translations');

    // Ensure that only the config we expected to have changed has.     $comparer = $this->configImporter()->getStorageComparer();
    $expected_changelist_spanish_collection = [
      'create' => [],
      // The view was untranslated but the translation exists so the installer       // performs the translation.       'update' => ['views.view.who_s_new'],
      'delete' => [],
      'rename' => [],
    ];
    
return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('language.negotiation')
      ->set('session.parameter', $form_state->getValue('language_negotiation_session_param'))
      ->save();

    parent::submitForm($form$form_state);
  }

}
$this->assertSession()->fieldNotExists("edit-role");

    // Test that create user gives an admin style message.     $edit = [
      'name' => $this->randomMachineName(),
      'mail' => $this->randomMachineName() . '@example.com',
      'pass[pass1]' => $pass = $this->randomString(),
      'pass[pass2]' => $pass,
      'notify' => FALSE,
    ];
    $this->drupalGet('admin/people/create');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()->pageTextContains('Created a new user account for ' . $edit['name'] . '. No email has been sent.');

    // Test that the cancel user page has admin fields.     $cancel_user = $this->createUser();
    $this->drupalGet('user/' . $cancel_user->id() . '/cancel');
    $this->assertSession()->responseContains('Are you sure you want to cancel the account ' . $cancel_user->getAccountName() . '?');
    $this->assertSession()->responseContains('Disable the account and keep its content.');

    // Test that cancel confirmation gives an admin style message.     $this->submitForm([], 'Confirm');
    $this->assertSession()->pageTextContains('Account ' . $cancel_user->getAccountName() . ' has been disabled.');

    
Home | Imprint | This part of the site doesn't use cookies.