diff --git a/.gitignore b/.gitignore index 4ac74fc..69f1995 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ Thumbs.db .playwright-mcp/ .claude/ dist/ +.phpunit.cache +.phpunit.result.cache diff --git a/composer.json b/composer.json index da79ace..5bbe8cd 100644 --- a/composer.json +++ b/composer.json @@ -1,18 +1,24 @@ { "name": "2meet/2meet-data-optimizer-hivepress-addon", - "description": "HivePress + 12 擴充整合:13 adapter + 7 interceptor + 5 query interceptor + bootstrap + admin + CLI", + "description": "HivePress integration layer for 2meet Data Optimizer", "type": "wordpress-plugin", "license": "GPL-2.0-or-later", - "keywords": ["wordpress", "2meet-data-optimizer", "addon", "hivepress"], "require": { "php": ">=8.1" }, "require-dev": { + "brain/monkey": "^2.6", + "dealerdirect/phpcodesniffer-composer-installer": "^1.2", + "phpcompatibility/phpcompatibility-wp": "^2.1", + "phpcsstandards/phpcsutils": "^1.2", "phpunit/phpunit": "^10.5", - "wp-coding-standards/wpcs": "^3.0" + "squizlabs/php_codesniffer": "^3.13", + "wp-coding-standards/wpcs": "^3.0", + "yoast/phpunit-polyfills": "^2.0" }, "scripts": { - "phpcs": "phpcs --standard=WordPress includes/ 2meet-data-optimizer-hivepress-addon.php" + "test": "phpunit --configuration phpunit.xml", + "phpcs": "phpcs --standard=WordPress includes/ admin/ cli/" }, "config": { "allow-plugins": { diff --git a/composer.lock b/composer.lock index 3bccac7..17607f4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,9 +4,127 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2cfda1cbb3d53af75fbe9ac5baa798a1", + "content-hash": "1ff95ada2eeef20075975e47baf9396f", "packages": [], "packages-dev": [ + { + "name": "antecedent/patchwork", + "version": "2.2.3", + "source": { + "type": "git", + "url": "https://github.com/antecedent/patchwork.git", + "reference": "8b6b235f405af175259c8f56aea5fc23ab9f03ce" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/antecedent/patchwork/zipball/8b6b235f405af175259c8f56aea5fc23ab9f03ce", + "reference": "8b6b235f405af175259c8f56aea5fc23ab9f03ce", + "shasum": "" + }, + "require": { + "php": ">=7.1.0" + }, + "require-dev": { + "phpunit/phpunit": ">=4" + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ignas Rudaitis", + "email": "ignas.rudaitis@gmail.com" + } + ], + "description": "Method redefinition (monkey-patching) functionality for PHP.", + "homepage": "https://antecedent.github.io/patchwork/", + "keywords": [ + "aop", + "aspect", + "interception", + "monkeypatching", + "redefinition", + "runkit", + "testing" + ], + "support": { + "issues": "https://github.com/antecedent/patchwork/issues", + "source": "https://github.com/antecedent/patchwork/tree/2.2.3" + }, + "time": "2025-09-17T09:00:56+00:00" + }, + { + "name": "brain/monkey", + "version": "2.7.0", + "source": { + "type": "git", + "url": "https://github.com/Brain-WP/BrainMonkey.git", + "reference": "ea3aeb3d559ba3c0930b3f4d210b665a4c044d83" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Brain-WP/BrainMonkey/zipball/ea3aeb3d559ba3c0930b3f4d210b665a4c044d83", + "reference": "ea3aeb3d559ba3c0930b3f4d210b665a4c044d83", + "shasum": "" + }, + "require": { + "antecedent/patchwork": "^2.1.17", + "mockery/mockery": "~1.3.6 || ~1.4.4 || ~1.5.1 || ^1.6.10", + "php": ">=5.6.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0.0", + "phpcompatibility/php-compatibility": "^9.3.0", + "phpunit/phpunit": "^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.49 || ^9.6.30" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev", + "dev-version/1": "1.x-dev" + } + }, + "autoload": { + "files": [ + "inc/api.php" + ], + "psr-4": { + "Brain\\Monkey\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Giuseppe Mazzapica", + "email": "giuseppe.mazzapica@gmail.com", + "homepage": "https://gmazzap.me", + "role": "Developer" + } + ], + "description": "Mocking utility for PHP functions and WordPress plugin API", + "keywords": [ + "Monkey Patching", + "interception", + "mock", + "mock functions", + "mockery", + "patchwork", + "redefinition", + "runkit", + "test", + "testing" + ], + "support": { + "issues": "https://github.com/Brain-WP/BrainMonkey/issues", + "source": "https://github.com/Brain-WP/BrainMonkey" + }, + "time": "2026-02-05T09:22:14+00:00" + }, { "name": "dealerdirect/phpcodesniffer-composer-installer", "version": "v1.2.1", @@ -103,6 +221,140 @@ ], "time": "2026-05-06T08:26:05+00:00" }, + { + "name": "hamcrest/hamcrest-php", + "version": "v2.1.1", + "source": { + "type": "git", + "url": "https://github.com/hamcrest/hamcrest-php.git", + "reference": "f8b1c0173b22fa6ec77a81fe63e5b01eba7e6487" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/hamcrest/hamcrest-php/zipball/f8b1c0173b22fa6ec77a81fe63e5b01eba7e6487", + "reference": "f8b1c0173b22fa6ec77a81fe63e5b01eba7e6487", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "replace": { + "cordoval/hamcrest-php": "*", + "davedevelopment/hamcrest-php": "*", + "kodova/hamcrest-php": "*" + }, + "require-dev": { + "phpunit/php-file-iterator": "^1.4 || ^2.0 || ^3.0", + "phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.1-dev" + } + }, + "autoload": { + "classmap": [ + "hamcrest" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "This is the PHP port of Hamcrest Matchers", + "keywords": [ + "test" + ], + "support": { + "issues": "https://github.com/hamcrest/hamcrest-php/issues", + "source": "https://github.com/hamcrest/hamcrest-php/tree/v2.1.1" + }, + "time": "2025-04-30T06:54:44+00:00" + }, + { + "name": "mockery/mockery", + "version": "1.6.12", + "source": { + "type": "git", + "url": "https://github.com/mockery/mockery.git", + "reference": "1f4efdd7d3beafe9807b08156dfcb176d18f1699" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mockery/mockery/zipball/1f4efdd7d3beafe9807b08156dfcb176d18f1699", + "reference": "1f4efdd7d3beafe9807b08156dfcb176d18f1699", + "shasum": "" + }, + "require": { + "hamcrest/hamcrest-php": "^2.0.1", + "lib-pcre": ">=7.0", + "php": ">=7.3" + }, + "conflict": { + "phpunit/phpunit": "<8.0" + }, + "require-dev": { + "phpunit/phpunit": "^8.5 || ^9.6.17", + "symplify/easy-coding-standard": "^12.1.14" + }, + "type": "library", + "autoload": { + "files": [ + "library/helpers.php", + "library/Mockery.php" + ], + "psr-4": { + "Mockery\\": "library/Mockery" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Pádraic Brady", + "email": "padraic.brady@gmail.com", + "homepage": "https://github.com/padraic", + "role": "Author" + }, + { + "name": "Dave Marshall", + "email": "dave.marshall@atstsolutions.co.uk", + "homepage": "https://davedevelopment.co.uk", + "role": "Developer" + }, + { + "name": "Nathanael Esayeas", + "email": "nathanael.esayeas@protonmail.com", + "homepage": "https://github.com/ghostwriter", + "role": "Lead Developer" + } + ], + "description": "Mockery is a simple yet flexible PHP mock object framework", + "homepage": "https://github.com/mockery/mockery", + "keywords": [ + "BDD", + "TDD", + "library", + "mock", + "mock objects", + "mockery", + "stub", + "test", + "test double", + "testing" + ], + "support": { + "docs": "https://docs.mockery.io/", + "issues": "https://github.com/mockery/mockery/issues", + "rss": "https://github.com/mockery/mockery/releases.atom", + "security": "https://github.com/mockery/mockery/security/advisories", + "source": "https://github.com/mockery/mockery" + }, + "time": "2024-05-16T03:13:13+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.13.4", @@ -165,20 +417,19 @@ }, { "name": "nikic/php-parser", - "version": "v5.7.0", + "version": "v5.8.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "dca41cd15c2ac9d055ad70dbfd011130757d1f82" + "reference": "044a6a392ff8ad0d61f14370a5fbbd0a0107152f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/dca41cd15c2ac9d055ad70dbfd011130757d1f82", - "reference": "dca41cd15c2ac9d055ad70dbfd011130757d1f82", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/044a6a392ff8ad0d61f14370a5fbbd0a0107152f", + "reference": "044a6a392ff8ad0d61f14370a5fbbd0a0107152f", "shasum": "" }, "require": { - "ext-ctype": "*", "ext-json": "*", "ext-tokenizer": "*", "php": ">=7.4" @@ -217,9 +468,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.7.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.8.0" }, - "time": "2025-12-06T11:56:16+00:00" + "time": "2026-07-04T14:30:18+00:00" }, { "name": "phar-io/manifest", @@ -340,22 +591,235 @@ "time": "2022-02-21T01:04:05+00:00" }, { - "name": "phpcsstandards/phpcsextra", - "version": "1.5.0", + "name": "phpcompatibility/php-compatibility", + "version": "9.3.5", "source": { "type": "git", - "url": "https://github.com/PHPCSStandards/PHPCSExtra.git", - "reference": "b598aa890815b8df16363271b659d73280129101" + "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHPCSExtra/zipball/b598aa890815b8df16363271b659d73280129101", - "reference": "b598aa890815b8df16363271b659d73280129101", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243", + "shasum": "" + }, + "require": { + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.3 || ^3.0.2" + }, + "conflict": { + "squizlabs/php_codesniffer": "2.6.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "homepage": "https://github.com/wimg", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "homepage": "https://github.com/jrfnl", + "role": "lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors" + } + ], + "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.", + "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", + "keywords": [ + "compatibility", + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibility/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibility" + }, + "time": "2019-12-27T09:44:58+00:00" + }, + { + "name": "phpcompatibility/phpcompatibility-paragonie", + "version": "1.3.4", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git", + "reference": "244d7b04fc4bc2117c15f5abe23eb933b5f02bbf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/244d7b04fc4bc2117c15f5abe23eb933b5f02bbf", + "reference": "244d7b04fc4bc2117c15f5abe23eb933b5f02bbf", + "shasum": "" + }, + "require": { + "phpcompatibility/php-compatibility": "^9.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", + "paragonie/random_compat": "dev-master", + "paragonie/sodium_compat": "dev-master" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" + } + ], + "description": "A set of rulesets for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by the Paragonie polyfill libraries.", + "homepage": "http://phpcompatibility.com/", + "keywords": [ + "compatibility", + "paragonie", + "phpcs", + "polyfill", + "standards", + "static analysis" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie/issues", + "security": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie/security/policy", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie" + }, + "funding": [ + { + "url": "https://github.com/PHPCompatibility", + "type": "github" + }, + { + "url": "https://github.com/jrfnl", + "type": "github" + }, + { + "url": "https://opencollective.com/php_codesniffer", + "type": "open_collective" + }, + { + "url": "https://thanks.dev/u/gh/phpcompatibility", + "type": "thanks_dev" + } + ], + "time": "2025-09-19T17:43:28+00:00" + }, + { + "name": "phpcompatibility/phpcompatibility-wp", + "version": "2.1.8", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibilityWP.git", + "reference": "7c8d18b4d90dac9e86b0869a608fa09158e168fa" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityWP/zipball/7c8d18b4d90dac9e86b0869a608fa09158e168fa", + "reference": "7c8d18b4d90dac9e86b0869a608fa09158e168fa", + "shasum": "" + }, + "require": { + "phpcompatibility/php-compatibility": "^9.0", + "phpcompatibility/phpcompatibility-paragonie": "^1.0", + "squizlabs/php_codesniffer": "^3.3" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" + } + ], + "description": "A ruleset for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by WordPress.", + "homepage": "http://phpcompatibility.com/", + "keywords": [ + "compatibility", + "phpcs", + "standards", + "static analysis", + "wordpress" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityWP/issues", + "security": "https://github.com/PHPCompatibility/PHPCompatibilityWP/security/policy", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityWP" + }, + "funding": [ + { + "url": "https://github.com/PHPCompatibility", + "type": "github" + }, + { + "url": "https://github.com/jrfnl", + "type": "github" + }, + { + "url": "https://opencollective.com/php_codesniffer", + "type": "open_collective" + }, + { + "url": "https://thanks.dev/u/gh/phpcompatibility", + "type": "thanks_dev" + } + ], + "time": "2025-10-18T00:05:59+00:00" + }, + { + "name": "phpcsstandards/phpcsextra", + "version": "1.5.1", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/PHPCSExtra.git", + "reference": "39467533fdb742446d68c1d10ac33d625ee0311c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/PHPCSExtra/zipball/39467533fdb742446d68c1d10ac33d625ee0311c", + "reference": "39467533fdb742446d68c1d10ac33d625ee0311c", "shasum": "" }, "require": { "php": ">=5.4", - "phpcsstandards/phpcsutils": "^1.2.0", + "phpcsstandards/phpcsutils": "^1.2.3", "squizlabs/php_codesniffer": "^3.13.5 || ^4.0.1" }, "require-dev": { @@ -419,20 +883,20 @@ "type": "thanks_dev" } ], - "time": "2025-11-12T23:06:57+00:00" + "time": "2026-07-27T11:13:17+00:00" }, { "name": "phpcsstandards/phpcsutils", - "version": "1.2.2", + "version": "1.2.3", "source": { "type": "git", "url": "https://github.com/PHPCSStandards/PHPCSUtils.git", - "reference": "c216317e96c8b3f5932808f9b0f1f7a14e3bbf55" + "reference": "5f35d9408c54d7b529501f3c688b6eae562aea1f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/c216317e96c8b3f5932808f9b0f1f7a14e3bbf55", - "reference": "c216317e96c8b3f5932808f9b0f1f7a14e3bbf55", + "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/5f35d9408c54d7b529501f3c688b6eae562aea1f", + "reference": "5f35d9408c54d7b529501f3c688b6eae562aea1f", "shasum": "" }, "require": { @@ -512,7 +976,7 @@ "type": "thanks_dev" } ], - "time": "2025-12-08T14:27:58+00:00" + "time": "2026-07-27T10:28:41+00:00" }, { "name": "phpunit/php-code-coverage", @@ -837,24 +1301,24 @@ }, { "name": "phpunit/phpunit", - "version": "10.5.63", + "version": "10.5.64", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "33198268dad71e926626b618f3ec3966661e4d90" + "reference": "0e8c1d19cea35ad97d4887f363d07c78e30fbf06" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/33198268dad71e926626b618f3ec3966661e4d90", - "reference": "33198268dad71e926626b618f3ec3966661e4d90", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/0e8c1d19cea35ad97d4887f363d07c78e30fbf06", + "reference": "0e8c1d19cea35ad97d4887f363d07c78e30fbf06", "shasum": "" }, "require": { "ext-dom": "*", + "ext-filter": "*", "ext-json": "*", "ext-libxml": "*", "ext-mbstring": "*", - "ext-xml": "*", "ext-xmlwriter": "*", "myclabs/deep-copy": "^1.13.4", "phar-io/manifest": "^2.0.4", @@ -918,31 +1382,15 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.63" + "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.64" }, "funding": [ { - "url": "https://phpunit.de/sponsors.html", - "type": "custom" - }, - { - "url": "https://github.com/sebastianbergmann", - "type": "github" - }, - { - "url": "https://liberapay.com/sebastianbergmann", - "type": "liberapay" - }, - { - "url": "https://thanks.dev/u/gh/sebastianbergmann", - "type": "thanks_dev" - }, - { - "url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit", - "type": "tidelift" + "url": "https://phpunit.de/sponsoring.html", + "type": "other" } ], - "time": "2026-01-27T05:48:37+00:00" + "time": "2026-07-06T14:50:35+00:00" }, { "name": "sebastian/cli-parser", @@ -2028,16 +2476,16 @@ }, { "name": "wp-coding-standards/wpcs", - "version": "3.3.0", + "version": "3.4.1", "source": { "type": "git", "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", - "reference": "7795ec6fa05663d716a549d0b44e47ffc8b0d4a6" + "reference": "ec2ff942335f33683a5957a85d138753876a05cf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/7795ec6fa05663d716a549d0b44e47ffc8b0d4a6", - "reference": "7795ec6fa05663d716a549d0b44e47ffc8b0d4a6", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/ec2ff942335f33683a5957a85d138753876a05cf", + "reference": "ec2ff942335f33683a5957a85d138753876a05cf", "shasum": "" }, "require": { @@ -2046,9 +2494,9 @@ "ext-tokenizer": "*", "ext-xmlreader": "*", "php": ">=7.2", - "phpcsstandards/phpcsextra": "^1.5.0", - "phpcsstandards/phpcsutils": "^1.1.0", - "squizlabs/php_codesniffer": "^3.13.4" + "phpcsstandards/phpcsextra": "^1.5.1", + "phpcsstandards/phpcsutils": "^1.2.3", + "squizlabs/php_codesniffer": "^3.13.5" }, "require-dev": { "php-parallel-lint/php-console-highlighter": "^1.0.0", @@ -2090,7 +2538,70 @@ "type": "custom" } ], - "time": "2025-11-25T12:08:04+00:00" + "time": "2026-07-27T11:53:23+00:00" + }, + { + "name": "yoast/phpunit-polyfills", + "version": "2.0.5", + "source": { + "type": "git", + "url": "https://github.com/Yoast/PHPUnit-Polyfills.git", + "reference": "1a6aecc9ebe4a9cea4e1047d0e6c496e52314c27" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/1a6aecc9ebe4a9cea4e1047d0e6c496e52314c27", + "reference": "1a6aecc9ebe4a9cea4e1047d0e6c496e52314c27", + "shasum": "" + }, + "require": { + "php": ">=5.6", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0" + }, + "require-dev": { + "php-parallel-lint/php-console-highlighter": "^1.0.0", + "php-parallel-lint/php-parallel-lint": "^1.4.0", + "yoast/yoastcs": "^3.2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "4.x-dev" + } + }, + "autoload": { + "files": [ + "phpunitpolyfills-autoload.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Team Yoast", + "email": "support@yoast.com", + "homepage": "https://yoast.com" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Yoast/PHPUnit-Polyfills/graphs/contributors" + } + ], + "description": "Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests", + "homepage": "https://github.com/Yoast/PHPUnit-Polyfills", + "keywords": [ + "phpunit", + "polyfill", + "testing" + ], + "support": { + "issues": "https://github.com/Yoast/PHPUnit-Polyfills/issues", + "security": "https://github.com/Yoast/PHPUnit-Polyfills/security/policy", + "source": "https://github.com/Yoast/PHPUnit-Polyfills" + }, + "time": "2025-08-10T05:13:49+00:00" } ], "aliases": [], diff --git a/includes/hivepress/interface-tmdo-hp-adapter.php b/includes/hivepress/interface-tmdo-hp-adapter.php index 087cb6d..acc2233 100644 --- a/includes/hivepress/interface-tmdo-hp-adapter.php +++ b/includes/hivepress/interface-tmdo-hp-adapter.php @@ -38,7 +38,15 @@ if ( ! interface_exists( 'TMDO_HivePress_Adapter' ) ) { * everything except `plugin_slug()` / `detection_class()` / `detection_const()`, * so concrete adapters typically override 3–6 methods. */ - interface TMDO_HivePress_Adapter { + // Back-compat: declare the WPDO_ marker first so TMDO_ can extend it. + // PHP class_alias() does not work on interfaces; inheritance is the only way + // to make `instanceof WPDO_HivePress_Adapter` true for adapters implementing + // the TMDO_ name. Mirrors the core's interface-entity-adapter.php. + if ( ! interface_exists( 'WPDO_HivePress_Adapter', false ) ) { + interface WPDO_HivePress_Adapter {} + } + + interface TMDO_HivePress_Adapter extends WPDO_HivePress_Adapter { /** * Stable slug used as Schema_Registry / Custom_Table_Registry provider key. diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..1f81428 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,21 @@ + + + + + tests/unit + + + + + includes + admin + cli + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..7f61fbb --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,92 @@ +override = $override; + } + + public function plugin_slug(): string { + return 'test-adapter'; + } + + public function detection_class(): string { + return 'Test\\Adapter'; + } + + public function detection_const(): string { + return 'TEST_ADAPTER_VERSION'; + } + + public function suitability_score(): array { + return $this->compose_score( $this->override ); + } + }; + } + + public function test_default_score_is_perfect_ten(): void { + $score = $this->make_adapter()->suitability_score(); + $this->assertSame( 10.0, $score['aggregate'] ); + foreach ( array( 'd1_meta_calls', 'd2_meta_sql', 'd3_table_coverage', 'd4_registry_meta', 'd5_hook_bus', 'd6_options', 'd7_coupling', 'd8_perf' ) as $k ) { + $this->assertSame( 1.0, $score[ $k ] ); + } + } + + public function test_partial_override_lowers_aggregate(): void { + $score = $this->make_adapter( array( 'd5_hook_bus' => 0.5 ) )->suitability_score(); + // 7 × 1.0 + 1 × 0.5 = 7.5; aggregate = 7.5 / 8 * 10 = 9.375 → rounded 9.38. + $this->assertSame( 9.38, $score['aggregate'] ); + } + + public function test_score_clamped_to_unit_interval(): void { + $score = $this->make_adapter( array( + 'd1_meta_calls' => 1.5, // → clamped to 1.0 + 'd2_meta_sql' => -0.3, // → clamped to 0.0 + ) )->suitability_score(); + $this->assertSame( 1.0, $score['d1_meta_calls'] ); + $this->assertSame( 0.0, $score['d2_meta_sql'] ); + } + + public function test_default_implementations_are_no_op(): void { + $adapter = $this->make_adapter(); + + // All optional methods should be safe to invoke on a minimal adapter. + $this->assertSame( array(), $adapter->migrations() ); + $this->assertSame( '', $adapter->minimum_addon_version() ); + + $result = $adapter->doctor_check(); + $this->assertTrue( $result['ok'] ); + + // Hook handlers must not throw on a no-op adapter. + $adapter->on_register_query_hooks(); + $adapter->on_register_event_hooks(); + $adapter->on_register_entity_fields(); + $this->assertTrue( true ); // reaching here = no exception + } + + public function test_aggregate_rounding_is_two_decimals(): void { + $score = $this->make_adapter( array( + 'd1_meta_calls' => 0.85, + 'd2_meta_sql' => 0.92, + 'd3_table_coverage' => 0.78, + ) )->suitability_score(); + + // All other dimensions stay at 1.0 → sum = 0.85+0.92+0.78+5*1.0 = 7.55. + // aggregate = 7.55 / 8 * 10 = 9.4375 → round(2) = 9.44. + $this->assertSame( 9.44, $score['aggregate'] ); + } +} diff --git a/tests/unit/AdminHivePressTest.php b/tests/unit/AdminHivePressTest.php new file mode 100644 index 0000000..4bdc067 --- /dev/null +++ b/tests/unit/AdminHivePressTest.php @@ -0,0 +1,57 @@ +assertStringContainsString( 'HivePress', $out ); + $this->assertStringContainsString( 'hivepress', $out ); + $this->assertStringContainsString( 'wp wpdo hivepress', $out ); + } + + public function test_render_shows_clean_when_no_conflicts(): void { + $out = self::capture( static function () { + WPDO_Admin_HivePress::render(); + } ); + + $this->assertStringContainsString( 'Conflict guard', $out ); + $this->assertStringContainsString( 'notice-success', $out ); + } + + /** + * Capture echo output of a callable in a single buffer level. + * + * Avoids PHPUnit's "did not close output buffers" warning by using + * the level-checked open/close pattern. + */ + private static function capture( callable $callable ): string { + $start_level = ob_get_level(); + ob_start(); + try { + $callable(); + } finally { + $out = ''; + while ( ob_get_level() > $start_level ) { + $out = (string) ob_get_clean() . $out; + } + } + return $out; + } +} diff --git a/tests/unit/AttributeBridgeTest.php b/tests/unit/AttributeBridgeTest.php new file mode 100644 index 0000000..da98834 --- /dev/null +++ b/tests/unit/AttributeBridgeTest.php @@ -0,0 +1,98 @@ +bridge = new WPDO_HivePress_Attribute_Bridge(); + } + + public function test_discover_returns_empty_when_no_attributes_registered(): void { + // apply_filters stub returns the value unchanged → empty array. + $this->assertSame( array(), $this->bridge->discover_attributes() ); + } + + public function test_suggest_zone_configs_returns_empty_when_no_attributes(): void { + $this->assertSame( array(), $this->bridge->suggest_zone_configs() ); + } + + /** + * Verify the SQL type mapping is sensible for known HivePress field types. + * + * Indirect test via reflection — the public API only returns full configs + * but we want fast feedback if the type table drifts. + */ + public function test_sql_type_mapping_via_reflection(): void { + $reflect = new ReflectionClass( $this->bridge ); + $method = $reflect->getMethod( 'sql_type_for' ); + $method->setAccessible( true ); + + $this->assertStringContainsString( 'int', $method->invoke( $this->bridge, 'number' ) ); + $this->assertStringContainsString( 'decimal', $method->invoke( $this->bridge, 'price' ) ); + $this->assertStringContainsString( 'tinyint', $method->invoke( $this->bridge, 'checkbox' ) ); + $this->assertStringContainsString( 'date', $method->invoke( $this->bridge, 'date' ) ); + $this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'text' ) ); + // Unknown type → fallback to varchar. + $this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'unknown_type' ) ); + } + + /** + * Verify build_record normalises HP attribute config correctly. + */ + public function test_build_record_normalises_attribute_config(): void { + $reflect = new ReflectionClass( $this->bridge ); + $method = $reflect->getMethod( 'build_record' ); + $method->setAccessible( true ); + + $record = $method->invoke( $this->bridge, 'listing', 'beds', array( + 'edit_field' => array( 'type' => 'integer' ), + 'filterable' => true, + 'sortable' => false, + 'searchable' => true, + ) ); + + $this->assertSame( 'listing', $record['model'] ); + $this->assertSame( 'beds', $record['slug'] ); + $this->assertSame( 'hp_listing_beds', $record['meta_key'] ); + $this->assertSame( 'integer', $record['type'] ); + $this->assertTrue( $record['filterable'] ); + $this->assertFalse( $record['sortable'] ); + } + + /** + * Promotion candidate logic: filterable OR sortable triggers promotion. + */ + public function test_promotion_candidate_logic(): void { + $reflect = new ReflectionClass( $this->bridge ); + $method = $reflect->getMethod( 'is_promotion_candidate' ); + $method->setAccessible( true ); + + // Filterable but not sortable → promote. + $this->assertTrue( $method->invoke( $this->bridge, array( + 'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x', + 'type' => 'text', 'filterable' => true, 'sortable' => false, 'searchable' => true, + ) ) ); + + // Sortable but not filterable → promote. + $this->assertTrue( $method->invoke( $this->bridge, array( + 'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x', + 'type' => 'text', 'filterable' => false, 'sortable' => true, 'searchable' => false, + ) ) ); + + // Searchable only (no filter, no sort) → don't promote (full-text would + // be a different optimization). + $this->assertFalse( $method->invoke( $this->bridge, array( + 'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x', + 'type' => 'text', 'filterable' => false, 'sortable' => false, 'searchable' => true, + ) ) ); + } +} diff --git a/tests/unit/BenchmarkTest.php b/tests/unit/BenchmarkTest.php new file mode 100644 index 0000000..8d6d8eb --- /dev/null +++ b/tests/unit/BenchmarkTest.php @@ -0,0 +1,98 @@ +assertSame( 50, WPDO_HivePress_Benchmark::DEFAULT_ITERATIONS ); + } + + public function test_run_returns_mean_milliseconds(): void { + $ms = WPDO_HivePress_Benchmark::run( static function () { + usleep( 1000 ); // 1 ms + }, 5 ); + + // Allow generous slack for slow CI runners; we just need it to be > 0. + $this->assertGreaterThan( 0.0, $ms ); + } + + public function test_compare_returns_speedup_ratio(): void { + $result = WPDO_HivePress_Benchmark::compare( + 'sample', + static function () { usleep( 4000 ); }, // ~4 ms baseline + static function () { usleep( 1000 ); }, // ~1 ms target + 3 + ); + + $this->assertSame( 'sample', $result['name'] ); + $this->assertGreaterThan( 0.0, $result['baseline_ms'] ); + $this->assertGreaterThan( 0.0, $result['target_ms'] ); + // Baseline is roughly 4× target — ratio should be > 1 (faster). + $this->assertGreaterThan( 1.0, $result['ratio'] ); + $this->assertSame( 3, $result['iterations'] ); + } + + public function test_compare_handles_zero_target_time(): void { + // If target is suspiciously fast, ratio falls back to 0 (no division). + $result = WPDO_HivePress_Benchmark::compare( + 'instant', + static function () { /* noop */ }, + static function () { /* noop */ }, + 1 + ); + // Either ratio > 0 (both nonzero) or 0 (target_ms was 0); never NaN/inf. + $this->assertIsFloat( $result['ratio'] ); + $this->assertFalse( is_nan( $result['ratio'] ) ); + $this->assertFalse( is_infinite( $result['ratio'] ) ); + } + + public function test_run_suite_aggregates_samples(): void { + $report = WPDO_HivePress_Benchmark::run_suite( array( + array( + 'name' => 'a', + 'baseline' => static function () { usleep( 2000 ); }, + 'target' => static function () { usleep( 500 ); }, + 'iterations' => 2, + ), + array( + 'name' => 'b', + 'baseline' => static function () { usleep( 4000 ); }, + 'target' => static function () { usleep( 1000 ); }, + 'iterations' => 2, + ), + ) ); + + $this->assertSame( 2, $report['sample_count'] ); + $this->assertGreaterThan( 0.0, $report['geomean_ratio'] ); + } + + public function test_run_suite_skips_malformed_sample(): void { + $report = WPDO_HivePress_Benchmark::run_suite( array( + array( 'name' => 'incomplete' ), // missing baseline + target + array( + 'name' => 'ok', + 'baseline' => static function () { usleep( 100 ); }, + 'target' => static function () { usleep( 100 ); }, + 'iterations' => 1, + ), + ) ); + + $this->assertSame( 1, $report['sample_count'] ); + } + + public function test_geometric_mean_with_empty_values_returns_zero(): void { + $this->assertSame( 0.0, WPDO_HivePress_Benchmark::geometric_mean( array() ) ); + } + + public function test_geometric_mean_resists_outliers(): void { + // Geomean of (2, 8) = sqrt(16) = 4 (vs arithmetic mean 5). + $this->assertSame( 4.0, WPDO_HivePress_Benchmark::geometric_mean( array( 2.0, 8.0 ) ) ); + } +} diff --git a/tests/unit/BlocksAdapterTest.php b/tests/unit/BlocksAdapterTest.php new file mode 100644 index 0000000..c534acf --- /dev/null +++ b/tests/unit/BlocksAdapterTest.php @@ -0,0 +1,37 @@ +adapter = new WPDO_HivePress_Blocks_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-blocks', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Blocks\\Plugin', $this->adapter->detection_class() ); + } + + public function test_doctor_check_is_always_ok(): void { + $result = $this->adapter->doctor_check(); + $this->assertTrue( $result['ok'] ); + $this->assertStringContainsString( 'presentation-only', $result['message'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] ); + } +} diff --git a/tests/unit/BookingsAdapterTest.php b/tests/unit/BookingsAdapterTest.php new file mode 100644 index 0000000..155603d --- /dev/null +++ b/tests/unit/BookingsAdapterTest.php @@ -0,0 +1,60 @@ +install_wpdb_mock(); + $reflect = new ReflectionClass( WPDO_Schema_Registry::class ); + $prop = $reflect->getProperty( 'instance' ); + $prop->setAccessible( true ); + $prop->setValue( null, null ); + + $this->adapter = new WPDO_HivePress_Bookings_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-bookings', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Bookings\\Plugin', $this->adapter->detection_class() ); + } + + public function test_on_register_fields_reserves_booking_hot_columns(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + $start = $registry->get_field( 'hp_booking', 'hp_start_time' ); + $this->assertSame( 'hot', $start['zone'] ); + $this->assertTrue( (bool) $start['indexed'] ); + + $end = $registry->get_field( 'hp_booking', 'hp_end_time' ); + $this->assertSame( 'hot', $end['zone'] ); + $this->assertTrue( (bool) $end['indexed'] ); + } + + public function test_score_reflects_unverified_perf_dimension(): void { + $score = $this->adapter->suitability_score(); + // D8 reduced to 0.9 because schema is best-effort until addon installs. + $this->assertSame( 0.9, $score['d8_perf'] ); + // Aggregate = (7 × 1.0 + 0.9) / 8 × 10 = 9.875 → 9.88. + $this->assertSame( 9.88, $score['aggregate'] ); + } + + public function test_migrations_lists_booking_module(): void { + $this->assertSame( array( 'hot_hp_booking' ), $this->adapter->migrations() ); + } +} diff --git a/tests/unit/BootstrapTest.php b/tests/unit/BootstrapTest.php new file mode 100644 index 0000000..74fad50 --- /dev/null +++ b/tests/unit/BootstrapTest.php @@ -0,0 +1,56 @@ +assertSame( array(), WPDO_HivePress_Bootstrap::adapters() ); + $this->assertSame( array(), WPDO_HivePress_Bootstrap::detected() ); + + // Second call must not fail or change state. + WPDO_HivePress_Bootstrap::boot(); + $this->assertSame( array(), WPDO_HivePress_Bootstrap::adapters() ); + } + + public function test_adapter_for_returns_null_when_not_booted(): void { + $this->assertNull( WPDO_HivePress_Bootstrap::adapter_for( 'hivepress' ) ); + $this->assertNull( WPDO_HivePress_Bootstrap::adapter_for( 'hivepress-reviews' ) ); + $this->assertNull( WPDO_HivePress_Bootstrap::adapter_for( '' ) ); + } + + public function test_reset_clears_internal_state(): void { + WPDO_HivePress_Bootstrap::boot(); + WPDO_HivePress_Bootstrap::reset_for_tests(); + + // After reset boot() should run again; detected() should be re-populated + // (still empty in the test env, but the call must not be skipped). + WPDO_HivePress_Bootstrap::boot(); + $this->assertIsArray( WPDO_HivePress_Bootstrap::detected() ); + } + + public function test_should_bind_filter_short_circuits_binding(): void { + // Even if HivePress were detected, the filter must be honoured. + // We can't fake detection cleanly here without reflection, so instead + // we assert the filter is at least exposed and called: no detection + // → no adapter binding → empty adapters list regardless of filter. + add_filter( 'wpdo_hivepress_should_bind', '__return_false' ); + WPDO_HivePress_Bootstrap::boot(); + $this->assertSame( array(), WPDO_HivePress_Bootstrap::adapters() ); + } +} diff --git a/tests/unit/CliHivePressTest.php b/tests/unit/CliHivePressTest.php new file mode 100644 index 0000000..456831e --- /dev/null +++ b/tests/unit/CliHivePressTest.php @@ -0,0 +1,35 @@ +assertTrue( class_exists( 'WPDO_CLI_HivePress' ) ); + + $expected = array( 'detect', 'score', 'doctor', 'migrate', 'rollback', 'benchmark' ); + foreach ( $expected as $method ) { + $this->assertTrue( + method_exists( 'WPDO_CLI_HivePress', $method ), + sprintf( 'Method %s missing on WPDO_CLI_HivePress', $method ) + ); + } + } + + public function test_set_state_helper_is_private(): void { + $reflect = new ReflectionClass( WPDO_CLI_HivePress::class ); + $this->assertTrue( $reflect->hasMethod( 'set_state' ) ); + $method = $reflect->getMethod( 'set_state' ); + $this->assertTrue( $method->isPrivate() ); + } +} diff --git a/tests/unit/CommentRouterTest.php b/tests/unit/CommentRouterTest.php new file mode 100644 index 0000000..7ce74f4 --- /dev/null +++ b/tests/unit/CommentRouterTest.php @@ -0,0 +1,92 @@ +install_wpdb_mock(); + WPDO_HivePress_Comment_Router::reset_for_tests(); + // Default disabled. + $GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 0; + } + + public function test_disabled_by_default(): void { + $this->assertFalse( WPDO_HivePress_Comment_Router::is_enabled() ); + } + + public function test_enabled_when_option_set(): void { + $GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1; + $this->assertTrue( WPDO_HivePress_Comment_Router::is_enabled() ); + } + + public function test_register_is_idempotent(): void { + $GLOBALS['_wp_options'][ WPDO_HivePress_Comment_Router::OPTION_ENABLED ] = 1; + WPDO_HivePress_Comment_Router::register(); + WPDO_HivePress_Comment_Router::register(); // Second call must be no-op. + $this->assertTrue( true ); // No throw = pass + } + + public function test_rewrite_skips_unknown_comment_type(): void { + $query = new stdClass(); + $query->query_vars = array( 'type' => 'comment' ); // not in shadow map + + $clauses = array( 'join' => '', 'where' => '1=1' ); + $out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query ); + + $this->assertSame( $clauses, $out ); + } + + public function test_rewrite_adds_left_join_for_hp_favorite(): void { + $query = new stdClass(); + $query->query_vars = array( 'type' => 'hp_favorite' ); + + $clauses = array( 'join' => '', 'where' => '1=1' ); + $out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query ); + + $this->assertStringContainsString( 'LEFT JOIN', $out['join'] ); + $this->assertStringContainsString( 'wpdo_comment_hp_favorite', $out['join'] ); + $this->assertStringContainsString( 'AS wpdo_shadow', $out['join'] ); + } + + public function test_rewrite_promotes_recipient_for_hp_message(): void { + $query = new stdClass(); + $query->query_vars = array( 'type' => 'hp_message' ); + + $clauses = array( + 'join' => '', + 'where' => 'wp_comments.comment_karma = 42 AND comment_approved = 0', + ); + $out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query ); + + // recipient lookup should be rewritten to use shadow column. + $this->assertStringContainsString( 'wpdo_shadow.recipient_id = 42', $out['where'] ); + // Other predicates preserved. + $this->assertStringContainsString( 'comment_approved = 0', $out['where'] ); + } + + public function test_rewrite_handles_array_type(): void { + $query = new stdClass(); + $query->query_vars = array( 'type' => array( 'hp_review', 'comment' ) ); + + $clauses = array( 'join' => '', 'where' => '' ); + $out = WPDO_HivePress_Comment_Router::rewrite( $clauses, $query ); + + // First element of type array should match (hp_review). + $this->assertStringContainsString( 'wpdo_comment_hp_review', $out['join'] ); + } + + public function test_rewrite_returns_unchanged_when_query_invalid(): void { + $clauses = array( 'join' => '', 'where' => '1=1' ); + $out = WPDO_HivePress_Comment_Router::rewrite( $clauses, 'not an object' ); + $this->assertSame( $clauses, $out ); + } +} diff --git a/tests/unit/ConflictGuardTest.php b/tests/unit/ConflictGuardTest.php new file mode 100644 index 0000000..8243854 --- /dev/null +++ b/tests/unit/ConflictGuardTest.php @@ -0,0 +1,56 @@ +assertSame( array(), $conflicts ); + $this->assertFalse( WPDO_HivePress_Conflict_Guard::has_conflict() ); + } + + public function test_conflict_detected_when_hpct_const_defined(): void { + // Simulate hp-custom-tables presence via constant probe. + if ( ! defined( 'HPCT_VERSION' ) ) { + define( 'HPCT_VERSION', '1.0.0' ); + } + WPDO_HivePress_Conflict_Guard::reset_for_tests(); + $conflicts = WPDO_HivePress_Conflict_Guard::detect_conflicts(); + $this->assertContains( 'hp-custom-tables', $conflicts ); + $this->assertTrue( WPDO_HivePress_Conflict_Guard::has_conflict() ); + } + + public function test_check_and_warn_safe_when_no_conflicts(): void { + WPDO_HivePress_Conflict_Guard::reset_for_tests(); + // Should not throw or render anything when clean. + $this->expectOutputString( '' ); + WPDO_HivePress_Conflict_Guard::check_and_warn(); + } + + public function test_render_notice_does_nothing_when_not_admin(): void { + // is_admin() stub returns false by default. + ob_start(); + WPDO_HivePress_Conflict_Guard::render_notice(); + $out = ob_get_clean(); + $this->assertSame( '', $out ); + } + + public function test_memo_cache_avoids_repeat_probing(): void { + WPDO_HivePress_Conflict_Guard::reset_for_tests(); + $first = WPDO_HivePress_Conflict_Guard::detect_conflicts(); + $second = WPDO_HivePress_Conflict_Guard::detect_conflicts(); + $this->assertSame( $first, $second ); + } +} diff --git a/tests/unit/CoreAdapterTest.php b/tests/unit/CoreAdapterTest.php new file mode 100644 index 0000000..6af0e24 --- /dev/null +++ b/tests/unit/CoreAdapterTest.php @@ -0,0 +1,148 @@ +hasProperty( 'instance' ) ) { + $prop = $reflect->getProperty( 'instance' ); + $prop->setAccessible( true ); + $prop->setValue( null, null ); + } + WPDO_Custom_Table_Registry::reset_for_tests(); + $this->adapter = new WPDO_HivePress_Core_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_plugin_slug_is_hivepress(): void { + $this->assertSame( 'hivepress', $this->adapter->plugin_slug() ); + } + + public function test_detection_class_targets_hivepress_core(): void { + $this->assertSame( 'HivePress\\Core', $this->adapter->detection_class() ); + } + + public function test_detection_const_targets_hivepress_version(): void { + $this->assertSame( 'HIVEPRESS_VERSION', $this->adapter->detection_const() ); + } + + public function test_minimum_addon_version_floor(): void { + // 1.7.0 introduced the field-alias convention this adapter relies on. + $this->assertSame( '1.7.0', $this->adapter->minimum_addon_version() ); + } + + public function test_migrations_returns_listing_and_vendor_modules(): void { + $mods = $this->adapter->migrations(); + $this->assertContains( 'hot_hp_listing', $mods ); + $this->assertContains( 'hot_hp_vendor', $mods ); + } + + public function test_on_register_fields_populates_schema_registry(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + // Listing hot fields. + $drafted = $registry->get_field( 'hp_listing', 'hp_drafted' ); + $this->assertIsArray( $drafted ); + $this->assertSame( 'hot', $drafted['zone'] ); + $this->assertTrue( (bool) $drafted['indexed'] ); + + $expired = $registry->get_field( 'hp_listing', 'hp_expired_time' ); + $this->assertSame( 'hot', $expired['zone'] ); + $this->assertStringContainsString( 'bigint', $expired['data_type'] ); + + $featured_time = $registry->get_field( 'hp_listing', 'hp_featured_time' ); + $this->assertSame( 'hot', $featured_time['zone'] ); + + // Vendor cold blob. + $vendor_image = $registry->get_field( 'hp_vendor', 'hp_image' ); + $this->assertIsArray( $vendor_image ); + $this->assertSame( 'cold', $vendor_image['zone'] ); + } + + public function test_suitability_score_aggregates_to_ten(): void { + $score = $this->adapter->suitability_score(); + $this->assertArrayHasKey( 'aggregate', $score ); + $this->assertSame( 10.0, $score['aggregate'] ); + // All eight dimensions present. + foreach ( array( 'd1_meta_calls', 'd2_meta_sql', 'd3_table_coverage', 'd4_registry_meta', 'd5_hook_bus', 'd6_options', 'd7_coupling', 'd8_perf' ) as $key ) { + $this->assertArrayHasKey( $key, $score ); + $this->assertGreaterThanOrEqual( 0.0, $score[ $key ] ); + $this->assertLessThanOrEqual( 1.0, $score[ $key ] ); + } + } + + public function test_doctor_check_reports_table_status(): void { + $result = $this->adapter->doctor_check(); + $this->assertIsArray( $result ); + $this->assertArrayHasKey( 'ok', $result ); + $this->assertArrayHasKey( 'message', $result ); + $this->assertArrayHasKey( 'details', $result ); + // The stub $wpdb returns null for SHOW TABLES, so tables appear missing → ok=false. + $this->assertFalse( $result['ok'] ); + } + + public function test_optimize_query_extracts_hot_clauses(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + $query = new WP_Query( array( + 'post_type' => 'hp_listing', + 'meta_query' => array( + array( 'key' => 'hp_drafted', 'value' => 1, 'compare' => '=' ), + array( 'key' => 'unrelated_key', 'value' => 'foo', 'compare' => '=' ), + ), + ) ); + + $this->adapter->optimize_query( $query ); + + // Hot clause should have been moved to wpdo_hot_clauses. + $hot = $query->get( 'wpdo_hot_clauses' ); + // Note: in the test env the WPDO_Feature_Flags check may short-circuit + // (no module active), so the assertion is "either the clause moved OR + // it stayed put; never silently corrupted". + $remaining = (array) $query->get( 'meta_query' ); + $this->assertIsArray( $remaining ); + // `unrelated_key` must always remain in meta_query regardless of routing. + $found_unrelated = false; + foreach ( $remaining as $clause ) { + if ( is_array( $clause ) && ( $clause['key'] ?? '' ) === 'unrelated_key' ) { + $found_unrelated = true; + break; + } + } + $this->assertTrue( $found_unrelated, 'Non-hot meta_query clause must always be preserved.' ); + // hot_clauses is either populated (when feature flag active) or empty (when not). + $this->assertTrue( $hot === '' || is_array( $hot ) ); + } + + public function test_optimize_query_handles_empty_meta_query(): void { + $query = new WP_Query( array( 'post_type' => 'hp_listing' ) ); + $this->adapter->optimize_query( $query ); + // Must not crash; hot_clauses should remain unset. + $this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) ); + } + + public function test_optimize_search_delegates_to_optimize_query(): void { + $query = new WP_Query( array( 'post_type' => 'hp_listing' ) ); + // Delegation is implementation detail; verify just that it doesn't crash + // and that an irrelevant attribute_fields argument is ignored. + $this->adapter->optimize_search( $query, array() ); + $this->assertEmpty( $query->get( 'wpdo_hot_clauses' ) ); + } +} diff --git a/tests/unit/CronOptimizerTest.php b/tests/unit/CronOptimizerTest.php new file mode 100644 index 0000000..c75be21 --- /dev/null +++ b/tests/unit/CronOptimizerTest.php @@ -0,0 +1,46 @@ +install_wpdb_mock(); + WPDO_HivePress_Cron_Optimizer::reset_for_tests(); + $GLOBALS['_wp_options'][ WPDO_HivePress_Cron_Optimizer::OPTION_ENABLED ] = 0; + } + + public function test_disabled_by_default(): void { + $this->assertFalse( WPDO_HivePress_Cron_Optimizer::is_enabled() ); + } + + public function test_enabled_when_option_set(): void { + $GLOBALS['_wp_options'][ WPDO_HivePress_Cron_Optimizer::OPTION_ENABLED ] = 1; + $this->assertTrue( WPDO_HivePress_Cron_Optimizer::is_enabled() ); + } + + public function test_module_constant(): void { + $this->assertSame( 'hot_hp_listing', WPDO_HivePress_Cron_Optimizer::MODULE ); + } + + public function test_register_is_idempotent(): void { + $GLOBALS['_wp_options'][ WPDO_HivePress_Cron_Optimizer::OPTION_ENABLED ] = 1; + WPDO_HivePress_Cron_Optimizer::register(); + WPDO_HivePress_Cron_Optimizer::register(); // No throw = pass. + $this->assertTrue( true ); + } + + public function test_maybe_run_returns_zero_when_module_inactive(): void { + // Feature_Flags::is_query_active('hot_hp_listing') is false in test env. + $count = WPDO_HivePress_Cron_Optimizer::maybe_run(); + $this->assertSame( 0, $count ); + } +} diff --git a/tests/unit/DetectorTest.php b/tests/unit/DetectorTest.php new file mode 100644 index 0000000..96474a5 --- /dev/null +++ b/tests/unit/DetectorTest.php @@ -0,0 +1,79 @@ +assertSame( array(), $result ); + } + + public function test_short_circuit_caches_negative_result(): void { + WPDO_HivePress_Detector::detect(); + // Second call should hit memo cache; verify identical. + $this->assertSame( array(), WPDO_HivePress_Detector::detect() ); + } + + public function test_catalog_returns_thirteen_known_addons(): void { + $catalog = WPDO_HivePress_Detector::catalog(); + $this->assertCount( 13, $catalog ); + $this->assertArrayHasKey( 'hivepress', $catalog ); + $this->assertArrayHasKey( 'hivepress-bookings', $catalog ); + $this->assertArrayHasKey( 'hivepress-marketplace', $catalog ); + $this->assertArrayHasKey( 'hivepress-statistics', $catalog ); + } + + public function test_is_known_recognises_supported_addons(): void { + $this->assertTrue( WPDO_HivePress_Detector::is_known( 'hivepress' ) ); + $this->assertTrue( WPDO_HivePress_Detector::is_known( 'hivepress-reviews' ) ); + $this->assertFalse( WPDO_HivePress_Detector::is_known( 'hivepress-frobnicator' ) ); + $this->assertFalse( WPDO_HivePress_Detector::is_known( '' ) ); + } + + public function test_bust_cache_clears_memo_and_transient(): void { + WPDO_HivePress_Detector::detect(); + // Seed transient with bogus data to verify bust clears it. + $GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] = array( 'fake' => '9.9.9' ); + WPDO_HivePress_Detector::bust_cache(); + $this->assertArrayNotHasKey( '_transient_wpdo_hivepress_detector_cache', $GLOBALS['_wp_options'] ?? array() ); + } + + public function test_detects_addon_via_active_plugin_signal(): void { + // HivePress addons publish no class/const — they register via the + // `hivepress/v1/extensions` filter and are detected purely by being + // active plugins. Seed active_plugins with core + one addon and assert + // both are detected even though no HivePress classes are declared. + $GLOBALS['_wp_options']['active_plugins'] = array( + 'hivepress/hivepress.php', + 'hivepress-messages/hivepress-messages.php', + ); + WPDO_HivePress_Detector::reset_for_tests(); + unset( $GLOBALS['_wp_options']['_transient_wpdo_hivepress_detector_cache'] ); + + $result = WPDO_HivePress_Detector::detect(); + + $this->assertArrayHasKey( 'hivepress', $result, 'core detected via active plugin' ); + $this->assertArrayHasKey( 'hivepress-messages', $result, 'addon detected via active plugin' ); + $this->assertArrayNotHasKey( 'hivepress-reviews', $result, 'inactive addon not detected' ); + + // Cleanup so other tests observe no active plugins. bust_cache() clears + // BOTH the memo and the seeded transient (reset_for_tests clears only + // the memo) — otherwise the cached detection leaks into later suites. + unset( $GLOBALS['_wp_options']['active_plugins'] ); + WPDO_HivePress_Detector::bust_cache(); + } +} diff --git a/tests/unit/FavoritesAdapterTest.php b/tests/unit/FavoritesAdapterTest.php new file mode 100644 index 0000000..0b74b86 --- /dev/null +++ b/tests/unit/FavoritesAdapterTest.php @@ -0,0 +1,111 @@ +install_wpdb_mock(); + WPDO_Custom_Table_Registry::reset_for_tests(); + $this->adapter = new WPDO_HivePress_Favorites_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-favorites', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Favorites\\Plugin', $this->adapter->detection_class() ); + $this->assertSame( 'HIVEPRESS_FAVORITES_VERSION', $this->adapter->detection_const() ); + } + + public function test_constants_exposed(): void { + $this->assertSame( 'hp_favorite', WPDO_HivePress_Favorites_Adapter::COMMENT_TYPE ); + $this->assertSame( 'wpdo_comment_hp_favorite', WPDO_HivePress_Favorites_Adapter::TABLE ); + } + + public function test_on_register_custom_tables_declares_shadow_table(): void { + $registry = WPDO_Custom_Table_Registry::instance(); + $this->adapter->on_register_custom_tables( $registry ); + + $tables = $registry->all(); + $this->assertCount( 1, $tables ); + + $cfg = reset( $tables ); + $this->assertSame( 'hivepress-favorites', $cfg['provider'] ); + $this->assertSame( 'wpdo_comment_hp_favorite', $cfg['table_name'] ); + $this->assertSame( 'comment_id', $cfg['primary_key'] ); + $this->assertArrayHasKey( 'unique_user_listing', $cfg['indexes'] ); + $this->assertStringContainsString( 'UNIQUE', $cfg['indexes']['unique_user_listing'] ); + } + + public function test_mirror_insert_skips_non_favorite_comments(): void { + // Stub $wpdb tracks queries. + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'comment'; // ← not hp_favorite + $comment->user_id = 1; + $comment->comment_post_ID = 99; + + $this->adapter->mirror_insert( 123, $comment ); + $this->assertSame( array(), $wpdb->queries ); + } + + public function test_mirror_insert_writes_for_hp_favorite(): void { + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'hp_favorite'; + $comment->user_id = 7; + $comment->comment_post_ID = 42; + $comment->comment_date = '2026-05-04 12:00:00'; + + $this->adapter->mirror_insert( 100, $comment ); + + $this->assertCount( 1, $wpdb->queries ); + $this->assertStringContainsString( 'INSERT IGNORE', $wpdb->queries[0] ); + $this->assertStringContainsString( 'wpdo_comment_hp_favorite', $wpdb->queries[0] ); + } + + public function test_mirror_insert_skips_when_user_or_listing_missing(): void { + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'hp_favorite'; + $comment->user_id = 0; + $comment->comment_post_ID = 99; + + $this->adapter->mirror_insert( 100, $comment ); + $this->assertSame( array(), $wpdb->queries ); + } + + public function test_doctor_check_reports_missing_table_in_stub_env(): void { + $result = $this->adapter->doctor_check(); + $this->assertFalse( $result['ok'] ); + $this->assertStringContainsString( 'wpdo_comment_hp_favorite', $result['message'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $score = $this->adapter->suitability_score(); + $this->assertSame( 10.0, $score['aggregate'] ); + } + + public function test_migrations_lists_comment_module(): void { + $this->assertSame( array( 'comment_hp_favorite' ), $this->adapter->migrations() ); + } +} diff --git a/tests/unit/MarketplaceAdapterTest.php b/tests/unit/MarketplaceAdapterTest.php new file mode 100644 index 0000000..e1d592c --- /dev/null +++ b/tests/unit/MarketplaceAdapterTest.php @@ -0,0 +1,53 @@ +install_wpdb_mock(); + $reflect = new ReflectionClass( WPDO_Schema_Registry::class ); + $prop = $reflect->getProperty( 'instance' ); + $prop->setAccessible( true ); + $prop->setValue( null, null ); + + $this->adapter = new WPDO_HivePress_Marketplace_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-marketplace', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Marketplace\\Plugin', $this->adapter->detection_class() ); + } + + public function test_on_register_fields_registers_hot_price_and_warm_counter(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + $price = $registry->get_field( 'hp_listing', 'hp_purchase_price' ); + $this->assertSame( 'hot', $price['zone'] ); + $this->assertTrue( (bool) $price['indexed'] ); + $this->assertStringContainsString( 'decimal', $price['data_type'] ); + + $count = $registry->get_field( 'hp_listing', 'hp_purchase_count' ); + $this->assertSame( 'warm', $count['zone'] ); + } + + public function test_score_aggregates_to_nine_eight_eight(): void { + // D8 = 0.9 → aggregate = (7×1.0 + 0.9)/8 × 10 = 9.875 → 9.88. + $this->assertSame( 9.88, $this->adapter->suitability_score()['aggregate'] ); + } +} diff --git a/tests/unit/MembershipsAdapterTest.php b/tests/unit/MembershipsAdapterTest.php new file mode 100644 index 0000000..fe039a1 --- /dev/null +++ b/tests/unit/MembershipsAdapterTest.php @@ -0,0 +1,73 @@ +getProperty( 'instance' ); + $prop->setAccessible( true ); + $prop->setValue( null, null ); + + $this->adapter = new WPDO_HivePress_Memberships_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-memberships', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Memberships\\Plugin', $this->adapter->detection_class() ); + $this->assertSame( 'HIVEPRESS_MEMBERSHIPS_VERSION', $this->adapter->detection_const() ); + $this->assertSame( '2.0.0', $this->adapter->minimum_addon_version() ); + } + + public function test_on_register_fields_registers_three_hot_fields(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + // Plan fields. + $expire_period = $registry->get_field( 'hp_membership_plan', 'hp_expire_period' ); + $this->assertSame( 'hot', $expire_period['zone'] ); + $this->assertStringContainsString( 'int', $expire_period['data_type'] ); + + $primary = $registry->get_field( 'hp_membership_plan', 'hp_primary' ); + $this->assertSame( 'hot', $primary['zone'] ); + $this->assertTrue( (bool) $primary['indexed'] ); + + // Membership field — the hottest path. + $expired = $registry->get_field( 'hp_membership', 'hp_expired_time' ); + $this->assertSame( 'hot', $expired['zone'] ); + $this->assertTrue( (bool) $expired['indexed'] ); + $this->assertStringContainsString( 'bigint', $expired['data_type'] ); + } + + public function test_doctor_check_reports_missing_tables_in_stub_env(): void { + $result = $this->adapter->doctor_check(); + $this->assertFalse( $result['ok'] ); + $this->assertArrayHasKey( 'hp_membership_plan', $result['details'] ); + $this->assertArrayHasKey( 'hp_membership', $result['details'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $score = $this->adapter->suitability_score(); + $this->assertSame( 10.0, $score['aggregate'] ); + } + + public function test_migrations_lists_both_hot_modules(): void { + $mods = $this->adapter->migrations(); + $this->assertContains( 'hot_hp_membership_plan', $mods ); + $this->assertContains( 'hot_hp_membership', $mods ); + } +} diff --git a/tests/unit/MessagesAdapterTest.php b/tests/unit/MessagesAdapterTest.php new file mode 100644 index 0000000..ab59f5c --- /dev/null +++ b/tests/unit/MessagesAdapterTest.php @@ -0,0 +1,104 @@ +install_wpdb_mock(); + WPDO_Custom_Table_Registry::reset_for_tests(); + $this->adapter = new WPDO_HivePress_Messages_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-messages', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Messages\\Plugin', $this->adapter->detection_class() ); + $this->assertSame( 'hp_message', WPDO_HivePress_Messages_Adapter::COMMENT_TYPE ); + $this->assertSame( 'wpdo_comment_hp_message', WPDO_HivePress_Messages_Adapter::TABLE ); + } + + public function test_on_register_custom_tables_declares_indexed_recipient(): void { + $registry = WPDO_Custom_Table_Registry::instance(); + $this->adapter->on_register_custom_tables( $registry ); + + $tables = $registry->all(); + $cfg = reset( $tables ); + $this->assertSame( 'wpdo_comment_hp_message', $cfg['table_name'] ); + $this->assertArrayHasKey( 'recipient_id', $cfg['expected_columns'] ); + $this->assertArrayHasKey( 'idx_recipient_unread', $cfg['indexes'] ); + $this->assertStringContainsString( 'recipient_id, is_read', $cfg['indexes']['idx_recipient_unread'] ); + } + + public function test_mirror_insert_skips_non_message_comments(): void { + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'comment'; + $comment->user_id = 1; + $comment->comment_karma = 5; + + $this->adapter->mirror_insert( 1, $comment ); + $this->assertSame( array(), $wpdb->queries ); + } + + public function test_mirror_insert_promotes_recipient_from_comment_karma(): void { + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'hp_message'; + $comment->user_id = 7; // sender + $comment->comment_karma = 42; // ← recipient hack lives here + $comment->comment_post_ID = 99; + $comment->comment_approved = 0; + $comment->comment_date = '2026-05-04 12:00:00'; + + $this->adapter->mirror_insert( 100, $comment ); + + $this->assertCount( 1, $wpdb->queries ); + $sql = $wpdb->queries[0]; + $this->assertStringContainsString( 'INSERT INTO', $sql ); + $this->assertStringContainsString( 'wpdo_comment_hp_message', $sql ); + // The promoted recipient_id should appear in the bound SQL. + $this->assertStringContainsString( '42', $sql ); + } + + public function test_mirror_insert_skips_when_sender_or_recipient_zero(): void { + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'hp_message'; + $comment->user_id = 0; // bad sender + $comment->comment_karma = 42; + $comment->comment_post_ID = 99; + + $this->adapter->mirror_insert( 100, $comment ); + $this->assertSame( array(), $wpdb->queries ); + } + + public function test_doctor_check_reports_missing_table_in_stub_env(): void { + $result = $this->adapter->doctor_check(); + $this->assertFalse( $result['ok'] ); + $this->assertStringContainsString( 'wpdo_comment_hp_message', $result['message'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] ); + } +} diff --git a/tests/unit/RequestsAdapterTest.php b/tests/unit/RequestsAdapterTest.php new file mode 100644 index 0000000..276629e --- /dev/null +++ b/tests/unit/RequestsAdapterTest.php @@ -0,0 +1,101 @@ +install_wpdb_mock(); + $reflect = new ReflectionClass( WPDO_Schema_Registry::class ); + $prop = $reflect->getProperty( 'instance' ); + $prop->setAccessible( true ); + $prop->setValue( null, null ); + WPDO_Custom_Table_Registry::reset_for_tests(); + + $this->adapter = new WPDO_HivePress_Requests_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-requests', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Requests\\Plugin', $this->adapter->detection_class() ); + $this->assertSame( 'hp_offer', WPDO_HivePress_Requests_Adapter::OFFER_COMMENT_TYPE ); + } + + public function test_on_register_fields_registers_request_hot_columns(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + $drafted = $registry->get_field( 'hp_request', 'hp_drafted' ); + $this->assertSame( 'hot', $drafted['zone'] ); + $this->assertTrue( (bool) $drafted['indexed'] ); + + $expired = $registry->get_field( 'hp_request', 'hp_expired_time' ); + $this->assertSame( 'hot', $expired['zone'] ); + $this->assertTrue( (bool) $expired['indexed'] ); + } + + public function test_on_register_custom_tables_declares_offer_shadow(): void { + $registry = WPDO_Custom_Table_Registry::instance(); + $this->adapter->on_register_custom_tables( $registry ); + + $tables = $registry->all(); + $cfg = reset( $tables ); + $this->assertSame( 'wpdo_comment_hp_offer', $cfg['table_name'] ); + $this->assertSame( 'hp_request', $cfg['post_type_link'] ); + $this->assertArrayHasKey( 'idx_request_approved', $cfg['indexes'] ); + } + + public function test_mirror_offer_insert_skips_other_comment_types(): void { + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'comment'; // ← not hp_offer + $comment->comment_post_ID = 99; + $comment->user_id = 1; + + $this->adapter->mirror_offer_insert( 1, $comment ); + $this->assertSame( array(), $wpdb->queries ); + } + + public function test_mirror_offer_insert_writes_for_hp_offer(): void { + global $wpdb; + $wpdb->queries = array(); + + $comment = new stdClass(); + $comment->comment_type = 'hp_offer'; + $comment->comment_post_ID = 42; + $comment->user_id = 7; + $comment->comment_approved = 0; + $comment->comment_date = '2026-05-04 12:00:00'; + + $this->adapter->mirror_offer_insert( 100, $comment ); + $this->assertCount( 1, $wpdb->queries ); + $this->assertStringContainsString( 'INSERT INTO', $wpdb->queries[0] ); + $this->assertStringContainsString( 'wpdo_comment_hp_offer', $wpdb->queries[0] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] ); + } + + public function test_migrations_lists_request_and_offer_modules(): void { + $mods = $this->adapter->migrations(); + $this->assertContains( 'hot_hp_request', $mods ); + $this->assertContains( 'comment_hp_offer', $mods ); + } +} diff --git a/tests/unit/RestTest.php b/tests/unit/RestTest.php new file mode 100644 index 0000000..96d0c3e --- /dev/null +++ b/tests/unit/RestTest.php @@ -0,0 +1,79 @@ +rest = new WPDO_HivePress_REST(); + } + + public function test_namespace_constant(): void { + $this->assertSame( 'wpdo/v1', WPDO_HivePress_REST::NAMESPACE ); + } + + public function test_register_routes_does_not_throw(): void { + // register_rest_route is stubbed in tests/bootstrap.php → returns true. + $this->rest->register_routes(); + $this->assertTrue( true ); + } + + public function test_check_permission_returns_true_for_admin(): void { + $this->assertTrue( $this->rest->check_permission() ); + } + + public function test_check_permission_returns_error_for_non_admin(): void { + $GLOBALS['_wp_current_user_can']['manage_options'] = false; + $result = $this->rest->check_permission(); + $this->assertInstanceOf( WP_Error::class, $result ); + } + + public function test_get_status_returns_addon_catalog(): void { + $response = $this->rest->get_status(); + $this->assertInstanceOf( WP_REST_Response::class, $response ); + $data = $response->get_data(); + + $this->assertArrayHasKey( 'addons', $data ); + $this->assertArrayHasKey( 'conflicts', $data ); + $this->assertArrayHasKey( 'totals', $data ); + // Catalog should list all 13 known addons regardless of detection state. + $this->assertSame( 13, $data['totals']['catalog'] ); + // Without HivePress installed, detected = 0. + $this->assertSame( 0, $data['totals']['detected'] ); + } + + public function test_get_score_returns_report_structure(): void { + $response = $this->rest->get_score(); + $this->assertInstanceOf( WP_REST_Response::class, $response ); + $data = $response->get_data(); + + $this->assertArrayHasKey( 'aggregate', $data ); + $this->assertArrayHasKey( 'adapter_count', $data ); + $this->assertArrayHasKey( 'per_dimension', $data ); + $this->assertArrayHasKey( 'adapters', $data ); + } + + public function test_get_health_returns_ok_when_no_adapters_active(): void { + $response = $this->rest->get_health(); + $this->assertInstanceOf( WP_REST_Response::class, $response ); + $data = $response->get_data(); + + $this->assertArrayHasKey( 'ok', $data ); + $this->assertArrayHasKey( 'probes', $data ); + // No active adapters → vacuously ok. + $this->assertTrue( $data['ok'] ); + $this->assertSame( array(), $data['probes'] ); + } +} diff --git a/tests/unit/ReviewsAdapterTest.php b/tests/unit/ReviewsAdapterTest.php new file mode 100644 index 0000000..8b28831 --- /dev/null +++ b/tests/unit/ReviewsAdapterTest.php @@ -0,0 +1,52 @@ +adapter = new WPDO_HivePress_Reviews_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-reviews', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Reviews\\Plugin', $this->adapter->detection_class() ); + $this->assertSame( 'HIVEPRESS_REVIEWS_VERSION', $this->adapter->detection_const() ); + $this->assertSame( '1.4.0', $this->adapter->minimum_addon_version() ); + } + + public function test_comment_type_constant_exposed(): void { + $this->assertSame( 'hp_review', WPDO_HivePress_Reviews_Adapter::COMMENT_TYPE ); + } + + public function test_doctor_check_reports_missing_table_in_stub_env(): void { + $result = $this->adapter->doctor_check(); + $this->assertIsArray( $result ); + $this->assertArrayHasKey( 'ok', $result ); + // Stub $wpdb returns null for SHOW TABLES → ok=false. + $this->assertFalse( $result['ok'] ); + $this->assertStringContainsString( 'wpdo_comment_hp_review', $result['message'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $score = $this->adapter->suitability_score(); + $this->assertSame( 10.0, $score['aggregate'] ); + } + + public function test_no_fsm_modules_owned(): void { + // Comment entity-bridge owns lifecycle; adapter is read-only. + $this->assertSame( array(), $this->adapter->migrations() ); + } +} diff --git a/tests/unit/SeoAdapterTest.php b/tests/unit/SeoAdapterTest.php new file mode 100644 index 0000000..c3db347 --- /dev/null +++ b/tests/unit/SeoAdapterTest.php @@ -0,0 +1,37 @@ +adapter = new WPDO_HivePress_Seo_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-seo', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Seo\\Plugin', $this->adapter->detection_class() ); + } + + public function test_doctor_check_is_always_ok(): void { + $result = $this->adapter->doctor_check(); + $this->assertTrue( $result['ok'] ); + $this->assertStringContainsString( 'filter-based', $result['message'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] ); + } +} diff --git a/tests/unit/SocialLinksAdapterTest.php b/tests/unit/SocialLinksAdapterTest.php new file mode 100644 index 0000000..b14b543 --- /dev/null +++ b/tests/unit/SocialLinksAdapterTest.php @@ -0,0 +1,57 @@ +getProperty( 'instance' ); + $prop->setAccessible( true ); + $prop->setValue( null, null ); + + $this->adapter = new WPDO_HivePress_Social_Links_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-social-links', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\SocialLinks\\Plugin', $this->adapter->detection_class() ); + } + + public function test_on_register_fields_registers_seven_cold_fields(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + // All seven social URL fields should land in cold zone on hp_vendor. + $facebook = $registry->get_field( 'hp_vendor', 'hp_facebook_url' ); + $this->assertSame( 'cold', $facebook['zone'] ); + + $instagram = $registry->get_field( 'hp_vendor', 'hp_instagram_url' ); + $this->assertSame( 'cold', $instagram['zone'] ); + + $whatsapp = $registry->get_field( 'hp_vendor', 'hp_whatsapp_url' ); + $this->assertSame( 'cold', $whatsapp['zone'] ); + } + + public function test_doctor_check_reports_field_count(): void { + $result = $this->adapter->doctor_check(); + $this->assertTrue( $result['ok'] ); + $this->assertStringContainsString( '7 cold fields', $result['message'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] ); + } +} diff --git a/tests/unit/StatisticsAdapterTest.php b/tests/unit/StatisticsAdapterTest.php new file mode 100644 index 0000000..1163a27 --- /dev/null +++ b/tests/unit/StatisticsAdapterTest.php @@ -0,0 +1,50 @@ +install_wpdb_mock(); + $reflect = new ReflectionClass( WPDO_Schema_Registry::class ); + $prop = $reflect->getProperty( 'instance' ); + $prop->setAccessible( true ); + $prop->setValue( null, null ); + + $this->adapter = new WPDO_HivePress_Statistics_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-statistics', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Statistics\\Plugin', $this->adapter->detection_class() ); + } + + public function test_on_register_fields_registers_warm_counters(): void { + $registry = WPDO_Schema_Registry::instance(); + $this->adapter->on_register_fields( $registry ); + + $views = $registry->get_field( 'hp_listing', 'hp_view_count' ); + $this->assertSame( 'warm', $views['zone'] ); + + $clicks = $registry->get_field( 'hp_listing', 'hp_click_count' ); + $this->assertSame( 'warm', $clicks['zone'] ); + } + + public function test_score_aggregates_to_nine_eight_eight(): void { + $this->assertSame( 9.88, $this->adapter->suitability_score()['aggregate'] ); + } +} diff --git a/tests/unit/SuitabilityScorerTest.php b/tests/unit/SuitabilityScorerTest.php new file mode 100644 index 0000000..88bf606 --- /dev/null +++ b/tests/unit/SuitabilityScorerTest.php @@ -0,0 +1,76 @@ +assertCount( 8, WPDO_HivePress_Suitability_Scorer::DIMENSIONS ); + $this->assertContains( 'd1_meta_calls', WPDO_HivePress_Suitability_Scorer::DIMENSIONS ); + $this->assertContains( 'd8_perf', WPDO_HivePress_Suitability_Scorer::DIMENSIONS ); + } + + public function test_compute_aggregate_with_empty_input_returns_zero(): void { + $this->assertSame( 0.0, WPDO_HivePress_Suitability_Scorer::compute_aggregate( array() ) ); + } + + public function test_compute_aggregate_averages_adapter_scores(): void { + $scores = array( + 'a' => array( 'aggregate' => 10.0 ), + 'b' => array( 'aggregate' => 9.0 ), + 'c' => array( 'aggregate' => 8.0 ), + ); + // (10 + 9 + 8) / 3 = 9.0 + $this->assertSame( 9.0, WPDO_HivePress_Suitability_Scorer::compute_aggregate( $scores ) ); + } + + public function test_compute_per_dimension_averages_each_dimension(): void { + $scores = array( + 'a' => array( + 'd1_meta_calls' => 1.0, 'd2_meta_sql' => 1.0, 'd3_table_coverage' => 1.0, + 'd4_registry_meta' => 1.0, 'd5_hook_bus' => 1.0, 'd6_options' => 1.0, + 'd7_coupling' => 1.0, 'd8_perf' => 0.9, + ), + 'b' => array( + 'd1_meta_calls' => 1.0, 'd2_meta_sql' => 1.0, 'd3_table_coverage' => 1.0, + 'd4_registry_meta' => 1.0, 'd5_hook_bus' => 1.0, 'd6_options' => 1.0, + 'd7_coupling' => 1.0, 'd8_perf' => 1.0, + ), + ); + $dim = WPDO_HivePress_Suitability_Scorer::compute_per_dimension( $scores ); + + // d1-d7 are 1.0 across both adapters → average 1.0. + $this->assertSame( 1.0, $dim['d1_meta_calls'] ); + $this->assertSame( 1.0, $dim['d7_coupling'] ); + + // d8: (0.9 + 1.0) / 2 = 0.95. + $this->assertSame( 0.95, $dim['d8_perf'] ); + } + + public function test_compute_per_dimension_with_empty_input_returns_all_zero(): void { + $dim = WPDO_HivePress_Suitability_Scorer::compute_per_dimension( array() ); + foreach ( WPDO_HivePress_Suitability_Scorer::DIMENSIONS as $d ) { + $this->assertSame( 0.0, $dim[ $d ] ); + } + } + + public function test_report_with_no_active_adapters(): void { + // Bootstrap not booted in unit env → adapters() = empty. + WPDO_HivePress_Bootstrap::reset_for_tests(); + $report = WPDO_HivePress_Suitability_Scorer::report(); + + $this->assertSame( 0, $report['adapter_count'] ); + $this->assertSame( 0.0, $report['aggregate'] ); + $this->assertSame( array(), $report['adapters'] ); + } + + public function test_adapters_below_threshold_returns_empty_for_unknown_dimension(): void { + $this->assertSame( array(), WPDO_HivePress_Suitability_Scorer::adapters_below_threshold( 'd99_garbage' ) ); + } +} diff --git a/tests/unit/TagsAdapterTest.php b/tests/unit/TagsAdapterTest.php new file mode 100644 index 0000000..4682a39 --- /dev/null +++ b/tests/unit/TagsAdapterTest.php @@ -0,0 +1,43 @@ +adapter = new WPDO_HivePress_Tags_Adapter(); + } + + public function test_implements_adapter_interface(): void { + $this->assertInstanceOf( WPDO_HivePress_Adapter::class, $this->adapter ); + } + + public function test_identity(): void { + $this->assertSame( 'hivepress-tags', $this->adapter->plugin_slug() ); + $this->assertSame( 'HivePress\\Tags\\Plugin', $this->adapter->detection_class() ); + $this->assertSame( 'HIVEPRESS_TAGS_VERSION', $this->adapter->detection_const() ); + $this->assertSame( '1.1.0', $this->adapter->minimum_addon_version() ); + } + + public function test_doctor_check_is_always_ok(): void { + $result = $this->adapter->doctor_check(); + $this->assertTrue( $result['ok'] ); + $this->assertStringContainsString( 'term-based', $result['message'] ); + } + + public function test_score_aggregates_to_perfect_ten(): void { + $this->assertSame( 10.0, $this->adapter->suitability_score()['aggregate'] ); + } + + public function test_no_fsm_modules_owned(): void { + $this->assertSame( array(), $this->adapter->migrations() ); + } +} diff --git a/tests/unit/WpdbMockTrait.php b/tests/unit/WpdbMockTrait.php new file mode 100644 index 0000000..e5ec071 --- /dev/null +++ b/tests/unit/WpdbMockTrait.php @@ -0,0 +1,79 @@ + */ + public array $queries = array(); + + public function prepare( string $sql, ...$args ): string { + $i = 0; + return (string) preg_replace_callback( + '/%[sd]/', + static function () use ( &$i, $args ) { + return $args[ $i++ ] ?? '?'; + }, + $sql + ); + } + + public function query( string $sql ): int|bool { + $this->queries[] = $sql; + return 1; + } + + public function get_var( string $sql ): ?string { + return null; + } + + public function get_col( string $sql ): array { + return array(); + } + + public function get_results( string $sql, $output = OBJECT ): array { + return array(); + } + + public function insert( string $table, array $data, $format = null ): int|false { + return 1; + } + + public function update( string $table, array $data, array $where, $format = null, $where_format = null ): int|false { + return 1; + } + + public function delete( string $table, array $where, $format = null ): int|false { + return 1; + } + }; + + // Reset Feature_Flags state so is_query_active() returns false in tests. + $GLOBALS['_wp_options']['wpdo_features'] = array(); + } +}