Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Adds a kernel test
Browse files Browse the repository at this point in the history
1. Adds a kernel test
2. Remove outdated verification steps from tide_webform.module
  • Loading branch information
vincent-gao committed Apr 12, 2023
1 parent 1331175 commit c20eb1b
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 100 deletions.
108 changes: 16 additions & 92 deletions modules/tide_webform_jsonapi/src/Resource/AddWebform.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Drupal\jsonapi\JsonApiResource\ResourceObjectData;
use Drupal\jsonapi\ResourceResponse;
use Drupal\jsonapi_resources\Resource\EntityQueryResourceBase;
use Drupal\tide_webform_jsonapi\TideWebformJsonapiHelper;
use Drupal\webform\Entity\Webform;
use Drupal\webform\WebformSubmissionForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -27,6 +28,13 @@
*/
final class AddWebform extends EntityQueryResourceBase implements ContainerInjectionInterface {

/**
* The ResourceTypeRepository.
*
* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepository
*/
protected $tideWebformJsonapiHelper;

/**
* The ResourceTypeRepository.
*
Expand All @@ -44,7 +52,8 @@ final class AddWebform extends EntityQueryResourceBase implements ContainerInjec
/**
* {@inheritdoc}
*/
public function __construct(ResourceTypeRepository $resource_type_repository, EntityResource $resource) {
public function __construct(TideWebformJsonapiHelper $tide_webform_jsonapi_helper, ResourceTypeRepository $resource_type_repository, EntityResource $resource) {
$this->tideWebformJsonapiHelper = $tide_webform_jsonapi_helper;
$this->resourceTypeRepository = $resource_type_repository;
$this->resource = $resource;
}
Expand All @@ -54,6 +63,7 @@ public function __construct(ResourceTypeRepository $resource_type_repository, En
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('tide_webform_jsonapi.helper'),
$container->get('jsonapi.resource_type.repository'),
$container->get('jsonapi.entity_resource')
);
Expand All @@ -78,7 +88,7 @@ public static function create(ContainerInterface $container) {
* Thrown if the entity could not be saved.
*/
public function process(Request $request, Webform $webform): ResourceResponse {
// Purely business logic part to get the webform_submission entity.
// Purely business logic to get the webform_submission entity.
$resource_type = $this->resourceTypeRepository->get(
'webform_submission',
$webform->id()
Expand All @@ -100,13 +110,13 @@ public function process(Request $request, Webform $webform): ResourceResponse {
$entity->save();
// Massage, organise and verify the data.
$original_elements = $webform->getElementsDecodedAndFlattened();
$supported_validations = $this->getSupportedValidateElements();
$results = $this->webformValidateSettingsExtractor($supported_validations, $original_elements);
$supported_validations = $this->tideWebformJsonapiHelper->getSupportedValidateElements();
$results = $this->tideWebformJsonapiHelper->webformValidateSettingsExtractor($supported_validations, $original_elements);
$new_array = [];
foreach ($results as $key => $r) {
$new_array[$key] = $this->attachValidateSettingsToPayload($r);
$new_array[$key] = $this->tideWebformJsonapiHelper->attachValidateSettingsToPayload($r);
}
$errors = $this->validatePayload($entity->getData(), $new_array);
$errors = $this->tideWebformJsonapiHelper->validatePayload($entity->getData(), $new_array);
// Let webform core checks words and characters.
$internal_errors = WebformSubmissionForm::validateWebformSubmission($entity);
// Prepare error messages.
Expand Down Expand Up @@ -140,90 +150,4 @@ public function getRouteResourceTypes(Route $route, string $route_name): array {
return $this->getResourceTypesByEntityTypeId('webform_submission');
}

/**
* Extracts validate settings from the webform settings.
*/
public function webformValidateSettingsExtractor(array $supported_validation, array $original_elements): array {
$res = [];
// Iterate through webform values.
foreach ($original_elements as $w_key => $items) {
// Iterate through supported validation keys and their values.
foreach ($supported_validation as $key => $value) {
// If the current value is an array, check for a match with the key.
if (is_array($value)) {
if (array_key_exists($key, $items)) {
$res[$w_key][$key] = $items[$key];
}
}
else {
// If the value exists in the items and is not an array,
// store it in the result.
if (array_key_exists($value, $items)) {
$res[$w_key][$value] = $items[$value];
}
}
}
}
return $res;
}

/**
* Supported validators.
*/
private function getSupportedValidateElements() {
return [
'#required',
'#required_error',
'#pattern',
'#pattern_error',
];
}

/**
* Attached supported validator to Payload.
*/
private function attachValidateSettingsToPayload(array $payload) {
$output = [];
$mapping = [
'#required' => 'required',
'#required_error' => 'required',
'#pattern' => 'pattern',
'#pattern_error' => 'pattern',
];
foreach ($payload as $key => $value) {
if (isset($mapping[$key])) {
$output[$mapping[$key]][] = $value;
}
}
return $output;
}

/**
* Verifies the payload.
*/
private function validatePayload(array $payload, array $massaged_validates_array) {
$results = [];
foreach ($payload as $id => $value) {
if (array_key_exists($id, $massaged_validates_array)) {
if (!empty($this->generateErrorString($value, $massaged_validates_array[$id]))) {
$results[$id] = $this->generateErrorString($value, $massaged_validates_array[$id]);
}
}
}
return $results;
}

/**
* Generates error messages.
*/
private function generateErrorString($value, array $arr) {
$res = [];
foreach ($arr as $k => $v) {
if (call_user_func('tide_webform_jsonapi_' . $k . '_validate', $value, $v) !== TRUE) {
$res[] = call_user_func('tide_webform_jsonapi_' . $k . '_validate', $value, $v);
}
}
return $res;
}

}
96 changes: 96 additions & 0 deletions modules/tide_webform_jsonapi/src/TideWebformJsonapiHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Drupal\tide_webform_jsonapi;

/**
* Provides helper functions for tide_webform_jsonapi.
*/
class TideWebformJsonapiHelper {

/**
* Extracts validate settings from the webform settings.
*/
public function webformValidateSettingsExtractor(array $supported_validation, array $original_elements): array {
$res = [];
// Iterate through webform values.
foreach ($original_elements as $w_key => $items) {
// Iterate through supported validation keys and their values.
foreach ($supported_validation as $key => $value) {
// If the current value is an array, check for a match with the key.
if (is_array($value)) {
if (array_key_exists($key, $items)) {
$res[$w_key][$key] = $items[$key];
}
}
else {
// If the value exists in the items and is not an array,
// store it in the result.
if (array_key_exists($value, $items)) {
$res[$w_key][$value] = $items[$value];
}
}
}
}
return $res;
}

/**
* Supported validators.
*/
public function getSupportedValidateElements() {
return [
'#required',
'#required_error',
'#pattern',
'#pattern_error',
];
}

/**
* Attached supported validator to Payload.
*/
public function attachValidateSettingsToPayload(array $payload) {
$output = [];
$mapping = [
'#required' => 'required',
'#required_error' => 'required',
'#pattern' => 'pattern',
'#pattern_error' => 'pattern',
];
foreach ($payload as $key => $value) {
if (isset($mapping[$key])) {
$output[$mapping[$key]][] = $value;
}
}
return $output;
}

/**
* Verifies the payload.
*/
public function validatePayload(array $payload, array $massaged_validates_array) {
$results = [];
foreach ($payload as $id => $value) {
if (array_key_exists($id, $massaged_validates_array)) {
if (!empty($this->generateErrorString($value, $massaged_validates_array[$id]))) {
$results[$id] = $this->generateErrorString($value, $massaged_validates_array[$id]);
}
}
}
return $results;
}

/**
* Generates error messages.
*/
public function generateErrorString($value, array $arr) {
$res = [];
foreach ($arr as $k => $v) {
if (call_user_func('tide_webform_' . $k . '_validate', $value, $v) !== TRUE) {
$res[] = call_user_func('tide_webform_' . $k . '_validate', $value, $v);
}
}
return $res;
}

}
Loading

0 comments on commit c20eb1b

Please sign in to comment.