View source
<?php
namespace Drupal\Tests\formassembly\Unit;
use Drupal\Core\Asset\AssetCollectionRendererInterface;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\AttachmentsResponseProcessorInterface;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Render\RendererInterface;
use Drupal\formassembly\Component\Render\FormassemblyHtmlResponseAttachmentsProcessor;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
class FormassemblyHtmlResponseAttachmentsProcessorTest extends UnitTestCase {
protected $attachmentProcessor;
protected $assetResolver;
protected $cssCollectionRenderer;
protected $jsCollectionRenderer;
protected $requestStack;
protected $renderer;
protected $moduleHandler;
protected function setUp() {
parent::setUp();
$this->attachmentProcessor = $this
->getMockBuilder(AttachmentsResponseProcessorInterface::class)
->disableOriginalConstructor()
->getMock();
$this->attachmentProcessor
->method('processAttachments')
->willReturnArgument(0);
$this->assetResolver = $this
->prophesize(AssetResolverInterface::class);
$this->cssCollectionRenderer = $this
->prophesize(AssetCollectionRendererInterface::class);
$this->jsCollectionRenderer = $this
->prophesize(AssetCollectionRendererInterface::class);
$this->requestStack = $this
->prophesize(RequestStack::class);
$this->renderer = $this
->prophesize(RendererInterface::class);
$this->moduleHandler = $this
->prophesize(ModuleHandlerInterface::class);
}
public function testProcessAttachments() {
$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.');
}
public function testProcessAttachmentsNoAdditions() {
$response = new HtmlResponse();
$config_factory = $this
->getConfigFactoryStub();
$response = $this
->getFormassemblyAttachmentProcessor($config_factory)
->processAttachments($response);
$processedAttachments = $response
->getAttachments();
$this
->assertEmpty($processedAttachments);
}
protected function getFormassemblyAttachmentProcessor(ConfigFactoryInterface $config_factory) {
return new FormassemblyHtmlResponseAttachmentsProcessor($this->attachmentProcessor, $this->assetResolver
->reveal(), $config_factory, $this->cssCollectionRenderer
->reveal(), $this->jsCollectionRenderer
->reveal(), $this->requestStack
->reveal(), $this->renderer
->reveal(), $this->moduleHandler
->reveal());
}
}