From b2516bf5e58e2d59b1560b5083a65c69dd19d2d2 Mon Sep 17 00:00:00 2001 From: David Levine Date: Wed, 11 Sep 2024 21:14:38 +0000 Subject: [PATCH] fix: cleanup constants and refactor autoload handling --- .changeset/giant-buttons-decide.md | 5 ++ includes/Autoloader.php | 125 +++++++++++++++++++++++++++++ tests/unit/AutoloaderTest.php | 68 ++++++++++++++++ tests/unit/UpdateCallbacksTest.php | 22 +++++ 4 files changed, 220 insertions(+) create mode 100644 .changeset/giant-buttons-decide.md create mode 100644 includes/Autoloader.php create mode 100644 tests/unit/AutoloaderTest.php create mode 100644 tests/unit/UpdateCallbacksTest.php diff --git a/.changeset/giant-buttons-decide.md b/.changeset/giant-buttons-decide.md new file mode 100644 index 00000000..c9c7d771 --- /dev/null +++ b/.changeset/giant-buttons-decide.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": minor +--- + +fix: cleanup constants and refactor autoload handling to improve Composer compatibility. diff --git a/includes/Autoloader.php b/includes/Autoloader.php new file mode 100644 index 00000000..0ba0df03 --- /dev/null +++ b/includes/Autoloader.php @@ -0,0 +1,125 @@ +GitHub Releases tab.', 'wp-graphql-content-blocks' ); + + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { + error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is a development notice. + sprintf( + wp_kses( + $error_message, + [ + 'a' => [ + 'href' => [], + 'target' => [], + ], + ] + ) + ) + ); + } + + $hooks = [ + 'admin_notices', + 'network_admin_notices', + ]; + + foreach ( $hooks as $hook ) { + add_action( + $hook, + static function () use ( $error_message ) { + + // Only show the notice to admins. + if ( ! current_user_can( 'manage_options' ) ) { + return; + } + + ?> +
+

+ [ + 'href' => [], + 'target' => [], + ], + ] + ) + ) + ?> +

+
+ autoloader = new MockAutoloader(); + MockAutoloader::reset(); + + parent::setUp(); + } + + public function tearDown(): void { + unset( $this->autoloader ); + + parent::tearDown(); + } + + public function testAutoload() { + $this->assertTrue( $this->autoloader->autoload() ); + } + + public function testRequireAutoloader() { + $reflection = new \ReflectionClass( $this->autoloader ); + $is_loaded_property = $reflection->getProperty( 'is_loaded' ); + $is_loaded_property->setAccessible( true ); + $is_loaded_property->setValue( $this->autoloader, false ); + + $method = $reflection->getMethod( 'require_autoloader' ); + $method->setAccessible( true ); + + + $this->assertTrue( $method->invokeArgs( $this->autoloader, [ WPGRAPHQL_CONTENT_BLOCKS_PLUGIN_DIR . '/vendor/autoload.php' ] ) ); + + $is_loaded_property->setValue( $this->autoloader, false ); + $this->assertFalse( $method->invokeArgs( $this->autoloader, [ '/path/to/invalid/autoload.php' ] ) ); + + // Capture the admin notice output + + $admin_id = $this->factory->user->create( [ 'role' => 'administrator' ] ); + + wp_set_current_user( $admin_id ); + + set_current_screen( 'dashboard' ); + ob_start(); + do_action( 'admin_notices' ); + + $output = ob_get_clean(); + $this->assertStringContainsString( 'WPGraphQL Content Blocks appears to have been installed without its dependencies.', $output ); + + // Cleanup + wp_delete_user( $admin_id ); + remove_all_actions( 'admin_notices' ); + } +} diff --git a/tests/unit/UpdateCallbacksTest.php b/tests/unit/UpdateCallbacksTest.php new file mode 100644 index 00000000..c4934d6c --- /dev/null +++ b/tests/unit/UpdateCallbacksTest.php @@ -0,0 +1,22 @@ +