messagesByType example



  /** * Assert that the pluggable mail system is functional. */
  public function testErrorMessageDisplay() {
    // Switch mail backends.     $this->configureDefaultMailInterface('test_php_mail_failure');

    // Test with errors displayed to users.     \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en');
    $messages = \Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR);
    $this->assertEquals('Unable to send email. Contact the site administrator if the problem persists.', $messages[0]);
    \Drupal::messenger()->deleteAll();

    // Test without errors displayed to users.     \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en', ['_error_message' => '']);
    $this->assertEmpty(\Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR));
  }

  /** * Tests that message sending may be canceled. * * @see mail_cancel_test_mail_alter() */
// The test for \Drupal::service('file_system')->moveUploadedFile()     // triggering a warning is unavoidable. We're interested in what happens     // afterwards in _file_save_upload_from_form().     if ($this->state->get('file_test.disable_error_collection')) {
      define('SIMPLETEST_COLLECT_ERRORS', FALSE);
    }

    $form['file_test_upload']['#upload_validators'] = $validators;
    $form['file_test_upload']['#upload_location'] = $destination;

    $this->messenger->addStatus($this->t('Number of error messages before _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));
    $file = _file_save_upload_from_form($form['file_test_upload']$form_state, 0, $form_state->getValue('file_test_replace'));
    $this->messenger->addStatus($this->t('Number of error messages after _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));

    if ($file) {
      $form_state->setValue('file_test_upload', $file);
      $this->messenger->addStatus($this->t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()]));
      $this->messenger->addStatus($this->t('File name is @filename.', ['@filename' => $file->getFilename()]));
      $this->messenger->addStatus($this->t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()]));
      $this->messenger->addStatus($this->t('You WIN!'));
    }
    elseif ($file === FALSE) {
      
$formState = new FormState();

    $publishForm->buildForm($form$formState$workspace);

    $logger
      ->expects($this->once())
      ->method('log')
      ->with(RfcLogLevel::ERROR, 'Unexpected error');

    $publishForm->submitForm($form$formState);

    $messages = $messenger->messagesByType(MessengerInterface::TYPE_ERROR);
    $this->assertCount(1, $messages);
    $this->assertEquals('Publication failed. All errors have been logged.', $messages[0]);
  }

}

  protected function assertNoWarningMessages(ViewExecutable $view): void {
    $messenger = $this->container->get('messenger');

    $view->validate();
    $this->assertCount(0, $messenger->messagesByType(MessengerInterface::TYPE_WARNING));
  }

  /** * Assert the warning messages are shown after changing the page_1 display. * * @param \Drupal\views\ViewExecutable $view * The view to check. * @param array $unequal_options * An array of options that should be unequal. * * @throws \Exception * * @internal */
->set(['content_translation', 'translation_form']$translation_form);
    if ($is_submitted) {
      $form_state->setSubmitted();
    }
    $form_state->setRebuild($is_rebuilding);

    $handler = $this->entityTypeManager->getHandler($this->entityTypeId, 'translation');
    $actual = $handler->entityFormSharedElements($element$form_state$element);

    $this->assertEquals($expected$actual);
    if ($display_warning) {
      $messages = $this->messenger->messagesByType('warning');
      $this->assertCount(1, $messages);
      $expected_message = sprintf('Fields that apply to all languages are hidden to avoid conflicting changes. <a href="%s">Edit them on the original language form</a>.', $entity->toUrl('edit-form')->toString());
      $this->assertSame($expected_message(string) reset($messages));
    }
  }

  /** * Returns test cases for ::testEntityFormSharedElements(). * * @return array[] * An array of test cases, each one containing the element to alter, the * form state, and the expected altered element. */
$this->messenger->addStatus('Second message with <em>markup!</em> (not removed).');
    $messages = $this->messenger->deleteByType(MessengerInterface::TYPE_STATUS);
    // Remove the first.     unset($messages[0]);

    // Re-add the second.     foreach ($messages as $message) {
      $this->messenger->addStatus($message);
    }

    // Check we only have the second one.     $this->assertCount(1, $this->messenger->messagesByType(MessengerInterface::TYPE_STATUS));
    $this->assertContainsEquals('Second message with <em>markup!</em> (not removed).', $this->messenger->deleteByType(MessengerInterface::TYPE_STATUS));

  }

  /** * Tests we don't add duplicates. * * @covers ::all * @covers ::addStatus * @covers ::addWarning * @covers ::addError * @covers ::deleteByType * @covers ::deleteAll */
Home | Imprint | This part of the site doesn't use cookies.