Skip to content

Commit 3af2b1c

Browse files
committed
Add kernel tests for some generic blocks
1 parent 773bacc commit 3af2b1c

File tree

6 files changed

+467
-1
lines changed

6 files changed

+467
-1
lines changed

html/modules/custom/ghi_blocks/src/Plugin/Block/GHIBlockBase.php

+3
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,9 @@ protected function addButtonsToCleanValueKeys(array $form, FormStateInterface $f
11471147
public function isPreviewSubmit(FormStateInterface $form_state) {
11481148
$current_subform = $form_state->get('current_subform');
11491149
$triggering_element = $form_state->getTriggeringElement();
1150+
if (!$triggering_element) {
1151+
return FALSE;
1152+
}
11501153
$action = end($triggering_element['#parents']);
11511154
$values = $form_state->getValues();
11521155
return $action == 'preview' && !array_key_exists($current_subform, $values);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Drupal\Tests\ghi_blocks\Kernel;
4+
5+
use Drupal\KernelTests\KernelTestBase;
6+
use Drupal\layout_builder\SectionComponent;
7+
8+
/**
9+
* Base class for block kernel tests.
10+
*
11+
* @group ghi_blocks
12+
*/
13+
abstract class BlockKernelTestBase extends KernelTestBase {
14+
15+
/**
16+
* Modules to enable.
17+
*
18+
* @var array
19+
*/
20+
protected static $modules = [
21+
'system',
22+
'user',
23+
'node',
24+
'taxonomy',
25+
'field',
26+
'layout_builder',
27+
'layout_discovery',
28+
'layout_builder',
29+
'text',
30+
'filter',
31+
'file',
32+
'token',
33+
'path',
34+
'path_alias',
35+
'pathauto',
36+
'migrate',
37+
'hpc_api',
38+
'ghi_form_elements',
39+
'ghi_subpages',
40+
'ghi_sections',
41+
'ghi_blocks',
42+
'ghi_base_objects',
43+
];
44+
45+
/**
46+
* Get a block plugin.
47+
*
48+
* @param string $plugin_id
49+
* The plugin id.
50+
* @param array $configuration
51+
* The hpc-specific configuration.
52+
* @param string $label
53+
* The label.
54+
* @param bool $label_display
55+
* Whether the label should be displayed or not.
56+
*
57+
* @return \Drupal\ghi_blocks\Plugin\Block\Generic\ExternalWidget
58+
* The block plugin.
59+
*/
60+
protected function createBlockPlugin($plugin_id, $configuration, $label = '<none>', $label_display = FALSE) {
61+
$configuration = [
62+
'id' => $plugin_id,
63+
'label' => $label,
64+
'label_display' => $label_display,
65+
'provider' => 'ghi_blocks',
66+
'hpc' => $configuration,
67+
];
68+
$component = new SectionComponent('10000000-0000-1000-a000-000000000000', 'content', $configuration);
69+
return $component?->getPlugin();
70+
}
71+
72+
}

html/modules/custom/ghi_blocks/tests/src/Kernel/BlocksWithManagedFilesTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Drupal\Tests\ghi_content\Kernel;
3+
namespace Drupal\Tests\ghi_blocks\Kernel;
44

55
use Drupal\KernelTests\KernelTestBase;
66
use Drupal\Tests\user\Traits\UserCreationTrait;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Drupal\Tests\ghi_blocks\Kernel;
4+
5+
use Drupal\Core\Form\FormState;
6+
use Drupal\ghi_blocks\Plugin\Block\Generic\Datawrapper;
7+
8+
/**
9+
* Tests the datawrapper block plugin.
10+
*
11+
* @group ghi_blocks
12+
*/
13+
class DatawrapperBlockTest extends BlockKernelTestBase {
14+
15+
const EMBED_CODE_VALID = '<iframe src="https://datawrapper.dwcdn.net/CHART_ID"></iframe>';
16+
const EMBED_CODE_INVALID = '<iframe src="https://invalid.url/CHART_ID"></iframe>';
17+
18+
/**
19+
* Tests the link bock.
20+
*/
21+
public function testBlockProperties() {
22+
$plugin = $this->getBlockPlugin();
23+
$this->assertInstanceOf(Datawrapper::class, $plugin);
24+
25+
// Make the private method callable.
26+
$method = (new \ReflectionClass(Datawrapper::class))->getMethod('getAllowedHosts');
27+
$allowed_hosts = $method->invoke($plugin);
28+
$this->assertCount(1, $allowed_hosts);
29+
$this->assertArrayHasKey('datawrapper.dwcdn.net', $allowed_hosts);
30+
}
31+
32+
/**
33+
* Tests the block build.
34+
*/
35+
public function testBlockBuild() {
36+
$plugin = $this->getBlockPlugin();
37+
$build = $plugin->buildContent();
38+
$this->assertNull($build);
39+
40+
$plugin = $this->getBlockPlugin(self::EMBED_CODE_VALID);
41+
$build = $plugin->buildContent();
42+
$this->assertArrayHasKey(0, $build);
43+
}
44+
45+
/**
46+
* Tests the link block forms.
47+
*/
48+
public function testBlockForms() {
49+
$plugin = $this->getBlockPlugin();
50+
51+
$form_state = new FormState();
52+
$form_state->set('block', $plugin);
53+
$form = $plugin->getConfigForm(['#parents' => []], $form_state);
54+
$this->assertArrayHasKey('embed', $form);
55+
56+
// Prepare form validation.
57+
$form['embed']['#parents'] = ['container'];
58+
$form_state->set('current_subform', 'basic');
59+
60+
// Validate a valid embed code.
61+
$form_state->setValue(['basic', 'embed'], self::EMBED_CODE_VALID);
62+
$plugin->blockValidate(['container' => $form], $form_state);
63+
$this->assertEmpty($form_state->getErrors());
64+
65+
// Validate an invalid embed code.
66+
$form_state->setValue(['basic', 'embed'], self::EMBED_CODE_INVALID);
67+
$plugin->blockValidate(['container' => $form], $form_state);
68+
$this->assertNotEmpty($form_state->getErrors());
69+
}
70+
71+
/**
72+
* Get a link block plugin.
73+
*
74+
* @param array $embed
75+
* The embed code for the plugin.
76+
*
77+
* @return \Drupal\ghi_blocks\Plugin\Block\Generic\ExternalWidget
78+
* The block plugin.
79+
*/
80+
private function getBlockPlugin($embed = '') {
81+
$configuration = [
82+
'embed' => $embed,
83+
];
84+
return $this->createBlockPlugin('generic_datawrapper', $configuration);
85+
}
86+
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace Drupal\Tests\ghi_blocks\Kernel;
4+
5+
use Drupal\Core\Form\FormState;
6+
use Drupal\ghi_blocks\Plugin\Block\Generic\ExternalWidget;
7+
use Drupal\Tests\ghi_base_objects\Traits\BaseObjectTestTrait;
8+
9+
/**
10+
* Tests the external widget block plugin.
11+
*
12+
* @group ghi_blocks
13+
*/
14+
class ExternalWidgetBlockTest extends BlockKernelTestBase {
15+
16+
use BaseObjectTestTrait;
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function setUp(): void {
22+
parent::setUp();
23+
24+
$this->installEntitySchema('base_object');
25+
$this->createBaseObjectType([
26+
'id' => 'plan',
27+
]);
28+
}
29+
30+
/**
31+
* Tests the link bock.
32+
*/
33+
public function testBlockProperties() {
34+
$widget = $this->buildWidgetConfiguration();
35+
$plugin = $this->getBlockPlugin([$widget]);
36+
$this->assertInstanceOf(ExternalWidget::class, $plugin);
37+
38+
// Make the private method callable.
39+
$method = (new \ReflectionClass(ExternalWidget::class))->getMethod('getAllowedHosts');
40+
$allowed_hosts = $method->invoke($plugin);
41+
$this->assertCount(4, $allowed_hosts);
42+
$this->assertArrayHasKey('humdata.org', $allowed_hosts);
43+
$this->assertArrayHasKey('powerbi.com', $allowed_hosts);
44+
$this->assertArrayHasKey('tableau.com', $allowed_hosts);
45+
$this->assertArrayHasKey('experience.arcgis.com', $allowed_hosts);
46+
}
47+
48+
/**
49+
* Tests the block build.
50+
*/
51+
public function testBlockBuild() {
52+
$widgets = [
53+
$this->buildWidgetConfiguration('https://app.powerbi.com/view'),
54+
];
55+
$plugin = $this->getBlockPlugin($widgets);
56+
57+
$build = $plugin->buildContent();
58+
$this->assertArrayHasKey(0, $build);
59+
$this->assertArrayNotHasKey(1, $build);
60+
61+
// Add an additional empty widget.
62+
$widgets = [
63+
$this->buildWidgetConfiguration('https://app.powerbi.com/view'),
64+
$this->buildWidgetConfiguration(),
65+
];
66+
$plugin = $this->getBlockPlugin($widgets);
67+
68+
$build = $plugin->buildContent();
69+
$this->assertArrayHasKey(0, $build);
70+
$this->assertArrayNotHasKey(1, $build);
71+
}
72+
73+
/**
74+
* Tests the block build with no widgets.
75+
*/
76+
public function testBlockBuildNoWidgets() {
77+
$widget = $this->buildWidgetConfiguration();
78+
$plugin = $this->getBlockPlugin([$widget]);
79+
80+
$build = $plugin->buildContent();
81+
$this->assertNull($build);
82+
}
83+
84+
/**
85+
* Tests the link block forms.
86+
*/
87+
public function testBlockForms() {
88+
$plugin = $this->getBlockPlugin();
89+
90+
$form_state = new FormState();
91+
$form_state->set('block', $plugin);
92+
$form = $plugin->getConfigForm(['#parents' => []], $form_state);
93+
$this->assertArrayHasKey('select_number', $form);
94+
$this->assertArrayHasKey('widgets', $form);
95+
96+
// Prepare form validation.
97+
$form['widgets'][1]['widget_url']['#parents'] = ['container', 'widgets', 1];
98+
$form_state->set('current_subform', 'basic');
99+
100+
// Validate a valid embed code.
101+
$form_state->setValue(['basic', 'select_number'], 1);
102+
$form_state->setValue(['basic', 'widgets', 1], $this->buildWidgetConfiguration());
103+
$plugin->blockValidate(['container' => $form], $form_state);
104+
$this->assertNotEmpty($form_state->getErrors());
105+
106+
$form_state->clearErrors();
107+
$form_state->setValue(['basic', 'widgets', 1], $this->buildWidgetConfiguration('https://app.powerbi.com/view'));
108+
$plugin->blockValidate(['container' => $form], $form_state);
109+
$this->assertEmpty($form_state->getErrors());
110+
111+
$form_state->clearErrors();
112+
$form_state->setValue(['basic', 'widgets', 1], $this->buildWidgetConfiguration('https://invalid.url/view'));
113+
$plugin->blockValidate(['container' => $form], $form_state);
114+
$this->assertNotEmpty($form_state->getErrors());
115+
}
116+
117+
/**
118+
* Get a link block plugin.
119+
*
120+
* @param array $widgets
121+
* The widget configurations to add to the plugin.
122+
*
123+
* @return \Drupal\ghi_blocks\Plugin\Block\Generic\ExternalWidget
124+
* The block plugin.
125+
*/
126+
private function getBlockPlugin($widgets = []) {
127+
$configuration = [
128+
'select_number' => min(count($widgets), 2),
129+
'widgets' => $widgets,
130+
];
131+
return $this->createBlockPlugin('generic_external_widget', $configuration);
132+
}
133+
134+
/**
135+
* Build a widget configuration.
136+
*
137+
* @param string $url
138+
* The widget url.
139+
* @param bool $process
140+
* Whether to process the url.
141+
* @param bool $skip_validation
142+
* Whether to skipt the validation.
143+
* @param string $height
144+
* The height of the widget.
145+
*
146+
* @return array
147+
* The configuration array for a widget.
148+
*/
149+
private function buildWidgetConfiguration($url = '', $process = TRUE, $skip_validation = FALSE, $height = '600px') {
150+
return [
151+
'widget_url' => $url,
152+
'process_widget_url' => $process,
153+
'widget_url_skip_validation' => $skip_validation,
154+
'widget_height' => $height,
155+
];
156+
}
157+
158+
}

0 commit comments

Comments
 (0)