Skip to content

Commit

Permalink
Add PHPCS config and fix errors (#75)
Browse files Browse the repository at this point in the history
* Autofix WordPress-Core CS

* Fix WordPress.PHP.YodaConditions

* Fix Squiz.PHP.DisallowMultipleAssignments

* Fix WordPress.PHP.StrictInArray

* Fix WordPress.NamingConventions.ValidVariableName

* Fix critical error: missing break in WXR_Parser_XML::tag_open()

* Ignore WordPress.DB.PreparedSQL

* Fix capitalizing our ID in register_importer()

* Ignore WordPress.Files.FileName for tests

* Fix unstable caching configuration in CI

* Fix HTML indentation in WP_Import::import_options()

* Revert "Ignore WordPress.Files.FileName for tests"

This reverts commit 6e6069c.

* Add PHPCS configuration file

* Add PHPCS to CI

* Separate CI job for PHPCS
  • Loading branch information
szepeviktor authored Apr 17, 2020
1 parent 7238644 commit 64e575a
Show file tree
Hide file tree
Showing 13 changed files with 747 additions and 487 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist: trusty

language: php

notifications:
Expand All @@ -8,11 +9,10 @@ notifications:

cache:
directories:
- vendor
# Cache directory for older Composer versions.
- $HOME/.composer/cache/files
- $HOME/.composer/cache
# Cache directory for more recent Composer versions.
- $HOME/.cache/composer/files
- $HOME/.cache/composer

env:
global:
Expand All @@ -24,6 +24,11 @@ matrix:
env: WP_VERSION=trunk
- php: 7.4
- php: 7.3
- php: 7.3
script:
- |
composer global require wp-coding-standards/wpcs phpcompatibility/phpcompatibility-wp dealerdirect/phpcodesniffer-composer-installer
phpcs
- php: 7.2
- php: 7.1
- php: 7.0
Expand Down
24 changes: 24 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<ruleset name="WordPressImporterCodingStandards">
<description>Coding Standards for WordPress Importer.</description>

<!-- Supported PHP versions -->
<config name="testVersion" value="5.2-"/>

<file>.</file>
<exclude-pattern>*/vendor/</exclude-pattern>

<rule ref="WordPress-Core">
<exclude name="WordPress.PHP.StrictComparisons"/>
<exclude name="WordPress.WP.I18n"/>
</rule>
<rule ref="WordPress.Files.FileName">
<exclude-pattern>*/phpunit/tests/*\.php$</exclude-pattern>
</rule>
<!-- next up:
<rule ref="WordPress-Extra">
<exclude name="WordPress.Security.EscapeOutput"/>
</rule>
-->
<rule ref="PHPCompatibilityWP"/>
</ruleset>
3 changes: 2 additions & 1 deletion phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
$_tests_dir = getenv( 'WP_TESTS_DIR' );

// Check if we're installed in a src checkout.
if ( ! $_tests_dir && false !== ( $pos = stripos( __FILE__, '/src/wp-content/plugins/' ) ) ) {
$pos = stripos( __FILE__, '/src/wp-content/plugins/' );
if ( ! $_tests_dir && false !== $pos ) {
$_tests_dir = substr( __FILE__, 0, $pos ) . '/tests/phpunit/';
}

Expand Down
4 changes: 3 additions & 1 deletion phpunit/tests/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ protected function _import_wp( $filename, $users = array(), $fetch_files = true
$this->assertTrue( ! empty( $file ), 'Path to import file is empty.' );
$this->assertTrue( is_file( $file ), 'Import file is not a file.' );

$authors = $mapping = $new = array();
$authors = array();
$mapping = array();
$new = array();
$i = 0;

// each user is either mapped to a given ID, mapped to a new user
Expand Down
6 changes: 4 additions & 2 deletions phpunit/tests/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function setUp() {
global $wpdb;
// crude but effective: make sure there's no residual data in the main tables
foreach ( array( 'posts', 'postmeta', 'comments', 'terms', 'term_taxonomy', 'term_relationships', 'users', 'usermeta' ) as $table ) {
// phpcs:ignore WordPress.DB.PreparedSQL
$wpdb->query( "DELETE FROM {$wpdb->$table}" );
}
}
Expand Down Expand Up @@ -258,7 +259,8 @@ function test_ordering_of_importers() {
'def1' => array( 'def1' ),
'xyz1' => array( 'xyz1' ),
'XYZ2' => array( 'XYZ2' ),
), get_importers()
),
get_importers()
);
$wp_importers = $_wp_importers; // Restore global state
}
Expand Down Expand Up @@ -289,7 +291,7 @@ public function test_slashes_should_not_be_stripped() {

$comments = get_comments(
array(
'post_id' => $posts[0]->post_ID
'post_id' => $posts[0]->post_ID,
)
);
$this->assertNotEmpty( $comments );
Expand Down
40 changes: 30 additions & 10 deletions phpunit/tests/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ function test_wxr_version_1_1() {
'author_display_name' => 'John Doe',
'author_first_name' => 'John',
'author_last_name' => 'Doe',
), $result['authors']['john'], $message
),
$result['authors']['john'],
$message
);
$this->assertEquals(
array(
Expand All @@ -72,15 +74,19 @@ function test_wxr_version_1_1() {
'category_parent' => '',
'cat_name' => 'alpha',
'category_description' => 'The alpha category',
), $result['categories'][0], $message
),
$result['categories'][0],
$message
);
$this->assertEquals(
array(
'term_id' => 22,
'tag_slug' => 'clippable',
'tag_name' => 'Clippable',
'tag_description' => 'The Clippable post_tag',
), $result['tags'][0], $message
),
$result['tags'][0],
$message
);
$this->assertEquals(
array(
Expand All @@ -90,7 +96,9 @@ function test_wxr_version_1_1() {
'term_parent' => '',
'term_name' => 'bieup',
'term_description' => 'The bieup post_tax',
), $result['terms'][0], $message
),
$result['terms'][0],
$message
);

$this->assertEquals( 2, count( $result['posts'] ), $message );
Expand All @@ -113,15 +121,19 @@ function test_wxr_version_1_1() {
'slug' => 'bieup',
'domain' => 'post_tax',
),
), $result['posts'][0]['terms'], $message
),
$result['posts'][0]['terms'],
$message
);
$this->assertEquals(
array(
array(
'key' => '_wp_page_template',
'value' => 'default',
),
), $result['posts'][1]['postmeta'], $message
),
$result['posts'][1]['postmeta'],
$message
);
}
}
Expand Down Expand Up @@ -154,7 +166,9 @@ function test_wxr_version_1_0() {
'slug' => 'uncategorized',
'domain' => 'category',
),
), $result['posts'][0]['terms'], $message
),
$result['posts'][0]['terms'],
$message
);
$this->assertEquals(
array(
Expand All @@ -173,7 +187,9 @@ function test_wxr_version_1_0() {
'slug' => 'roar',
'domain' => 'tag',
),
), $result['posts'][2]['terms'], $message
),
$result['posts'][2]['terms'],
$message
);
$this->assertEquals(
array(
Expand All @@ -192,7 +208,9 @@ function test_wxr_version_1_0() {
'slug' => 'face',
'domain' => 'tag',
),
), $result['posts'][3]['terms'], $message
),
$result['posts'][3]['terms'],
$message
);

$this->assertEquals(
Expand All @@ -201,7 +219,9 @@ function test_wxr_version_1_0() {
'key' => '_wp_page_template',
'value' => 'default',
),
), $result['posts'][1]['postmeta'], $message
),
$result['posts'][1]['postmeta'],
$message
);
}
}
Expand Down
7 changes: 6 additions & 1 deletion phpunit/tests/postmeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ function test_serialized_postmeta_with_slashes() {
$expected_integer = '1';
$expected_string = '¯\_(ツ)_/¯';
$expected_array = array( 'key' => '¯\_(ツ)_/¯' );
$expected_array_nested = array( 'key' => array( 'foo' => '¯\_(ツ)_/¯', 'bar' => '\o/' ) );
$expected_array_nested = array(
'key' => array(
'foo' => '¯\_(ツ)_/¯',
'bar' => '\o/',
),
);

$this->assertEquals( $expected_string, get_post_meta( 10, 'string', true ) );
$this->assertEquals( $expected_array, get_post_meta( 10, 'array', true ) );
Expand Down
Loading

0 comments on commit 64e575a

Please sign in to comment.