From fbbcfa0662d85de87cfa09f89f92df05f2d0dfb0 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Thu, 16 Jul 2026 11:08:27 -0400 Subject: [PATCH] fix(terms): erase generated terms via the correct flag option key Term generation flags terms in the option fakerpress.module_flag.terms (plural, via get_slug()), but fetch()/delete() read and cleared the singular fakerpress.module_flag.term, so 'Erase faked data' always collected an empty set and never removed generated categories/tags. Align fetch()/delete() to the plural key with regression coverage for the flag -> fetch -> delete cycle. Fixes #219 --- readme.txt | 1 + src/FakerPress/Module/Term.php | 4 +- tests/wpunit/Module/TermEraseTest.php | 80 +++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/wpunit/Module/TermEraseTest.php diff --git a/readme.txt b/readme.txt index ad2bdf1..acaee6a 100644 --- a/readme.txt +++ b/readme.txt @@ -108,6 +108,7 @@ Thank you for wanting to make FakerPress better for everyone! [We salute you](ht = 0.9.2 — 15 of July 2026 = +* Fix - "Erase faked data" now deletes generated categories and other terms; `Term::fetch()`/`delete()` were reading the singular `fakerpress.module_flag.term` option while generation wrote the plural `fakerpress.module_flag.terms`, so no terms were ever collected for deletion. [[#228](https://github.com/bordoni/fakerpress/pull/228)] * Fix - Generate terms in the selected taxonomy again; the Terms endpoint no longer lets the singular `taxonomy` alias's `category` default overwrite the admin form's plural `taxonomies` value, so choosing Tags now creates tags instead of categories. [[#227](https://github.com/bordoni/fakerpress/pull/227)] * Fix - Stop the `explode(): Passing null to parameter #2` PHP 8.1+ deprecation when generating posts with a sparse payload; the `Post` module now defaults every splittable request value (comment status, author, post types, HTML tags, post parent, image origins) to an empty string before splitting. [[#224](https://github.com/bordoni/fakerpress/pull/224)] * Fix - Resolve the fatal error on WordPress 7.0 when generating attachments (`Call to undefined function FakerPress\Module\media_handle_sideload()`); WordPress 7.0 preloads `wp-admin/includes/file.php`, so the single `download_url()` guard skipped `media.php`. Each admin include is now guarded by the function it actually provides. [[#221](https://github.com/bordoni/fakerpress/pull/221)] diff --git a/src/FakerPress/Module/Term.php b/src/FakerPress/Module/Term.php index 47980f3..7a5a8de 100644 --- a/src/FakerPress/Module/Term.php +++ b/src/FakerPress/Module/Term.php @@ -38,7 +38,7 @@ public function hook(): void { * @inheritDoc */ public static function fetch( array $args = [] ): array { - return get_option( 'fakerpress.module_flag.term', [] ); + return get_option( 'fakerpress.module_flag.' . static::get_slug(), [] ); } /** @@ -54,7 +54,7 @@ public static function delete( $items ) { } } - delete_option( 'fakerpress.module_flag.term' ); + delete_option( 'fakerpress.module_flag.' . static::get_slug() ); return $deleted; } diff --git a/tests/wpunit/Module/TermEraseTest.php b/tests/wpunit/Module/TermEraseTest.php new file mode 100644 index 0000000..ae26d6b --- /dev/null +++ b/tests/wpunit/Module/TermEraseTest.php @@ -0,0 +1,80 @@ +filter_save_response( + null, + [ + 'name' => 'FP Erase ' . $taxonomy . ' ' . wp_generate_password( 6, false ), + 'taxonomy' => $taxonomy, + 'description' => 'Generated by FakerPress test.', + 'parent_term' => 0, + ], + $module + ); + } + + /** + * fetch() must return the terms that creation flagged (same option key on both sides). + * + * @test + */ + public function it_should_fetch_flagged_terms(): void { + $term_id = $this->create_flagged_term( 'category' ); + + $flagged = Term::fetch(); + + $this->assertNotEmpty( $flagged, 'fetch() should return the flagged terms, not an empty array.' ); + $this->assertArrayHasKey( 'category', $flagged ); + $this->assertContains( $term_id, $flagged['category'] ); + } + + /** + * delete() must remove the flagged terms and clear the flag option — the full erase path. + * + * @test + */ + public function it_should_delete_flagged_terms_and_clear_the_option(): void { + $category_id = $this->create_flagged_term( 'category' ); + $tag_id = $this->create_flagged_term( 'post_tag' ); + + $items = Term::fetch(); + $this->assertNotEmpty( $items ); + + Term::delete( $items ); + + $this->assertNull( get_term( $category_id ), 'Generated category should be deleted.' ); + $this->assertNull( get_term( $tag_id ), 'Generated tag should be deleted.' ); + $this->assertEmpty( Term::fetch(), 'The flag option should be cleared after delete().' ); + } +}