Skip to content

Commit

Permalink
Merge pull request #566 from pedro-mendonca/dev
Browse files Browse the repository at this point in the history
Include tests in WPCS lint
  • Loading branch information
pedro-mendonca committed Jul 15, 2024
2 parents 1a331e3 + 18141a3 commit 55cb0db
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 88 deletions.
1 change: 0 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<exclude-pattern>*/lib/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>

<!-- Only scan PHP files. -->
<arg name="extensions" value="php"/>
Expand Down
1 change: 0 additions & 1 deletion tests/phpstan/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @package Translation_Stats
*/


// Set Translation Stats plugin version.
if ( ! defined( 'TRANSLATION_STATS_VERSION' ) ) {
define( 'TRANSLATION_STATS_VERSION', '1.2.5' );
Expand Down
24 changes: 12 additions & 12 deletions tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
* @package Translation_Stats
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
$translation_stats_tests_dir = getenv( 'WP_TESTS_DIR' );

if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
if ( ! $translation_stats_tests_dir ) {
$translation_stats_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
$translation_stats_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $translation_stats_phpunit_polyfills_path ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $translation_stats_phpunit_polyfills_path ); // phpcs:ignore
}

if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) {
echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ( ! file_exists( "{$translation_stats_tests_dir}/includes/functions.php" ) ) {
echo "Could not find {$translation_stats_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit( 1 );
}

// Give access to tests_add_filter() function.
require_once "{$_tests_dir}/includes/functions.php";
require_once "{$translation_stats_tests_dir}/includes/functions.php";

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
function translation_stats_manually_load_plugin() {
require dirname( dirname( __DIR__ ) ) . '/translation-stats.php';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
tests_add_filter( 'muplugins_loaded', 'translation_stats_manually_load_plugin' );

// Start up the WP testing environment.
require "{$_tests_dir}/includes/bootstrap.php";
require "{$translation_stats_tests_dir}/includes/bootstrap.php";
23 changes: 18 additions & 5 deletions tests/phpunit/includes/test-admin-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package Translation_Stats
*/

Use Translation_Stats\Admin_Notice;
use Translation_Stats\Admin_Notice;


/**
Expand Down Expand Up @@ -69,6 +69,9 @@ public function provide_test_sanitize_type() {
* Test type sanitization.
*
* @dataProvider provide_test_sanitize_type
*
* @param string $type The type of the admin notice.
* @param string $expected_result The expected result for the test.
*/
public function test_sanitize_type( $type, $expected_result ) {

Expand All @@ -79,7 +82,6 @@ public function test_sanitize_type( $type, $expected_result ) {
$admin_notice = new Admin_Notice( $args );

$this->assertSame( $admin_notice->type, $expected_result );

}

/**
Expand Down Expand Up @@ -291,6 +293,17 @@ public function provide_test_notice_html() {
* Test wrap sanitization.
*
* @dataProvider provide_test_notice_html
*
* @param string $type The type of the admin notice.
* @param bool $notice_alt Wether to set as notice-alt.
* @param bool $inline Wether to set as inline.
* @param bool $dismissible Wether to set as dismissible.
* @param array $additional_classes The type of the admin notice.
* @param bool $update_icon Wether to use the update-icon.
* @param string $message The message of the admin notice.
* @param string $wrap The HTML to wrap the admin notice message.
* @param string $extra_html The extra HTML markup to add to the end of the admin message.
* @param string $expected_result The expected result for the test.
*/
public function test_notice_html( $type, $notice_alt, $inline, $dismissible, $additional_classes, $update_icon, $message, $wrap, $extra_html, $expected_result ) {

Expand All @@ -312,7 +325,6 @@ public function test_notice_html( $type, $notice_alt, $inline, $dismissible, $ad
$markup = $admin_notice->notice_html();

$this->assertSame( $markup, $expected_result );

}

/**
Expand Down Expand Up @@ -367,6 +379,9 @@ public function provide_test_sanitize_wrap() {
* Test wrap sanitization.
*
* @dataProvider provide_test_sanitize_wrap
*
* @param string $wrap The HTML to wrap the admin notice message.
* @param string $expected_result The expected result for the test.
*/
public function test_sanitize_wrap( $wrap, $expected_result ) {

Expand All @@ -377,7 +392,6 @@ public function test_sanitize_wrap( $wrap, $expected_result ) {
$admin_notice = new Admin_Notice( $args );

$this->assertSame( $admin_notice->wrap, $expected_result );

}

/**
Expand All @@ -394,6 +408,5 @@ public function test_render() {
$admin_notice = new Admin_Notice( $args );

$admin_notice->render();

}
}
33 changes: 16 additions & 17 deletions tests/phpunit/includes/test-locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* @package Translation_Stats
*/

Use Translation_Stats\Locale;
Use Translation_Stats\GP_Locale;
use Translation_Stats\Locale;
use Translation_Stats\GP_Locale;


/**
Expand All @@ -20,29 +20,28 @@ class Test_Locale extends WP_UnitTestCase {
public function test_construct() {

$pt = new GP_Locale();
$pt->english_name = 'Portuguese (Portugal)';
$pt->native_name = 'Português';

$pt->english_name = 'Portuguese (Portugal)';
$pt->native_name = 'Português';
$pt->lang_code_iso_639_1 = 'pt';
$pt->country_code = 'pt';
$pt->wp_locale = 'pt_PT';
$pt->slug = 'pt';
$pt->google_code = 'pt-PT';
$pt->facebook_locale = 'pt_PT';
$pt->country_code = 'pt';
$pt->wp_locale = 'pt_PT';
$pt->slug = 'pt';
$pt->google_code = 'pt-PT';
$pt->facebook_locale = 'pt_PT';

$locale = new Locale( $pt );

$this->assertNull( $locale->translations );

$this->assertSame(
$locale->locale_slug,
'pt/default'
);
$locale->locale_slug,
'pt/default'
);

$this->assertSame(
$locale->wporg_subdomain,
'pt'
);

$locale->wporg_subdomain,
'pt'
);
}

}
8 changes: 3 additions & 5 deletions tests/phpunit/includes/test-locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* @package Translation_Stats
*/

Use Translation_Stats\Locales;
Use Translation_Stats\GP_Locales;
use Translation_Stats\Locales;
use Translation_Stats\GP_Locales;


/**
Expand All @@ -22,10 +22,8 @@ public function test_instance() {
$this->assertFalse( isset( $GLOBALS['translation_stats_locales'] ) );

// Get wordpress.org Locales.
$locales = Locales::locales();
Locales::locales();

$this->assertTrue( isset( $GLOBALS['translation_stats_locales'] ) );

}

}
11 changes: 5 additions & 6 deletions tests/phpunit/includes/test-translations-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package Translation_Stats
*/

Use Translation_Stats\Translations_API;
use Translation_Stats\Translations_API;


/**
Expand Down Expand Up @@ -37,7 +37,6 @@ public function test_get_plugin_slug() {
$plugin_slug = Translations_API::get_plugin_slug( 'translation-stats' );

$this->assertSame( $plugin_slug, 'translation-stats' );

}


Expand All @@ -61,7 +60,6 @@ public function test_translations_api_get_plugin() {
$response_code = wp_remote_retrieve_response_code( $response );

$this->assertSame( $response_code, 404 );

}


Expand Down Expand Up @@ -99,7 +97,6 @@ public function test_plugin_subprojects() {
$plugin_subprojects,
$expected
);

}


Expand Down Expand Up @@ -180,6 +177,10 @@ public function provide_test_translate_url() {
* Test the translate site URL.
*
* @dataProvider provide_test_translate_url
*
* @param string $project The project.
* @param bool $api Wether to get an API URL.
* @param string $expected_result The expected result for the test.
*/
public function test_translate_url( $project, $api, $expected_result ) {

Expand All @@ -189,7 +190,5 @@ public function test_translate_url( $project, $api, $expected_result ) {
$translate_url,
$expected_result
);

}

}
9 changes: 3 additions & 6 deletions tests/phpunit/includes/test-translations-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package Translation_Stats
*/

Use Translation_Stats\Translation_Stats;
use Translation_Stats\Translation_Stats;


/**
Expand All @@ -23,7 +23,7 @@ public function test_plugin_action_links() {
'deactivate' => '<a href="#" id="deactivate-akismet" aria-label="Deactivate Akismet">Deactivate</a>',
);

$translation_stats = new Translation_Stats;
$translation_stats = new Translation_Stats();

$plugin_action_links = $translation_stats->plugin_action_links( $links );

Expand All @@ -34,7 +34,6 @@ public function test_plugin_action_links() {
'deactivate' => '<a href="#" id="deactivate-akismet" aria-label="Deactivate Akismet">Deactivate</a>',
)
);

}


Expand All @@ -43,7 +42,7 @@ public function test_plugin_action_links() {
*/
public function test_allowed_pages() {

$translation_stats = new Translation_Stats;
$translation_stats = new Translation_Stats();

// Allowed pages.
$this->assertTrue( $translation_stats->allowed_pages( 'plugins.php' ) );
Expand All @@ -57,7 +56,5 @@ public function test_allowed_pages() {
$this->assertFalse( $translation_stats->allowed_pages( null ) );
$this->assertFalse( $translation_stats->allowed_pages( true ) );
$this->assertFalse( $translation_stats->allowed_pages( false ) );

}

}
Loading

0 comments on commit 55cb0db

Please sign in to comment.