Skip to content
Merged
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
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
pull_request:

permissions:
contents: read

jobs:
lint:
name: PHP ${{ matrix.php-version }} Syntax Check
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}

- name: Check PHP syntax
run: find . -name "*.php" -not -path "./vendor/*" -print0 | xargs -0 -n1 php -l

test:
name: PHP ${{ matrix.php-version }} Tests
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: composer

- name: Install dependencies
run: composer install --no-interaction --prefer-dist

- name: Run tests
run: vendor/bin/phpunit
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
composer.lock
.phpunit.cache/
.phpunit.result.cache
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require-dev": {
"phpunit/phpunit": "^9.6 || ^10.5 || ^11.0"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="PlusAd Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
142 changes: 142 additions & 0 deletions tests/MatchWhitelistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* Tests for matchWhitelist function in plusadController
*/
class MatchWhitelistTest extends TestCase
{
protected function setUp(): void
{
Context::reset();
TestHelper::reset();
ModuleObject::resetInstances();
}

/**
* Helper to create a plusadController instance
*/
private function createController(): plusadController
{
return new plusadController();
}

/**
* Test: Empty domain_list → whitelist disabled
* When no domains are configured, non-empty URLs are blocked.
* Empty URL always passes.
*/
public function testEmptyDomainListDisablesWhitelist(): void
{
$controller = $this->createController();
$controller->module_info->domain_list = '';

// Empty URL always returns true
$this->assertTrue($controller->matchWhitelist(''));

// Non-empty URLs are blocked when whitelist is empty
$this->assertFalse($controller->matchWhitelist('http://example.com'));
$this->assertFalse($controller->matchWhitelist('https://example.com'));
$this->assertFalse($controller->matchWhitelist('http://test.org'));
}

/**
* Test: Null domain_list behaves same as empty
*/
public function testNullDomainListDisablesWhitelist(): void
{
$controller = $this->createController();
$controller->module_info->domain_list = null;

$this->assertTrue($controller->matchWhitelist(''));
$this->assertFalse($controller->matchWhitelist('http://example.com'));
}

/**
* Test: Single domain (example.com)
* Only URLs on example.com should be allowed.
* Matching should be domain-only, path is irrelevant.
*/
public function testSingleDomainWhitelist(): void
{
$controller = $this->createController();
$controller->module_info->domain_list = 'example.com';

// Matching domain passes
$this->assertTrue($controller->matchWhitelist('http://example.com'));
$this->assertTrue($controller->matchWhitelist('https://example.com'));

// Matching domain with various paths passes (domain-only comparison)
$this->assertTrue($controller->matchWhitelist('http://example.com/'));
$this->assertTrue($controller->matchWhitelist('http://example.com/path/to/page'));
$this->assertTrue($controller->matchWhitelist('https://example.com/some/path'));
$this->assertTrue($controller->matchWhitelist('http://example.com/page?query=value'));
$this->assertTrue($controller->matchWhitelist('https://example.com/page#anchor'));

// Non-matching domain fails
$this->assertFalse($controller->matchWhitelist('http://other.com'));
$this->assertFalse($controller->matchWhitelist('https://notexample.com'));
$this->assertFalse($controller->matchWhitelist('http://example.org'));
}

/**
* Test: Two domains separated by \n (example.com and example1.com)
* Both domains should be allowed, others should be blocked.
*/
public function testMultipleDomainWhitelist(): void
{
$controller = $this->createController();
$controller->module_info->domain_list = "example.com\nexample1.com";

// First domain passes
$this->assertTrue($controller->matchWhitelist('http://example.com'));
$this->assertTrue($controller->matchWhitelist('https://example.com'));
$this->assertTrue($controller->matchWhitelist('http://example.com/path'));

// Second domain passes
$this->assertTrue($controller->matchWhitelist('http://example1.com'));
$this->assertTrue($controller->matchWhitelist('https://example1.com'));
$this->assertTrue($controller->matchWhitelist('http://example1.com/path'));

// Non-matching domain fails
$this->assertFalse($controller->matchWhitelist('http://other.com'));
$this->assertFalse($controller->matchWhitelist('https://example2.com'));
$this->assertFalse($controller->matchWhitelist('http://notexample.com'));
}

/**
* Test: Domain matching is path-independent
* Regardless of the path, only the domain should matter.
*/
public function testDomainMatchingIgnoresPath(): void
{
$controller = $this->createController();
$controller->module_info->domain_list = 'example.com';

// Same domain with different paths all pass
$this->assertTrue($controller->matchWhitelist('http://example.com/'));
$this->assertTrue($controller->matchWhitelist('http://example.com/page'));
$this->assertTrue($controller->matchWhitelist('http://example.com/deep/path/to/resource'));
$this->assertTrue($controller->matchWhitelist('http://example.com/page?query=value'));
$this->assertTrue($controller->matchWhitelist('https://example.com/page#anchor'));

// Different domain with various paths all fail
$this->assertFalse($controller->matchWhitelist('http://other.com/'));
$this->assertFalse($controller->matchWhitelist('http://other.com/page'));
$this->assertFalse($controller->matchWhitelist('http://other.com/deep/path'));
}

/**
* Test: \r\n line endings are handled correctly
*/
public function testWindowsLineEndings(): void
{
$controller = $this->createController();
$controller->module_info->domain_list = "example.com\r\nexample1.com";

$this->assertTrue($controller->matchWhitelist('http://example.com'));
$this->assertTrue($controller->matchWhitelist('http://example1.com'));
$this->assertFalse($controller->matchWhitelist('http://other.com'));
}
}
Loading
Loading