Skip to content

Commit

Permalink
EWPP-82: Add additional test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergepavle committed Jul 30, 2020
1 parent 9c586cb commit af834ed
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 22 deletions.
112 changes: 90 additions & 22 deletions modules/oe_media_iframe/tests/src/Functional/MediaIframeFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Drupal\Tests\oe_media_iframe\Functional;

use Drupal\node\Entity\Node;
use Drupal\filter\Entity\FilterFormat;
use Drupal\media\Entity\MediaType;
use Drupal\Tests\media\Functional\MediaFunctionalTestBase;

/**
Expand All @@ -14,53 +15,120 @@
*/
class MediaIframeFormatTest extends MediaFunctionalTestBase {

/**
* {@inheritdoc}
*/
public $defaultTheme = 'stable';

/**
* {@inheritdoc}
*/
public static $modules = [
'oe_media_iframe',
'oe_media_demo',
];

/**
* The media type.
*
* @var \Drupal\media\MediaTypeInterface
*/
protected $mediaType;

/**
* The source field.
*
* @var \Drupal\field\FieldConfigInterface
*/
protected $sourceField;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
\Drupal::configFactory()
->getEditable('media.settings')
->set('standalone_url', TRUE)
->save(TRUE);

$this->container->get('router.builder')->rebuild();

/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = MediaType::create([
'id' => 'test_iframe',
'label' => 'Test iframe source',
'source' => 'oe_media_iframe',
]);
$media_type->save();
$source = $media_type->getSource();
$source_field = $source->createSourceField($media_type);
$source_configuration = $source->getConfiguration();
$source_configuration['source_field'] = $source_field->getName();
$source->setConfiguration($source_configuration);
$source_field->getFieldStorageDefinition()->save();
$source_field->save();
$media_type->set('source_configuration', [
'source_field' => $source_field->getName(),
]);
$media_type->save();
$form_display = \Drupal::service('entity_display.repository')->getFormDisplay('media', $media_type->id());
$source->prepareFormDisplay($media_type, $form_display);
$form_display->save();
$view_display = \Drupal::service('entity_display.repository')->getViewDisplay('media', $media_type->id());
$source->prepareViewDisplay($media_type, $view_display);
$view_display->save();

$this->sourceField = $source_field;
$this->mediaType = $media_type;

// Create user with permission for using oe_media_iframe text format.
$this->adminUser = $this->createUser(array_merge(static::$adminUserPermissions, [FilterFormat::load('oe_media_iframe')->getPermissionName()]));
}

/**
* Test text formats selected for Media source.
*/
public function testMediaSourceTextFormats(): void {
foreach ($this->getFixtures() as $case_name => $test_data) {
$media = $this->storage->create([
'bundle' => 'video_iframe',
'bundle' => $this->mediaType->id(),
'name' => 'Test iframe media',
'oe_media_iframe' => $test_data['html'],
$this->sourceField->getName() => $test_data['html'],
'status' => TRUE,
]);
$media->save();

$node = Node::create([
'type' => 'oe_media_demo',
'title' => 'Test Demo page',
'field_oe_demo_video_iframe' => [
[
'target_id' => $media->id(),
],
],
'status' => TRUE,
]
);
$node->save();

foreach ($test_data['formats_output'] as $format_name => $expected) {
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/structure/media/manage/video_iframe');
$this->drupalGet('admin/structure/media/manage/' . $this->mediaType->id());
$this->getSession()->getPage()->selectFieldOption('Text format', $format_name);
$this->getSession()->getPage()->pressButton('Save');
$this->drupalLogout();
$this->drupalGet('node/' . $node->id());
$this->drupalGet('media/' . $media->id());
$this->assertSession()->responseContains($expected);
}
}

}

/**
* Test text formats selected for Media source.
*/
public function testMediaSourceWidgetConfiguration(): void {
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/structure/media/manage/' . $this->mediaType->id() . '/form-display');
$this->assertTrue($this->assertSession()->optionExists('fields[' . $this->sourceField->getName() . '][region]', 'content')->isSelected());
$this->assertTrue($this->assertSession()->optionExists('fields[' . $this->sourceField->getName() . '][type]', 'oe_media_iframe_textarea')->isSelected());
$this->assertTrue($this->assertSession()->optionExists('fields[' . $this->sourceField->getName() . '][type]', 'oe_media_iframe_textarea')->isSelected());

$this->drupalPostForm(NULL, [], $this->sourceField->getName() . '_settings_edit');
$this->assertText('Widget settings: Media iframe text area');
$this->assertSession()->pageTextContains('Rows');
$this->assertSession()->pageTextContains('Placeholder');
$this->drupalGet('media/add/' . $this->mediaType->id());
$this->assertSession()->fieldExists('Iframe');
$this->assertSession()->pageTextContains('Allowed HTML tags: <iframe allowfullscreen height importance loading name referrerpolicy sandbox src width mozallowfullscreen webkitAllowFullScreen scrolling frameborder>');
}

/**
* Array of data for testing text formats.
*
Expand All @@ -79,13 +147,13 @@ protected function getFixtures(): array {
'test iframe with used attributes for EbS Live' => [
'html' => '<iframe src="http://web:8080/tests/fixtures/example.html" width="800" height="600" frameborder="0" allow allowfullscreen allowpaymentrequest csp importance loading name referrerpolicy sandbox srcdoc mozallowfullscreen webkitAllowFullScreen scrolling><a href="#">invalid</a></iframe><script type="text/javascript">alert(\'no js\')</script>',
'formats_output' => [
'oe_media_iframe' => '<iframe src="http://web:8080/tests/fixtures/example.html" width="800" height="600" frameborder="0" allow="" allowfullscreen="" allowpaymentrequest="" csp="" importance="" loading="" name="" referrerpolicy="" sandbox="" srcdoc="" mozallowfullscreen="" webkitallowfullscreen="" scrolling="" id="">invalid</iframe>alert(\'no js\')',
'oe_media_iframe' => '<iframe src="http://web:8080/tests/fixtures/example.html" width="800" height="600" frameborder="0" allowfullscreen="" importance="" loading="" name="" referrerpolicy="" sandbox="" mozallowfullscreen="" webkitallowfullscreen="" scrolling="" id="">invalid</iframe>alert(\'no js\')',
],
],
'test iframe with all allowed attributes' => [
'html' => '<iframe src="http://web:8080/tests/fixtures/example.html" width="800" height="600" frameborder="0" allow allowfullscreen allowpaymentrequest csp importance loading name referrerpolicy sandbox srcdoc mozallowfullscreen webkitAllowFullScreen scrolling accesskey autocapitalize class contenteditable data-test data-test2 dir draggable dropzone exportparts hidden id inputmode is itemid itemprop itemref itemscope itemtype lang part slot spellcheck style tabindex title translate><a href="#">invalid</a></iframe><script type="text/javascript">alert(\'no js\')</script>',
'formats_output' => [
'oe_media_iframe' => '<iframe src="http://web:8080/tests/fixtures/example.html" width="800" height="600" frameborder="0" allow="" allowfullscreen="" allowpaymentrequest="" csp="" importance="" loading="" name="" referrerpolicy="" sandbox="" srcdoc="" mozallowfullscreen="" webkitallowfullscreen="" scrolling="" lang="" id="" xml:lang="">invalid</iframe>alert(\'no js\')',
'oe_media_iframe' => '<iframe src="http://web:8080/tests/fixtures/example.html" width="800" height="600" frameborder="0" allowfullscreen="" importance="" loading="" name="" referrerpolicy="" sandbox="" mozallowfullscreen="" webkitallowfullscreen="" scrolling="" lang="" id="" xml:lang="">invalid</iframe>alert(\'no js\')',
],
],
'test iframe with not allowed attribute' => [
Expand Down
147 changes: 147 additions & 0 deletions modules/oe_media_iframe/tests/src/Kernel/IframeFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_media_iframe\Kernel;

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\MediaType;

/**
* Tests the field iframe formatter.
*/
class IframeFormatterTest extends KernelTestBase {

/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'field',
'text',
'system',
'options',
'filter',
'user',
'image',
'file',
'media',
'oe_media',
'oe_media_iframe',
];

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The field name.
*
* @var string
*/
protected $fieldName;

/**
* The display.
*
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/
protected $display;

/**
* The media type.
*
* @var \Drupal\media\MediaTypeInterface
*/
protected $mediaType;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig([
'system',
'field',
'options',
'user',
'media',
'oe_media',
'oe_media_iframe',
]);
\Drupal::service('router.builder')->rebuild();
$this->installEntitySchema('media');
$this->installEntitySchema('user');

/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = MediaType::create([
'id' => 'test_iframe',
'label' => 'Test iframe source',
'source' => 'oe_media_iframe',
]);
$media_type->save();
$source = $media_type->getSource();
$source_field = $source->createSourceField($media_type);
$source_configuration = $source->getConfiguration();
$source_configuration['source_field'] = $source_field->getName();
$source->setConfiguration($source_configuration);
$source_field->getFieldStorageDefinition()->save();
$source_field->save();
$media_type->set('source_configuration', [
'source_field' => $source_field->getName(),
]);
$media_type->save();
$view_display = \Drupal::service('entity_display.repository')
->getViewDisplay('media', $media_type->id());
$source->prepareViewDisplay($media_type, $view_display);
$view_display->save();
$this->display = $view_display;

$this->fieldName = $source_field->getName();
$this->mediaType = $media_type;
$this->entityTypeManager = \Drupal::entityTypeManager();
}

/**
* Tests iframe formatter output.
*/
public function testIframeFormatter(): void {
$value = '<iframe src="http://web:8080/tests/fixtures/example.html" invalid-attribute="with value" width="800" height="600" frameborder="0" allow allowfullscreen allowpaymentrequest csp importance loading name referrerpolicy sandbox srcdoc mozallowfullscreen webkitAllowFullScreen scrolling accesskey autocapitalize class contenteditable data-test data-test2 dir draggable dropzone exportparts hidden id inputmode is itemid itemprop itemref itemscope itemtype lang part slot spellcheck style tabindex title translate><a href="#">invalid</a></iframe><script type="text/javascript">alert(\'no js\')</script>';

$entity = $this->entityTypeManager->getStorage('media')->create([
'bundle' => $this->mediaType->id(),
'name' => 'Test iframe media',
'status' => TRUE,
]);
$entity->{$this->fieldName}->value = $value;

// Verify that all allowed attributes is present and disallowed is removed.
$this->renderEntityFields($entity, $this->display);
$this->assertNoRaw($value);
$this->assertRaw('<iframe src="http://web:8080/tests/fixtures/example.html" width="800" height="600" frameborder="0" allowfullscreen="" importance="" loading="" name="" referrerpolicy="" sandbox="" mozallowfullscreen="" webkitallowfullscreen="" scrolling="" lang="" id="" xml:lang="">invalid</iframe>alert(\'no js\')');
}

/**
* Renders fields of a given entity with a given display.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity object with attached fields to render.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display to render the fields in.
*
* @return string
* The rendered entity fields.
*/
protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display): string {
$content = $display->build($entity);
$content = $this->render($content);
return $content;
}

}

0 comments on commit af834ed

Please sign in to comment.