You are here

public function FormassemblyHtmlResponseAttachmentsProcessorTest::testProcessAttachments in FormAssembly 8

@covers ::processAttachments

File

tests/src/Unit/FormassemblyHtmlResponseAttachmentsProcessorTest.php, line 104

Class

FormassemblyHtmlResponseAttachmentsProcessorTest
Unit test calls for FormassemblyHtmlResponseAttachmentsProcessor.

Namespace

Drupal\Tests\formassembly\Unit

Code

public function testProcessAttachments() {

  // Create a response with fa_form_attachments attachments and ensure that
  // they are properly converted to html_head attachments. Also ensure that
  // the other html_head elements are added to create the necessary
  // javascript in the right order.
  $response = new HtmlResponse();
  $attachments['fa_form_attachments'] = [
    0 => [
      '#type' => 'fa_form_inline_js',
      '#value' => "console.log('test')",
      '#weight' => 6,
    ],
    1 => [
      '#type' => 'fa_form_external_css',
      '#rel' => 'stylesheet',
      '#href' => 'http://example.com/example.css',
      '#weight' => 1,
    ],
    2 => [
      '#type' => 'fa_form_external_js',
      '#src' => 'http://example.com/example.js',
      '#weight' => 0,
    ],
  ];
  $response
    ->setAttachments($attachments);
  $config_factory = $this
    ->getConfigFactoryStub([
    'formassembly.api.oauth' => [
      'instance' => NULL,
    ],
  ]);
  $response = $this
    ->getFormassemblyAttachmentProcessor($config_factory)
    ->processAttachments($response);
  $processedAttachments = $response
    ->getAttachments();
  $this
    ->assertEquals('fa_form_external_js', $processedAttachments["html_head"][0][0]["#type"]);
  $this
    ->assertEquals('fa_form_external_css', $processedAttachments["html_head"][1][0]["#type"]);
  $this
    ->assertEquals('fa_form_inline_js', $processedAttachments["html_head"][2][0]["#type"]);
  $this
    ->assertArrayNotHasKey('fa_form_attachments', $processedAttachments, 'The fa_form_attachments attachments are converted to html_head attachments.');
}