Skip to content
Snippets Groups Projects
Commit df33c69c authored by Roderik Muit's avatar Roderik Muit Committed by Wolfgang Ziegler
Browse files

Issue #3443366 by roderik: Re-factor tests for better maintenance

parent 42eb74c4
No related branches found
No related tags found
1 merge request!41Resolve #3443366 "Convert thunder processors"
Pipeline #156175 passed with warnings
......@@ -92,12 +92,25 @@ abstract class CustomElementsFieldFormatterBase extends PluginBase implements Cu
return $this->configuration['view_mode'];
}
/**
* Gets the display component's configured name.
*/
protected function getComponentName() {
return $this->configuration['name'] ?? '';
}
/**
* Gets the display component's configured weight.
*/
protected function getComponentWeight() {
return $this->configuration['weight'] ?? 0;
}
/**
* Checks that item is a slot or not.
*/
protected function isSlot() {
$config = $this->configuration;
return (bool) ($config['is_slot'] ?? FALSE);
return (bool) ($this->configuration['is_slot'] ?? FALSE);
}
}
......@@ -21,7 +21,7 @@ class RawCeFieldFormatter extends CustomElementsFieldFormatterBase {
* {@inheritdoc}
*/
public function build(FieldItemListInterface $items, CustomElement $custom_element, $langcode = NULL) {
$this->processRaw($items, $custom_element, $this->isSlot(), $this->getFieldDefinition()->getName());
$this->processRaw($items, $custom_element, $this->isSlot(), $this->getComponentName());
}
/**
......@@ -39,6 +39,7 @@ class RawCeFieldFormatter extends CustomElementsFieldFormatterBase {
protected function processRaw(FieldItemListInterface $field_items, CustomElement $custom_element, bool $is_slot, string $element_name) {
$property = $field_items->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName();
$values = array_column($field_items->getValue(), $property);
// @todo hmmmm look at cardinality?
if (count($values) === 1) {
$values = reset($values);
}
......
......@@ -211,15 +211,15 @@ class DefaultContentEntityProcessor implements CustomElementProcessorInterface {
// Set custom element name if configured.
$custom_element->setTag($display->getCustomElementName());
foreach ($display->getComponents() as $field_name => $field_component) {
foreach ($display->getComponents() as $field_name => $display_component) {
if ($this->fieldIsAccessible($entity, $field_name, $custom_element)) {
if (isset($field_definitions[$field_name])) {
$field_definition = $entity->get($field_name)->getFieldDefinition();
/** @var \Drupal\custom_elements\CustomElementsFieldFormatterInterface $formatter */
$formatter = $this->cePluginManager->createInstance($field_component['ce_formatter'] ?? 'auto', [
$formatter = $this->cePluginManager->createInstance($display_component['ce_formatter'] ?? 'auto', [
'field_definition' => $field_definition,
'view_mode' => $viewMode,
] + $field_component
] + $display_component
);
$formatter->prepareBuild([$entity->get($field_name)]);
......
......@@ -67,8 +67,6 @@ class CustomElementsRenderMarkupTest extends BrowserTestBase {
'uri' => 'public://example.jpg',
]);
$this->image->save();
$config = $this->config('custom_elements.settings');
$config->save();
// Enable fields that are also enabled in Core entity view displays.
$ce_storage = \Drupal::service('entity_type.manager')->getStorage('entity_ce_display');
......@@ -101,7 +99,7 @@ class CustomElementsRenderMarkupTest extends BrowserTestBase {
* @return string
* The rendered markup.
*/
private function renderCustomElement(CustomElement $element) {
protected function renderCustomElement(CustomElement $element) {
$render = [
'#theme' => 'custom_element',
'#custom_element' => $element,
......@@ -109,6 +107,31 @@ class CustomElementsRenderMarkupTest extends BrowserTestBase {
return (string) $this->container->get('renderer')->renderPlain($render);
}
/**
* Compares multiline XML fragments.
*
* @param string $expected_markup
* Expected string.
* @param string $tested_markup
* String to be tested.
* @param bool $compare_as_text
* Do not compare as XML (which means e.g. it's OK for attributes to be
* reordered, explicit closing tag == self-closing tag, ...). As of PHP8.1,
* some PHPunit configurations do not report the actual XML comparison
* error. Pass TRUE:
* - if the format of the XML (e.g. the way a tag is closed) matters;
* - if the markup isn't valid XML;
* - (temporarily?) if it's not clear why a test is failing.
*/
protected function assertXmlEquals($expected_markup, $tested_markup, $compare_as_text = FALSE) {
if ($compare_as_text) {
Assert::assertEquals($this->trim($expected_markup), $this->trim($tested_markup));
}
else {
Assert::assertXmlStringEqualsXmlString($this->trim($expected_markup), $this->trim($tested_markup));
}
}
/**
* Helper to trim strings. Removes line-endings.
*
......@@ -118,7 +141,7 @@ class CustomElementsRenderMarkupTest extends BrowserTestBase {
* @return string
* Trimmed sting.
*/
private function trim($string) {
protected function trim($string) {
// Editors strip trailing spaces, so do so for the generated markup.
// Besides that drop new lines.
$string = preg_replace("/ *\n */m", "", $string);
......@@ -152,9 +175,6 @@ class CustomElementsRenderMarkupTest extends BrowserTestBase {
'format' => 'restricted_html',
],
]);
$this->node->field_paragraphs = [
0 => ['entity' => $paragraph],
];
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
......@@ -164,7 +184,7 @@ class CustomElementsRenderMarkupTest extends BrowserTestBase {
<p><strong>Some</strong> example text</p>
</pg-text>
EOF;
Assert::assertXmlStringEqualsXmlString($this->trim($expected_markup), $this->trim($markup));
$this->assertXmlEquals($expected_markup, $markup);
}
/**
......@@ -187,13 +207,13 @@ EOF;
<p><strong>Some</strong> example text</p>
</pg-quote>
EOF;
Assert::assertXmlStringEqualsXmlString($this->trim($expected_markup), $this->trim($markup));
$this->assertXmlEquals($expected_markup, $markup);
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphLinkProcessor
*/
public function doTestLinkParagraph() {
public function doTestLinkParagraph($expected_markup = '', $force_compare_text = FALSE) {
$paragraph = Paragraph::create([
'type' => 'link',
'field_link' => [
......@@ -205,16 +225,18 @@ EOF;
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
$expected_markup = <<<EOF
<pg-link href="http://example.com" title="Example site" type="link" view-mode="full"/>
EOF;
Assert::assertXmlStringEqualsXmlString($this->trim($expected_markup), $this->trim($markup));
if (!$expected_markup) {
$expected_markup = <<<EOF
<pg-link href="http://example.com" title="Example site" type="link" view-mode="full"/>
EOF;
}
$this->assertXmlEquals($expected_markup, $markup);
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphTwitterProcessor
*/
public function doTestTwitterParagraph() {
public function doTestTwitterParagraph($expected_markup = '', $force_compare_text = FALSE) {
$paragraph = Paragraph::create([
'type' => 'twitter',
'field_media' => [
......@@ -228,16 +250,18 @@ EOF;
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
$expected_markup = <<<EOF
if (!$expected_markup) {
$expected_markup = <<<EOF
<pg-twitter src="https://twitter.com/the_real_fago/status/1189191210709049344" type="twitter" view-mode="full"/>
EOF;
Assert::assertXmlStringEqualsXmlString($this->trim($expected_markup), $this->trim($markup));
}
$this->assertXmlEquals($expected_markup, $markup, $force_compare_text);
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphVideoProcessor
*/
public function doTestVideoParagraph() {
public function doTestVideoParagraph($expected_markup = '', $force_compare_text = FALSE) {
$paragraph = Paragraph::create([
'type' => 'video',
'field_video' => [
......@@ -251,10 +275,12 @@ EOF;
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
$expected_markup = <<<EOF
if (!$expected_markup) {
$expected_markup = <<<EOF
<pg-video src="https://www.youtube.com/embed/IPR36uraNwc" thumbnail-src="https://img.youtube.com/vi/IPR36uraNwc/maxresdefault.jpg" type="video" view-mode="full"/>
EOF;
Assert::assertXmlStringEqualsXmlString($this->trim($expected_markup), $this->trim($markup));
}
$this->assertXmlEquals($expected_markup, $markup, $force_compare_text);
}
/**
......@@ -288,7 +314,7 @@ EOF;
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphGalleryProcessor
*/
public function doTestGalleryParagraph() {
public function doTestGalleryParagraph($expected_markup = '', $force_compare_text = FALSE) {
$media_image_data = [
'bundle' => 'image',
'thumbnail' => [
......@@ -321,8 +347,24 @@ EOF;
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
if (!$expected_markup) {
$expected_json = $this->expectedGalleryParagraphJson();
$expected_markup = <<<EOF
<pg-gallery sources="$expected_json" type="gallery" view-mode="full"/>
EOF;
}
$this->assertXmlEquals($expected_markup, $markup, $force_compare_text);
}
/**
* Returns dynamic expected value (so other test class can use it too).
*
* @return string
* Expected value.
*/
protected function expectedGalleryParagraphJson() {
$image_url = $this->image->uri->url;
$expected_json = htmlspecialchars(json_encode([
return htmlspecialchars(json_encode([
[
'url' => $image_url,
'thumbnail-url' => $image_url,
......@@ -337,11 +379,6 @@ EOF;
',
],
]));
$expected_markup = <<<EOF
<pg-gallery sources="$expected_json" type="gallery" view-mode="full"/>
EOF;
// Editors strip trailing spaces, so do so for the generated markup.
Assert::assertXmlStringEqualsXmlString($this->trim($expected_markup), $this->trim(str_replace(" \n", "\n", $markup)));
}
/**
......
......@@ -3,116 +3,24 @@
namespace Drupal\Tests\custom_elements\Functional;
use Drupal\custom_elements\CustomElement;
use Drupal\custom_elements\CustomElementGeneratorTrait;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Assert;
/**
* Test rendering custom elements into markup with .
* Test rendering custom elements into Vue3 markup.
*
* @group custom_elements
*/
class CustomElementsRenderMarkupVue3Test extends BrowserTestBase {
use CustomElementGeneratorTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'custom_elements_test_paragraphs',
'custom_elements_everywhere',
'custom_elements_thunder',
];
/**
* {@inheritdoc}
*/
protected $strictConfigSchema = FALSE;
/**
* The node to use for testing.
*
* @var \Drupal\node\NodeInterface
*/
protected $node;
/**
* The image used for testing.
*
* @var \Drupal\file\FileInterface
*/
protected $image;
class CustomElementsRenderMarkupVue3Test extends CustomElementsRenderMarkupTest {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->node = Node::create([
'type' => 'article',
'title' => 'test',
]);
\Drupal::service('file_system')
->copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = File::create([
'uri' => 'public://example.jpg',
]);
$this->image->save();
$config = $this->config('custom_elements.settings');
$config->set('markup_style', 'vue-3');
$config->save();
// Make paragraph field visible in CE display.
/** @var \Drupal\custom_elements\Entity\EntityCeDisplayInterface $ce_display */
$ce_display = \Drupal::service('entity_type.manager')->getStorage('entity_ce_display')->load('node.article.default');
$ce_display->setComponent('field_paragraphs', [
'ce_formatter' => 'auto',
'is_slot' => TRUE,
]);
$ce_display->save();
}
/**
* Helper to render a custom element into markup.
*
* @param \Drupal\custom_elements\CustomElement $element
* The element.
*
* @return string
* The rendered markup.
*/
private function renderCustomElement(CustomElement $element) {
$render = [
'#theme' => 'custom_element',
'#custom_element' => $element,
];
return (string) $this->container->get('renderer')->renderPlain($render);
}
/**
* Helper to trim strings. Removes line-endings.
*
* @param string $string
* String to trim.
*
* @return string
* Trimmed sting.
*/
private function trim($string) {
// Editors strip trailing spaces, so do so for the generated markup.
// Besides that drop new lines.
$string = preg_replace("/ *\n */m", "", $string);
return preg_replace("/> +</", "><", $string);
}
/**
......@@ -130,204 +38,46 @@ class CustomElementsRenderMarkupVue3Test extends BrowserTestBase {
$this->doTestGalleryParagraph();
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphTextProcessor
*/
public function doTestTextParagraph() {
$paragraph = Paragraph::create([
'type' => 'text',
'field_title' => 'The title',
'field_text' => [
'value' => '<strong>Some</strong> example text',
'format' => 'restricted_html',
],
]);
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
$expected_markup = <<<EOF
<pg-text type="text" view-mode="full" title="The title">
<p><strong>Some</strong> example text</p>
</pg-text>
EOF;
Assert::assertEquals($this->trim($expected_markup), $this->trim($markup));
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphQuoteProcessor
*/
public function doTestQuoteParagraph() {
$paragraph = Paragraph::create([
'type' => 'quote',
'field_text' => [
'value' => '<strong>Some</strong> example text',
'format' => 'restricted_html',
],
]);
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
$expected_markup = <<<EOF
<pg-quote type="quote" view-mode="full">
<p><strong>Some</strong> example text</p>
</pg-quote>
EOF;
Assert::assertEquals($this->trim($expected_markup), $this->trim($markup));
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphLinkProcessor
*/
public function doTestLinkParagraph() {
$paragraph = Paragraph::create([
'type' => 'link',
'field_link' => [
'uri' => 'http://example.com',
'title' => 'Example site',
],
]);
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
public function doTestLinkParagraph($expected_markup = '', $force_compare_text = FALSE) {
$expected_markup = <<<EOF
<pg-link type="link" view-mode="full" title="Example site" href="http://example.com"></pg-link>
EOF;
Assert::assertEquals($this->trim($expected_markup), $this->trim($markup));
parent::doTestLinkParagraph($expected_markup, TRUE);
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphTwitterProcessor
*/
public function doTestTwitterParagraph() {
$paragraph = Paragraph::create([
'type' => 'twitter',
'field_media' => [
Media::create([
'bundle' => 'twitter',
'field_url' => 'https://twitter.com/the_real_fago/status/1189191210709049344',
]),
],
]);
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
public function doTestTwitterParagraph($expected_markup = '', $force_compare_text = FALSE) {
$expected_markup = <<<EOF
<pg-twitter type="twitter" view-mode="full" src="https://twitter.com/the_real_fago/status/1189191210709049344"></pg-twitter>
EOF;
Assert::assertEquals($this->trim($expected_markup), $this->trim($markup));
parent::doTestTwitterParagraph($expected_markup, TRUE);
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphVideoProcessor
*/
public function doTestVideoParagraph() {
$paragraph = Paragraph::create([
'type' => 'video',
'field_video' => [
Media::create([
'bundle' => 'video',
'field_media_video_embed_field' => 'https://www.youtube.com/watch?v=IPR36uraNwc',
]),
],
]);
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
public function doTestVideoParagraph($expected_markup = '', $force_compare_text = FALSE) {
$expected_markup = <<<EOF
<pg-video type="video" view-mode="full" src="https://www.youtube.com/embed/IPR36uraNwc" thumbnail-src="https://img.youtube.com/vi/IPR36uraNwc/maxresdefault.jpg"></pg-video>
EOF;
Assert::assertEquals($this->trim($expected_markup), $this->trim($markup));
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphImageProcessor
*/
public function doTestImageParagraph() {
$paragraph = Paragraph::create([
'type' => 'image',
'field_image' => [
Media::create([
'bundle' => 'image',
'field_image' => [
'target_id' => $this->image->id(),
],
'field_copyright' => 'custom elements copyright',
'field_description' => '<strong>Custom Elements</strong> <p>image</p> description',
'field_source' => 'custom elements images source',
]),
],
]);
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
$expected_markup = <<<EOF
<pg-image type="image" view-mode="full" src="{$this->image->uri->url}" copyright="custom elements copyright" caption="&lt;p&gt;&amp;lt;strong&amp;gt;Custom Elements&amp;lt;/strong&amp;gt; &amp;lt;p&amp;gt;image&amp;lt;/p&amp;gt; description&lt;/p&gt;"></pg-image>
EOF;
Assert::assertEquals($this->trim($expected_markup), $this->trim($markup));
parent::doTestVideoParagraph($expected_markup, TRUE);
}
/**
* @covers \Drupal\custom_elements_thunder\Processor\ParagraphGalleryProcessor
*/
public function doTestGalleryParagraph() {
$media_image_data = [
'bundle' => 'image',
'thumbnail' => [
'target_id' => $this->image->id(),
],
'field_image' => [
'target_id' => $this->image->id(),
],
];
$media_image_1 = Media::create($media_image_data);
$media_image_2 = Media::create($media_image_data +
[
'field_copyright' => 'copyright',
'field_description' => 'description',
'field_source' => 'source',
]);
$paragraph = Paragraph::create([
'type' => 'gallery',
'field_media' => [
Media::create([
'bundle' => 'gallery',
'field_media_images' => [
0 => ['entity' => $media_image_1],
1 => ['entity' => $media_image_2],
],
]),
],
]);
$custom_element = $this->getCustomElementGenerator()
->generate($paragraph, 'full');
$markup = $this->renderCustomElement($custom_element);
$image_url = $this->image->uri->url;
$expected_json = htmlspecialchars(json_encode([
[
'url' => $image_url,
'thumbnail-url' => $image_url,
'alt' => '',
],
[
'url' => $image_url,
'thumbnail-url' => $image_url,
'alt' => '',
'copyright' => 'copyright',
'description' => '<p>description</p>
',
],
]));
public function doTestGalleryParagraph($expected_markup = '', $force_compare_text = FALSE) {
$expected_json = $this->expectedGalleryParagraphJson();
// Parent's output difference: "sources" vs ":sources". (Not the end tag.)
$expected_markup = <<<EOF
<pg-gallery type="gallery" view-mode="full" :sources="$expected_json"></pg-gallery>
EOF;
Assert::assertEquals($this->trim($expected_markup), $this->trim($markup));
parent::doTestGalleryParagraph($expected_markup, TRUE);
}
/**
......@@ -376,6 +126,8 @@ EOF;
*/
public function testNodeRendering() {
// Test rendering with new vue-3 style.
// Unlike in the parent method, the node does not (yet?) get a media field
// value.
$paragraph = Paragraph::create([
'title' => 'Title',
'type' => 'text',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment