Skip to content

Commit

Permalink
Merge pull request #39 from JDGrimes/develop
Browse files Browse the repository at this point in the history
0.4.0
  • Loading branch information
JDGrimes authored Dec 20, 2017
2 parents 8b72daf + 425d24b commit 091f273
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 36 deletions.
29 changes: 23 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# Travis CI config.

language: php
php:
- 5.6
- 5.5
- 5.4
- 5.3
- hhvm

matrix:
include:
- php: 7.2
- php: 7.1
- php: 7.0
- php: 5.6
- php: 5.5
- php: 5.4
# Use Ubuntu Precise because Trusty doesn't support PHP 5.3.
- php: 5.3
dist: precise
- php: nightly
allow_failures:
- php: nightly

before_script: |
if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." || $TRAVIS_PHP_VERSION == nightly ]]; then
export PATH="$HOME/.config/composer/vendor/bin:$PATH"
composer global require "phpunit/phpunit=^5.7.15"
fi
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ To see the basic usage and check that everything is working, type the command:
Usage
-----

`$ wp-l10n-validator -[1c] TEXTDOMAIN [CONFIG]`
`$ wp-l10n-validator -[1c] TEXTDOMAIN [CONFIG] [-- FILE ...]`

This validates all `.php` files in the current directory for proper gettexting.

Arguments:
* `TEXTDOMAIN` - The textdomain used in your project.
* `CONFIG` - Configuration to use. Corressponds to one of the directories in `/config` (`wordpress` by default).
* `FILE` - One or more files to validate. You must pass `--` before the list of files, like this: `wp-l10n-validator textdomain -- a.php b.php`

Flags:
* `1` - Parse only one file at a time.
Expand Down Expand Up @@ -84,11 +85,22 @@ These are the options that you can specify in the the JSON config file:
* `textdomain` - Your project's textdomain.
* `basedir` - The main directory of your project (if different from the current directory).
* `config` - The configuration to use ([see CLI arguments above](#usage)).
* `cache` - The file to store the cache in. The default is `wp-l10n-validator.cache`.
* `cache` - The file to store the cache in. The default is `.wp-l10n-validator-cache.json`.
* `ignores-cache` - The file to store the specific ignores cache in. The default is
`wp-l10n-validator-ignores.cache`. See the `-c` flag above for more information.
`.wp-l10n-validator-ignores-cache.json`. See the `-c` flag above for more information.
* `ignores-tolerance` - The number of lines of difference to allow for when checking
against the ignores cache. The default is 5.
* `ignores-rules` - Configure which rules are used to determine if a string should be
ignored. It is an associative array with boolean values:
* `all-lowercase` — Ignore all strings that contain no uppercase characters. This
is a very useful rule to enable if you don't use any translatable strings that
are all lowercase. It is disabled by default to avoid false negatives. Add this
to your config to enable it:
```json
"ignores-rules": {
"all-lowercase": true
},
```
* `ignored-functions` - An associative array of functions to ignore. The value can be
an array of specific arguments to be ignored (by argument number), or simply `true`.
To ignore a class method, add it like this: `My_Class::my_method`. This will only
Expand Down
9 changes: 9 additions & 0 deletions config/default/ignores.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,32 @@
'uasort' => true,
'usort' => true,
'version_compare' => true,

// Predefined methods.
'DateTime::__construct' => true,
)
);

$parser->add_ignored_atts(
array(
'action',
'align',
'aria-hidden',
'autocomplete',
'class',
'cols',
'enctype',
'for',
'height',
'href',
'http-equiv',
'id',
'max',
'method',
'min',
'name',
'onclick',
'rows',
'scope',
'size',
'style',
Expand Down
4 changes: 4 additions & 0 deletions config/wordpress/ignores.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
$parser->add_ignored_functions(
array(
// Functions.
'_get_list_table' => array( 1 ),
'_deprecated_argument' => array( 1, 2 ),
'_deprecated_file' => array( 1, 2 ),
'_deprecated_function' => true,
Expand All @@ -27,6 +28,7 @@
'apply_filters' => array( 1 ),
'check_admin_referer' => true,
'check_ajax_referer' => true,
'checked' => true,
'current_time' => true,
'current_user_can' => true,
'dbDelta' => true,
Expand All @@ -44,6 +46,7 @@
'get_post_type_archive_link' => true,
'get_post_types' => array( 2, 3 ),
'get_site_transient' => true,
'get_submit_button' => array( 2, 3, 4 ),
'get_term' => true,
'get_transient' => true,
'get_user_by' => true,
Expand Down Expand Up @@ -94,6 +97,7 @@
'wp_redirect' => true,
'wp_register_script' => true,
'wp_register_style' => true,
'wp_reschedule_event' => array( 1, 2, 3 ),
'wp_reset_vars' => true,
'wp_safe_redirect' => true,
'wp_schedule_event' => array( 1, 2, 3 ),
Expand Down
5 changes: 4 additions & 1 deletion example-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"config":"wordpress",
"cache":"wp-l10n-validator-cache.json",
"ignores-cache":"wp-l10n-validator-ignores.json",
"ignores-rules": {
"all-lowercase": true
},
"ignored-functions": {
"my_plugin_func":true,
"my_plugin_do_something":true,
Expand All @@ -29,4 +32,4 @@
"/sub-dir/file.php"
],
"bootstrap":"./tools/wp-l10n-validator/config.php"
}
}
8 changes: 8 additions & 0 deletions tests/data/encapsed-strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@
'slug' => $slug,
)
);

$class_name = 'Some_Class_To_Ignore';
$constant_name = 'SOMETHING_IGNORED';

$class_name = 'some-class';
$class_name = 'another-';
$string = 'a string-with-hyphens';
$string = 'Twenty-one';
2 changes: 1 addition & 1 deletion tests/data/no-config/no-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

function do_some_things( $var ) {

$var = apply_filters( 'some-filter' );
$var = apply_filters( 'some filter' );

return $var;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/data/no-config/other.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Another file.
*/

function do_some_things( $var ) {
return $var;
}
28 changes: 28 additions & 0 deletions tests/data/with-config/ignored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* Ignored file.
*/

function do_some_things() {

global $wpdb;

return $wpdb->get_results( 'SELECT * FROM ' . $wpdb->posts );
}

function do_some_other_stuff() {

echo 'Hello world';
echo 'Dlrow olleh';
}

function display_message() {

_e( 'Message', 'textdomain' );
func_to_ignore( 'yes' );
}

class AClass {
protected $ignored = 'parent';
}
3 changes: 2 additions & 1 deletion tests/data/with-config/wp-l10n-validator.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"textdomain":"textdomain",
"config":"wordpress",
"ignored-paths": [ "/ignored" ],
"ignored-functions": {
"func_to_ignore":true
},
Expand All @@ -10,4 +11,4 @@
"ignored-strings": [
"Dlrow olleh"
]
}
}
37 changes: 34 additions & 3 deletions tests/tests/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_default_usage() {

$output = $this->run_command( 'wp-l10n-validator textdomain default', '/no-config' );
$this->assertEquals(
"/no-config.php#9 apply_filters( 1 ): Non gettexted string 'some-filter'"
"/no-config.php#9 apply_filters( 1 ): Non gettexted string 'some filter'"
. "\n/no-config.php#16: Non gettexted string 'Hello world'"
. "\n/no-config.php#21 _e( 1 ): Non gettexted string 'Message'"
. "\n/no-config.php#21 _e( 2 ): Non gettexted string 'textdomain'"
Expand Down Expand Up @@ -65,7 +65,7 @@ public function test_ignores_cache_generation() {
$this->assertEmpty( $output );
$this->assertEquals( 0, $this->exit_code );

$ignores_cache = dirname( __DIR__ ) . '/data/with-config/wp-l10n-validator-ignores.cache';
$ignores_cache = dirname( __DIR__ ) . '/data/with-config/.wp-l10n-validator-ignores-cache.json';

if ( ! ($content = @file_get_contents( $ignores_cache )) )
$this->fail( 'The ignores cache file was not generated, or could not be read.' );
Expand All @@ -76,6 +76,37 @@ public function test_ignores_cache_generation() {
$this->assertEquals( 0, $this->exit_code );
}

/**
* Test passing a list of files to check via the command line.
*
* @since 0.4.0
*/
public function test_files_passed() {

$output = $this->run_command( 'wp-l10n-validator -- other.php', '/no-config' );
$this->assertEquals( 0, strpos( $output, 'Usage:' ) );
$this->assertEquals( 1, $this->exit_code );

$output = $this->run_command( 'wp-l10n-validator textdomain -- other.php', '/no-config' );
$this->assertEquals( '', $output );
$this->assertEquals( 0, $this->exit_code );

// With a dot before the file name.
$output = $this->run_command( 'wp-l10n-validator textdomain -- ./no-config.php', '/no-config' );
$this->assertEquals( "/no-config.php#16: Non gettexted string 'Hello world'", $output );
$this->assertEquals( 1, $this->exit_code );

// Multiple files.
$output = $this->run_command( 'wp-l10n-validator textdomain -- other.php no-config.php', '/no-config' );
$this->assertEquals( "/no-config.php#16: Non gettexted string 'Hello world'", $output );
$this->assertEquals( 1, $this->exit_code );

// Passing an ignored file.
$output = $this->run_command( 'wp-l10n-validator textdomain -- ignored.php with-config.php', '/with-config' );
$this->assertEquals( "/with-config.php#16: Non gettexted string 'Hello world'", $output );
$this->assertEquals( 1, $this->exit_code );
}

/**
* Run a command and return the output.
*
Expand Down Expand Up @@ -109,7 +140,7 @@ protected function run_command( $command, $working_dir ) {
fclose( $pipes[3] );
proc_close( $process );

@unlink( $working_dir . '/wp-l10n-validator.cache' );
@unlink( $working_dir . '/.wp-l10n-validator-cache.json' );

return trim( $output );
}
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/encapsed-strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public function test_nongettexted_encapsed_strings_found() {
$this->assertFoundNonGettextedString( 'Don\'t' );
$this->assertFoundNonGettextedString( 'this!' );
$this->assertFoundNonGettextedString( 'and/or' );
$this->assertFoundNonGettextedString( 'a string-with-hyphens' );
$this->assertFoundNonGettextedString( 'Twenty-one' );
}
}
Loading

0 comments on commit 091f273

Please sign in to comment.