Clean up some naming & create helper functions for tests

This commit is contained in:
Kelly Dwan 2019-11-15 11:56:01 -05:00
parent a2a88c56a4
commit f9c8075af8
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D

View file

@ -8,35 +8,27 @@ defined( 'WPINC' ) || die();
class Test_Email extends WP_UnitTestCase { class Test_Email extends WP_UnitTestCase {
// phpcs:ignore PSR2.Classes.PropertyDeclaration.Multiple // phpcs:ignore PSR2.Classes.PropertyDeclaration.Multiple
protected static $valid_pledge, $valid_action, $valid_action_page, $valid_action_url, $valid_token; protected static $pledge, $action, $page, $action_url, $token;
/** /**
* Setup fixtures that are shared across all tests. * Setup fixtures that are shared across all tests.
*/ */
public static function wpSetUpBeforeClass() { public static function wpSetUpBeforeClass() {
$valid_pledge_params = array( $pledge_id = self::factory()->post->create( array(
'post_type' => PLEDGE_POST_TYPE, 'post_type' => PLEDGE_POST_TYPE,
'post_title' => 'Valid Pledge', 'post_title' => 'Valid Pledge',
'post_status' => 'publish', 'post_status' => 'publish',
); ) );
$valid_action_page_params = array( $page_id = self::factory()->post->create( array(
'post_type' => 'page', 'post_type' => 'page',
'post_title' => 'For Organizers', 'post_title' => 'For Organizers',
'post_status' => 'publish', 'post_status' => 'publish',
); ) );
$valid_pledge_id = self::factory()->post->create( $valid_pledge_params ); self::$pledge = get_post( $pledge_id );
self::$page = get_post( $page_id );
self::$valid_pledge = get_post( $valid_pledge_id ); self::$action = 'confirm_pledge_email';
$valid_action_page_id = self::factory()->post->create( $valid_action_page_params );
self::$valid_action_page = get_post( $valid_action_page_id );
self::$valid_action = 'confirm_pledge_email';
// todo better example action to use, like contributor verifying participation? or is this one just as good?
// should probably use the manage one once that's implemented, b/c should test the `view` context and the `update` context.
// use this one for now, though.
self::verify_before_class_fixtures(); self::verify_before_class_fixtures();
} }
@ -47,11 +39,11 @@ class Test_Email extends WP_UnitTestCase {
* @return void * @return void
*/ */
protected static function verify_before_class_fixtures() { protected static function verify_before_class_fixtures() {
self::assertSame( 'object', gettype( self::$valid_action_page ) ); self::assertSame( 'object', gettype( self::$page ) );
self::assertSame( 'For Organizers', self::$valid_action_page->post_title ); self::assertSame( 'For Organizers', self::$page->post_title );
self::assertSame( 'object', gettype( self::$valid_pledge ) ); self::assertSame( 'object', gettype( self::$pledge ) );
self::assertSame( 'Valid Pledge', self::$valid_pledge->post_title ); self::assertSame( 'Valid Pledge', self::$pledge->post_title );
self::assertSame( PLEDGE_POST_TYPE, self::$valid_pledge->post_type ); self::assertSame( PLEDGE_POST_TYPE, self::$pledge->post_type );
} }
/** /**
@ -65,78 +57,78 @@ class Test_Email extends WP_UnitTestCase {
* *
* This must be called before every test, because the process of verifying a valid token will delete it. * This must be called before every test, because the process of verifying a valid token will delete it.
*/ */
self::$valid_action_url = get_authentication_url( self::$valid_pledge->ID, self::$valid_action, self::$valid_action_page->ID ); self::$action_url = get_authentication_url( self::$pledge->ID, self::$action, self::$page->ID );
self::$valid_token = get_post_meta( self::$valid_pledge->ID, TOKEN_PREFIX . self::$valid_action, true ); self::$token = get_post_meta( self::$pledge->ID, TOKEN_PREFIX . self::$action, true );
// Verify that the fixtures are setup correctly. // Verify that the fixtures are setup correctly.
$action_url_args = wp_parse_args( wp_parse_url( self::$valid_action_url, PHP_URL_QUERY ) ); $action_url_args = wp_parse_args( wp_parse_url( self::$action_url, PHP_URL_QUERY ) );
$this->assertSame( 'array', gettype( self::$valid_token ) ); $this->assertSame( 'array', gettype( self::$token ) );
$this->assertSame( $action_url_args['action'], self::$valid_action ); $this->assertSame( $action_url_args['action'], self::$action );
$this->assertSame( $action_url_args['auth_token'], self::$valid_token['value'] ); $this->assertSame( $action_url_args['auth_token'], self::$token['value'] );
}
/**
* Helper function to create & get a token.
*/
private function _get_token( $pledge_id, $action, $page_id, $single = true ) {
get_authentication_url( $pledge_id, $action, $page_id, $single );
return get_post_meta( $pledge_id, TOKEN_PREFIX . $action, true );
} }
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_valid_token_accepted() { public function test_valid_token_accepted() {
$verified = is_valid_authentication_token( self::$valid_pledge->ID, self::$valid_action, self::$valid_token['value'] ); $verified = is_valid_authentication_token( self::$pledge->ID, self::$action, self::$token['value'] );
$this->assertTrue( $verified ); $this->assertTrue( $verified );
// todo test that `view` and `update` contexts work as well, when those are added
// maybe need to test some failures for that too.
} }
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
* * @dataProvider data_invalid_token_provider
* @dataProvider data_invalid_token_rejected
*/ */
public function test_invalid_token_rejected( $invalid_token ) { public function test_invalid_tokens_are_rejected( $token_to_validate ) {
/* $verified = is_valid_authentication_token(
* It's expected that some of the values passed in won't have a `value` item, so fallback to the item self::$pledge->ID,
* itself in those cases, to avoid PHPUnit throwing an exception. self::$action,
*/ $token_to_validate
$invalid_token_value = $invalid_token['value'] ?? $invalid_token; );
$verified = is_valid_authentication_token( self::$valid_pledge->ID, self::$valid_action, $invalid_token_value );
$this->assertSame( false, $verified ); $this->assertSame( false, $verified );
} }
/** /**
* Test that invalid tokens are rejected. * Provide invalid tokens to be used by `test_invalid_tokens_are_rejected`.
* *
* Note that data providers can't access fixtures. * Note that data providers can't access fixtures.
* See https://phpunit.readthedocs.io/en/7.4/writing-tests-for-phpunit.html#data-providers. * See https://phpunit.readthedocs.io/en/7.4/writing-tests-for-phpunit.html#data-providers.
* *
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function data_invalid_token_rejected() { public function data_invalid_token_provider() {
return array( return array(
'non-existent-token' => array( false ), // Simulates `get_post_meta()` return value. 'non-existent-token' => array( false ), // Simulates `get_post_meta()` return value.
'wrong-data-type' => array( 'this string is not an array' ), 'wrong-data-type' => array( 'this string is not an array' ),
'wrong-array-items' => array( 'this' => "doesn't have `value` and `expiration` items" ), 'wrong-array-items' => array( 'this' => "doesn't have `value` and `expiration` items" ),
'invalid-value' => array( 'Valid tokens will never contain special characters like !@#$%^&*()' ),
'invalid-value' => array(
array(
'value' => 'Valid tokens will never contain special characters like !@#$%^&*()',
'expiration' => time() + HOUR_IN_SECONDS,
),
),
); );
} }
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_expired_token_rejected() { public function test_expired_tokens_are_rejected() {
$expired_token = self::$valid_token; $expired_token = self::$token;
$expired_token['expiration'] = time() - 1; $expired_token['expiration'] = time() - 1;
update_post_meta( self::$valid_pledge->ID, TOKEN_PREFIX . self::$valid_action, $expired_token ); update_post_meta( self::$pledge->ID, TOKEN_PREFIX . self::$action, $expired_token );
$verified = is_valid_authentication_token( self::$valid_pledge->ID, self::$valid_action, self::$valid_token['value'] ); $verified = is_valid_authentication_token(
self::$pledge->ID,
self::$action,
self::$token['value']
);
$this->assertSame( false, $verified ); $this->assertSame( false, $verified );
} }
@ -144,10 +136,10 @@ class Test_Email extends WP_UnitTestCase {
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_used_token_rejected() { public function test_used_tokens_are_rejected() {
// The token should be deleted once it's used/verified for the first time. // The token should be deleted once it's used/verified for the first time.
$first_verification = is_valid_authentication_token( self::$valid_pledge->ID, self::$valid_action, self::$valid_token['value'] ); $first_verification = is_valid_authentication_token( self::$pledge->ID, self::$action, self::$token['value'] );
$second_verification = is_valid_authentication_token( self::$valid_pledge->ID, self::$valid_action, self::$valid_token['value'] ); $second_verification = is_valid_authentication_token( self::$pledge->ID, self::$action, self::$token['value'] );
$this->assertSame( true, $first_verification ); $this->assertSame( true, $first_verification );
$this->assertSame( false, $second_verification ); $this->assertSame( false, $second_verification );
@ -156,8 +148,8 @@ class Test_Email extends WP_UnitTestCase {
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_valid_token_rejected_for_other_pages() { public function test_valid_tokens_are_rejected_for_other_pages() {
$verified = is_valid_authentication_token( self::$valid_action_page->ID, self::$valid_action, self::$valid_token['value'] ); $verified = is_valid_authentication_token( self::$page->ID, self::$action, self::$token['value'] );
$this->assertSame( false, $verified ); $this->assertSame( false, $verified );
} }
@ -165,14 +157,17 @@ class Test_Email extends WP_UnitTestCase {
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_valid_token_rejected_for_other_actions() { public function test_valid_tokens_are_rejected_for_other_actions() {
// Setup another valid token for the other action. // Generate new token on pledge.
$other_valid_action = 'confirm_contributor_participation'; $new_action = 'confirm_contributor_participation';
// todo update this when the action for that step is created, so that they match and show that valid actions. self::_get_token( self::$pledge->ID, $new_action, self::$page->ID );
$other_valid_action_url = get_authentication_url( self::$valid_pledge->ID, $other_valid_action, self::$valid_action_page->ID );
// Intentionally mismatch the token and action. // Intentionally mismatch the token and action.
$verified = is_valid_authentication_token( self::$valid_pledge->ID, $other_valid_action, self::$valid_token['value'] ); $verified = is_valid_authentication_token(
self::$pledge->ID,
$new_action,
self::$token['value']
);
$this->assertSame( false, $verified ); $this->assertSame( false, $verified );
} }
@ -180,28 +175,23 @@ class Test_Email extends WP_UnitTestCase {
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_valid_token_rejected_for_other_pledge() { public function test_valid_tokens_are_rejected_for_other_pledges() {
$other_valid_pledge_params = array( $new_pledge_id = self::factory()->post->create( array(
'post_type' => PLEDGE_POST_TYPE, 'post_type' => PLEDGE_POST_TYPE,
'post_title' => 'Other Valid Pledge', 'post_title' => 'Other Valid Pledge',
'post_status' => 'publish', 'post_status' => 'publish',
); ) );
$other_valid_pledge_id = self::factory()->post->create( $other_valid_pledge_params ); $new_pledge = get_post( $new_pledge_id );
$other_valid_pledge = get_post( $other_valid_pledge_id ); $new_token = self::_get_token( $new_pledge->ID, self::$action, self::$page->ID );
// Create a valid token for the other pledge.
get_authentication_url( $other_valid_pledge->ID, self::$valid_action, self::$valid_action_page->ID );
$other_valid_token = get_post_meta( $other_valid_pledge->ID, TOKEN_PREFIX . self::$valid_action, true );
// Intentionally mismatch the pledge and token. // Intentionally mismatch the pledge and token.
$verified = is_valid_authentication_token( $other_valid_pledge_id, self::$valid_action, self::$valid_token['value'] ); $verified = is_valid_authentication_token( $new_pledge_id, self::$action, self::$token['value'] );
$this->assertSame( 'Other Valid Pledge', $other_valid_pledge->post_title ); $this->assertSame( 'Other Valid Pledge', $new_pledge->post_title );
$this->assertSame( 'array', gettype( $other_valid_token ) ); $this->assertSame( 'array', gettype( $new_token ) );
$this->assertArrayHasKey( 'value', $other_valid_token ); $this->assertArrayHasKey( 'value', $new_token );
$this->assertNotSame( $other_valid_token['value'], self::$valid_token['value'] ); $this->assertNotSame( $new_token['value'], self::$token['value'] );
$this->assertSame( false, $verified ); $this->assertSame( false, $verified );
} }
@ -209,14 +199,13 @@ class Test_Email extends WP_UnitTestCase {
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_reusable_token_is_reusable() { public function test_reusable_token_is_reusable() {
$reusable_action = 'manage_pledge'; $action = 'manage_pledge';
get_authentication_url( self::$valid_pledge->ID, $reusable_action, self::$valid_action_page->ID, false ); $token = self::_get_token( self::$pledge->ID, $action, self::$page->ID, false );
$reusable_token = get_post_meta( self::$valid_pledge->ID, TOKEN_PREFIX . $reusable_action, true );
// The token should be usable multiple times. // The token should be usable multiple times.
$first_verification = is_valid_authentication_token( self::$valid_pledge->ID, $reusable_action, $reusable_token['value'] ); $first_verification = is_valid_authentication_token( self::$pledge->ID, $action, $token['value'] );
$second_verification = is_valid_authentication_token( self::$valid_pledge->ID, $reusable_action, $reusable_token['value'] ); $second_verification = is_valid_authentication_token( self::$pledge->ID, $action, $token['value'] );
$third_verification = is_valid_authentication_token( self::$valid_pledge->ID, $reusable_action, $reusable_token['value'] ); $third_verification = is_valid_authentication_token( self::$pledge->ID, $action, $token['value'] );
$this->assertSame( true, $first_verification ); $this->assertSame( true, $first_verification );
$this->assertSame( true, $second_verification ); $this->assertSame( true, $second_verification );
@ -226,15 +215,14 @@ class Test_Email extends WP_UnitTestCase {
/** /**
* @covers ::is_valid_authentication_token * @covers ::is_valid_authentication_token
*/ */
public function test_expired_reusable_token_rejected() { public function test_expired_reusable_tokens_are_rejected() {
$reusable_action = 'manage_pledge'; $action = 'manage_pledge';
get_authentication_url( self::$valid_pledge->ID, $reusable_action, self::$valid_action_page->ID, false ); $token = self::_get_token( self::$pledge->ID, $action, self::$page->ID, false );
$reusable_token = get_post_meta( self::$valid_pledge->ID, TOKEN_PREFIX . $reusable_action, true );
$reusable_token['expiration'] = time() - 1; $token['expiration'] = time() - 1;
update_post_meta( self::$valid_pledge->ID, TOKEN_PREFIX . $reusable_action, $reusable_token ); update_post_meta( self::$pledge->ID, TOKEN_PREFIX . $action, $token );
$verified = is_valid_authentication_token( self::$valid_pledge->ID, $reusable_action, $reusable_token['value'] ); $verified = is_valid_authentication_token( self::$pledge->ID, $action, $token['value'] );
$this->assertSame( false, $verified ); $this->assertSame( false, $verified );
} }