Skip to content

Commit 6d32947

Browse files
committed
Working on refactoring plugin. Adding settings tests. Moving templates under "Templates". Refactoring iframe functionality.
1 parent 9daaa46 commit 6d32947

28 files changed

+641
-268
lines changed

plugins/hwp-previews/TESTING.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ The plugin provides scripts to set up a local WordPress environment for testing,
5555

5656
## Running Tests
5757

58-
@TODO
58+
Currently the plugin has the following suite of tests
59+
60+
1. WP Unit Tests - (Unit and Integration Tests)
61+
2. E2E Tests - Playright tests
5962

6063
### WPUnit (WordPress-aware Unit/Integration) Tests
6164

@@ -69,24 +72,12 @@ sh bin/local/run-unit-tests.sh coverage
6972
> You can also add coverage e.g. `sh bin/local/run-unit-tests.sh coverage --coverage-html` and the output will be saved in [tests/_output/coverage/dashboard.html](tests/_output/coverage/dashboard.html)
7073
7174

72-
### Functional Tests
73-
74-
Run functional tests (simulate web requests):
75-
76-
```bash
77-
composer run test:functional
78-
# or
79-
vendor/bin/codecept run functional
80-
```
81-
82-
### Acceptance Tests
75+
### E2WTests
8376

8477
Run browser-based acceptance tests:
8578

8679
```bash
87-
composer run test:acceptance
88-
# or
89-
vendor/bin/codecept run acceptance
80+
sh bin/local/run-e2e-tests.sh coverage
9081
```
9182

9283
### All Tests
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
# Run Playwright E2E tests for GitHub Actions
4+
5+
set -e
6+
7+
# Install dependencies if needed
8+
npm ci
9+
10+
# Install composer dependencies
11+
composer install
12+
13+
# Install Playwright browsers
14+
npx playwright install --with-deps
15+
16+
# Start wp-env
17+
npm run wp-env start
18+
19+
# Run Playwright tests
20+
npm run test:e2e
21+
22+
# Stop wp-env
23+
npm run wp-env stop

plugins/hwp-previews/codeception.dist.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ extensions:
3131
- lucatume\WPBrowser\Command\MonkeyCachePath
3232
- lucatume\WPBrowser\Command\RunAll
3333
- lucatume\WPBrowser\Command\RunOriginal
34+
exclude:
35+
- src/Templates
36+
- src/Templates/*
3437
coverage:
3538
enabled: true
3639
remote: false
@@ -48,6 +51,7 @@ coverage:
4851
- /tests/*
4952
- /vendor-prefixed/*
5053
- /vendor/*
54+
- /src/Templates/*
5155
show_only_summary: false
5256
modules:
5357
config:

plugins/hwp-previews/composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,15 @@
128128
],
129129
"php:psalm": "psalm",
130130
"php:psalm:info": "psalm --show-info=true",
131-
"php:psalm:fix": "psalm --alter"
131+
"php:psalm:fix": "psalm --alter",
132+
"test": [
133+
"sh bin/local/run-unit-tests.sh",
134+
"sh bin/local/run-e2e-tests.sh"
135+
],
136+
"test:unit": "sh bin/local/run-unit-tests.sh",
137+
"test:unit:coverage": "sh bin/local/run-unit-tests.sh coverage",
138+
"test:unit:coverage-html": "sh bin/local/run-unit-tests.sh coverage --coverage-html",
139+
"test:e2e": "sh bin/local/run-e2e-tests.sh"
132140
},
133141
"support": {
134142
"docs": "https://github.com/wpengine/hwptoolkit/tree/main/docs",

plugins/hwp-previews/hwp-previews.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ function hwp_previews_constants(): void {
8484
if ( ! defined( 'HWP_PREVIEWS_TEXT_DOMAIN' ) ) {
8585
define( 'HWP_PREVIEWS_TEXT_DOMAIN', 'hwp-previews' );
8686
}
87-
88-
// Plugin Template Directory.
89-
if ( ! defined( 'HWP_PREVIEWS_TEMPLATE_DIR' ) ) {
90-
define( 'HWP_PREVIEWS_TEMPLATE_DIR', trailingslashit( HWP_PREVIEWS_PLUGIN_DIR ) . '/src/Admin/Settings/Templates/' );
91-
}
9287
}
9388

9489
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh

plugins/hwp-previews/src/Admin/Settings/Fields/Field/URL_Input_Field.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ public function get_input_type(): string {
4343
* @param string $value
4444
*/
4545
private function fix_url( string $value ): string {
46-
// Remove HTML tags, trim, encode spaces, add protocol.
46+
47+
48+
if ( '' === $value ) {
49+
return '';
50+
}
51+
52+
// Remove <script> tags and their content to prevent XSS.
53+
$value = preg_replace( '#<script.*?>.*?</script>#is', '', $value );
54+
55+
// Remove HTML tags except curly braces, trim, encode spaces, add protocol.
4756
$value = preg_replace( '/<(?!\{)[^>]+>/', '', $value );
4857
$value = trim( str_replace( ' ', '%20', (string) $value ) );
4958

@@ -55,7 +64,7 @@ private function fix_url( string $value ): string {
5564
if ( $has_prootocol ) {
5665
return $value;
5766
}
58-
$protocol = is_ssl() ? 'https://' : 'http://';
59-
return $protocol . ltrim( $value, '/' );
67+
$protocol = is_ssl() ? 'https://' : 'http://';
68+
return $protocol . ltrim( $value, '/' );
6069
}
6170
}

plugins/hwp-previews/src/Admin/Settings_Page.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
88
use HWP\Previews\Admin\Settings\Menu\Menu_Page;
99
use HWP\Previews\Admin\Settings\Settings_Form_Manager;
10-
use HWP\Previews\Post\Type\Contracts\Post_Types_Config_Interface;
11-
use HWP\Previews\Post\Type\Post_Types_Config_Registry;
1210
use HWP\Previews\Preview\Parameter\Preview_Parameter_Registry;
11+
use HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface;
12+
use HWP\Previews\Preview\Post\Type\Post_Types_Config_Registry;
1313

1414
class Settings_Page {
1515
/**
@@ -23,7 +23,7 @@ class Settings_Page {
2323
protected Preview_Parameter_Registry $parameters;
2424

2525
/**
26-
* @var \HWP\Previews\Post\Type\Contracts\Post_Types_Config_Interface The post types available for previews.
26+
* @var \HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface The post types available for previews.
2727
*/
2828
protected Post_Types_Config_Interface $types_config;
2929

@@ -80,7 +80,7 @@ public function register_settings_pages(): void {
8080
__( 'HWP Previews Settings', 'hwp-previews' ),
8181
'HWP Previews',
8282
self::PLUGIN_MENU_SLUG,
83-
trailingslashit( HWP_PREVIEWS_TEMPLATE_DIR ) . 'settings-page-main.php',
83+
trailingslashit(HWP_PREVIEWS_PLUGIN_DIR) . 'src/Templates/admin.php',
8484
[
8585
'hwp_previews_main_page_config' => [
8686
'tabs' => $post_types,

0 commit comments

Comments
 (0)