Skip to content

Conversation

@soderlind
Copy link
Owner

This pull request introduces major improvements to testing safety and database management, as well as critical bug fixes for the ContentPoll plugin. The most significant changes ensure that running tests can no longer delete production vote data, with robust protections and documentation for safe test execution. Additionally, the release notes in CHANGELOG.md detail a series of critical bug fixes and architectural changes across recent versions, particularly focused on preventing data loss during migrations and plugin updates.

Testing Safety and Documentation Improvements:

  • Added multiple layers of protection to prevent tests from deleting production vote data, including the use of a PHPUNIT_TEST constant, database migration safeguards, and explicit checks for test database prefixes or a confirmation flag.
  • Introduced a new test runner script (run-tests.sh) supporting both safe and explicit truncation modes, and added comprehensive testing documentation (TESTING.md, tests/README.md, docs/TEST-DATABASE-SETUP.md). [1] [2]
  • Updated the test suite and bootstrap logic to only truncate data if the database prefix is clearly for testing (e.g., test_*, phpunit_*), or if explicitly confirmed by the user.
  • Enhanced the README.md with clear instructions for safe test execution and database safety configuration.

Database and Migration Robustness:

  • Refactored database management into a dedicated DatabaseManager class using the singleton pattern, and moved initialization to the plugins_loaded hook with semantic versioning for migration tracking.
  • Fixed critical bugs where migrations could leave the database in a partially migrated state, causing unique constraint conflicts and potential vote data loss.
  • Ensured that production data is always protected during development and testing, with clear upgrade and migration instructions in the changelog.

Block Attribute and Asset Updates:

  • Added a new pollId attribute to the vote block, further decoupling poll identity from the internal blockId and supporting future enhancements. [1] [2]
  • Updated asset versioning for block scripts to reflect the latest changes. [1] [2]

Changelog and Release Notes:

  • Expanded CHANGELOG.md with detailed entries for versions 0.7.0 through 0.7.6, documenting all critical fixes, architectural changes, and new features, as well as providing links for version comparisons. [1] [2]

Test Result Artifacts:

  • Updated .phpunit.cache/test-results to reflect the expanded and improved test coverage.

These changes collectively make the plugin much safer to develop and test, while also improving the maintainability and reliability of database migrations and poll identity handling.

- Updated the VoteStorageService class to change the parameter and variable names from `block_id` to `poll_id` for clarity and consistency with the new database schema.
- Modified the `record_vote` method to insert votes using `poll_id` and updated the error handling for duplicate votes to check against the new unique constraint on `(poll_id, hashed_token)`.
- Adjusted the `get_aggregate` method to retrieve aggregate data based on `poll_id` instead of `block_id`.
- Changed the `get_user_vote` method to use `poll_id` for fetching the user's vote.
- Updated the corresponding test cases in VoteStorageServiceTest and AggregationTest to reflect the changes in the database schema and ensure proper functionality.

Enhance test safety and isolation

- Added a constant `PHPUNIT_TEST` in the bootstrap file to prevent migrations during PHPUnit tests, ensuring that tests run against a stable schema without altering production data.
- Implemented checks in the test bootstrap to truncate the vote table only if the database prefix indicates a test environment, preventing accidental data loss during development.
- Updated the test cases to ensure they correctly handle the new schema and constraints, including changes to how duplicates are checked and how results are aggregated.

Create comprehensive testing documentation

- Added a detailed `TESTING.md` file outlining the testing setup, including safe testing practices, running tests with a clean database, and CI/CD configuration.
- Included instructions for setting up a separate test database and how to run tests safely without affecting production data.
- Documented the test database setup process in `TEST-DATABASE-SETUP.md`, providing examples and best practices for developers.

Implement a DatabaseManager for schema management

- Introduced a new `DatabaseManager` class to handle database operations, including table creation and schema migrations, ensuring a consistent state across different environments.
- The `DatabaseManager` uses a singleton pattern to manage the database connection and operations, preventing multiple instances and ensuring thread safety.
- Implemented methods for initializing the database, ensuring the table exists, and running migrations as needed, with checks to prevent data loss during testing.

Overall, these changes improve code clarity, enhance testing safety, and provide a robust framework for managing database interactions within the ContentPoll AI plugin.
@soderlind soderlind merged commit 7388b5b into main Nov 14, 2025
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.

2 participants