Skip to content

Commit 2181c73

Browse files
committed
Add kernel tests for some generic blocks
1 parent 773bacc commit 2181c73

11 files changed

+969
-17
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ public function needsContextConfiguration() {
577577
* TRUE if the blocks base object can be configured, FASLE otherwise.
578578
*/
579579
public function canConfigureContexts() {
580-
$instance = $this->formState->get('block') ?? $this;
580+
$instance = $this->formState?->get('block') ?? $this;
581581
$base_objects_per_bundle = [];
582582
$can_configure = FALSE;
583583

@@ -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);
@@ -1719,7 +1722,7 @@ public function getCurrentPlanObject() {
17191722
*/
17201723
public function getContextValue($name) {
17211724
$contexts = $this->getContexts();
1722-
return $contexts[$name] ? $contexts[$name]->getContextValue() : NULL;
1725+
return array_key_exists($name, $contexts) ? $contexts[$name]->getContextValue() : NULL;
17231726
}
17241727

17251728
/**

html/modules/custom/ghi_blocks/src/Plugin/Block/Generic/LinkCarousel.php

-10
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,6 @@ public function getConfigForm(array $form, FormStateInterface $form_state) {
148148
return $form;
149149
}
150150

151-
/**
152-
* Validate handler for portlet configuration form.
153-
*/
154-
public function blockValidate($form, FormStateInterface $form_state) {
155-
if ($this->isPreviewSubmit($form_state)) {
156-
return;
157-
}
158-
159-
}
160-
161151
/**
162152
* {@inheritdoc}
163153
*/

html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/CarouselItem.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ public function getDescription() {
8686
/**
8787
* Get the image.
8888
*
89-
* @return \Drupal\file\Entity\File
90-
* A file object for the image file.
89+
* @return \Drupal\file\Entity\File|null
90+
* A file object for the image file or NULL.
9191
*/
9292
public function getImage() {
93-
return File::load(reset($this->config['value']['image']));
93+
$image_ids = $this->config['value']['image'] ?? [];
94+
return !empty($image_ids) ? File::load(reset($image_ids)) : NULL;
9495
}
9596

9697
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
/**
73+
* Call a private or protected method on the given class.
74+
*
75+
* @param object $class
76+
* The object.
77+
* @param string $method_name
78+
* The method name.
79+
* @param array $arguments
80+
* Optional arguments for the method.
81+
*
82+
* @return mixed
83+
* The return of the method call.
84+
*/
85+
protected function callPrivateMethod($class, $method_name, $arguments = NULL) {
86+
// Make the private method callable.
87+
$method = (new \ReflectionClass($class::class))->getMethod($method_name);
88+
return $arguments ? $method->invokeArgs($class, $arguments) : $method->invoke($class);
89+
}
90+
91+
}

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

+1-2
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;
@@ -43,7 +43,6 @@ class BlocksWithManagedFilesTest extends KernelTestBase {
4343
'path_alias',
4444
'pathauto',
4545
'hpc_api',
46-
// 'hpc_common',
4746
'ghi_form_elements',
4847
'ghi_subpages',
4948
'ghi_sections',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 block.
20+
*/
21+
public function testBlockProperties() {
22+
$plugin = $this->getBlockPlugin();
23+
$this->assertInstanceOf(Datawrapper::class, $plugin);
24+
25+
$allowed_hosts = $this->callPrivateMethod($plugin, 'getAllowedHosts');
26+
$this->assertCount(1, $allowed_hosts);
27+
$this->assertArrayHasKey('datawrapper.dwcdn.net', $allowed_hosts);
28+
}
29+
30+
/**
31+
* Tests the block build.
32+
*/
33+
public function testBlockBuild() {
34+
$plugin = $this->getBlockPlugin();
35+
$build = $plugin->buildContent();
36+
$this->assertNull($build);
37+
38+
$plugin = $this->getBlockPlugin(self::EMBED_CODE_VALID);
39+
$build = $plugin->buildContent();
40+
$this->assertArrayHasKey(0, $build);
41+
}
42+
43+
/**
44+
* Tests the block forms.
45+
*/
46+
public function testBlockForms() {
47+
$plugin = $this->getBlockPlugin();
48+
49+
$form_state = new FormState();
50+
$form_state->set('block', $plugin);
51+
$form = $plugin->getConfigForm(['#parents' => []], $form_state);
52+
$this->assertArrayHasKey('embed', $form);
53+
54+
// Prepare form validation.
55+
$form['embed']['#parents'] = ['container'];
56+
$form_state->set('current_subform', 'basic');
57+
58+
// Validate a valid embed code.
59+
$form_state->setValue(['basic', 'embed'], self::EMBED_CODE_VALID);
60+
$plugin->blockValidate(['container' => $form], $form_state);
61+
$this->assertEmpty($form_state->getErrors());
62+
63+
// Validate an invalid embed code.
64+
$form_state->setValue(['basic', 'embed'], self::EMBED_CODE_INVALID);
65+
$plugin->blockValidate(['container' => $form], $form_state);
66+
$this->assertNotEmpty($form_state->getErrors());
67+
}
68+
69+
/**
70+
* Get a block plugin.
71+
*
72+
* @param array $embed
73+
* The embed code for the plugin.
74+
*
75+
* @return \Drupal\ghi_blocks\Plugin\Block\Generic\Datawrapper
76+
* The block plugin.
77+
*/
78+
private function getBlockPlugin($embed = '') {
79+
$configuration = [
80+
'embed' => $embed,
81+
];
82+
return $this->createBlockPlugin('generic_datawrapper', $configuration);
83+
}
84+
85+
}

0 commit comments

Comments
 (0)