Skip to content

Commit 5ecba67

Browse files
joshua-salsadigitalgargsuchiJoshua Fernandes
authored
Feature/improve tests (#57)
* Added unit tests. * Implemented Test Cases & fixed lint & deprecated issues. * Skip this rule as the redirect is not a node on empty. * Removed swp file. --------- Co-authored-by: Suchi <gargsuchi@gmail.com> Co-authored-by: Joshua Fernandes <“joshua.fernandes@salsa.digital”>
1 parent 7ba0a74 commit 5ecba67

21 files changed

+1743
-73
lines changed

src/OpenFisca/Payload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function findKey(string $key, array $parents = []): ?array {
202202
* The key path e.g. 'persons.PersonA.salary', or NULL if not found.
203203
*/
204204
public function findKeyPath(string $key, array $parents = []): ?string {
205-
$key_path = $this->findKey($key);
205+
$key_path = $this->findKey($key, $parents);
206206
return is_array($key_path) ? implode('.', $key_path) : NULL;
207207
}
208208

src/Plugin/WebformHandler/OpenFiscaJourneyHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class OpenFiscaJourneyHandler extends WebformHandlerBase {
3434
/**
3535
* Current request.
3636
*
37-
* @var Symfony\Component\HttpFoundation\Request
37+
* @var \Symfony\Component\HttpFoundation\Request
3838
*/
3939
protected Request $request;
4040

@@ -48,14 +48,14 @@ class OpenFiscaJourneyHandler extends WebformHandlerBase {
4848
/**
4949
* RAC content helper.
5050
*
51-
* @var Drupal\webform_openfisca\RacContentHelperInterface
51+
* @var \Drupal\webform_openfisca\RacContentHelperInterface
5252
*/
5353
protected RacContentHelperInterface $racContentHelper;
5454

5555
/**
5656
* The debug data from the last API call to OpenFisca.
5757
*
58-
* @var arraystring\Drupal\webform_openfisca\OpenFisca\Payload|null
58+
* @var array<string, \Drupal\webform_openfisca\OpenFisca\Payload\RequestPayload|\Drupal\webform_openfisca\OpenFisca\Payload\ResponsePayload|null>
5959
*/
6060
protected array $recentDebugData = [];
6161

@@ -180,7 +180,9 @@ protected function prepareOpenfiscaPayload(WebformSubmissionInterface $webform_s
180180
foreach ($result_keys as $result_key) {
181181
if (in_array($result_key, $paths)) {
182182
// This is one of the inputs. Do not NULL it.
183+
// @codeCoverageIgnoreStart
183184
continue;
185+
// @codeCoverageIgnoreEnd
184186
}
185187
// The result_key will be in the format
186188
// variable_entity.entity_key.variable_name

src/RacContentHelper.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,17 @@ protected function findRacBlockContentForWebform(string $webform_id): array {
124124
if ($paragraphs) {
125125
$paragraph_entities = $this->entityTypeManager->getStorage('paragraph')->loadMultiple($paragraphs);
126126
foreach ($paragraph_entities as $paragraph) {
127-
$blocks[] = $paragraph->getParentEntity()->id();
127+
if (!$paragraph instanceof ParagraphInterface) {
128+
continue;
129+
}
130+
$parent = $paragraph->getParentEntity();
131+
// Parent can be NULL when paragraphs are loaded via loadMultiple() in
132+
// some environments; skip so we do not trigger invalid ID.
133+
// @codeCoverageIgnoreStart - branch depends on Paragraphs runtime.
134+
if ($parent !== NULL) {
135+
$blocks[] = $parent->id();
136+
}
137+
// @codeCoverageIgnoreEnd
128138
}
129139
}
130140

@@ -190,22 +200,26 @@ protected function findRulesForBlock(string|int $block_id): ?array {
190200
/** @var \Drupal\block_content\BlockContentInterface|null $block */
191201
$block = $block_content_storage->load($block_id);
192202

203+
if (!$block instanceof BlockContentInterface) {
204+
return NULL;
205+
}
206+
193207
// Find the block field name with paragraph type block_rac_elements.
194208
$field_name = $this->findBlockFieldName($block);
195209

196-
if (!empty($field_name)) {
197-
if (!$block instanceof BlockContentInterface
198-
|| !$block->hasField($field_name)
199-
|| !($block->get($field_name) instanceof EntityReferenceFieldItemListInterface)
200-
|| $block->get($field_name)->isEmpty()
210+
if ($field_name !== NULL && $field_name !== '') {
211+
$field_name_str = (string) $field_name;
212+
if (!$block->hasField($field_name_str)
213+
|| !($block->get($field_name_str) instanceof EntityReferenceFieldItemListInterface)
214+
|| $block->get($field_name_str)->isEmpty()
201215
) {
202216
return NULL;
203217
}
204218

205-
$paragraph = $block->get($field_name)->entity;
219+
$paragraph = $block->get($field_name_str)->entity;
206220

207221
$rac_element_paragraphs = [];
208-
if ($paragraph && $paragraph->hasField('field_block_rules')) {
222+
if ($paragraph instanceof ParagraphInterface && $paragraph->hasField('field_block_rules')) {
209223
$rac_element_paragraphs = $paragraph->get('field_block_rules');
210224
$operator = $paragraph->get('field_operator')->getValue();
211225
}
@@ -223,7 +237,7 @@ protected function findRulesForBlock(string|int $block_id): ?array {
223237
continue;
224238
}
225239
// Get the field that contains multiple paragraph references.
226-
$block_rac_elements_field = $paragraph_entity->get($field_name);
240+
$block_rac_elements_field = $paragraph_entity->get($field_name_str);
227241
$rule_operator = $paragraph_entity->get('field_rules_operator')->value;
228242

229243
if (!$block_rac_elements_field instanceof EntityReferenceFieldItemListInterface || $block_rac_elements_field->isEmpty()) {
@@ -261,6 +275,8 @@ protected function findRulesForBlock(string|int $block_id): ?array {
261275
$visibility_rule['parent_operator'] = $operator ?? 'AND';
262276
return $visibility_rule;
263277
}
278+
279+
return NULL;
264280
}
265281
// @codeCoverageIgnoreStart
266282
catch (InvalidPluginDefinitionException | PluginNotFoundException) {
@@ -450,8 +466,8 @@ public function findVisibleBlocksForWebform(string $webform_id, array $matching_
450466
foreach ($block_ids as $block_id) {
451467
// Get the rules for this block.
452468
$rules = $this->findRulesForBlock($block_id);
453-
if (!is_array($rules) || empty($rules)) {
454-
// No rules mean the block is always visible.
469+
// No rules (or empty rules array) mean the block is always visible.
470+
if (!is_array($rules) || empty($rules['rules'] ?? [])) {
455471
$visible_blocks[] = $block_id;
456472
continue;
457473
}

tests/modules/webform_openfisca_test/config/install/webform.webform.test_dac.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,6 @@ third_party_settings:
182182
}
183183
}
184184
fisca_entity_roles: '[]'
185-
fisca_immediate_response_mapping: |-
186-
{
187-
"aus_citizen_or_permanent_resident": true,
188-
"has_disability": true
189-
}
190185
fisca_return_key: 'persons.personA.disability_allowance_eligible,persons.personA.disability_allowance_benefit,persons.personA.monthly_income_exceeds_limit'
191186
weight: 0
192187
open: null

tests/modules/webform_openfisca_test/config/install/webform.webform.test_invalid_api.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@ third_party_settings:
158158
fisca_parameter_tokens: 'disability_allowance_max_income,income_tax_rate'
159159
fisca_parameters: '[]'
160160
fisca_entity_roles: '[]'
161-
fisca_immediate_response_mapping: |-
162-
{
163-
"aus_citizen_or_permanent_resident": true,
164-
"has_disability": true
165-
}
166161
fisca_return_key: 'persons.personA.disability_allowance_eligible,persons.personA.disability_allowance_benefit,persons.personA.monthly_income_exceeds_limit'
167162
weight: 0
168163
open: null

tests/modules/webform_openfisca_test/config/install/webform.webform.test_invalid_api_nolog.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ third_party_settings:
170170
"is_array": false
171171
}
172172
}
173-
fisca_immediate_response_mapping: |-
174-
{
175-
"aus_citizen_or_permanent_resident": true,
176-
"has_disability": true
177-
}
178173
fisca_return_key: 'persons.personA.disability_allowance_eligible,persons.personA.disability_allowance_benefit,persons.personA.monthly_income_exceeds_limit'
179174
weight: 0
180175
open: null

tests/modules/webform_openfisca_test/config/install/webform.webform.test_invalid_api_period.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ third_party_settings:
170170
"is_array": false
171171
}
172172
}
173-
fisca_immediate_response_mapping: |-
174-
{
175-
"aus_citizen_or_permanent_resident": true,
176-
"has_disability": true
177-
}
178173
fisca_return_key: 'persons.personA.disability_allowance_eligible,persons.personA.disability_allowance_benefit,persons.personA.monthly_income_exceeds_limit'
179174
weight: 0
180175
open: null

tests/modules/webform_openfisca_test/config/install/webform.webform.test_no_api.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,6 @@ third_party_settings:
182182
}
183183
}
184184
fisca_entity_roles: '[]'
185-
fisca_immediate_response_mapping: |-
186-
{
187-
"aus_citizen_or_permanent_resident": true,
188-
"has_disability": true
189-
}
190185
fisca_return_key: 'persons.personA.disability_allowance_eligible,persons.personA.disability_allowance_benefit,persons.personA.monthly_income_exceeds_limit'
191186
weight: 0
192187
open: null

tests/src/Functional/WebformOpenFiscaFunctionalTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@ class WebformOpenFiscaFunctionalTest extends WebformBrowserTestBase {
1414

1515
/**
1616
* {@inheritdoc}
17+
*
18+
* List dependencies (options, entity_reference_revisions) before
19+
* webform_openfisca so config that requires them is installed after they are
20+
* enabled, avoiding UnmetDependenciesException.
1721
*/
1822
protected static $modules = [
19-
'webform_openfisca',
20-
'paragraphs',
21-
'webform',
22-
'token',
2323
'node',
2424
'path',
25-
'menu_ui',
2625
'text',
2726
'field_ui',
27+
'menu_ui',
28+
'options',
29+
'entity_reference_revisions',
30+
'paragraphs',
31+
'token',
32+
'webform',
33+
'webform_openfisca',
2834
];
2935

3036
/**

0 commit comments

Comments
 (0)