diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b480c461..b1e6d69f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,11 +1,11 @@ name: Tests, Behat, Coding Standards on: push jobs: - test: + test-unit: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.3', '7.2'] + php-versions: ['8.3', '7.4'] steps: - uses: actions/checkout@master - name: Setup PHP @@ -14,11 +14,13 @@ jobs: php-version: ${{ matrix.php-versions }} extensions: mbstring, intl ini-values: post_max_size=256M, short_open_tag=On - tools: phpunit, composer - - name: Run tests + tools: composer + - name: Install run: | composer install - ./vendor/bin/phpunit -c ./phpunit.xml.dist + - name: Run tests + run: | + composer test:unit behat: runs-on: ubuntu-latest services: diff --git a/composer.json b/composer.json index 5867e99d..2211e200 100644 --- a/composer.json +++ b/composer.json @@ -25,18 +25,15 @@ "php": ">=7.0" }, "require-dev": { - "behat/mink": "1.7.1", - "behat/mink-goutte-driver": "^1.2", - "brain/monkey": "^1.5", - "composer/package-versions-deprecated": "^1.11", - "dmore/behat-chrome-extension": "^1.3", - "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1", - "friends-of-behat/mink-extension": "^2.5", - "paulgibbs/behat-wordpress-extension": "^3.3", + "behat/mink": "^v1.11.0", + "behat/mink-browserkit-driver": "^v2.2.0", + "dmore/behat-chrome-extension": "^1.4.0", + "dealerdirect/phpcodesniffer-composer-installer": "^v1.0.0", + "friends-of-behat/mink-extension": "^v2.7.5", "phpcompatibility/phpcompatibility-wp": "^2.1", - "phpunit/phpunit": "^4", "wp-cli/wp-cli-bundle": "@stable", - "wp-coding-standards/wpcs": "^3.0" + "wp-coding-standards/wpcs": "^3.0.1", + "yoast/wp-test-utils": "^1.2.0" }, "autoload": { "psr-4": { @@ -61,7 +58,8 @@ ], "lint-php": [ "phpcs --standard=phpcs.xml -s" - ] + ], + "test:unit": [ "phpunit -c phpunit.xml.dist --testsuite unit" ] }, "config": { "allow-plugins": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3047bcc1..a10ee074 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,25 +1,24 @@ - - - - - tests/Unit - - - tests/Integration - - - - - + + + + tests/Unit + tests/Unit/AntispamBeeTest.php + + + tests/Integration + + + + + diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index 732be189..00000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,45 +0,0 @@ -getName() ) ) { case 'unit': $this->stub_functions(); diff --git a/tests/Unit/AntispamBeeTest.php b/tests/Unit/AntispamBeeTest.php index 9e979dc5..19239b15 100644 --- a/tests/Unit/AntispamBeeTest.php +++ b/tests/Unit/AntispamBeeTest.php @@ -3,12 +3,16 @@ namespace AntispamBee\Tests\Unit\Core; use Antispam_Bee as Testee; -use AntispamBee\Tests\TestCase; -use Brain\Monkey\Functions; +use Yoast\WPTestUtils\BrainMonkey\TestCase; + +use function Brain\Monkey\Functions\expect; +use function Brain\Monkey\Functions\when; /** * Test case for the factory class. * + * TODO: This is a legacy test from 2.x. Update or remove. + * * @since 2.7.0 */ class FactoryTest extends TestCase { @@ -18,20 +22,20 @@ class FactoryTest extends TestCase { * * @since 2.7.0 */ - protected function setUp() { - parent::setUp(); + protected function set_up() { + parent::set_up(); - Functions::when( 'get_bloginfo' )->justReturn( 'https://domain.com/' ); - Functions::when( 'wp_parse_url' )->alias('parse_url'); - Functions::when( 'is_admin' )->justReturn( false ); - Functions::expect( 'wp_unslash' ) + when( 'get_bloginfo' )->justReturn( 'https://domain.com/' ); + when( 'wp_parse_url' )->alias('parse_url'); + when( 'is_admin' )->justReturn( false ); + expect( 'wp_unslash' ) ->andReturnUsing( function( $data ) { return $data; } ); - Functions::when( 'get_option' )->justReturn( $this->get_options() ); + when( 'get_option' )->justReturn( $this->get_options() ); Testee::init(); } @@ -81,18 +85,16 @@ public function test_gets_ip_address() { * * @covers Testee::handle_incoming_request() * - * @param array $comment Comment overrides to use. - * @param string $reason Expected spam reason to catch. */ - public function test_spam_reasons( $comment, $reason ) { - $comment = array_merge( $this->get_base_comment(), $comment ); + public function test_spam_reasons($comment, $reason) { + $comment = array_merge($this->get_base_comment(), $comment); $_SERVER['HTTP_CLIENT_IP'] = '12.23.34.45'; $_SERVER['REQUEST_URI'] = 'https://domain.com/wp-comments-post.php'; $_POST['comment'] = $comment; // This is where we check for the spam reason that was detected. - Functions::expect( 'add_comment_meta' )->once() + expect( 'add_comment_meta' )->once() ->with( 1, 'antispam_bee_reason', @@ -133,7 +135,7 @@ public function spam_reasons_data_provider() { // @ToDo: static $_reason array( array( - 'comment_content' => "this is a pharmacy, why does it work now?.", + 'comment_content' => "this is a pharmacy, why does it work now?.", 'comment_author_email' => 'test@yandex.ru', ), 'regexp', diff --git a/tests/Unit/Rules/EmptyDataTest.php b/tests/Unit/Rules/EmptyDataTest.php new file mode 100644 index 00000000..5e36d07f --- /dev/null +++ b/tests/Unit/Rules/EmptyDataTest.php @@ -0,0 +1,33 @@ + ContentTypeHelper::COMMENT_TYPE ); + self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result for empty comment' ); + + $item['comment_content'] = 'This is a test.'; + self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result for empty author IP' ); + + $item['comment_author_IP'] = '192.0.2.91'; + when( 'get_option' )->justReturn( false ); + self::assertSame( 0, EmptyData::verify( $item ), 'unexpected result with no name required' ); + + when( 'get_option' )->justReturn( true ); + self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result without name and mail' ); + + $item['comment_author_email'] = 'comments@example.com'; + self::assertSame( 999, EmptyData::verify( $item ), 'unexpected result without author name' ); + + $item['comment_author'] = 'A. Tester'; + self::assertSame( 0, EmptyData::verify( $item ), 'unexpected result with name and mail' ); + } +}