# Additional image workflow regressions

Initially checked on 2026-10-27 against the working tree containing automatic
upload optimization. On 2026-10-28, the six failing exploratory scenarios and
their unused fault-injection helper were removed at the user's request. The six
passing scenarios remain enabled. Production code was not changed.

The new tests use disposable `ksf_tests` databases and their own fixture files.
WordPress image generation, database writes, REST operations, and restore cleanup
are real. The converter response is substituted.

## Verification after removing the exploratory tests

The upload and workflow test classes pass on both databases:

| Run | Tests | Assertions | Failures | Errors |
| --- | ---: | ---: | ---: | ---: |
| MySQL 8, upload and workflow tests | 25 | 534 | 0 | 0 |
| MariaDB 10.11, upload and workflow tests | 25 | 534 | 0 | 0 |

## Initial investigation results (before removing exploratory tests)

| Run | Tests | Passed | Failed | Errors |
| --- | ---: | ---: | ---: | ---: |
| MySQL 8, complete PHP suite | 464 | 458 | 6 | 0 |
| MariaDB 10.11, new regression group | 12 | 6 | 6 | 0 |

All previous 452 tests passed in the complete run. The same six new scenarios
failed on both databases. The failures below are historical findings; their
tests have been removed, and no production fixes were made.

| New scenario | Result |
| --- | --- |
| Three optimize/restore cycles retaining each WebP generation | Pass |
| Retain the first WebP generation, delete later generations during restore | Pass |
| Preserve title, caption, description, and alt edits made after optimization | Pass |
| Reject a corrupt bitstream that starts with a valid RIFF/WEBP signature | Pass |
| Preserve alpha in a transparent image, its WebP sizes, and the restored PNG | Pass |
| Success → failed conversion → success uploads keep independent restore state | Pass |
| One failed WebP sub-size must not publish an incomplete optimized image | Fail; test removed |
| Native WordPress retry after a partial WebP failure must preserve restore | Fail; test removed |
| Permanent attachment deletion must not leave an unreachable restore record | Fail; test removed |
| Restore with an original whose image header was corrupted | Fail; test removed |
| Restore with an original changed from 320×240 to 12×8 pixels | Fail; test removed |
| Restore with a missing original 80×60 size | Fail; test removed |

## Diagnosis of the removed scenarios

1. **Damaged original backups can be published as a successful restore.**
   In all three damaged-backup scenarios, restore succeeds, deletes the valid
   WebP files when cleanup is requested, and removes the journal. The resulting
   PNG either cannot be decoded, disagrees with the saved dimensions, or has a
   missing derivative referenced by attachment metadata.

   [can_restore_image()](../includes/class-image-optimizer.php#L1128) checks that
   the original full-size path exists. File fingerprints protect the replacement
   WebP files, but original image headers, dimensions, and original sub-size
   availability are not validated. The shared restore method publishes the saved
   paths and metadata, verifies the database values, and removes the journal;
   [replacement cleanup](../includes/class-image-optimizer.php#L1199) then removes
   the verified WebP files. The removed test accepted either a safe refusal preserving
   the WebP and journal, or a successful repair producing coherent image files.

2. **A partial native WebP resize is treated as successful optimization.**
   The removed fault-injection editor returned `WP_Error` only for the 80-pixel WebP size.
   Original PNG sizes are generated successfully. WordPress's
   `_wp_make_subsizes()` skips the failed size and returns metadata for the
   remaining size. The optimizer's
   [generated metadata checks](../includes/class-image-optimizer.php#L910)
   accept that partial metadata and activate the journal. The response therefore
   reports WebP success with `ksf_release_small` missing, even though the original
   already has both required sizes.

3. **A native retry makes the partial result unrestorable.**
   The partially generated WebP is initially restorable. After the simulated
   failure is removed, `wp_update_image_subsizes()` generates the missing size.
   Attachment metadata now describes both WebP sizes, while the journal still
   contains the earlier partial snapshot. The strict comparison in
   [can_restore_image()](../includes/class-image-optimizer.php#L1116) returns
   `replacement_changed`. A native retry is thus interpreted as a later image
   replacement. This is separate from detecting edited image bytes: the only
   change in this test is WordPress completing its missing size.

4. **Permanent attachment deletion leaves a journal orphan (now fixed).**
   The original investigation found that `wp_delete_attachment( $id, true )`
   removed the attached WebPs but retained the `active` journal and original PNGs.
   The new `delete_attachment` integration removes recorded originals and verified
   replacement files, then clears the journal under the attachment's operation lock.
   Missing originals are harmless; fresh pending journals use the same cleanup.
   Files outside the recorded metadata or with unverified replacement ownership
   are preserved. Core continues deleting its currently referenced files.

   [ImageOptimizerDeletionTest.php](Unit/ImageOptimizerDeletionTest.php) now covers
   this fix, safe interrupted-operation cleanup, file ownership, locking, and retries.
   [ImageOptimizationUploadTest.php](Unit/ImageOptimizationUploadTest.php) also
   covers permanent deletion after a real native automatically optimized upload.

## Running the retained tests

Run from the plugin directory with the test services in `compose.yaml` running:

```bash
export WP_TESTS_DIR="$PWD/vendor/wp-phpunit/wp-phpunit"
export WP_PHPUNIT__TESTS_CONFIG="$PWD/tests/wp-tests-config.php"

KSF_TESTS_DB_HOST=127.0.0.1:13380 vendor/bin/phpunit --configuration phpunit.xml.dist --group image-workflow-regressions
KSF_TESTS_DB_HOST=127.0.0.1:13306 vendor/bin/phpunit --configuration phpunit.xml.dist --group image-workflow-regressions
```

Both commands run the six retained scenarios. The complete suite includes these
tests by default.

The added coverage lives in
[ImageOptimizationWorkflowRegressionTest.php](Unit/ImageOptimizationWorkflowRegressionTest.php)
and one new case in [ImageOptimizationUploadTest.php](Unit/ImageOptimizationUploadTest.php).
