diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ba3d18c..3e02b49 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,17 +4,23 @@ on: [push, pull_request] jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: fail-fast: true matrix: - php: [7.4, '8.0', 8.1, 8.2] + os: [ubuntu-latest] + php: [8.1, 8.2, 8.3] + laravel: [9.*, 10.*, 11.*] + stability: [prefer-lowest, prefer-stable] + exclude: + - php: 8.1 + laravel: 11.* - name: PHP${{ matrix.php }} + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -28,7 +34,9 @@ jobs: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Install dependencies - run: composer update --prefer-dist --no-interaction + run: | + composer require "illuminate/support:${{ matrix.laravel }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/phpunit + run: vendor/bin/pest diff --git a/.gitignore b/.gitignore index 30a3ccd..26e4749 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /vendor composer.lock .php-cs-fixer.cache -.phpunit.result.cache diff --git a/LICENSE b/LICENSE index 1eded4e..8546540 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ Webnuvola -Copyright 2019-2023 +Copyright 2019-2024 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ along with this program. If not, see . WordPress - Web publishing software -Copyright 2011-2023 by the contributors +Copyright 2011-2024 by the contributors This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/composer.json b/composer.json index 2d3ac1e..449f971 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,12 @@ } ], "require": { - "php": "^7.4 || ^8.0" + "php": "^8.1" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.14", - "illuminate/support": "^8.0 || ^9.0 || ^10.0", - "phpunit/phpunit": "^9.6.4" + "illuminate/support": "^9.0 || ^10.0 || ^11.0", + "pestphp/pest": "^2.34" }, "autoload": { "files": [ @@ -31,13 +31,16 @@ } }, "suggest": { - "illuminate/support": "Required to use antispambot_html function (^9.0||^10.0)" + "illuminate/support": "Required to use antispambot_html function (^9.0||^10.0||^11.0)" }, "scripts": { "test": "phpunit --testdox" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } }, "minimum-stability": "stable", "prefer-stable": true diff --git a/phpunit.xml.dist b/phpunit.xml similarity index 60% rename from phpunit.xml.dist rename to phpunit.xml index 5595351..0c12bb9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml @@ -1,17 +1,17 @@ - - - ./src - - - - tests + + ./tests + + + ./src + + diff --git a/src/Antispambot.php b/src/Antispambot.php index f392309..c3f7075 100644 --- a/src/Antispambot.php +++ b/src/Antispambot.php @@ -6,11 +6,6 @@ class Antispambot { /** * Converts email addresses characters to HTML entities to block spam bots. - * Based on WordPress function antispambot. - * - * @param string $email_address - * @param bool $hex_encoding - * @return string * * @see https://developer.wordpress.org/reference/functions/antispambot/ */ @@ -35,19 +30,9 @@ public static function antispambot(string $email_address, bool $hex_encoding = f /** * Add leading zeros when necessary. * - * If you set the threshold to '4' and the number is '10', then you will get - * back '0010'. If you set the threshold to '4' and the number is '5000', then you - * will get back '5000'. - * - * Uses sprintf to append the amount of zeros based on the $threshold parameter - * and the size of the number. If the number is large enough, then no zeros will - * be appended. - * - * @param int|string $number - * @param int $threshold - * @return string + * @see https://developer.wordpress.org/reference/functions/zeroise/ */ - protected static function zeroise($number, int $threshold): string + protected static function zeroise(int|string $number, int $threshold): string { return sprintf('%0' . $threshold . 's', $number); } diff --git a/tests/AntispambotTest.php b/tests/AntispambotTest.php deleted file mode 100644 index 33d3294..0000000 --- a/tests/AntispambotTest.php +++ /dev/null @@ -1,74 +0,0 @@ -assertNotEquals($email, $antispam); - $this->assertEquals($email, $this->decodeEmail($antispam)); - } - } - - public function testEmailObfuscationWithHexEncoding(): void - { - foreach (self::$emails as $email) { - $antispam = Antispambot::antispambot($email, true); - $this->assertNotEquals($email, $antispam); - $antispam = str_replace('+', '+', $antispam); - $this->assertEquals($email, html_entity_decode(urldecode($antispam), ENT_HTML5)); - } - } - - public function testAntispambotFunction(): void - { - $email = 'demo@example.com'; - - $result = antispambot($email); - $return = preg_match('/"([^"]+)"/', $result, $matches); - $this->assertEquals(1, $return); - $this->assertEquals('mailto:' . $email, $this->decodeEmail($matches[1])); - $this->assertEquals($email, $this->decodeEmail(strip_tags($result))); - - $result = antispambot($email, 'Contact us'); - $return = preg_match('/"([^"]+)"/', $result, $matches); - $this->assertEquals(1, $return); - $this->assertEquals('mailto:' . $email, $this->decodeEmail($matches[1])); - $this->assertEquals('Contact us', strip_tags($result)); - - $result = antispambot($email, 'Contact us', ['class' => 'text-white', 'target' => '_blank']); - $return = preg_match('/"([^"]+)"/', $result, $matches); - $this->assertEquals(1, $return); - $this->assertEquals('mailto:' . $email, $this->decodeEmail($matches[1])); - $this->assertEquals('Contact us', strip_tags($result)); - $this->assertNotEquals(false, strpos($result, 'class="text-white"')); - $this->assertNotEquals(false, strpos($result, 'target="_blank"')); - } - - public function testAntispambotHtmlFunction(): void - { - antispambot_html('demo@example.com'); - $this->assertTrue(true); - } - - protected function decodeEmail(string $encodedEmail): string - { - return html_entity_decode($encodedEmail, ENT_HTML5); - } -} diff --git a/tests/Datasets/Emails.php b/tests/Datasets/Emails.php new file mode 100644 index 0000000..4a6c69b --- /dev/null +++ b/tests/Datasets/Emails.php @@ -0,0 +1,9 @@ +in('Feature'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..92bc416 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,10 @@ +not->toBe($email) + ->and(decode_email($antispambot))->toBe($email); +})->with('emails'); + +it('can obfuscate email with hex encoding', function (string $email) { + $antispambot = Antispambot::antispambot($email, true); + + expect($antispambot) + ->not->toBe($email) + ->and(html_entity_decode(urldecode(str_replace('+', '+', $antispambot)), ENT_HTML5))->toBe($email); +})->with('emails'); + +test('antispambot function return value', function (string $email) { + $result = antispambot($email); + $return = preg_match('/"([^"]+)"/', $result, $matches); + expect($return) + ->toBe(1) + ->and(decode_email($matches[1]))->toBe('mailto:' . $email) + ->and(decode_email(strip_tags($result)))->toBe($email); + + $result = antispambot($email, 'Contact us'); + $return = preg_match('/"([^"]+)"/', $result, $matches); + expect($return) + ->toBe(1) + ->and(decode_email($matches[1]))->toBe('mailto:' . $email) + ->and(strip_tags($result))->toBe('Contact us'); + + $result = antispambot($email, 'Contact us', ['class' => 'text-white', 'target' => '_blank']); + $return = preg_match('/"([^"]+)"/', $result, $matches); + expect($return) + ->toBe(1) + ->and(decode_email($matches[1]))->toBe('mailto:' . $email) + ->and(strip_tags($result))->toBe('Contact us') + ->and(str_contains($result, 'class="text-white"'))->toBeTrue() + ->and(str_contains($result, 'target="_blank"'))->toBeTrue(); +})->with(['demo@example.com']); + +test('antispambot_html function return value', function (string $email) { + $antispambotHtml = antispambot_html($email); + + expect($antispambotHtml) + ->toBeInstanceOf(HtmlString::class) + ->and(decode_email(strip_tags((string) $antispambotHtml)))->toBe($email); +})->with(['demo@example.com']);