From 8a7307afc362f553463a3d93a2f4dfc977a395ce Mon Sep 17 00:00:00 2001 From: Ta5r Date: Wed, 12 Feb 2025 18:46:42 +0530 Subject: [PATCH 1/6] dev : uncomment NEXT_PUBLIC_URI by default. --- src/Modules/EnvGenerator/Generator.php | 2 +- tests/Integration/GeneratorTest.php | 4 ++-- tests/Integration/RestControllerTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Modules/EnvGenerator/Generator.php b/src/Modules/EnvGenerator/Generator.php index 9cec7f9..f99394f 100644 --- a/src/Modules/EnvGenerator/Generator.php +++ b/src/Modules/EnvGenerator/Generator.php @@ -107,7 +107,7 @@ protected function prepare_variable( string $name, ?string $value ): ?string { $env_output = sprintf( '%s=%s', $name, $resolved_value ); // Comment out variables if they're not required and have the default value. - if ( ! $required && $resolved_value === $default ) { + if ( ! $required && $resolved_value === $default && 'NEXT_PUBLIC_URL' !== $name ) { $env_output = '# ' . $env_output; } diff --git a/tests/Integration/GeneratorTest.php b/tests/Integration/GeneratorTest.php index 2f1cf5e..cf07e63 100644 --- a/tests/Integration/GeneratorTest.php +++ b/tests/Integration/GeneratorTest.php @@ -62,7 +62,7 @@ public function testGenerateEnvContent(): void { NODE_TLS_REJECT_UNAUTHORIZED=5 # The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app. -# NEXT_PUBLIC_URL=http://localhost:3000 +NEXT_PUBLIC_URL=http://localhost:3000 # The WordPress "frontend" domain URL NEXT_PUBLIC_WORDPRESS_URL=https://headless-demo.local @@ -130,7 +130,7 @@ public function testDefaultValuesForEnvContent(): void { # NODE_TLS_REJECT_UNAUTHORIZED=0 # The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app. -# NEXT_PUBLIC_URL=http://localhost:3000 +NEXT_PUBLIC_URL=http://localhost:3000 # The WordPress "frontend" domain URL NEXT_PUBLIC_WORDPRESS_URL=https://headless-demo.local diff --git a/tests/Integration/RestControllerTest.php b/tests/Integration/RestControllerTest.php index 392b52f..ba4b89a 100644 --- a/tests/Integration/RestControllerTest.php +++ b/tests/Integration/RestControllerTest.php @@ -88,7 +88,7 @@ public function testGenerateEnvEndpoint(): void { $this->assertNotEmpty( $actual_data['content'] ); $search = '\n'; $replace = ''; - $expected = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.\n# NEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); + $expected = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.\nNEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); $this->assertEquals( $expected, str_replace( $search, $replace, $actual_data['content'] ) ); From 7280c1e02af8872b6273fc3f4ce63b3e3b9de670 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 13 Feb 2025 12:45:58 +0530 Subject: [PATCH 2/6] revert : previous approach --- src/Modules/EnvGenerator/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Modules/EnvGenerator/Generator.php b/src/Modules/EnvGenerator/Generator.php index f99394f..9cec7f9 100644 --- a/src/Modules/EnvGenerator/Generator.php +++ b/src/Modules/EnvGenerator/Generator.php @@ -107,7 +107,7 @@ protected function prepare_variable( string $name, ?string $value ): ?string { $env_output = sprintf( '%s=%s', $name, $resolved_value ); // Comment out variables if they're not required and have the default value. - if ( ! $required && $resolved_value === $default && 'NEXT_PUBLIC_URL' !== $name ) { + if ( ! $required && $resolved_value === $default ) { $env_output = '# ' . $env_output; } From 98fb1924bc2625b33cbd145444a15a6633058e62 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 13 Feb 2025 12:48:04 +0530 Subject: [PATCH 3/6] chore : update required status for vars in the registry and supporting update in the access-function. --- access-functions.php | 4 ++-- src/Modules/EnvGenerator/VariableRegistry.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/access-functions.php b/access-functions.php index 10d541e..98f6c2b 100644 --- a/access-functions.php +++ b/access-functions.php @@ -72,8 +72,8 @@ function snapwp_helper_get_env_variables() { $upload_path = '/' . ltrim( str_replace( ABSPATH, '', $upload_dir['basedir'] ), '/' ); return [ - 'NODE_TLS_REJECT_UNAUTHORIZED' => '', - 'NEXT_PUBLIC_URL' => '', + 'NODE_TLS_REJECT_UNAUTHORIZED' => '0', + 'NEXT_PUBLIC_URL' => 'http://localhost:3000', 'NEXT_PUBLIC_WORDPRESS_URL' => untrailingslashit( get_home_url() ), 'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => graphql_get_endpoint(), 'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => $upload_path, diff --git a/src/Modules/EnvGenerator/VariableRegistry.php b/src/Modules/EnvGenerator/VariableRegistry.php index 3a95714..d3497a9 100644 --- a/src/Modules/EnvGenerator/VariableRegistry.php +++ b/src/Modules/EnvGenerator/VariableRegistry.php @@ -22,12 +22,12 @@ class VariableRegistry { 'NODE_TLS_REJECT_UNAUTHORIZED' => [ 'description' => 'Enable if connecting to a self-signed cert', 'default' => '0', - 'required' => false, + 'required' => true, ], 'NEXT_PUBLIC_URL' => [ 'description' => 'The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.', 'default' => 'http://localhost:3000', - 'required' => false, + 'required' => true, ], 'NEXT_PUBLIC_WORDPRESS_URL' => [ 'description' => 'The WordPress "frontend" domain URL', From 22a48d720cae91a82c7430f355d0b1fe456e7da5 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 13 Feb 2025 12:48:36 +0530 Subject: [PATCH 4/6] fix : zero value being treated as empty. --- src/Modules/EnvGenerator/Generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Modules/EnvGenerator/Generator.php b/src/Modules/EnvGenerator/Generator.php index 9cec7f9..0f6a797 100644 --- a/src/Modules/EnvGenerator/Generator.php +++ b/src/Modules/EnvGenerator/Generator.php @@ -95,7 +95,7 @@ protected function prepare_variable( string $name, ?string $value ): ?string { $required = ! empty( $variable['required'] ); // Check if a required variable has a value. - if ( $required && empty( $value ) ) { + if ( $required && empty( $value ) && '0' !== $value ) { throw new \InvalidArgumentException( 'Required variables must have a value.' ); } From 351b6a7d7c051386576d3a5ec23540c5406d2101 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 13 Feb 2025 12:48:56 +0530 Subject: [PATCH 5/6] chore : update integration tests for Generator and RESTController --- tests/Integration/GeneratorTest.php | 6 +++--- tests/Integration/RestControllerTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Integration/GeneratorTest.php b/tests/Integration/GeneratorTest.php index cf07e63..4082afb 100644 --- a/tests/Integration/GeneratorTest.php +++ b/tests/Integration/GeneratorTest.php @@ -111,8 +111,8 @@ public function testDefaultValuesForEnvContent(): void { // CASE : For NODE_TLS_REJECT_UNAUTHORIZED with no default value, Generator class should comment out the variable in .ENV content. $values = [ - 'NODE_TLS_REJECT_UNAUTHORIZED' => '', - 'NEXT_PUBLIC_URL' => '', + 'NODE_TLS_REJECT_UNAUTHORIZED' => '0', + 'NEXT_PUBLIC_URL' => 'http://localhost:3000', 'NEXT_PUBLIC_WORDPRESS_URL' => 'https://headless-demo.local', 'NEXT_PUBLIC_GRAPHQL_ENDPOINT' => '/test_endpoint', 'NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH' => '', @@ -127,7 +127,7 @@ public function testDefaultValuesForEnvContent(): void { // Define expected content. $expectedContent = ' # Enable if connecting to a self-signed cert -# NODE_TLS_REJECT_UNAUTHORIZED=0 +NODE_TLS_REJECT_UNAUTHORIZED=0 # The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app. NEXT_PUBLIC_URL=http://localhost:3000 diff --git a/tests/Integration/RestControllerTest.php b/tests/Integration/RestControllerTest.php index ba4b89a..e8c93fa 100644 --- a/tests/Integration/RestControllerTest.php +++ b/tests/Integration/RestControllerTest.php @@ -88,7 +88,7 @@ public function testGenerateEnvEndpoint(): void { $this->assertNotEmpty( $actual_data['content'] ); $search = '\n'; $replace = ''; - $expected = "\n# Enable if connecting to a self-signed cert\n# NODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.\nNEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); + $expected = "\n# Enable if connecting to a self-signed cert\nNODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.\nNEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); $this->assertEquals( $expected, str_replace( $search, $replace, $actual_data['content'] ) ); From 1ea807174809291e5023fb44765c2a865dca930e Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 13 Feb 2025 16:53:01 +0530 Subject: [PATCH 6/6] chore : update generated comments and matching tests --- docs/rest-api.md | 2 +- src/Modules/EnvGenerator/Generator.php | 1 + src/Modules/EnvGenerator/VariableRegistry.php | 4 ++-- tests/Integration/GeneratorTest.php | 8 ++++---- tests/Integration/RestControllerTest.php | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/rest-api.md b/docs/rest-api.md index a978476..9a2ce70 100644 --- a/docs/rest-api.md +++ b/docs/rest-api.md @@ -23,7 +23,7 @@ curl -X GET \ This endpoint does not require any parameters to be passed in the request body. The .env file content is generated based on WordPress settings. Unchanged variables will be commented out. - - `NODE_TLS_REJECT_UNAUTHORIZED`: Enable if connecting to a self-signed cert. (Default: `0`) + - `NODE_TLS_REJECT_UNAUTHORIZED`: Only enable if connecting to a self-signed cert. (Default: `0`) - `NEXT_PUBLIC_URL` (Required): The headless frontend domain URL. (Default: `http://localhost:3000`) - `NEXT_PUBLIC_WORDPRESS_URL` (Required): The WordPress "frontend" domain URL. - `NEXT_PUBLIC_GRAPHQL_ENDPOINT`: The WordPress GraphQL endpoint. (Default: `graphql`) diff --git a/src/Modules/EnvGenerator/Generator.php b/src/Modules/EnvGenerator/Generator.php index 0f6a797..44ac4e0 100644 --- a/src/Modules/EnvGenerator/Generator.php +++ b/src/Modules/EnvGenerator/Generator.php @@ -95,6 +95,7 @@ protected function prepare_variable( string $name, ?string $value ): ?string { $required = ! empty( $variable['required'] ); // Check if a required variable has a value. + // @todo: handle NODE_TLS_REJECT_UNAUTHORIZED by checking the NEXT_PUBLIC_URL. if ( $required && empty( $value ) && '0' !== $value ) { throw new \InvalidArgumentException( 'Required variables must have a value.' ); } diff --git a/src/Modules/EnvGenerator/VariableRegistry.php b/src/Modules/EnvGenerator/VariableRegistry.php index d3497a9..ce47f11 100644 --- a/src/Modules/EnvGenerator/VariableRegistry.php +++ b/src/Modules/EnvGenerator/VariableRegistry.php @@ -20,12 +20,12 @@ class VariableRegistry { */ private const VARIABLES = [ 'NODE_TLS_REJECT_UNAUTHORIZED' => [ - 'description' => 'Enable if connecting to a self-signed cert', + 'description' => 'Only enable if connecting to a self-signed cert', 'default' => '0', 'required' => true, ], 'NEXT_PUBLIC_URL' => [ - 'description' => 'The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.', + 'description' => 'The headless frontend domain URL. Make sure the value matches the URL used by your frontend app.', 'default' => 'http://localhost:3000', 'required' => true, ], diff --git a/tests/Integration/GeneratorTest.php b/tests/Integration/GeneratorTest.php index 4082afb..0e10fcc 100644 --- a/tests/Integration/GeneratorTest.php +++ b/tests/Integration/GeneratorTest.php @@ -58,10 +58,10 @@ public function testGenerateEnvContent(): void { $content = $generator->generate(); $expectedContent = ' -# Enable if connecting to a self-signed cert +# Only enable if connecting to a self-signed cert NODE_TLS_REJECT_UNAUTHORIZED=5 -# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app. +# The headless frontend domain URL. Make sure the value matches the URL used by your frontend app. NEXT_PUBLIC_URL=http://localhost:3000 # The WordPress "frontend" domain URL @@ -126,10 +126,10 @@ public function testDefaultValuesForEnvContent(): void { // Define expected content. $expectedContent = ' -# Enable if connecting to a self-signed cert +# Only enable if connecting to a self-signed cert NODE_TLS_REJECT_UNAUTHORIZED=0 -# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app. +# The headless frontend domain URL. Make sure the value matches the URL used by your frontend app. NEXT_PUBLIC_URL=http://localhost:3000 # The WordPress "frontend" domain URL diff --git a/tests/Integration/RestControllerTest.php b/tests/Integration/RestControllerTest.php index e8c93fa..535e5fb 100644 --- a/tests/Integration/RestControllerTest.php +++ b/tests/Integration/RestControllerTest.php @@ -88,7 +88,7 @@ public function testGenerateEnvEndpoint(): void { $this->assertNotEmpty( $actual_data['content'] ); $search = '\n'; $replace = ''; - $expected = "\n# Enable if connecting to a self-signed cert\nNODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Uncomment this line and ensure the value matches the URL used by your frontend app.\nNEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); + $expected = "\n# Only enable if connecting to a self-signed cert\nNODE_TLS_REJECT_UNAUTHORIZED=0\n\n# The headless frontend domain URL. Make sure the value matches the URL used by your frontend app.\nNEXT_PUBLIC_URL=http://localhost:3000\n\n# The WordPress \"frontend\" domain URL\nNEXT_PUBLIC_WORDPRESS_URL=" . get_home_url() . "\n\n# The WordPress GraphQL endpoint\nNEXT_PUBLIC_GRAPHQL_ENDPOINT=" . graphql_get_endpoint() . "\n\n# The WordPress Uploads directory path\n# NEXT_PUBLIC_WORDPRESS_UPLOADS_PATH=/" . str_replace( ABSPATH, '', wp_get_upload_dir()['basedir'] ) . "\n\n# The WordPress REST URL Prefix\n# NEXT_PUBLIC_WORDPRESS_REST_URL_PREFIX=/" . rest_get_url_prefix() . "\n\n# Token used for authenticating GraphQL introspection queries\nINTROSPECTION_TOKEN=" . IntrospectionToken::get_token(); $this->assertEquals( $expected, str_replace( $search, $replace, $actual_data['content'] ) );