You are here

mailgun_test.module in Mailgun 8

Mailgun test module.

File

tests/modules/mailgun_test/mailgun_test.module
View source
<?php

/**
 * @file
 * Mailgun test module.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mailgun_test_form_mailgun_admin_settings_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Replace settings form validate with fake one.
  $index = array_search('::validateForm', $form['#validate']);
  $form['#validate'][$index] = 'mailgun_test_settings_form_validate';
}

/**
 * Overridden validate for test purposes.
 */
function mailgun_test_settings_form_validate($form, FormStateInterface $form_state) {

  /** @var \Drupal\mailgun\MailgunHandlerInterface $mailgun_handler */
  $mailgun_handler = \Drupal::service('mailgun.mail_handler');
  if ($form_state
    ->getValue('api_key') === 'key-1234567890workingabcdefghijklmno') {

    // Do nothing. It's just to emulate correct API key.
  }
  elseif ($mailgun_handler
    ->validateMailgunApiKey($form_state
    ->getValue('api_key')) === FALSE) {
    $form_state
      ->setErrorByName('api_key', "Couldn't connect to the Mailgun API. Please check your API settings.");
  }
}

Functions