Skip to content

Add validation for OS form#22913

Open
eduardomozart wants to merge 36 commits intoglpi-project:11.0/bugfixesfrom
eduardomozart:add-validation-for-os-form
Open

Add validation for OS form#22913
eduardomozart wants to merge 36 commits intoglpi-project:11.0/bugfixesfrom
eduardomozart:add-validation-for-os-form

Conversation

@eduardomozart
Copy link
Contributor

@eduardomozart eduardomozart commented Jan 30, 2026

Checklist before requesting a review

Please delete options that are not relevant.

  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.
  • I have added tests that prove my fix is effective or that my feature works.
  • [] This change requires a documentation update.

Description

Empty Operating System records could be created and persisted in glpi_items_operatingsystems, causing database bloat and incorrect tab counts. This occurred when submitting the OS form with all fields left empty or set to zero.

Validation logic (src/Item_OperatingSystem.php):

  • prepareInputForAdd(): Returns false when all OS fields are empty, preventing record creation
  • prepareInputForUpdate(): Deletes the record when all fields are cleared during update
  • areAllFieldsEmpty(): Checks 11 fields of /templates/pages/assets/operatingsystem.html.twig (OS ID, version, architecture, service pack, kernel version, edition, license number, license ID, company, owner, host ID). A record is valid if any of the 11 checked fields has a non-empty, non-zero value.

Test coverage (tests/functional/Item_OperatingSystemTest.php):

  • Verifies empty OS add/update prevention
  • Confirms valid partial records are allowed (e.g., only license number populated)
  • Validates automatic unlinking on update-to-empty

Screenshots (if appropriate):

image

Copilot AI and others added 6 commits January 30, 2026 04:02
Co-authored-by: eduardomozart <2974895+eduardomozart@users.noreply.github.com>
Co-authored-by: eduardomozart <2974895+eduardomozart@users.noreply.github.com>
Co-authored-by: eduardomozart <2974895+eduardomozart@users.noreply.github.com>
…lify validation logic

Co-authored-by: eduardomozart <2974895+eduardomozart@users.noreply.github.com>
…s-form

Prevent empty Operating System records from being created or persisted
@eduardomozart eduardomozart marked this pull request as draft January 30, 2026 04:19
@eduardomozart eduardomozart changed the title Add validation for os form Add validation for OS form Jan 30, 2026
Introduce testIsUsed in HasOperatingSystemCapacityTest to verify a capacity's check() returns true for a fresh asset and becomes false after creating a related item. The test initializes an asset definition with the target capacity, creates an asset, asserts the capacity is initially available, creates the related resource via the data provider target class, and finally asserts the capacity is reported as used.
Replace legacy capacity->check() assertions with capacity->isUsed($class) and update expectations accordingly. Add testGetCapacityUsageDescription to verify getCapacityUsageDescription returns the expected formatted usage strings (for 0 and 1 usages) using the provideGetCapacityUsageDescription data provider, creating test assets and related items to assert usage counts.
Update operating system validation and tests:

- Change session message to advise using delete to remove an OS instead of updating with empty values.
- Tighten field-check logic to only call trim() on string inputs (add is_string guard) to avoid trimming non-string values.
- Update functional tests to reflect that empty OS records can be added at the model level (assertGreaterThan for add) and that update() returns true while emitting the session error message and preserving original values. Tests also assert the updated error message and add an extra assertion for the architecture field to ensure integrity.

Files changed: src/Item_OperatingSystem.php, tests/functional/Item_OperatingSystemTest.php.
When updating an Operating System record, merge incoming input with the existing DB fields and reject the update only if the resulting record would be entirely empty. Tighten per-field checks to skip keys that aren't present and treat only positive integers or non-empty trimmed strings as populated. Update functional tests to expect add/update of fully empty OS data to be rejected and to assert the appropriate error behavior.
Adjust validation in Item_OperatingSystem to treat numeric values (including numeric strings) consistently: replace is_int() with is_numeric() and make the string branch exclude numeric strings. This prevents numeric strings like "0" from being treated as non-empty text and incorrectly failing validation, and also supports floats and numeric input correctly.
@eduardomozart eduardomozart marked this pull request as ready for review January 31, 2026 23:41
@cedric-anne cedric-anne requested a review from trasher March 11, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

It's possible to add & update an Operating System to a Computer with all fields empty

3 participants