diff --git a/tests/checks/CodeCheckTestBase.php b/tests/checks/CodeCheckTestBase.php index 6f316ee..7680529 100644 --- a/tests/checks/CodeCheckTestBase.php +++ b/tests/checks/CodeCheckTestBase.php @@ -15,7 +15,7 @@ protected function checkFile( $filename ) { $file = array( new AnalyzedPhpFile( $filename, $file['php'][0] ) ); } - $this->assertFalse( $this->check->check( $file ) ); + $this->check->check( $file ); return $this->check->get_errors(); } diff --git a/tests/checks/test-BatcacheVariantCheck.php b/tests/checks/test-BatcacheVariantCheck.php new file mode 100644 index 0000000..91b6efd --- /dev/null +++ b/tests/checks/test-BatcacheVariantCheck.php @@ -0,0 +1,19 @@ +checkFile( 'BatcacheVariantTest1.inc' ); + $error_slug = wp_list_pluck( $actual_errors, 'slug' ); + $this->assertContains( 'batcache-variant-error', $error_slug ); + } + + public function testBatcacheVariantPositive() { + $actual_errors = $this->checkFile( 'BatcacheVariantTest2.inc' ); + $error_slug = wp_list_pluck( $actual_errors, 'slug' ); + $this->assertNotContains( 'batcache-variant-error', $error_slug ); + } + +} diff --git a/tests/data/BatcacheVariantTest1.inc b/tests/data/BatcacheVariantTest1.inc new file mode 100644 index 0000000..22e4fd3 --- /dev/null +++ b/tests/data/BatcacheVariantTest1.inc @@ -0,0 +1,7 @@ + function( $node ) { + $name = $node->name->toString(); + if ( 'vary_cache_on_function' == $name ) { + $value = $node->args[0]->value; + if ( $value instanceof PhpParser\Node\Scalar\String ) { + $function_string = $value->value; + if ( preg_match('/include|require|echo|print|dump|export|open|sock|unlink|`|eval/i', $function_string, $matches) ) { + $this->add_error( + 'batcache-variant-error', + 'Illegal word "'.$matches[0].'" in variant determiner.', + BaseScanner::LEVEL_BLOCKER + ); + } + if ( !preg_match('/\$_/', $function_string) ) { + $this->add_error( + 'batcache-variant-error', + 'Variant determiner should refer to at least one $_ superglobal.', + BaseScanner::LEVEL_BLOCKER + ); + } + } + } + } + ) ); + } +} diff --git a/vip-scanner/config-vip-scanner.php b/vip-scanner/config-vip-scanner.php index e9f688b..86ff0bc 100644 --- a/vip-scanner/config-vip-scanner.php +++ b/vip-scanner/config-vip-scanner.php @@ -11,6 +11,7 @@ VIP_Scanner::get_instance()->register_review( 'WP.com Theme Review', array( 'AtPackageCheck', + 'BatcacheVariantCheck', 'BodyClassCheck', 'CDNCheck', 'CheckedSelectedDisabledCheck', @@ -54,6 +55,7 @@ ) ); VIP_Scanner::get_instance()->register_review( 'VIP Theme Review', array( + 'BatcacheVariantCheck', 'CheckedSelectedDisabledCheck', 'DeprecatedConstantsCheck', 'DeprecatedFunctionsCheck',