Completed
Push — update/sync_use_heartbeat ( 7f0452...e730a0 )
by
unknown
427:04 queued 417:50
created

WP_Test_Jetpack_Sync_Module_Stats   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 119
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 35
loc 119
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_sends_stats_data_on_heartbeat() 18 18 1
A test_dont_send_expensive_data_on_heartbeat() 17 17 1
A test_sends_stats_data_on_heartbeat_on_multisite() 0 39 2
A make_site_active() 0 10 1
A delete_connection_options() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class WP_Test_Jetpack_Sync_Module_Stats extends WP_Test_Jetpack_Sync_Base {
4
5
	/**
6
	 * Test sends stats data on heartbeat
7
	 *
8
	 * @expectedDeprecated Jetpack_Heartbeat::cron_exec
9
	 * @return void
10
	 */
11 View Code Duplication
	public function test_sends_stats_data_on_heartbeat() {
12
		$this->make_site_active();
13
14
		$heartbeat = Jetpack_Heartbeat::init();
15
		add_filter( 'jetpack_heartbeat_stats_array', array( $heartbeat, 'add_stats_to_heartbeat' ) );
16
17
		add_filter( 'pre_http_request', array( $this, 'pre_http_request_success' ) );
18
		$heartbeat->cron_exec();
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Heartbeat::cron_exec() has been deprecated with message: since 8.9.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
19
		remove_filter( 'pre_http_request', array( $this, 'pre_http_request_success' ) );
20
21
		$this->sender->do_sync();
22
23
		$action = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_heartbeat_stats' );
24
25
		$this->delete_connection_options();
26
27
		$this->assertEquals( JETPACK__VERSION, $action->args[0]['version'] );
28
	}
29
30
	/**
31
	 * Test dont send expensive data on heartbeat
32
	 *
33
	 * @expectedDeprecated Jetpack_Heartbeat::cron_exec
34
	 * @return void
35
	 */
36 View Code Duplication
	public function test_dont_send_expensive_data_on_heartbeat() {
37
		$this->make_site_active();
38
39
		$heartbeat = Jetpack_Heartbeat::init();
40
		add_filter( 'jetpack_heartbeat_stats_array', array( $heartbeat, 'add_stats_to_heartbeat' ) );
41
42
		add_filter( 'pre_http_request', array( $this, 'pre_http_request_success' ) );
43
		$heartbeat->cron_exec();
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Heartbeat::cron_exec() has been deprecated with message: since 8.9.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
44
		remove_filter( 'pre_http_request', array( $this, 'pre_http_request_success' ) );
45
		$this->sender->do_sync();
46
47
		$action = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_heartbeat_stats' );
48
49
		$this->delete_connection_options();
50
51
		$this->assertFalse( isset( $action->args[0]['users'] ) );
52
	}
53
54
	/**
55
	 * Test sends stats data on heartbeat on multisite
56
	 *
57
	 * @expectedDeprecated Jetpack_Heartbeat::cron_exec
58
	 * @return void
59
	 */
60
	public function test_sends_stats_data_on_heartbeat_on_multisite() {
61
		global $wpdb;
62
63
		if ( ! is_multisite() ) {
64
			$this->markTestSkipped( 'Run it in multi site mode' );
65
		}
66
67
		$user_id         = $this->factory->user->create();
68
		$mu_blog_user_id = $this->factory->user->create();
69
70
		// Create a different blog.
71
		$suppress      = $wpdb->suppress_errors();
72
		$other_blog_id = wpmu_create_blog( 'foo.com', '', 'My Blog', $user_id );
73
		$wpdb->suppress_errors( $suppress );
74
75
		// Create a user from within that blog (won't be synced).
76
		switch_to_blog( $other_blog_id );
77
		$this->make_site_active();
78
79
		add_user_to_blog( $other_blog_id, $mu_blog_user_id, 'administrator' );
80
81
		$heartbeat = Jetpack_Heartbeat::init();
82
		add_filter( 'jetpack_heartbeat_stats_array', array( $heartbeat, 'add_stats_to_heartbeat' ) );
83
84
		add_filter( 'pre_http_request', array( $this, 'pre_http_request_success' ) );
85
		$heartbeat->cron_exec();
0 ignored issues
show
Deprecated Code introduced by
The method Jetpack_Heartbeat::cron_exec() has been deprecated with message: since 8.9.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
		remove_filter( 'pre_http_request', array( $this, 'pre_http_request_success' ) );
87
88
		$this->sender->do_sync();
89
90
		$action = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_heartbeat_stats' );
91
92
		$this->delete_connection_options();
93
94
		restore_current_blog();
95
96
		$this->assertEquals( JETPACK__VERSION, $action->args[0]['version'] );
97
		$this->assertFalse( isset( $action->args[0]['users'] ) );
98
	}
99
100
	/**
101
	 * Sets the 'master_user' and 'user_tokens' options so the site is considered connected.
102
	 */
103
	private function make_site_active() {
104
		$user_id = 1;
105
		Jetpack_Options::update_option( 'master_user', $user_id );
106
		Jetpack_Options::update_option(
107
			'user_tokens',
108
			array(
109
				$user_id => 'apple.a.' . $user_id,
110
			)
111
		);
112
	}
113
114
	/**
115
	 * Deletes the 'master_user' and 'user_tokens" options.
116
	 */
117
	private function delete_connection_options() {
118
		Jetpack_Options::delete_option( 'master_user' );
119
		Jetpack_Options::delete_option( 'user_tokens' );
120
	}
121
}
122