diff --git a/.github/workflows/js-unit-tests.yml b/.github/workflows/js-unit-tests.yml index 13e97b7bb8..b29d6495d8 100644 --- a/.github/workflows/js-unit-tests.yml +++ b/.github/workflows/js-unit-tests.yml @@ -24,7 +24,21 @@ jobs: with: node-version: "22" - # The tests in tests/js only import Node built-ins and dependency-free - # modules from js/src, so no npm install is needed. + # Some tests parse plugin source with @babel/core, so install dev dependencies. + - name: Cache dependencies + id: cache-npm + uses: actions/cache@v5 + with: + path: | + ~/.npm + node_modules + key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node-modules- + + - name: Install dependencies + if: steps.cache-npm.outputs.cache-hit != 'true' + run: npm ci --include=dev --legacy-peer-deps + - name: Run JS unit tests run: node --test tests/js/*.test.mjs diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 8ecf445e11..6426bc2ff7 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -19,6 +19,20 @@ jobs: env: WP_DEVELOP_DIR: /tmp/wordpress/ WP_MULTISITE: ${{ matrix.multisite }} + # The ubuntu-26.04 runner image no longer ships MySQL, so run it as a service. + services: + mysql: + image: mysql:8.4 + env: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: wordpress_tests + ports: + - 3306:3306 + options: >- + --health-cmd="mysqladmin ping -h 127.0.0.1 -proot" + --health-interval=5s + --health-timeout=5s + --health-retries=20 strategy: matrix: include: @@ -72,6 +86,7 @@ jobs: sed -i "s/youremptytestdbnamehere/wordpress_tests/" wp-tests-config.php sed -i "s/yourusernamehere/root/" wp-tests-config.php sed -i "s/yourpasswordhere/root/" wp-tests-config.php + sed -i "s/'localhost'/'127.0.0.1'/" wp-tests-config.php - name: Read WP core's required Node version id: wp-node @@ -89,10 +104,6 @@ jobs: cd /tmp/wordpress npm ci npm run build:dev - - name: Creating database - run: | - sudo /etc/init.d/mysql start - mysql -u root -proot -e "CREATE DATABASE wordpress_tests;" - name: PhpUnit tests run: | cd "/tmp/wordpress/src/wp-content/plugins/formidable" diff --git a/classes/helpers/FrmBuilderPreviewHelper.php b/classes/helpers/FrmBuilderPreviewHelper.php index ff5afbd1c5..b881a05c3b 100644 --- a/classes/helpers/FrmBuilderPreviewHelper.php +++ b/classes/helpers/FrmBuilderPreviewHelper.php @@ -94,7 +94,7 @@ public static function supports( $field, $field_obj, $display ) { * @param bool $supported Whether the preview can be drawn in the browser. * @param object $field The field being added to the form builder. */ - return apply_filters( 'frm_builder_preview_in_browser', true, $field ); + return (bool) apply_filters( 'frm_builder_preview_in_browser', true, $field ); } /** diff --git a/classes/models/FrmFormApi.php b/classes/models/FrmFormApi.php index a94cc370aa..e0d7822b72 100644 --- a/classes/models/FrmFormApi.php +++ b/classes/models/FrmFormApi.php @@ -281,7 +281,7 @@ protected function api_url() { * @return bool */ protected function include_boosts() { - return str_starts_with( (string) $this->cache_key, 'frm_addons_l' ); + return str_starts_with( $this->cache_key, 'frm_addons_l' ); } /** diff --git a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveUselessParenthesesSniff.php b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveUselessParenthesesSniff.php index 7fc7cf2b15..a4e7c848c2 100644 --- a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveUselessParenthesesSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveUselessParenthesesSniff.php @@ -57,7 +57,7 @@ class RemoveUselessParenthesesSniff implements Sniff { /** * The Slevomat sniff instance, false when it is not installed, null until first use. * - * @var object|false|null + * @var false|object|null */ private $inner; @@ -102,7 +102,7 @@ public function process( File $phpcsFile, $stackPtr ) { /** * Load the Slevomat sniff once. * - * @return object|false + * @return false|object */ private function getInner() { if ( null === $this->inner ) { diff --git a/stubs.php b/stubs.php index 28c377451e..889f6eac8a 100644 --- a/stubs.php +++ b/stubs.php @@ -323,6 +323,17 @@ public static function mod_other_vals( $values = false, $location = 'front' ) { } class FrmProEntryFormatter extends FrmEntryFormatter { } + class FrmProFieldText extends FrmFieldText { + } + class FrmProCopy { + /** + * @param bool $force Whether to run the install even when the database is current. + * + * @return void + */ + public static function install( $force = false ) { + } + } /** * The Registration add-on's entry controller. Constructing it is what lets a * registration form's entry run its user creation, and the abilities do that @@ -858,6 +869,28 @@ function wp_get_ability_category( $slug ) { */ function wp_unregister_ability( $name ) { } + /** + * @param string $slug Category slug. + * + * @return WP_Ability_Category|null + */ + function wp_unregister_ability_category( $slug ) { + } + /** + * Added in WordPress 6.1, after the wordpress-stubs release this plugin pins. + * + * @param string $hook_name The name of the filter hook. + * + * @return int The number of times the filter hook has been applied. + */ + function did_filter( $hook_name ) { + } + /** + * The exception the WordPress test suite throws from wp_die(). Tests catch it, + * and mago analyzes the tests without loading the WordPress test library. + */ + class WPDieException extends Exception { + } } /** diff --git a/tests/js/paypalBootstrap.test.mjs b/tests/js/paypalBootstrap.test.mjs index eddb42081f..3756778ec9 100644 --- a/tests/js/paypalBootstrap.test.mjs +++ b/tests/js/paypalBootstrap.test.mjs @@ -56,6 +56,18 @@ test( 'PayPal initializes once on DOMContentLoaded when the script runs while lo assert.equal( state.initRuns, 1 ); } ); +/** + * Let pending microtasks run until a condition holds, giving up after a few ticks. + * + * @param {Function} isDone Returns true once the awaited state is reached. + * @return {Promise} Resolves when the condition holds or the tick limit is reached. + */ +async function flushMicrotasksUntil( isDone ) { + for ( let tick = 0; tick < 10 && ! isDone(); tick++ ) { + await Promise.resolve(); + } +} + /** * Exercise the real initializer while wallet discovery is still waiting. * @@ -101,14 +113,10 @@ async function checkOverlappingInitialization() { }; runInNewContext( `let thisForm, cardElementContainer, isRecurring, selectedMethod; let initRunId = 0;${ source.slice( start, end ) }`, context ); const first = context.paypalInit(); - for ( let tick = 0; tick < 10 && waiting < 1; tick++ ) { - await Promise.resolve(); - } + await flushMicrotasksUntil( () => waiting >= 1 ); assert.equal( waiting, 1 ); const second = context.paypalInit(); - for ( let tick = 0; tick < 10 && waiting < 2; tick++ ) { - await Promise.resolve(); - } + await flushMicrotasksUntil( () => waiting >= 2 ); assert.equal( waiting, 2 ); resume(); await Promise.all( [ first, second ] ); diff --git a/tests/phpunit/fields/test_FrmFieldAddress.php b/tests/phpunit/fields/test_FrmFieldAddress.php index 0848a343c1..d5fa1bc776 100644 --- a/tests/phpunit/fields/test_FrmFieldAddress.php +++ b/tests/phpunit/fields/test_FrmFieldAddress.php @@ -368,7 +368,7 @@ public function test_validate_checks_us_zip_format( $zip, $is_valid ) { } /** - * @return \Iterator | string)>> + * @return \Iterator */ public static function us_zip_provider() { yield 'five digits' => array( '62704', true ); @@ -603,7 +603,7 @@ public function test_address_string_to_array( $value, $expected ) { } /** - * @return \Iterator> + * @return \Iterator | string | null), array}> */ public static function address_string_provider() { yield 'six parts map in order' => array( diff --git a/tests/phpunit/fields/test_FrmFieldCombo.php b/tests/phpunit/fields/test_FrmFieldCombo.php index 79f3a99a95..e42e9614c9 100644 --- a/tests/phpunit/fields/test_FrmFieldCombo.php +++ b/tests/phpunit/fields/test_FrmFieldCombo.php @@ -312,8 +312,8 @@ public function test_get_export_headings() { ) ); - $field_id = $field['id']; - $field_name = $field['name']; + $field_id = (int) $field['id']; + $field_name = (string) $field['name']; $this->set_private_property( $combo_field, 'field', $field ); diff --git a/tests/phpunit/fields/test_FrmFieldsController.php b/tests/phpunit/fields/test_FrmFieldsController.php index ca18a3f755..37e9d795b7 100644 --- a/tests/phpunit/fields/test_FrmFieldsController.php +++ b/tests/phpunit/fields/test_FrmFieldsController.php @@ -225,7 +225,13 @@ public function test_builder_settings_remain_inline_without_deferred_collector() } public function test_builder_placeholder_manifest_preserves_attributes_and_order() { - $form_id = $this->factory->form->create(); + $form_id = $this->factory->form->create(); + + /** + * Filled in by FrmFieldsController::load_single_field() as each placeholder renders. + * + * @var stdClass $manifest + */ $manifest = (object) array( 'definitions' => array(), 'fields' => array(), diff --git a/tests/phpunit/misc/test_FrmAddon.php b/tests/phpunit/misc/test_FrmAddon.php index f3b1fa527e..f8152d2ef0 100644 --- a/tests/phpunit/misc/test_FrmAddon.php +++ b/tests/phpunit/misc/test_FrmAddon.php @@ -103,7 +103,7 @@ public function test_checked_recently() { * * @param mixed $payload What the API request should come back with. * - * @return \PHPUnit\Framework\MockObject\MockObject + * @return FrmTestAddon&\PHPUnit\Framework\MockObject\MockObject */ private function get_licensed_addon( $payload ) { $addon = $this->getMockBuilder( 'FrmTestAddon' ) diff --git a/tests/phpunit/phpunit-attributes-stubs.php b/tests/phpunit/phpunit-attributes-stubs.php index 6b28835272..d3b49287d7 100644 --- a/tests/phpunit/phpunit-attributes-stubs.php +++ b/tests/phpunit/phpunit-attributes-stubs.php @@ -2,44 +2,71 @@ declare(strict_types=1); -namespace PHPUnit\Framework\Attributes; - /** - * Attribute declarations for analysis jobs that do not install PHPUnit. + * PHPUnit declarations for analysis jobs that do not install PHPUnit. */ -#[\Attribute( \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )] -class Group { +namespace PHPUnit\Framework\Attributes { + + #[\Attribute( \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )] + class Group { + + /** + * @param string $name The group name. + */ + public function __construct( $name ) { + } + } + + #[\Attribute( \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )] + class DataProvider { + + /** + * @param string $methodName The static provider method name. + */ + public function __construct( $methodName ) { + } + } + + #[\Attribute( \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE )] + class CoversClass { + + /** + * @param class-string $className The covered class name. + */ + public function __construct( $className ) { + } + } /** - * @param string $name The group name. + * Marks tests whose target is outside the code coverage source directories. */ - public function __construct( $name ) { + #[\Attribute( \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD )] + class CoversNothing { } -} - -#[\Attribute( \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE )] -class DataProvider { /** - * @param string $methodName The static provider method name. + * Runs a test, or every test in a class, in its own PHP process. */ - public function __construct( $methodName ) { + #[\Attribute( \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD )] + class RunInSeparateProcess { + } + + #[\Attribute( \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD )] + class PreserveGlobalState { + + /** + * @param bool $enabled Whether the parent process globals are copied into the child process. + */ + public function __construct( $enabled ) { + } } } -#[\Attribute( \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE )] -class CoversClass { +namespace PHPUnit\Framework { /** - * @param class-string $className The covered class name. + * Thrown when an assertion fails. */ - public function __construct( $className ) { + class ExpectationFailedException extends \Exception { } } - -/** - * Marks tests whose target is outside the code coverage source directories. - */ -#[\Attribute( \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD )] -class CoversNothing { -}