Skip to content

Commit 172a63e

Browse files
committed
Add kernel tests for some generic blocks
1 parent 3041ed8 commit 172a63e

24 files changed

+1521
-91
lines changed

.docksal/commands/phpunit

+4-12
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,10 @@ PARALLEL=true
3434
args=""
3535
while [ "$1" != "" ]; do
3636
case $1 in
37-
-c | --clear-logs)
38-
CLEAR_LOGS=true
39-
;;
40-
--open-coverage)
41-
COVERAGE=true
42-
;;
43-
-s | --force-sequential)
44-
PARALLEL=false
45-
;;
46-
*)
47-
args="$args $1"
48-
;;
37+
--open-coverage ) COVERAGE=true
38+
;;
39+
*) args="$args $1"
40+
;;
4941
esac
5042
shift
5143
done

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

+5-10
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,6 @@ abstract class GHIBlockBase extends HPCBlockBase {
109109
*/
110110
protected $sectionManager;
111111

112-
/**
113-
* The section manager.
114-
*
115-
* @var \Drupal\ghi_subpages\SubpageManager
116-
*/
117-
protected $subpageManager;
118-
119112
/**
120113
* The selection criteria argument service.
121114
*
@@ -177,7 +170,6 @@ public static function create(ContainerInterface $container, array $configuratio
177170
$instance->layoutTempstoreRepository = $container->get('layout_builder.tempstore_repository');
178171
$instance->configurationContainerItemManager = $container->get('plugin.manager.configuration_container_item_manager');
179172
$instance->sectionManager = $container->get('ghi_sections.manager');
180-
$instance->subpageManager = $container->get('ghi_subpages.manager');
181173
$instance->selectionCriteriaArgument = $container->get('ghi_blocks.layout_builder_edit_page.selection_criteria_argument');
182174
$instance->moduleHandler = $container->get('module_handler');
183175
$instance->controllerResolver = $container->get('controller_resolver');
@@ -577,7 +569,7 @@ public function needsContextConfiguration() {
577569
* TRUE if the blocks base object can be configured, FASLE otherwise.
578570
*/
579571
public function canConfigureContexts() {
580-
$instance = $this->formState->get('block') ?? $this;
572+
$instance = $this->formState?->get('block') ?? $this;
581573
$base_objects_per_bundle = [];
582574
$can_configure = FALSE;
583575

@@ -1147,6 +1139,9 @@ protected function addButtonsToCleanValueKeys(array $form, FormStateInterface $f
11471139
public function isPreviewSubmit(FormStateInterface $form_state) {
11481140
$current_subform = $form_state->get('current_subform');
11491141
$triggering_element = $form_state->getTriggeringElement();
1142+
if (!$triggering_element) {
1143+
return FALSE;
1144+
}
11501145
$action = end($triggering_element['#parents']);
11511146
$values = $form_state->getValues();
11521147
return $action == 'preview' && !array_key_exists($current_subform, $values);
@@ -1719,7 +1714,7 @@ public function getCurrentPlanObject() {
17191714
*/
17201715
public function getContextValue($name) {
17211716
$contexts = $this->getContexts();
1722-
return $contexts[$name] ? $contexts[$name]->getContextValue() : NULL;
1717+
return array_key_exists($name, $contexts) ? $contexts[$name]->getContextValue() : NULL;
17231718
}
17241719

17251720
/**

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/Block/Plan/PlanGoverningEntitiesCaseloadsTable.php

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Drupal\ghi_form_elements\Traits\ConfigurationContainerTrait;
1818
use Drupal\hpc_downloads\Interfaces\HPCDownloadExcelInterface;
1919
use Drupal\hpc_downloads\Interfaces\HPCDownloadPNGInterface;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
2021

2122
/**
2223
* Provides a 'PlanGoverningEntitiesCaseloadsTable' block.
@@ -54,6 +55,23 @@ class PlanGoverningEntitiesCaseloadsTable extends GHIBlockBase implements Config
5455
use AttachmentTableTrait;
5556
use ConfigValidationTrait;
5657

58+
/**
59+
* The section manager.
60+
*
61+
* @var \Drupal\ghi_subpages\SubpageManager
62+
*/
63+
protected $subpageManager;
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
69+
/** @var static $instance */
70+
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
71+
$instance->subpageManager = $container->get('ghi_subpages.manager');
72+
return $instance;
73+
}
74+
5775
/**
5876
* {@inheritdoc}
5977
*/

html/modules/custom/ghi_blocks/src/Plugin/Block/Plan/PlanGoverningEntitiesTable.php

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Drupal\ghi_plans\Helpers\PlanStructureHelper;
1616
use Drupal\hpc_downloads\Interfaces\HPCDownloadExcelInterface;
1717
use Drupal\hpc_downloads\Interfaces\HPCDownloadPNGInterface;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
1819

1920
/**
2021
* Provides a 'PlanGoverningEntitiesTable' block.
@@ -55,6 +56,23 @@ class PlanGoverningEntitiesTable extends GHIBlockBase implements ConfigurableTab
5556
use ConfigurationItemClusterRestrictTrait;
5657
use TableSoftLimitTrait;
5758

59+
/**
60+
* The section manager.
61+
*
62+
* @var \Drupal\ghi_subpages\SubpageManager
63+
*/
64+
protected $subpageManager;
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
70+
/** @var static $instance */
71+
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
72+
$instance->subpageManager = $container->get('ghi_subpages.manager');
73+
return $instance;
74+
}
75+
5876
/**
5977
* {@inheritdoc}
6078
*/

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,96 @@
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+
'layout_builder',
22+
'layout_discovery',
23+
'migrate',
24+
'hpc_api',
25+
'ghi_form_elements',
26+
// 'ghi_subpages',
27+
'ghi_sections',
28+
'ghi_blocks',
29+
'ghi_base_objects',
30+
];
31+
32+
/**
33+
* Get a section component.
34+
*
35+
* @param string $plugin_id
36+
* The plugin id.
37+
* @param array $configuration
38+
* The hpc-specific configuration.
39+
* @param string $label
40+
* The label.
41+
* @param bool $label_display
42+
* Whether the label should be displayed or not.
43+
*
44+
* @return \Drupal\layout_builder\SectionComponent
45+
* The block plugin.
46+
*/
47+
protected function createSectionComponent($plugin_id, $configuration, $label = '<none>', $label_display = FALSE) {
48+
$configuration = [
49+
'id' => $plugin_id,
50+
'label' => $label,
51+
'label_display' => $label_display,
52+
'provider' => 'ghi_blocks',
53+
'hpc' => $configuration,
54+
];
55+
return new SectionComponent('10000000-0000-1000-a000-000000000000', 'content', $configuration);
56+
}
57+
58+
/**
59+
* Get a block plugin.
60+
*
61+
* @param string $plugin_id
62+
* The plugin id.
63+
* @param array $configuration
64+
* The hpc-specific configuration.
65+
* @param string $label
66+
* The label.
67+
* @param bool $label_display
68+
* Whether the label should be displayed or not.
69+
*
70+
* @return \Drupal\hpc_common\Plugin\HPCPluginInterface
71+
* The block plugin.
72+
*/
73+
protected function createBlockPlugin($plugin_id, $configuration, $label = '<none>', $label_display = FALSE) {
74+
return $this->createSectionComponent($plugin_id, $configuration, $label, $label_display)?->getPlugin();
75+
}
76+
77+
/**
78+
* Call a private or protected method on the given class.
79+
*
80+
* @param object $class
81+
* The object.
82+
* @param string $method_name
83+
* The method name.
84+
* @param array $arguments
85+
* Optional arguments for the method.
86+
*
87+
* @return mixed
88+
* The return of the method call.
89+
*/
90+
protected function callPrivateMethod($class, $method_name, $arguments = NULL) {
91+
// Make the private method callable.
92+
$method = (new \ReflectionClass($class::class))->getMethod($method_name);
93+
return $arguments ? $method->invokeArgs($class, $arguments) : $method->invoke($class);
94+
}
95+
96+
}

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

+28-45
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
<?php
22

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

5-
use Drupal\KernelTests\KernelTestBase;
65
use Drupal\Tests\user\Traits\UserCreationTrait;
76
use Drupal\file\Entity\File;
87
use Drupal\ghi_image\CropManager;
98
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
109
use Drupal\layout_builder\Section;
11-
use Drupal\layout_builder\SectionComponent;
1210
use Drupal\node\Entity\Node;
1311
use Drupal\node\Entity\NodeType;
1412

1513
/**
16-
* Tests aspects of the block logic.
14+
* Tests blocks that can have with managed files uploaded.
1715
*
1816
* @group ghi_blocks
1917
*/
20-
class BlocksWithManagedFilesTest extends KernelTestBase {
18+
class BlocksWithManagedFilesTest extends BlockKernelTestBase {
2119

2220
use UserCreationTrait;
2321

@@ -32,22 +30,13 @@ class BlocksWithManagedFilesTest extends KernelTestBase {
3230
'node',
3331
'taxonomy',
3432
'field',
35-
'layout_builder',
36-
'layout_discovery',
37-
'layout_builder',
3833
'text',
3934
'filter',
4035
'file',
4136
'token',
4237
'path',
4338
'path_alias',
4439
'pathauto',
45-
'hpc_api',
46-
// 'hpc_common',
47-
'ghi_form_elements',
48-
'ghi_subpages',
49-
'ghi_sections',
50-
'ghi_blocks',
5140
];
5241

5342
const BUNDLE = 'page';
@@ -198,42 +187,36 @@ public function testFilesAreNotDeletedByRevisionUpdates() {
198187
*/
199188
private function addLinkBlockWithFileToNode($node, $file) {
200189
$configuration = [
201-
'id' => 'links',
202-
'label' => '<none>',
203-
'label_display' => FALSE,
204-
'provider' => 'ghi_blocks',
205-
'hpc' => [
190+
'links' => [
206191
'links' => [
207-
'links' => [
208-
[
209-
'id' => 1,
210-
'item_type' => 'link',
211-
'config' => [
212-
'label' => 'Test link with image',
192+
[
193+
'id' => 1,
194+
'item_type' => 'link',
195+
'config' => [
196+
'label' => 'Test link with image',
197+
'link' => [
213198
'link' => [
214-
'link' => [
215-
'label' => NULL,
216-
'link_type' => 'custom',
217-
'link_custom' => [
218-
'url' => 'https://google.com',
219-
],
220-
'link_related' => [
221-
'target' => NULL,
222-
],
199+
'label' => NULL,
200+
'link_type' => 'custom',
201+
'link_custom' => [
202+
'url' => 'https://google.com',
223203
],
224-
],
225-
'image' => [
226-
'image' => [$file->id()],
227-
],
228-
'content' => [
229-
'date' => '2024-05-22',
230-
'description' => [
231-
'value' => '',
232-
'format' => 'wysiwyg_simple',
204+
'link_related' => [
205+
'target' => NULL,
233206
],
234-
'description_toggle' => 0,
235207
],
236208
],
209+
'image' => [
210+
'image' => [$file->id()],
211+
],
212+
'content' => [
213+
'date' => '2024-05-22',
214+
'description' => [
215+
'value' => '',
216+
'format' => 'wysiwyg_simple',
217+
],
218+
'description_toggle' => 0,
219+
],
237220
],
238221
],
239222
],
@@ -245,7 +228,7 @@ private function addLinkBlockWithFileToNode($node, $file) {
245228
0 => ['section' => new Section('layout_onecol', [], [])],
246229
];
247230
// Add a new component to the section with delta 0.
248-
$component = new SectionComponent('10000000-0000-1000-a000-000000000000', 'content', $configuration);
231+
$component = $this->createSectionComponent('links', $configuration);
249232
$sections[0]['section']->appendComponent($component);
250233

251234
// Store the modified sections in the node.

0 commit comments

Comments
 (0)