You are here

class JSMessageTestController in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/js_message_test/src/Controller/JSMessageTestController.php \Drupal\js_message_test\Controller\JSMessageTestController

Test Controller to show message links.

Hierarchy

Expanded class hierarchy of JSMessageTestController

1 file declares its use of JSMessageTestController
JsMessageTest.php in core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageTest.php

File

core/modules/system/tests/modules/js_message_test/src/Controller/JSMessageTestController.php, line 8

Namespace

Drupal\js_message_test\Controller
View source
class JSMessageTestController {

  /**
   * Gets the test types.
   *
   * @return string[]
   *   The test types.
   */
  public static function getTypes() {
    return [
      'status',
      'error',
      'warning',
    ];
  }

  /**
   * Gets the test messages selectors.
   *
   * @return string[]
   *   The test test messages selectors.
   *
   * @see core/modules/system/tests/themes/test_messages/templates/status-messages.html.twig
   */
  public static function getMessagesSelectors() {
    return [
      '',
      '[data-drupal-messages-other]',
    ];
  }

  /**
   * Displays links to show messages via Javascript.
   *
   * @return array
   *   Render array for links.
   */
  public function messageLinks() {
    $buttons = [];
    foreach (static::getMessagesSelectors() as $messagesSelector) {
      $buttons[$messagesSelector] = [
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => "Message area: {$messagesSelector}",
        '#attributes' => [
          'data-drupal-messages-area' => $messagesSelector,
        ],
      ];
      foreach (static::getTypes() as $type) {
        $buttons[$messagesSelector]["add-{$type}"] = [
          '#type' => 'html_tag',
          '#tag' => 'button',
          '#value' => "Add {$type}",
          '#attributes' => [
            'type' => 'button',
            'id' => "add-{$messagesSelector}-{$type}",
            'data-type' => $type,
            'data-action' => 'add',
          ],
        ];
        $buttons[$messagesSelector]["remove-{$type}"] = [
          '#type' => 'html_tag',
          '#tag' => 'button',
          '#value' => "Remove {$type}",
          '#attributes' => [
            'type' => 'button',
            'id' => "remove-{$messagesSelector}-{$type}",
            'data-type' => $type,
            'data-action' => 'remove',
          ],
        ];
      }
    }

    // Add alternative message area.
    $buttons[static::getMessagesSelectors()[1]]['messages-other-area'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'data-drupal-messages-other' => TRUE,
      ],
    ];
    $buttons['add-multiple'] = [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => "Add multiple",
      '#attributes' => [
        'type' => 'button',
        'id' => 'add-multiple',
        'data-action' => 'add-multiple',
      ],
    ];
    $buttons['remove-multiple'] = [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => "Remove multiple",
      '#attributes' => [
        'type' => 'button',
        'id' => 'remove-multiple',
        'data-action' => 'remove-multiple',
      ],
    ];
    $buttons['add-multiple-error'] = [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => "Add multiple 'error' and one 'status'",
      '#attributes' => [
        'type' => 'button',
        'id' => 'add-multiple-error',
        'data-action' => 'add-multiple-error',
      ],
    ];
    $buttons['remove-type'] = [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => "Remove 'error' type",
      '#attributes' => [
        'type' => 'button',
        'id' => 'remove-type',
        'data-action' => 'remove-type',
      ],
    ];
    $buttons['clear-all'] = [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => "Clear all",
      '#attributes' => [
        'type' => 'button',
        'id' => 'clear-all',
        'data-action' => 'clear-all',
      ],
    ];
    $buttons['id-no-status'] = [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => "Id no status",
      '#attributes' => [
        'type' => 'button',
        'id' => 'id-no-status',
        'data-action' => 'id-no-status',
      ],
    ];
    return $buttons + [
      '#attached' => [
        'library' => [
          'js_message_test/show_message',
        ],
        'drupalSettings' => [
          'testMessages' => [
            'selectors' => static::getMessagesSelectors(),
            'types' => static::getTypes(),
          ],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JSMessageTestController::getMessagesSelectors public static function Gets the test messages selectors.
JSMessageTestController::getTypes public static function Gets the test types.
JSMessageTestController::messageLinks public function Displays links to show messages via Javascript.