Skip to content

Commit 31afc89

Browse files
authored
Tests
1 parent 71f1ce3 commit 31afc89

File tree

15 files changed

+3375
-1
lines changed

15 files changed

+3375
-1
lines changed

.github/workflows/tests.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
16+
17+
name: PHP ${{ matrix.php-version }} Tests
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Setup PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: ${{ matrix.php-version }}
26+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd
27+
coverage: xdebug
28+
29+
- name: Validate composer.json and composer.lock
30+
run: composer validate --strict
31+
32+
- name: Cache Composer packages
33+
id: composer-cache
34+
uses: actions/cache@v3
35+
with:
36+
path: vendor
37+
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
38+
restore-keys: |
39+
${{ runner.os }}-php-${{ matrix.php-version }}-
40+
41+
- name: Install dependencies
42+
run: composer install --prefer-dist --no-progress --no-interaction
43+
44+
- name: Run PHPUnit tests
45+
run: composer test
46+
47+
- name: Run tests with coverage
48+
if: matrix.php-version == '8.1'
49+
run: composer test:coverage
50+
51+
- name: Check coverage threshold
52+
if: matrix.php-version == '8.1'
53+
run: composer coverage:check
54+
55+
- name: Upload coverage reports to Codecov
56+
if: matrix.php-version == '8.1'
57+
uses: codecov/codecov-action@v3
58+
with:
59+
file: ./coverage/clover.xml
60+
fail_ci_if_error: true
61+
62+
code-quality:
63+
runs-on: ubuntu-latest
64+
name: Code Quality
65+
66+
steps:
67+
- uses: actions/checkout@v4
68+
69+
- name: Setup PHP
70+
uses: shivammathur/setup-php@v2
71+
with:
72+
php-version: '8.1'
73+
extensions: dom, curl, libxml, mbstring, zip
74+
75+
- name: Install dependencies
76+
run: composer install --prefer-dist --no-progress --no-interaction
77+
78+
- name: Run PHPCS
79+
run: vendor/bin/phpcs --standard=phpcs.xml simple-wp-optimizer.php
80+
81+
- name: Run PHPMD
82+
run: vendor/bin/phpmd simple-wp-optimizer.php text phpmd.xml
83+
84+
- name: Run PHPStan
85+
run: vendor/bin/phpstan analyse --configuration=phpstan.neon

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,36 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- **DNS Prefetch Security Enhancement**: Enhanced DNS prefetch validation to reject file paths, query parameters, and fragments - only clean domains are now accepted
1111
- **Documentation Enhancement**: Added @since version tags to all PHPDoc blocks for better change tracking
1212
- **Developer Guidelines**: Created comprehensive CONTRIBUTING.md file with development standards, security requirements, and contribution workflow
13+
- **Comprehensive Unit Testing**: Implemented complete testing infrastructure with PHPUnit, WP_Mock, and 80% coverage requirement
14+
- **Test Coverage**: 18 core functions with comprehensive unit and integration tests
15+
- **Security Testing**: XSS prevention, DNS validation, CSRF protection, and input sanitization validation
16+
- **Performance Testing**: Caching mechanisms, optimization effects, and conditional loading verification
17+
- **CI/CD Integration**: GitHub Actions workflow with multi-PHP version testing (7.4-8.4) and automated quality checks
1318

1419
### Enhanced
1520
- **DNS Prefetch Optimization**: Improved DNS prefetch function with static caching, duplicate removal, AJAX detection, and enhanced domain validation
1621
- **Security Validation**: Strengthened DNS prefetch domain validation to prevent file path injection (e.g., `https://google.com/file.php` is now rejected)
1722
- **User Experience**: Updated DNS prefetch textarea description to clearly explain that only clean domains are allowed
1823
- **Code Quality**: Enhanced function documentation and maintained WordPress coding standards compliance
24+
- **Testing Infrastructure**: Complete test suite with fixtures, helpers, and comprehensive coverage reporting
1925

2026
### Security
2127
- **Domain Validation**: Enhanced DNS prefetch security to block file paths, query parameters, and URL fragments
2228
- **Input Sanitization**: Maintained comprehensive input validation and output escaping throughout all changes
2329
- **Clean Domain Output**: DNS prefetch now automatically strips paths and returns only clean domain URLs for security
30+
- **Security Testing**: Comprehensive security validation tests including XSS prevention and malicious input handling
2431

2532
### Performance
2633
- **Reduced Database Queries**: Option caching system minimizes repeated database calls
2734
- **Conditional Loading**: Admin assets only load when needed, reducing unnecessary resource usage
2835
- **Optimized DNS Prefetch**: Enhanced processing efficiency with static caching and validation improvements
2936

37+
### Developer Experience
38+
- **Testing Framework**: PHPUnit with WP_Mock for WordPress-specific testing
39+
- **Code Coverage**: 80% minimum coverage requirement with detailed reporting
40+
- **Quality Assurance**: PHPCS, PHPMD, and PHPStan integration for code quality
41+
- **Development Workflow**: Automated testing with GitHub Actions CI/CD pipeline
42+
3043
## [1.5.12] - 2025-08-02
3144
### Added
3245
- **Disable Post via Email**: New option to disable the post-via-email feature for enhanced security and performance.

composer.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,23 @@
2020
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
2121
"php-stubs/wordpress-stubs": "^6.8",
2222
"szepeviktor/phpstan-wordpress": "^1.3",
23-
"phpstan/phpstan": "^1.0"
23+
"phpstan/phpstan": "^1.0",
24+
"10up/wp_mock": "^1.0",
25+
"mockery/mockery": "^1.5",
26+
"phpunit/php-code-coverage": "^9.2",
27+
"sebastianbergmann/phpcov": "^8.2"
2428
},
2529
"config": {
2630
"allow-plugins": {
2731
"dealerdirect/phpcodesniffer-composer-installer": true
2832
}
33+
},
34+
"scripts": {
35+
"test": "phpunit",
36+
"test:coverage": "phpunit --coverage-html coverage --coverage-clover coverage.xml",
37+
"test:unit": "phpunit --testsuite=unit",
38+
"test:integration": "phpunit --testsuite=integration",
39+
"test:watch": "phpunit --watch",
40+
"coverage:check": "php scripts/check-coverage.php"
2941
}
3042
}

phpunit.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
bootstrap="tests/bootstrap.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
stopOnFailure="false"
9+
testdox="true"
10+
beStrictAboutTestsThatDoNotTestAnything="true"
11+
beStrictAboutOutputDuringTests="true"
12+
beStrictAboutChangesToGlobalState="true"
13+
>
14+
<testsuites>
15+
<testsuite name="unit">
16+
<directory>tests/unit</directory>
17+
</testsuite>
18+
<testsuite name="integration">
19+
<directory>tests/integration</directory>
20+
</testsuite>
21+
</testsuites>
22+
23+
<filter>
24+
<whitelist>
25+
<file>simple-wp-optimizer.php</file>
26+
</whitelist>
27+
</filter>
28+
29+
<coverage>
30+
<report>
31+
<html outputDirectory="coverage/html"/>
32+
<clover outputFile="coverage/clover.xml"/>
33+
<text outputFile="php://stdout" showUncoveredFiles="true"/>
34+
</report>
35+
<include>
36+
<file>simple-wp-optimizer.php</file>
37+
</include>
38+
</coverage>
39+
40+
<logging>
41+
<junit outputFile="coverage/junit.xml"/>
42+
</logging>
43+
44+
<php>
45+
<const name="WP_TESTS_PHPUNIT_POLYFILLS_PATH" value="vendor/yoast/phpunit-polyfills"/>
46+
</php>
47+
</phpunit>

readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ No, the plugin has a simple interface where you can toggle features on and off.
4949
* **PERFORMANCE**: Enhanced DNS prefetch function with static caching, duplicate removal, and AJAX detection
5050
* **SECURITY**: Enhanced DNS prefetch validation to reject file paths, query parameters, and fragments - only clean domains accepted
5151
* **SECURITY**: Strengthened domain validation to prevent file path injection (e.g., `https://google.com/file.php` now rejected)
52+
* **TESTING**: Implemented comprehensive unit testing infrastructure with PHPUnit and WP_Mock
53+
* **TESTING**: Added 18 core function tests with 80% coverage requirement and security validation
54+
* **TESTING**: Created CI/CD pipeline with GitHub Actions for multi-PHP version testing (7.4-8.4)
55+
* **TESTING**: Integrated automated code quality checks with PHPCS, PHPMD, and PHPStan
5256
* **DOCUMENTATION**: Added @since version tags to all PHPDoc blocks for better change tracking
5357
* **DEVELOPER EXPERIENCE**: Created comprehensive CONTRIBUTING.md file with development standards and security requirements
5458
* **USER EXPERIENCE**: Updated DNS prefetch textarea description to clearly explain clean domain requirements

0 commit comments

Comments
 (0)