department-of-veterans-affairs / va.gov-cms

Editor-centered management for Veteran-centered content.
https://prod.cms.va.gov
GNU General Public License v2.0
96 stars 70 forks source link

Identify CMS Cypress e2e tests for form logic #12220

Closed wesrowe closed 1 year ago

wesrowe commented 1 year ago

Description

We need to write cypress tests for cms forms that use conditional logic. To do so, we need to identify which conditional forms do not have tests and create tickets for each.

Note: Conditional forms are cms forms that use logic to show or hide form components depending on user input.

Acceptance Criteria

CMS Team

Please check the team(s) that will do this work.

chri5tia commented 1 year ago

Breaking this out into specific forms. For this sprint, identifying the forms that need tests would be a good first step.

chri5tia commented 1 year ago

This seemed like a good idea at the time, but I am not finding any other forms that use conditional logic. Moving forward, it would be a good idea for anyone who creates content using conditional logic to implement a cypress test for it or get ahold of me for a walk through.

jilladams commented 1 year ago

From Slack with Steve:

I suggest searching docroot/modules/custom for _form_alter You will find a lot of conditional stuff going on in various places. You'll find some oldish ones using hooks but mostly these should all be in event subscribers now.

chri5tia commented 1 year ago

Quick anatomy of node form conditional logic implementations, in two flavors. Both methods use The Hook Event Dispatcher module

  1. Javascript
    • Custom module declares the script in its libraries.yml file: Such as a script customModuleFormHelpers.js in the js directory:
custom_module_form_states_helpers:
  js:
    js/customModuleFormHelpers.js: {}
  public function addDisplayManagementToEventFields(array &$form) {
    $form['#attached']['library'][] = 'va_gov_custom_module/custom_module_form_states_helpers';
  }

2: PHP

  public function alterNameOfClassFormAlter(FormIdAlterEvent $event): void {
    ...instructions...
  }
- public static function getSubscribedEvents`(): array {`
    return [
      ...snip other stuff...
      FormHookEvents::FORM_ALTER => 'formAlter',
      'hook_event_dispatcher.form_node_name_of_form.alter' => 'alterNameOfClassFormAlter',
chri5tia commented 1 year ago

Knowing this, we can now make a list of which custom modules use the hook event dispatcher to alter a node add or edit form (lists public functions defined in EntityEventSubscriber.php by public function.

If I search the custom modules for any entity event subscriber that does a form alter:

va.gov-cms: grep -rl docroot/modules/custom/ -e 'FORM_ALTER' |grep EntityEventSubscriber              
docroot/modules/custom//va_gov_backend/src/EventSubscriber/EntityEventSubscriber.php
docroot/modules/custom//va_gov_vet_center/src/EventSubscriber/EntityEventSubscriber.php

Hm, let's search the custom modules code for form.alter and in theory, we can get a base list to drill down:

va.gov-cms: grep -rl docroot/modules/custom/ -e 'form.alter' |grep EntityEventSubscriber              
docroot/modules/custom//va_gov_workflow/src/EventSubscriber/EntityEventSubscriber.php
docroot/modules/custom//va_gov_banner/src/EventSubscriber/EntityEventSubscriber.php
docroot/modules/custom//va_gov_events/src/EventSubscriber/EntityEventSubscriber.php
docroot/modules/custom//va_gov_workflow_assignments/src/EventSubscriber/EntityEventSubscriber.php
docroot/modules/custom//va_gov_backend/src/EventSubscriber/EntityEventSubscriber.php
docroot/modules/custom//va_gov_vamc/src/EventSubscriber/EntityEventSubscriber.php
docroot/modules/custom//va_gov_vet_center/src/EventSubscriber/EntityEventSubscriber.php

Turning that into some bites and notes: