You are here

class SMTPConfigFormTest in SMTP Authentication Support 8

Validate requirements for SMTPConfigForm.

@group SMTP

Hierarchy

Expanded class hierarchy of SMTPConfigFormTest

File

tests/src/Unit/SMTPConfigFormTest.php, line 24

Namespace

Drupal\Tests\smtp\Unit
View source
class SMTPConfigFormTest extends UnitTestCase {

  /**
   * Test setup.
   */
  public function setup() {
    $this->mockConfigFactory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $this->mockConfig = $this
      ->prophesize(Config::class);
    $this->mockConfigFactory
      ->get('smtp.settings')
      ->willReturn($this->mockConfig
      ->reveal());
    $this->mockConfigFactory
      ->getEditable('smtp.settings')
      ->willReturn($this->mockConfig
      ->reveal());
    $this->mockConfigSystemSite = $this
      ->prophesize(Config::class);
    $this->mockConfigSystemSite
      ->get('name')
      ->willReturn('Site name');
    $this->mockConfigFactory
      ->get('system.site')
      ->willReturn($this->mockConfigSystemSite
      ->reveal());
    $this->mockMessenger = $this
      ->prophesize(Messenger::class);
    $this->mockEmailValidator = $this
      ->prophesize(EmailValidatorInterface::class);
    $this->mockCurrentUser = $this
      ->prophesize(AccountProxyInterface::class);
    $this->mockMailManager = $this
      ->prophesize(MailManagerInterface::class);
    $this->mockModuleHandler = $this
      ->prophesize(ModuleHandlerInterface::class);
    $mockContainer = $this->mockContainer = $this
      ->prophesize(ContainerInterface::class);
    $mockContainer
      ->get('config.factory')
      ->willReturn($this->mockConfigFactory
      ->reveal());
    $mockContainer
      ->get('messenger')
      ->willReturn($this->mockMessenger
      ->reveal());
    $mockContainer
      ->get('email.validator')
      ->willReturn($this->mockEmailValidator
      ->reveal());
    $mockContainer
      ->get('current_user')
      ->willReturn($this->mockCurrentUser
      ->reveal());
    $mockContainer
      ->get('plugin.manager.mail')
      ->willReturn($this->mockMailManager
      ->reveal());
    $mockContainer
      ->get('module_handler')
      ->willReturn($this->mockModuleHandler
      ->reveal());
    $mockStringTranslation = $this
      ->prophesize(TranslationInterface::class);
    $mockStringTranslation
      ->translate(Argument::any())
      ->willReturnArgument(0);
    $mockStringTranslation
      ->translate(Argument::any(), Argument::any())
      ->willReturnArgument(0);
    $mockStringTranslation
      ->translateString(Argument::any())
      ->willReturn('.');
    $mockContainer
      ->get('string_translation')
      ->willReturn($mockStringTranslation
      ->reveal());
    \Drupal::setContainer($this->mockContainer
      ->reveal());
  }

  /**
   * Sets the default smtp config.
   */
  public function setDefaultConfig() {
    $this->mockConfig
      ->get('smtp_on')
      ->willReturn(TRUE);
    $this->mockConfig
      ->get('smtp_host')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_hostbackup')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_port')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_protocol')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_autotls')
      ->willReturn(TRUE);
    $this->mockConfig
      ->get('smtp_timeout')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_username')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_password')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_from')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_fromname')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_allowhtml')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_client_hostname')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_client_helo')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_debugging')
      ->willReturn('');
    $this->mockConfig
      ->get('smtp_keepalive')
      ->willReturn(FALSE);
    $this->mockConfig
      ->get('smtp_reroute_address')
      ->willReturn('');
  }

  /**
   * Test if enabled message is properly shown.
   */
  public function testBuildFormEnabledMessage() {
    $this
      ->setDefaultConfig();
    $this->mockConfig
      ->get('smtp_on')
      ->willReturn(TRUE);
    $formBuilder = SMTPConfigForm::create($this->mockContainer
      ->reveal());
    $form = [];
    $formBuilder
      ->buildForm($form, new FormState());
    $this->mockMessenger
      ->addMessage(Argument::which('getUntranslatedString', 'SMTP module is active.'))
      ->shouldHaveBeenCalled();
  }

  /**
   * Test if enabled message is properly shown.
   */
  public function testBuildFormDisabledMessage() {
    $this
      ->setDefaultConfig();
    $this->mockConfig
      ->get('smtp_on')
      ->willReturn(FALSE);
    $formBuilder = SMTPConfigForm::create($this->mockContainer
      ->reveal());
    $form = [];
    $formBuilder
      ->buildForm($form, new FormState());
    $this->mockMessenger
      ->addMessage(Argument::which('getUntranslatedString', 'SMTP module is INACTIVE.'))
      ->shouldHaveBeenCalled();
  }

  /**
   * Test form id.
   */
  public function testGetFormId() {
    $formBuilder = SMTPConfigForm::create($this->mockContainer
      ->reveal());
    $form_id = $formBuilder
      ->getFormId();
    $this
      ->assertEquals('smtp_admin_settings', $form_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SMTPConfigFormTest::setDefaultConfig public function Sets the default smtp config.
SMTPConfigFormTest::setup public function Test setup.
SMTPConfigFormTest::testBuildFormDisabledMessage public function Test if enabled message is properly shown.
SMTPConfigFormTest::testBuildFormEnabledMessage public function Test if enabled message is properly shown.
SMTPConfigFormTest::testGetFormId public function Test form id.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340