Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@laceysanderson
Copy link
Member

@laceysanderson laceysanderson commented Feb 24, 2025

Development Task

Note: this was originally a bug fix but the bug is not easily reproducible anymore. Instead this has become focused on automated testing for fields to ensure that they are stable.*

Issue #2119

Description

Specifically, this PR will work to create tests around the PROPERTY FIELD specifically as an example for field testing in general:

  • Add more automated testing for the property field focusing on making sure that Chado and the Drupal field tables stay in sync. See "CRUD" tests for this.
    • Note: it doesn't check the underlying chado values.
  • Add automated testing for the ChadoPropertyWidgetDefault widget (textarea) to ensure that the values are being properly massaged.
    • Expand the scenarios to check the form element defaults in the edit scenario
    • Check the right number of form elements are available for property fields with multiple properties.
    • Note: The tests don't check scenarios that we expect to have errors for

The following scenarios are focused on:

  • Ensure that removing properties happens smoothly and Drupal/Chado remain in sync.
  • Ensure that the Drupal delta and Chado rank values are always in sync.
  • Ensure there is no swapping between property fields
  • Ensure you can add properties on create and/or edit

The following helper methods have been added to help test other fields:

  • tripal/tests/src/Traits/TripalEntityFieldTestTrait:
    • assertFieldValuesMatch()
    • setupTripalEntityAddForm()
    • setupTripalEntityEditForm()
    • populateTripalEntityFormState()
  • tripal_chado/tests/src/Traits/ChadoFieldTestTrait:
    • assertFieldWidgetsMatch()
    • assertArrayContainsElements()
    • setupChadoEntityFieldTestEnvironment()
    • setupChadoEntityFieldSystemUnderTest()

I will work on documenting this process in the TripalDocs repo in PR tripal/tripal_doc#105

Testing?

Manual Testing

This PR does touch the property fields themselves so it would be good to manually test them through the interface.

Automated Testing

ChadoPropertyTypeCRUDTest

This test replaces the old tripal_chado/tests/src/Kernel/Plugin/ChadoField/FieldType/ChadoPropertyTypeSimpleCRUDTest.php. It is more efficient and comprehensive plus actually tests the contents of the Drupal field tables.

It tests the following scenarios which are described in tripal_chado/tests/src/Kernel/Plugin/ChadoField/FieldType/ChadoPropertyType-TestInfo.yml

  • Simple project with no properties added on create or edit. There are two property fields attached so this checks that we are not forced to use the property fields ;-p
  • Project with no properties added on create but then 2 properties added for each of fields project_prop1 and project_prop2 on edit. This tests multiple properties per field, multiple property fields per content type and creation of properties on edit.
  • project with 2 properties for each of project_prop1 and project_prop2 on create. On edit the properties for project_prop1 are reordered but those for project_prop2 remain the same. This tests that properties can be added on create, that properties can be reordered without being changed and that reordering on one field does not affect the other field.

For each scenario

  1. The entity is created with the initial user input and we check this occurs without error.
  2. The entity we just created is loaded and we confirm the field values match our expectations. This checks both the values of the field in the entity and in the Drupal field table. It does not actually check chado.
  3. Make changes based on user input and check that we can save without error.
  4. Load the entity again and check that the changes are reflected both in the entity and in the drupal field tables.

ChadoPropertyWidgetFormTest

This widget test is brand new, never before done in Tripal ;-p

It tests the following scenarios which are described in tripal_chado/tests/src/Kernel/Plugin/ChadoField/FieldType/ChadoPropertyWidgetForm-TestInfo.yml

  • Simple project with no properties added on create or edit. There are two property fields attached so this checks that we are not forced to use the property fields ;-p
  • Project with no properties added on create but then 2 properties added for each of fields project_prop1 and project_prop2 on edit. This tests multiple properties per field, multiple property fields per content type and creation of properties on edit.
  • project with 2 properties for each of project_prop1 and project_prop2 on create. On edit the properties for project_prop1 are reordered but those for project_prop2 remain the same. This tests that properties can be added on create, that properties can be reordered without being changed and that reordering on one field does not affect the other field.

For each scenario

  • CREATE:
    • We can generate the entire TripalEntity create form without error using the field defaults. We also check the field widget form elements match our expectations here.
    • Manipulate the form state based on user input and check that form validation executes as expected (currently expects no errors).
    • If there are no errors from validate then also check that we can submit the form and save it. Then check the resulting field values match our expectations in the same way as for the CRUD test.
  • EDIT:
    • We can generate the TripalEntity edit form for the specific entity we just created without error. We check the field widget form elements match our expectations including checking the default values.
    • Manipulate the form state based on user input and check that form validation executes as expected (currently expects no errors).
    • If there are no errors from validate then also check that we can submit the form and save it. Then check the resulting field values match our expectations in the same way as for the CRUD test.

Code review

NOTE: This PR also fixes coding standards in the files I touched and adds coding standards checking to the docker devcontainer.

Specific updates to non-test files are:

  • tripal_chado/src/Plugin/DataType/ChadoDataType.php
    • getIterator() now needs to be \Traversable (deprecation fix)
    • onchange() method parameters have changed in Drupal core; update to match.
  • tripal_chado/src/Plugin/Field/FieldWidget/ChadoPropertyWidgetDefault.php:
    • formElement() support when term_id is already set in the items
    • formElement() consistently use #value for value type elements
    • massageFormValues() don't try to remove value format from empty properties.
    • settingsForm() use DI translation $this->t() rather than procedural t().
  • tripal_chado/src/TripalField/ChadoWidgetBase.php
    • Don't use t() in Exceptions as its not recommended.
    • massagePropertyFormValues() notify the developer if the field_name key is not present in the form values as it is now required.
    • massagePropertyFormValues() Only check each key of $value if it actually exists.

@laceysanderson laceysanderson added bug - confirmed For issues where a core developer has confirmed a bug exists. Group 1 - Tripal Content Types | Terms | Fields Any issue relating to Tripal Content including types, terms, and fields. Group 2 - Data Storage | Tripal DBX | Chado Any issue relating to biological data storage, Tripal DBX and Chado integration, Materialized Views Group 9 - Automated Testing Any issue related to automated testing including needed test cases, concerns, etc. Priority - High Any issue/PR which has a noticable impact on usability of the system + would often be encountered. labels Feb 24, 2025
@laceysanderson laceysanderson self-assigned this Feb 24, 2025
@laceysanderson laceysanderson linked an issue Feb 24, 2025 that may be closed by this pull request
@dsenalik
Copy link
Contributor

dsenalik commented Mar 2, 2025

I want to bring up a performance issue (as well as a nightmare for the relationship field #2128 )
When saving an entity through the UI, it gets saved twice.

$status = parent::save($form, $form_state);
// Entity ID is only available post-save, so we have waited
// to set the title and URL path alias until after saving.
// Unfortunately we have to re-load the entity as it is not
// fully updated post-save.
$msg = '';
try {
$entities = \Drupal::entityTypeManager()->getStorage('tripal_entity')->loadByProperties(['id' => $this->entity->id()]);
$this->entity = $entities[$this->entity->id()];
$this->entity->setTokenValues();
$this->entity->setTitle();
// We need to save here to save the title, but the alias will be saved
// with setAlias(). We save now, because if we save after setAlias(),
// the saved alias reverts to the form value!
$this->entity->save();

I originally inserted this second save, because we need to save the entity in case the title or URL format includes the entity ID value in it, and for a new entity it is not known until the entity is actually saved.
The perfomance issue is that the second save takes approximately 4 seconds for me right now, all just to update the entity title, which is a single field in the tripal_entity table.
The nightmare part is that the "deleting properties/linking records" part actually happens twice as a result.

One option is that I hope that we can justify writing to the DB table directly, as this is 1) a performance killer and 2) a problem for the relationship field as it will have two store_link properties, which can get swapped on an edit.

@laceysanderson laceysanderson changed the title Fix property fields and expand automated tests Automated tests for property fields, widgets and formatters May 29, 2025
@laceysanderson
Copy link
Member Author

laceysanderson commented May 30, 2025

NOTE: I just updated the PR description to better represent what this PR is actually now :-) Additionally, I actually have the widget testing working!!! 🎉

#2133 (comment)
This was already resolved in a separate PR.

Additionally, there have been other PRs fixing bugs in fields including some of those originally described in the issue.

@laceysanderson
Copy link
Member Author

We are now at 100% for all but the settings of the ChadoPropertyWidgetDefault!
Screenshot 2025-06-03 at 10 46 32 PM

Additionally, we have 74.19% coverage of ChadoWidgetBase::massagePropertyFormValues() and 100% coverage of ChadoWidgetBase::saveInitialValues()

Also, this is where we are for the field type:
Screenshot 2025-06-03 at 10 51 08 PM

@laceysanderson
Copy link
Member Author

Good catch 🙈 Thanks for the review!!!

Copy link
Contributor

@dsenalik dsenalik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does touch the property fields themselves so it would be good to manually test them through the interface.

So I did import 100 publications (by chance there were 100) because those have lots of properties, then edited one adding more properties and saving, worked as expected.

Two small comments from code review

Great job @laceysanderson 😍 - and the php sniffer addition is much appreciated

@laceysanderson laceysanderson merged commit 9136b32 into 4.x Jun 16, 2025
17 checks passed
@dsenalik dsenalik deleted the tv4g1-2119-propertyFields branch June 21, 2025 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug - confirmed For issues where a core developer has confirmed a bug exists. Group 1 - Tripal Content Types | Terms | Fields Any issue relating to Tripal Content including types, terms, and fields. Group 2 - Data Storage | Tripal DBX | Chado Any issue relating to biological data storage, Tripal DBX and Chado integration, Materialized Views Group 9 - Automated Testing Any issue related to automated testing including needed test cases, concerns, etc. Priority - High Any issue/PR which has a noticable impact on usability of the system + would often be encountered.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Property Fields are brittle.

3 participants