Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENEUROPA-2309: Add test for webtools snippet formatter. #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions tests/src/Kernel/WebtoolsSnippetFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_webtools\Kernel;

use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Xss;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\json_field\JsonMarkup;

/**
* Test the webtools snippet formatter.
*
* @coversDefaultClass \Drupal\oe_webtools\Plugin\Field\FieldFormatter\WebtoolsSnippetFormatter
*/
class WebtoolsSnippetFormatterTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'oe_webtools',
'json_field',
'field',
'user',
'entity_test',
'system',
];

/**
* The entity type.
*
* @var string
*/
protected $entityType;

/**
* The bundle of the entity.
*
* @var string
*/
protected $bundle;

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

/**
* The field name.
*
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/
protected $fieldName = 'test_field_media_webtools';

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->installEntitySchema('user');
$this->installEntitySchema('entity_test');

$this->installConfig(['system', 'field']);

$this->entityType = 'entity_test';
$this->bundle = $this->entityType;

$field_storage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => $this->entityType,
'type' => 'json',
]);
$field_storage->save();

$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->bundle,
]);
$field->save();

/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');

// Create a display for the default view mode.
$this->display = $display_repository->getViewDisplay($this->entityType, $this->bundle);
$this->display->setComponent($this->fieldName, [
'type' => 'webtools_snippet',
])->save();
}

/**
* Assert presence of 'Smart loader' with formatter.
*/
public function testFormatterLibrary(): void {
$data = '{"service":"map","map":{"background":["osmec"]},"version":"2.0"}';
$entity = EntityTest::create([
$this->fieldName => $data,
]);
$entity->save();

$build = $this->display->build($entity);
$html = $this->render($build);

// Assert the render contains the required library.
$this->assertContains('<script src="//europa.eu/webtools/load.js" defer></script>', $html);
}

/**
* Tests that the formatter is rendered correctly.
*
* @param string $data
* The media webtools field value.
*
* @dataProvider providerFormatter
*/
public function testFormatter(string $data): void {
$entity = EntityTest::create([
$this->fieldName => $data,
]);
$entity->save();

$build = $this->display->build($entity);
$output = (string) $this->container->get('renderer')->renderRoot($build);

// Assert correct format.
$this->assertContains('<script type="application/json">' . JsonMarkup::create(Json::encode(Json::decode($data))) . '</script>', $output);

// Assert the output is XSS filtered.
$this->assertTrue($output === Xss::filter($output, ['script', 'div']), 'The output is XSS filtered');

// Assert the script tags were escaped.
$this->assertTrue(substr_count($output, '</script') === 1, 'Script tags were escaped');

// Assert the html comment tags were escaped.
$this->assertTrue(substr_count($output, '<!--') === 0, 'Comment tags were escaped');
}

/**
* Data provider for testFormatter().
*
* @see ::testFormatter()
*
* @return array
* Data provider for the webtools snippet field.
*/
public function providerFormatter(): array {
return [
['{"service":"map","map":{"background":["osmec"]},"version":"2.0"}'],
['{"service":"map","<!--map":{"background":["osmec"]},"version":"2.0"}'],
['{"service":"map","custom":"</script>","map":{"background":["osmec"]},"version":"2.0"}'],
['{"service":"smk","type":"user","screen_name":"EU_Commission","count":3,"include_rts":false,"rts_display_original":false,"exclude_replies":true,"display_user":true,"display_user_pic":true,"auto_expand_photo":false,"auto_expand_video":false,"tweet_more_btn":true}'],
['{"service":"charts","data":{"webtools":{"jsonstat":{"url":"https://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/en/ttr00012?&geo=AT&geo=BE&geo=BG&geo=CY&geo=CZ&geo=DE&geo=DK&geo=EE&geo=EL&geo=ES&geo=FI&geo=FR&geo=HR&geo=HU&geo=IE&geo=IT&geo=LT&geo=LU&geo=LV&geo=MT&geo=NL&geo=NO&geo=PL&geo=PT&geo=RO&geo=SE&geo=SI&geo=SK&geo=UK&precision=1&time=2007&time=2008&time=2009&time=2010&time=2011&time=2012&time=2013&time=2014","options":{"series":"unit","categories":"time","sheets":"geo"}}},"xAxis":{"type":"category"}},"provider":"highcharts"'],
];
}

}