|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Smoke test for ability classes instantiated before WP_Ability is loaded. |
| 4 | + * |
| 5 | + * Run: php tests/smoke-deferred-ability-registration.php |
| 6 | + */ |
| 7 | + |
| 8 | +declare( strict_types=1 ); |
| 9 | + |
| 10 | +namespace DataMachine\Abilities { |
| 11 | + class PermissionHelper { |
| 12 | + public static function can_manage(): bool { |
| 13 | + return true; |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +namespace DataMachineCode\Workspace { |
| 19 | + class Workspace { |
| 20 | + public const ARTIFACT_CLEANUP_DEFAULT_LIMIT = 100; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +namespace { |
| 25 | + if ( ! defined( 'ABSPATH' ) ) { |
| 26 | + define( 'ABSPATH', sys_get_temp_dir() . '/data-machine-code-deferred-ability-registration/' ); |
| 27 | + } |
| 28 | + |
| 29 | + $GLOBALS['datamachine_code_registered_abilities'] = array(); |
| 30 | + $GLOBALS['datamachine_code_added_actions'] = array(); |
| 31 | + |
| 32 | + function wp_register_ability( string $name, array $definition ): void { |
| 33 | + $GLOBALS['datamachine_code_registered_abilities'][ $name ] = $definition; |
| 34 | + } |
| 35 | + |
| 36 | + function doing_action( string $hook ): bool { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + function did_action( string $hook ): int { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + function add_action( string $hook, callable $callback, int $priority = 10 ): void { |
| 45 | + $GLOBALS['datamachine_code_added_actions'][] = compact( 'hook', 'callback', 'priority' ); |
| 46 | + } |
| 47 | + |
| 48 | + require __DIR__ . '/../inc/Abilities/WorkspaceAbilities.php'; |
| 49 | + require __DIR__ . '/../inc/Abilities/CodeTaskAbilities.php'; |
| 50 | + |
| 51 | + new \DataMachineCode\Abilities\WorkspaceAbilities(); |
| 52 | + new \DataMachineCode\Abilities\CodeTaskAbilities(); |
| 53 | + |
| 54 | + $failures = array(); |
| 55 | + $assert = static function ( string $label, bool $condition ) use ( &$failures ): void { |
| 56 | + if ( $condition ) { |
| 57 | + echo " ok {$label}\n"; |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + $failures[] = $label; |
| 62 | + echo " fail {$label}\n"; |
| 63 | + }; |
| 64 | + |
| 65 | + echo "Data Machine Code deferred ability registration - smoke\n"; |
| 66 | + |
| 67 | + $assert( 'constructors defer when WP_Ability is unavailable', 2 === count( $GLOBALS['datamachine_code_added_actions'] ) ); |
| 68 | + $assert( 'constructors do not register before WP_Ability is available', array() === $GLOBALS['datamachine_code_registered_abilities'] ); |
| 69 | + |
| 70 | + class WP_Ability {} |
| 71 | + |
| 72 | + foreach ( $GLOBALS['datamachine_code_added_actions'] as $action ) { |
| 73 | + if ( 'wp_abilities_api_init' === $action['hook'] ) { |
| 74 | + $action['callback'](); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + $assert( 'workspace-show registers on deferred wp_abilities_api_init', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/workspace-show'] ) ); |
| 79 | + $assert( 'workspace-worktree-add registers on deferred wp_abilities_api_init', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/workspace-worktree-add'] ) ); |
| 80 | + $assert( 'create-code-task registers on deferred wp_abilities_api_init', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/create-code-task'] ) ); |
| 81 | + |
| 82 | + if ( ! empty( $failures ) ) { |
| 83 | + echo "\nFAIL: " . count( $failures ) . " assertion(s) failed\n"; |
| 84 | + exit( 1 ); |
| 85 | + } |
| 86 | + |
| 87 | + echo "\nOK\n"; |
| 88 | +} |
0 commit comments