|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Smoke test for ability classes instantiated after wp_abilities_api_init. |
| 4 | + * |
| 5 | + * Run: php tests/smoke-late-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-late-ability-registration/' ); |
| 27 | + } |
| 28 | + |
| 29 | + if ( ! class_exists( 'WP_Ability' ) ) { |
| 30 | + class WP_Ability {} |
| 31 | + } |
| 32 | + |
| 33 | + $GLOBALS['datamachine_code_registered_abilities'] = array(); |
| 34 | + $GLOBALS['datamachine_code_added_actions'] = array(); |
| 35 | + |
| 36 | + function wp_register_ability( string $name, array $definition ): void { |
| 37 | + $GLOBALS['datamachine_code_registered_abilities'][ $name ] = $definition; |
| 38 | + } |
| 39 | + |
| 40 | + function doing_action( string $hook ): bool { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + function did_action( string $hook ): int { |
| 45 | + return 'wp_abilities_api_init' === $hook ? 1 : 0; |
| 46 | + } |
| 47 | + |
| 48 | + function add_action( string $hook, callable $callback, int $priority = 10 ): void { |
| 49 | + $GLOBALS['datamachine_code_added_actions'][] = compact( 'hook', 'callback', 'priority' ); |
| 50 | + } |
| 51 | + |
| 52 | + require __DIR__ . '/../inc/Abilities/WorkspaceAbilities.php'; |
| 53 | + require __DIR__ . '/../inc/Abilities/CodeTaskAbilities.php'; |
| 54 | + |
| 55 | + new \DataMachineCode\Abilities\WorkspaceAbilities(); |
| 56 | + new \DataMachineCode\Abilities\CodeTaskAbilities(); |
| 57 | + |
| 58 | + $failures = array(); |
| 59 | + $assert = static function ( string $label, bool $condition ) use ( &$failures ): void { |
| 60 | + if ( $condition ) { |
| 61 | + echo " ok {$label}\n"; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $failures[] = $label; |
| 66 | + echo " fail {$label}\n"; |
| 67 | + }; |
| 68 | + |
| 69 | + echo "Data Machine Code late ability registration - smoke\n"; |
| 70 | + |
| 71 | + $assert( 'workspace-show registers after wp_abilities_api_init fired', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/workspace-show'] ) ); |
| 72 | + $assert( 'workspace-worktree-add registers after wp_abilities_api_init fired', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/workspace-worktree-add'] ) ); |
| 73 | + $assert( 'create-code-task registers after wp_abilities_api_init fired', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/create-code-task'] ) ); |
| 74 | + $assert( 'late constructors do not add inert wp_abilities_api_init callbacks', array() === $GLOBALS['datamachine_code_added_actions'] ); |
| 75 | + |
| 76 | + if ( ! empty( $failures ) ) { |
| 77 | + echo "\nFAIL: " . count( $failures ) . " assertion(s) failed\n"; |
| 78 | + exit( 1 ); |
| 79 | + } |
| 80 | + |
| 81 | + echo "\nOK\n"; |
| 82 | +} |
0 commit comments