From 3a5271330bc9c0eed1effa1871765600aee502e2 Mon Sep 17 00:00:00 2001 From: Mohammed Nassar Date: Mon, 23 Sep 2019 14:05:40 +0000 Subject: [PATCH 1/4] OPENEUROPA-2309: Add test for webtools snippet formatter. --- .../Kernel/WebtoolsSnippetFormatterTest.php | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 modules/oe_webtools_media/tests/src/Kernel/WebtoolsSnippetFormatterTest.php diff --git a/modules/oe_webtools_media/tests/src/Kernel/WebtoolsSnippetFormatterTest.php b/modules/oe_webtools_media/tests/src/Kernel/WebtoolsSnippetFormatterTest.php new file mode 100644 index 00000000..e183a92a --- /dev/null +++ b/modules/oe_webtools_media/tests/src/Kernel/WebtoolsSnippetFormatterTest.php @@ -0,0 +1,163 @@ +installEntitySchema('user'); + $this->installEntitySchema('entity_test'); + + $this->installConfig(['system', 'field']); + + $this->entityType = 'entity_test'; + $this->bundle = $this->entityType; + + $field_storage = FieldStorageConfig::create([ + 'field_name' => 'test_field_media_webtools', + 'entity_type' => $this->entityType, + 'type' => 'json', + ]); + $field_storage->save(); + + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => $this->bundle, + ]); + $field->save(); + + // The display mode. + $this->display = EntityViewDisplay::create([ + 'targetEntityType' => $this->entityType, + 'bundle' => $this->bundle, + 'mode' => 'default', + ]); + $this->display->setComponent('test_field_media_webtools', [ + 'type' => 'webtools_snippet', + ]); + $this->display->save(); + } + + /** + * Tests that the formatter contains the necessary library. + */ + public function testFormatterLibrary() { + $data = '{"service":"map","map":{"background":["osmec"]},"version":"2.0"}'; + $entity = EntityTest::create([ + 'test_field_media_webtools' => $data, + ]); + $entity->save(); + + $build = $this->display->build($entity); + $html = $this->render($build); + + // Assert the render contains the required library. + $this->assertContains('', $html); + } + + /** + * Tests that the formatter is rendered correctly. + * + * @param string $data + * The media webtools field value. + * + * @dataProvider providerFormatter + * + * @throws \Exception + */ + public function testFormatter($data) { + $entity = EntityTest::create([ + 'test_field_media_webtools' => $data, + ]); + $entity->save(); + + $build = $this->display->build($entity); + $output = (string) $this->container->get('renderer')->renderRoot($build); + + // Assert correct format. + $this->assertContains('', $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, 'assertTrue(substr_count($output, '