Apply coding standards to unit test (except some mocks)
This commit is contained in:
parent
5e2dd4b6e1
commit
c77e1ee012
@ -20,17 +20,17 @@ const ABSPATH = false;
|
|||||||
/**
|
/**
|
||||||
* The StatifyBlacklist base class.
|
* The StatifyBlacklist base class.
|
||||||
*/
|
*/
|
||||||
require_once( 'inc/class-statifyblacklist.php' );
|
require_once __DIR__ . '/../inc/class-statifyblacklist.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The StatifyBlacklist system class.
|
* The StatifyBlacklist system class.
|
||||||
*/
|
*/
|
||||||
require_once( 'inc/class-statifyblacklist-system.php' );
|
require_once __DIR__ . '/../inc/class-statifyblacklist-system.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The StatifyBlacklist admin class.
|
* The StatifyBlacklist admin class.
|
||||||
*/
|
*/
|
||||||
require_once( 'inc/class-statifyblacklist-admin.php' );
|
require_once __DIR__ . '/../inc/class-statifyblacklist-admin.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class StatifyBlacklistTest.
|
* Class StatifyBlacklistTest.
|
||||||
@ -43,6 +43,8 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test simple referer filter.
|
* Test simple referer filter.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_referer_filter() {
|
public function test_referer_filter() {
|
||||||
// Prepare Options: 2 blacklisted domains, disabled.
|
// Prepare Options: 2 blacklisted domains, disabled.
|
||||||
@ -103,8 +105,10 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test referer filter using regular expressions.
|
* Test referer filter using regular expressions.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testRefererRegexFilter() {
|
public function test_referer_regex_filter() {
|
||||||
// Prepare Options: 2 regular expressions.
|
// Prepare Options: 2 regular expressions.
|
||||||
StatifyBlacklist::$_options = array(
|
StatifyBlacklist::$_options = array(
|
||||||
'referer' => array(
|
'referer' => array(
|
||||||
@ -158,8 +162,10 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the upgrade methodology for configuration options.
|
* Test the upgrade methodology for configuration options.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testUpgrade() {
|
public function test_upgrade() {
|
||||||
// Create configuration of version 1.3.
|
// Create configuration of version 1.3.
|
||||||
$options13 = array(
|
$options13 = array(
|
||||||
'active_referer' => 1,
|
'active_referer' => 1,
|
||||||
@ -179,41 +185,43 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
StatifyBlacklist_System::upgrade();
|
StatifyBlacklist_System::upgrade();
|
||||||
|
|
||||||
// Retrieve updated options.
|
// Retrieve updated options.
|
||||||
$optionsUpdated = get_option( 'statify-blacklist' );
|
$options_updated = get_option( 'statify-blacklist' );
|
||||||
|
|
||||||
// Verify size against default options (no junk left).
|
// Verify size against default options (no junk left).
|
||||||
$this->assertEquals( 4, count( $optionsUpdated ) );
|
$this->assertEquals( 4, count( $options_updated ) );
|
||||||
$this->assertEquals( 4, count( $optionsUpdated['referer'] ) );
|
$this->assertEquals( 4, count( $options_updated['referer'] ) );
|
||||||
$this->assertEquals( 4, count( $optionsUpdated['target'] ) );
|
$this->assertEquals( 4, count( $options_updated['target'] ) );
|
||||||
$this->assertEquals( 2, count( $optionsUpdated['ip'] ) );
|
$this->assertEquals( 2, count( $options_updated['ip'] ) );
|
||||||
|
|
||||||
// Verify that original attributes are unchanged.
|
// Verify that original attributes are unchanged.
|
||||||
$this->assertEquals( $options13['active_referer'], $optionsUpdated['referer']['active'] );
|
$this->assertEquals( $options13['active_referer'], $options_updated['referer']['active'] );
|
||||||
$this->assertEquals( $options13['cron_referer'], $optionsUpdated['referer']['cron'] );
|
$this->assertEquals( $options13['cron_referer'], $options_updated['referer']['cron'] );
|
||||||
$this->assertEquals( $options13['referer'], $optionsUpdated['referer']['blacklist'] );
|
$this->assertEquals( $options13['referer'], $options_updated['referer']['blacklist'] );
|
||||||
$this->assertEquals( $options13['referer_regexp'], $optionsUpdated['referer']['regexp'] );
|
$this->assertEquals( $options13['referer_regexp'], $options_updated['referer']['regexp'] );
|
||||||
|
|
||||||
// Verify that new attributes are present in config and filled with default values (disabled, empty).
|
// Verify that new attributes are present in config and filled with default values (disabled, empty).
|
||||||
$this->assertEquals( 0, $optionsUpdated['target']['active'] );
|
$this->assertEquals( 0, $options_updated['target']['active'] );
|
||||||
$this->assertEquals( 0, $optionsUpdated['target']['cron'] );
|
$this->assertEquals( 0, $options_updated['target']['cron'] );
|
||||||
$this->assertEquals( 0, $optionsUpdated['target']['regexp'] );
|
$this->assertEquals( 0, $options_updated['target']['regexp'] );
|
||||||
$this->assertEquals( array(), $optionsUpdated['target']['blacklist'] );
|
$this->assertEquals( array(), $options_updated['target']['blacklist'] );
|
||||||
$this->assertEquals( 0, $optionsUpdated['ip']['active'] );
|
$this->assertEquals( 0, $options_updated['ip']['active'] );
|
||||||
$this->assertEquals( array(), $optionsUpdated['ip']['blacklist'] );
|
$this->assertEquals( array(), $options_updated['ip']['blacklist'] );
|
||||||
|
|
||||||
// Verify that version number has changed to current release.
|
// Verify that version number has changed to current release.
|
||||||
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $optionsUpdated['version'] );
|
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test CIDR address matching for IP filter (#7)
|
* Test CIDR address matching for IP filter (#7).
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testCidrMatch() {
|
public function test_cidr_match() {
|
||||||
// IPv4 tests.
|
// IPv4 tests.
|
||||||
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1' ) ) );
|
$this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1' ) ) );
|
||||||
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/32' ) ) );
|
$this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/32' ) ) );
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
'127.0.0.1/33',
|
'127.0.0.1/33',
|
||||||
@ -221,7 +229,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
'127.0.0.1/-1',
|
'127.0.0.1/-1',
|
||||||
@ -229,7 +237,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'192.0.2.123',
|
'192.0.2.123',
|
||||||
'192.0.2.0/24',
|
'192.0.2.0/24',
|
||||||
@ -237,7 +245,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'192.0.3.123',
|
'192.0.3.123',
|
||||||
'192.0.2.0/24',
|
'192.0.2.0/24',
|
||||||
@ -245,7 +253,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'192.0.2.123',
|
'192.0.2.123',
|
||||||
'192.0.2.120/29',
|
'192.0.2.120/29',
|
||||||
@ -253,16 +261,16 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'192.0.2.128',
|
'192.0.2.128',
|
||||||
'192.0.2.120/29',
|
'192.0.2.120/29',
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '10.11.12.13', '10.0.0.0/8' ) ) );
|
$this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '10.11.12.13', '10.0.0.0/8' ) ) );
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'10.11.12.345',
|
'10.11.12.345',
|
||||||
'10.0.0.0/8',
|
'10.0.0.0/8',
|
||||||
@ -271,12 +279,12 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// IPv6 tests.
|
// IPv6 tests.
|
||||||
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1' ) ) );
|
$this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1' ) ) );
|
||||||
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/128' ) ) );
|
$this->assertTrue( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/128' ) ) );
|
||||||
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/129' ) ) );
|
$this->assertFalse( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/129' ) ) );
|
||||||
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/-1' ) ) );
|
$this->assertFalse( invoke_static( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/-1' ) ) );
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'2001:db8:a0b:12f0:1:2:3:4',
|
'2001:db8:a0b:12f0:1:2:3:4',
|
||||||
'2001:db8:a0b:12f0::1/64 ',
|
'2001:db8:a0b:12f0::1/64 ',
|
||||||
@ -284,7 +292,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'2001:db8:a0b:12f0::123:456',
|
'2001:db8:a0b:12f0::123:456',
|
||||||
'2001:db8:a0b:12f0::1/96 ',
|
'2001:db8:a0b:12f0::1/96 ',
|
||||||
@ -292,7 +300,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
invokeStatic(
|
invoke_static(
|
||||||
StatifyBlacklist::class, 'cidr_match', array(
|
StatifyBlacklist::class, 'cidr_match', array(
|
||||||
'2001:db8:a0b:12f0::1:132:465',
|
'2001:db8:a0b:12f0::1:132:465',
|
||||||
'2001:db8:a0b:12f0::1/96 ',
|
'2001:db8:a0b:12f0::1/96 ',
|
||||||
@ -302,13 +310,15 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test sanitization of IP addresses
|
* Test sanitization of IP addresses.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testSanitizeIPs() {
|
public function test_sanitize_ips() {
|
||||||
// IPv4 tests.
|
// IPv4 tests.
|
||||||
$valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' );
|
$valid = array( '192.0.2.123', '192.0.2.123/32', '192.0.2.0/24', '192.0.2.128/25' );
|
||||||
$invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' );
|
$invalid = array( '12.34.56.789', '192.0.2.123/33', '192.0.2.123/-1' );
|
||||||
$result = invokeStatic( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) );
|
$result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) );
|
||||||
$this->assertNotFalse( $result );
|
$this->assertNotFalse( $result );
|
||||||
$this->assertInternalType( 'array', $result );
|
$this->assertInternalType( 'array', $result );
|
||||||
$this->assertEquals( $valid, $result );
|
$this->assertEquals( $valid, $result );
|
||||||
@ -327,7 +337,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
'2001:db8:a0b:12f0::/129',
|
'2001:db8:a0b:12f0::/129',
|
||||||
'1:2:3:4:5:6:7:8:9',
|
'1:2:3:4:5:6:7:8:9',
|
||||||
);
|
);
|
||||||
$result = invokeStatic( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) );
|
$result = invoke_static( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) );
|
||||||
$this->assertNotFalse( $result );
|
$this->assertNotFalse( $result );
|
||||||
$this->assertInternalType( 'array', $result );
|
$this->assertInternalType( 'array', $result );
|
||||||
$this->assertEquals( $valid, $result );
|
$this->assertEquals( $valid, $result );
|
||||||
@ -335,8 +345,10 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test IP filter (#7).
|
* Test IP filter (#7).
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testIPFilter() {
|
public function test_ip_filter() {
|
||||||
// Prepare Options: 2 blacklisted IPs, disabled.
|
// Prepare Options: 2 blacklisted IPs, disabled.
|
||||||
StatifyBlacklist::$_options = array(
|
StatifyBlacklist::$_options = array(
|
||||||
'referer' => array(
|
'referer' => array(
|
||||||
@ -405,8 +417,10 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test simple target filter.
|
* Test simple target filter.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testTargetFilter() {
|
public function test_target_filter() {
|
||||||
// Prepare Options: 2 blacklisted domains, disabled.
|
// Prepare Options: 2 blacklisted domains, disabled.
|
||||||
StatifyBlacklist::$_options = array(
|
StatifyBlacklist::$_options = array(
|
||||||
'referer' => array(
|
'referer' => array(
|
||||||
@ -476,9 +490,9 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
|
|
||||||
/** @ignore */
|
/** @ignore */
|
||||||
function invokeStatic( $class, $methodName, $parameters = array() ) {
|
function invoke_static( $class, $method_name, $parameters = array() ) {
|
||||||
$reflection = new \ReflectionClass( $class );
|
$reflection = new \ReflectionClass( $class );
|
||||||
$method = $reflection->getMethod( $methodName );
|
$method = $reflection->getMethod( $method_name );
|
||||||
$method->setAccessible( true );
|
$method->setAccessible( true );
|
||||||
|
|
||||||
return $method->invokeArgs( null, $parameters );
|
return $method->invokeArgs( null, $parameters );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user