Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Aug 2, 2023
1 parent 2bf05e8 commit b7e5193
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/phpunit/Utilities/Plugin_Request_Utility_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/

use WordPress\Plugin_Check\Checker\AJAX_Runner;
use WordPress\Plugin_Check\Checker\Check_Context;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks;
use WordPress\Plugin_Check\Checker\Checks\I18n_Usage_Check;
use WordPress\Plugin_Check\Checker\CLI_Runner;
use WordPress\Plugin_Check\Test_Data\Runtime_Check;
use WordPress\Plugin_Check\Test_Utils\Traits\With_Mock_Filesystem;
Expand Down Expand Up @@ -212,4 +216,70 @@ static function () use ( $custom_ignore_directories ) {
}
);
}

public function test_plugin_without_error_for_ignore_directories() {

$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-ignore-directories/load.php' );
$checks = new Checks();
$checks_to_run = array(
new I18n_Usage_Check(),
);

add_filter(
'wp_plugin_check_checks',
function( $checks ) {
return array(
'i18n_usage_check' => new I18n_Usage_Check(),
);
}
);

$results = $checks->run_checks( $check_context, $checks_to_run );

$this->assertInstanceOf( Check_Result::class, $results );
$this->assertEmpty( $results->get_warnings() );
$this->assertEmpty( $results->get_errors() );
}

public function test_plugin_with_error_for_ignore_directories() {

$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-ignore-directories/load.php' );
$checks = new Checks();
$checks_to_run = array(
new I18n_Usage_Check(),
);

add_filter(
'wp_plugin_check_checks',
function( $checks ) {
return array(
'i18n_usage_check' => new I18n_Usage_Check(),
);
}
);

add_filter( 'wp_plugin_check_ignore_directories', '__return_empty_array' );

$results = $checks->run_checks( $check_context, $checks_to_run );

$this->assertInstanceOf( Check_Result::class, $results );

$errors = $results->get_errors();

$this->assertNotEmpty( $errors );
$this->assertArrayHasKey( 'vendor/error.php', $errors );
$this->assertEquals( 2, $results->get_error_count() );

// Check for WordPress.WP.I18n.MissingTranslatorsComment error on Line no 26 and column no at 5.
$this->assertArrayHasKey( 11, $errors['vendor/error.php'] );
$this->assertArrayHasKey( 6, $errors['vendor/error.php'][11] );
$this->assertArrayHasKey( 'code', $errors['vendor/error.php'][11][6][0] );
$this->assertEquals( 'WordPress.WP.I18n.MissingTranslatorsComment', $errors['vendor/error.php'][11][6][0]['code'] );

// Check for WordPress.WP.I18n.NonSingularStringLiteralDomain error on Line no 33 and column no at 29.
$this->assertArrayHasKey( 18, $errors['vendor/error.php'] );
$this->assertArrayHasKey( 30, $errors['vendor/error.php'][18] );
$this->assertArrayHasKey( 'code', $errors['vendor/error.php'][18][30][0] );
$this->assertEquals( 'WordPress.WP.I18n.NonSingularStringLiteralDomain', $errors['vendor/error.php'][18][30][0]['code'] );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Plugin Name: Test Plugin i18n usage with Errors for Plugin Check
* Plugin URI: https://github.com/wordpress/plugin-check
* Description: Plugin Check plugin from the WordPress Performance Team, a collection of tests to help improve plugin performance.
* Requires at least: 6.0
* Requires PHP: 5.6
* Version: n.e.x.t
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-check-errors
*
* @package test-plugin-check-errors
*/

0 comments on commit b7e5193

Please sign in to comment.