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
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/.php-cs-fixer.dist.php export-ignore
/composer-unused.php export-ignore
/structarmed.php export-ignore
/infection.json.dist export-ignore
/infection.json5 export-ignore
/mkdocs.yml export-ignore
/phpstan-baseline.php export-ignore
/phpstan.neon.dist export-ignore
Expand Down
74 changes: 74 additions & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Mutation Testing

on:
pull_request:
branches:
- develop
paths:
- 'src/**.php'
- 'tests/**.php'
- 'composer.json'
- 'infection.json5'
- 'phpunit.dist.xml'
- 'phpstan.neon.dist'
- '.github/workflows/mutation.yml'
push:
branches:
- develop
paths:
- 'src/**.php'
- 'tests/**.php'
- 'composer.json'
- 'infection.json5'
- 'phpunit.dist.xml'
- 'phpstan.neon.dist'
- '.github/workflows/mutation.yml'

permissions:
contents: read

jobs:
mutation:
name: Infection
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[ci skip]')"

steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false

- name: Set up PHP
uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # v2.40.0
with:
php-version: '8.5'
extensions: sqlite3
coverage: xdebug
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: composer-8.5-${{ hashFiles('**/composer.json') }}
restore-keys: |
composer-8.5-
composer-

- name: Install dependencies
run: composer update --ansi

- name: Run mutation tests on changed lines
if: github.event_name == 'pull_request'
run: composer mutation:filter -- --ansi

- name: Run full mutation tests
if: github.event_name == 'push'
run: composer mutation:check -- --ansi
24 changes: 24 additions & 0 deletions bin/mutation-filter
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

cd "$(dirname "$0")/.." || exit 1

untracked=$(git ls-files --others --exclude-standard src tests)

restore() {
if [ -n "$untracked" ]; then
git reset --quiet -- $untracked
fi
}

trap restore EXIT

if [ -n "$untracked" ]; then
git add --intent-to-add -- $untracked
fi

vendor/bin/infection \
--static-analysis-tool=phpstan \
--with-uncovered \
--git-diff-lines \
--git-diff-base=origin/develop \
"$@"
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"codeigniter4/devkit": "^1.3",
"codeigniter4/framework": ">=4.3.5 <4.5.0 || ^4.5.1",
"firebase/php-jwt": "^7.0.3",
"infection/infection": "^0.32 || ^0.35",
"mikey179/vfsstream": "^1.6.7",
"mockery/mockery": "^1.0",
"phpstan/phpstan-strict-rules": "^2.0"
Expand Down Expand Up @@ -64,6 +65,7 @@
},
"config": {
"allow-plugins": {
"infection/extension-installer": true,
"phpstan/extension-installer": true
},
"sort-packages": true
Expand All @@ -89,7 +91,16 @@
"cs-fix": "php-cs-fixer fix --ansi --verbose --diff",
"deduplicate": "phpcpd app/ src/ --exclude src/Database/Migrations/2020-12-28-223112_create_auth_tables.php --exclude src/Authentication/Authenticators/HmacSha256.php",
"inspect": "vendor/bin/structarmed analyze",
"mutate": "infection --threads=2 --skip-initial-tests --coverage=build/phpunit",
"mutation:check": [
"Composer\\Config::disableProcessTimeout",
"@putenv PYRAMETER_DISABLED=1",
"infection --static-analysis-tool=phpstan --with-uncovered"
],
"mutation:filter": [
"Composer\\Config::disableProcessTimeout",
"@putenv PYRAMETER_DISABLED=1",
"bin/mutation-filter"
],
"sa": "@analyze",
"style": "@cs-fix",
"test": "phpunit"
Expand Down
19 changes: 0 additions & 19 deletions infection.json.dist

This file was deleted.

28 changes: 28 additions & 0 deletions infection.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "./vendor/infection/infection/resources/schema.json",
"source": {
"directories": [
"src/"
],
"excludes": [
"Config",
"Database/Migrations",
"Views"
]
},
"timeout": 30,
"threads": "max",
"dotsPerRow": "max",
"logs": {
"text": "build/infection.log"
},
"tmpDir": "build",
"minMsi": 72,
"minCoveredMsi": 79,
"ignoreMsiWithNoMutations": true,
"mutators": {
"@default": true
},
"bootstrap": "vendor/codeigniter4/framework/system/Test/bootstrap.php",
"testFramework": "phpunit"
}
69 changes: 69 additions & 0 deletions tests/Authentication/Filters/AuthRatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Authentication\Filters;

use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Shield\Filters\AuthRates;
use CodeIgniter\Throttle\Throttler;
use Config\Services;
use Tests\Support\TestCase;

/**
* @internal
*/
final class AuthRatesTest extends TestCase
{
public function testAllowsRequestWithinLimit(): void
{
$request = $this->createMock(IncomingRequest::class);
$request->method('getIPAddress')->willReturn('203.0.113.7');

$throttler = $this->createMock(Throttler::class);
$throttler->expects($this->once())
->method('check')
->with(md5('203.0.113.7'), 10, MINUTE, 1)
->willReturn(true);
Services::injectMock('throttler', $throttler);

$this->assertNull((new AuthRates())->before($request));
}

public function testRejectsRequestOverLimit(): void
{
$request = $this->createMock(IncomingRequest::class);
$request->method('getIPAddress')->willReturn('203.0.113.7');

$throttler = $this->createMock(Throttler::class);
$throttler->method('check')->willReturn(false);
$throttler->method('getTokenTime')->willReturn(42);
Services::injectMock('throttler', $throttler);

$response = (new AuthRates())->before($request);

$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertSame(429, $response->getStatusCode());
$this->assertSame(lang('Auth.throttled', [42]), $response->getReasonPhrase());
}

public function testIgnoresRequestThatIsNotIncomingRequest(): void
{
$throttler = $this->createMock(Throttler::class);
$throttler->expects($this->never())->method('check');
Services::injectMock('throttler', $throttler);

$this->assertNull((new AuthRates())->before($this->createStub(CLIRequest::class)));
}
}
17 changes: 17 additions & 0 deletions tests/Commands/GenerateDummyPasswordHashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

namespace Tests\Commands;

use CodeIgniter\Shield\Authentication\Passwords;
use CodeIgniter\Shield\Commands\GenerateDummyPasswordHash;
use CodeIgniter\Shield\Test\MockInputOutput;
use Config\Services;
use Tests\Support\TestCase;

/**
Expand Down Expand Up @@ -62,6 +64,21 @@ public function testGeneratesHashUsingCurrentArgon2idSettings(): void
$this->assertFalse(service('passwords')->needsRehash($hash));
}

public function testHashesThirtyTwoRandomBytesAsHex(): void
{
$passwords = $this->createMock(Passwords::class);
$passwords->expects($this->once())
->method('hash')
->with($this->matchesRegularExpression('/\A[0-9a-f]{64}\z/'))
->willReturn('$2y$04$hash');
Services::injectMock('passwords', $passwords);

$io = new MockInputOutput();
GenerateDummyPasswordHash::setInputOutput($io);

$this->assertNotFalse(command('shield:generate-dummy-hash'));
}

private function generateHash(): string
{
$io = new MockInputOutput();
Expand Down
9 changes: 9 additions & 0 deletions tests/Commands/Setup/ContentReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ public function testAddAfter(): void
$this->assertSame($expected, $output);
}

public function testAddReturnsTrueWhenTextWithRegexCharactersIsAlreadyPresent(): void
{
$replacer = new ContentReplacer();
$text = 'service(\'auth\')->routes($routes);';
$content = "<?php\n\n" . $text . "\n";

$this->assertTrue($replacer->add($content, $text, '/(.*)/su', '$1'));
}

public function testAddBefore(): void
{
$replacer = new ContentReplacer();
Expand Down
32 changes: 31 additions & 1 deletion tests/Commands/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,47 @@
use CodeIgniter\Shield\Test\MockInputOutput;
use Config\Email as EmailConfig;
use Config\Services;
use FilesystemIterator;
use org\bovigo\vfs\vfsStream;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Tests\Support\TestCase;

/**
* @internal
*/
final class SetupTest extends TestCase
{
private ?MockInputOutput $io = null;
private ?MockInputOutput $io = null;
private string $originalWorkingDirectory = '';
private string $workingDirectory = '';

protected function tearDown(): void
{
if ($this->workingDirectory !== '') {
chdir($this->originalWorkingDirectory);
$this->removeDirectory($this->workingDirectory);
}

parent::tearDown();

Setup::resetInputOutput();
}

private function removeDirectory(string $directory): void
{
$entries = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);

foreach ($entries as $entry) {
$entry->isDir() && ! $entry->isLink() ? rmdir($entry->getPathname()) : unlink($entry->getPathname());
}

rmdir($directory);
}

/**
* Set MockInputOutput and user inputs.
*
Expand Down Expand Up @@ -175,6 +199,12 @@ class Autoload extends AutoloadConfig
*/
private function createFilesystem(): string
{
$this->originalWorkingDirectory = (string) getcwd();
$this->workingDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'shield_setup_' . bin2hex(random_bytes(8));

mkdir($this->workingDirectory);
chdir($this->workingDirectory);

$root = vfsStream::setup('root');
vfsStream::copyFromFileSystem(
APPPATH,
Expand Down
Loading
Loading