Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Code Quality

on:
push:
branches: [ master, develop ]
pull_request:
branches: [ master ]

jobs:
phpcs:
name: PHP CodeSniffer
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: cs2pr

- name: Install dependencies
run: |
composer global require "squizlabs/php_codesniffer=*"
composer global require "wp-coding-standards/wpcs=*"
composer global require "phpcompatibility/phpcompatibility-wp=*"

- name: Configure PHPCS
run: |
phpcs --config-set installed_paths ~/.composer/vendor/wp-coding-standards/wpcs,~/.composer/vendor/phpcompatibility/phpcompatibility-wp
phpcs --config-set default_standard WordPress

- name: Run PHPCS
run: |
phpcs --standard=WordPress --extensions=php --ignore=scssphp/,tests/,bin/ --report=checkstyle . | cs2pr

phpstan:
name: PHPStan Analysis
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'

- name: Install dependencies
run: |
composer global require phpstan/phpstan

- name: Create PHPStan config
run: |
cat > phpstan.neon << EOF
parameters:
level: 5
paths:
- wp-scss.php
- options.php
- class/
excludePaths:
- scssphp/
- tests/
ignoreErrors:
# WordPress functions might not be recognized
- '#Function (get_option|update_option|add_option|wp_kses|sanitize_text_field) not found#'
- '#Function (get_stylesheet_directory|get_template_directory|wp_get_upload_dir) not found#'
- '#Function (add_action|add_filter|apply_filters) not found#'
EOF

- name: Run PHPStan
run: phpstan analyse --no-progress --error-format=github
68 changes: 68 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Tests

on:
push:
branches: [ master, develop ]
pull_request:
branches: [ master ]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
wordpress: ['latest', '6.0', '5.9']
exclude:
# Exclude PHP 8.2 with older WordPress versions that don't support it
- php: '8.2'
wordpress: '5.9'
- php: '8.2'
wordpress: '6.0'

name: PHP ${{ matrix.php }} / WordPress ${{ matrix.wordpress }}

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wp_scss_test
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

- name: Install WordPress test suite
run: |
bash bin/install-wp-tests.sh wp_scss_test root password 127.0.0.1:3306 ${{ matrix.wordpress }}

- name: Run tests
run: |
cd $GITHUB_WORKSPACE
phpunit

- name: Run tests with coverage (PHP 8.1 only)
if: matrix.php == '8.1' && matrix.wordpress == 'latest'
run: |
cd $GITHUB_WORKSPACE
phpunit --coverage-clover=coverage.xml

- name: Upload coverage to Codecov
if: matrix.php == '8.1' && matrix.wordpress == 'latest'
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
120 changes: 120 additions & 0 deletions README-TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Testing WP-SCSS Plugin

This document explains how to run tests for the WP-SCSS plugin to ensure the settings persistence bug fix and other functionality work correctly.

## Test Setup

### Prerequisites

1. **PHP 7.2+** (same as plugin requirement)
2. **MySQL/MariaDB** for test database
3. **PHPUnit** (automatically installed with WordPress test suite)

### Installation

1. **Install WordPress Test Suite:**
```bash
# Run from plugin root directory
bin/install-wp-tests.sh wp_scss_test root password localhost latest
```

Replace the database credentials as needed:
- `wp_scss_test` - test database name (will be created)
- `root` - database user
- `password` - database password
- `localhost` - database host

2. **Verify Setup:**
```bash
# Check if test environment is ready
ls /tmp/wordpress-tests-lib/
```

## Running Tests

### Run All Tests
```bash
phpunit
```

### Run Specific Test Files
```bash
# Test only settings functionality
phpunit tests/test-settings.php

# Test only integration scenarios
phpunit tests/test-wp-scss-integration.php
```

### Run Specific Test Methods
```bash
# Test the specific bug fix
phpunit --filter test_bug_fix_all_settings_preserved

# Test base location change scenario
phpunit --filter test_base_location_change_persistence
```

## Test Coverage

### Settings Sanitization Tests (`test-settings.php`)

- **test_sanitize_preserves_all_fields** - Ensures all 9 form fields are preserved
- **test_sanitize_adds_trailing_slashes** - Verifies directory path formatting
- **test_sanitize_checkbox_fields** - Tests checkbox handling (checked/unchecked)
- **test_sanitize_handles_empty_fields** - Tests behavior with missing data
- **test_sanitize_text_fields** - Tests XSS protection via sanitize_text_field()
- **test_bug_fix_all_settings_preserved** - Regression test for the original bug

### Integration Tests (`test-wp-scss-integration.php`)

- **test_settings_persistence_workflow** - Full save/retrieve cycle
- **test_base_location_change_persistence** - Specific bug scenario reproduction
- **test_development_mode_persistence** - Development settings scenarios
- **test_checkbox_form_behavior** - Real WordPress checkbox behavior
- **test_old_sanitize_method_would_fail** - Proves the bug was actually fixed

## The Bug We Fixed

**Original Problem:** The `sanitize()` method only preserved `scss_dir` and `css_dir` fields, causing all other settings (base location, compilation mode, etc.) to be lost on save.

**Tests That Prove the Fix:**
- `test_bug_fix_all_settings_preserved` - Shows all 9 fields are now saved
- `test_base_location_change_persistence` - Reproduces exact user scenario
- `test_old_sanitize_method_would_fail` - Demonstrates the old broken behavior

## Test Database

Tests use a separate database (`wp_scss_test` by default) that is:
- Automatically created by the install script
- Cleaned between test runs
- Completely separate from your development/production WordPress

## Continuous Integration

These tests can be integrated into CI/CD pipelines:

```yaml
# Example GitHub Actions workflow
- name: Setup test database
run: |
sudo systemctl start mysql
bin/install-wp-tests.sh wp_scss_test root password localhost latest

- name: Run tests
run: phpunit
```

## Troubleshooting

**"Could not find wp-tests-config.php"**
- Run the install script: `bin/install-wp-tests.sh ...`

**Database connection errors**
- Verify MySQL is running
- Check database credentials in install command
- Ensure database user has CREATE privileges

**Class not found errors**
- Ensure you're running tests from the plugin root directory
- Check that wp-scss.php and options.php exist
Loading
Loading