Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity Crisis: add unit tests for has_identity_crisis and get_mismatched_urls #21754

Merged
merged 4 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Add unit tests for a few methods


2 changes: 1 addition & 1 deletion projects/packages/identity-crisis/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetpack-identity-crisis",
"version": "0.4.1",
"version": "0.4.2-alpha",
"description": "Jetpack Identity Crisis",
"main": "_inc/admin.jsx",
"repository": "https://github.com/Automattic/jetpack-identity-crisis",
Expand Down
11 changes: 9 additions & 2 deletions projects/packages/identity-crisis/src/class-identity-crisis.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Identity_Crisis {
/**
* Package Version
*/
const PACKAGE_VERSION = '0.4.1';
const PACKAGE_VERSION = '0.4.2-alpha';

/**
* Instance of the object.
Expand Down Expand Up @@ -1226,7 +1226,14 @@ public static function get_mismatched_urls() {

$data = static::check_identity_crisis();

if ( ! $data ) {
if ( ! $data ||
! isset( $data['error_code'] ) ||
! isset( $data['wpcom_home'] ) ||
! isset( $data['home'] ) ||
! isset( $data['wpcom_siteurl'] ) ||
! isset( $data['siteurl'] )
) {
// The jetpack_sync_error_idc option is missing a key.
return false;
}

Expand Down
204 changes: 204 additions & 0 deletions projects/packages/identity-crisis/tests/php/test-identity-crisis.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,210 @@ public function test_check_http_response_for_idc_detected_migrated_for_idc() {
$this->assertNull( Jetpack_Options::get_option( 'migrate_for_idc', null ) );
}

/**
* Test the has_identity_crisis method.
*
* @param bool $check_identity_crisis The value that Identity_Crisis::check_identity_crisis should return.
* @param bool $safe_mode_confirmed The value of the Identity_Crisis::$safe_mode_confirmed property.
* @param bool $expected_result The value expected to be returned by the call to the has_identity_crisis method.
*
* @dataProvider data_provider_test_has_identity_crisis
*/
public function test_has_identity_crisis( $check_identity_crisis, $safe_mode_confirmed, $expected_result ) {
if ( $check_identity_crisis ) {
$this->check_identity_crisis_return_error( array( 'test' ) );
}

Identity_Crisis::$is_safe_mode_confirmed = $safe_mode_confirmed;

$result = Identity_Crisis::has_identity_crisis();

$this->clean_up_check_identity_crisis_return_error();
Identity_Crisis::$is_safe_mode_confirmed = false;

$this->assertSame( $expected_result, $result );
}

/**
* Data provider for the test_has_identity_crisis method.
*
* @return array The test data with the format:
* [
* 'check_identity_crisis' => (bool) The value that Identity_Crisis::check_identity_crisis should return.
* 'safe_mode_confirmed' => (bool) The value of the Identity_Crisis::$safe_mode_confirmed property.
* 'expected_result' => (bool) The value expected to be returned by the call to the has_identity_crisis method.
* ]
*/
public function data_provider_test_has_identity_crisis() {
return array(
'check idc is true and safe mode is true' => array(
'check_identity_crisis' => true,
'safe_mode_confirmed' => true,
'expected_result' => false,
),
'check idc is true and safe mode is false' => array(
'check_identity_crisis' => true,
'safe_mode_confirmed' => false,
'expected_result' => true,
),
'check idc is false and safe mode is true' => array(
'check_identity_crisis' => false,
'safe_mode_confirmed' => true,
'expected_result' => false,
),
'check idc is false and safe mode is false' => array(
'check_identity_crisis' => false,
'safe_mode_confirmed' => false,
'expected_result' => false,
),
);
}

/**
* Test the get_mismatched_urls method.
*
* @param mixed $idc_error The value of the jetpack_sync_idc_error option.
* @param mixed $expected_result The value that the get_mismatched_value method should return.
*
* @dataProvider data_provider_test_get_mismatched_urls
*/
public function test_get_mismatched_urls( $idc_error, $expected_result ) {
$this->check_identity_crisis_return_error( $idc_error );
$result = Identity_Crisis::get_mismatched_urls();
$this->clean_up_check_identity_crisis_return_error();

$this->assertSame( $expected_result, $result );
}

/**
* Data providerd for the test_get_mismatched_urls method.
*
* @return array The test data with the format:
* [
* 'idc_error' => (mixed) The value of the jetpack_sync_idc_error option.
* 'expected_result' => (mixed) The value that the get_mismatched_value method should return.
* ]
*/
public function data_provider_test_get_mismatched_urls() {
return array(
'false' => array(
'idc_error' => false,
'expected_result' => false,
),
'empty array' => array(
'idc_error' => array(),
'expected_result' => false,
),
'no error_code key' => array(
'idc_error' => array(
'no_error_code' => 'test',
'wpcom_siteurl' => 'example.com/wpcom_siteurl',
'wpcom_home' => 'example.com/wpcom_home',
'siteurl' => 'example.com/remote_siteurl',
'home' => 'example.com/remote_home',
),
'expected_result' => false,
),
'no wpcom_siteurl key' => array(
'idc_error' => array(
'error_code' => 'jetpack_url_mismatch',
'wpcom_home' => 'example.com/wpcom_home',
'siteurl' => 'example.com/remote_siteurl',
'home' => 'example.com/remote_home',
),
'expected_result' => false,
),
'no wpcom_home key' => array(
'idc_error' => array(
'error_code' => 'jetpack_url_mismatch',
'wpcom_siteurl' => 'example.com/wpcom_siteurl',
'siteurl' => 'example.com/remote_siteurl',
'home' => 'example.com/remote_home',
),
'expected_result' => false,
),
'no siteurl key' => array(
'idc_error' => array(
'error_code' => 'jetpack_url_mismatch',
'wpcom_siteurl' => 'example.com/wpcom_siteurl',
'wpcom_home' => 'example.com/wpcom_home',
'home' => 'example.com/remote_home',
),
'expected_result' => false,
),
'no home key' => array(
'idc_error' => array(
'error_code' => 'jetpack_url_mismatch',
'wpcom_siteurl' => 'example.com/wpcom_siteurl',
'wpcom_home' => 'example.com/wpcom_home',
'siteurl' => 'example.com/remote_siteurl',
),
'expected_result' => false,
),
'site_url_mismatch_error' => array(
'idc_error' => array(
'error_code' => 'jetpack_site_url_mismatch',
'wpcom_siteurl' => 'example.com/wpcom_siteurl',
'wpcom_home' => 'example.com/wpcom_home',
'siteurl' => 'example.com/remote_siteurl',
'home' => 'example.com/remote_home',
),
'expected_result' => array(
'wpcom_url' => 'example.com/wpcom_siteurl',
'current_url' => 'example.com/remote_siteurl',
),
),
'home_url_mismatch_error' => array(
'idc_error' => array(
'error_code' => 'jetpack_home_url_mismatch',
'wpcom_siteurl' => 'example.com/wpcom_siteurl',
'wpcom_home' => 'example.com/wpcom_home',
'siteurl' => 'example.com/remote_siteurl',
'home' => 'example.com/remote_home',
),
'expected_result' => array(
'wpcom_url' => 'example.com/wpcom_home',
'current_url' => 'example.com/remote_home',
),
),
'url_mismatch_error' => array(
'idc_error' => array(
'error_code' => 'jetpack_url_mismatch',
'wpcom_siteurl' => 'example.com/wpcom_siteurl',
'wpcom_home' => 'example.com/wpcom_home',
'siteurl' => 'example.com/remote_siteurl',
'home' => 'example.com/remote_home',
),
'expected_result' => array(
'wpcom_url' => 'example.com/wpcom_home',
'current_url' => 'example.com/remote_home',
),
),
);
}

/**
* Forces the Identity_Crisis::check_identity_crisis method to return the input idc error array.
*
* @param array $idc_error The idc error array to be returned.
*/
private function check_identity_crisis_return_error( $idc_error ) {
\Jetpack_Options::update_option( 'id', 'test' );
\Jetpack_Options::update_option( 'blog_token', 'test' );
add_filter( 'jetpack_sync_error_idc_validation', '__return_true' );
update_option( 'jetpack_sync_error_idc', $idc_error );
}

/**
* Clean up the settings from the check_identity_crisis_return_error method.
*/
private function clean_up_check_identity_crisis_return_error() {
\Jetpack_Options::delete_option( 'id', 'test' );
\Jetpack_Options::update_option( 'blog_token', 'test' );
remove_filter( 'jetpack_sync_error_idc_validation', '__return_true' );
delete_option( 'jetpack_sync_error_idc' );
}

/**
* Return string '1'.
*
Expand Down