Completed
Push — update/wpcom-block-editor-shor... ( 9897fe...f43ed6 )
by Jeremy
148:26 queued 138:20
created

WP_Test_Jetpack_Pre_Connection_JITMs   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_add_pre_connection_jitms() 0 13 1
A data_provider_test_add_pre_connection_jitms() 0 8 1
A test_add_pre_connection_jitms_button_link() 0 12 1
A test_add_pre_connection_jitms_existing_jitms() 0 25 1
A test_add_pre_connection_jitms_not_an_array() 0 14 1
1
<?php
2
/**
3
 * Contains the unit tests for the Jetpack_Pre_Connection_JITMs class.
4
 *
5
 * @package jetpack
6
 */
7
8
use PHPUnit\Framework\TestCase;
9
10
require_once dirname( __FILE__ ) . '/../../../class-jetpack-pre-connection-jitms.php';
11
12
/**
13
 * Class WP_Test_Jetpack_Pre_Connection_JITMs.
14
 */
15
class WP_Test_Jetpack_Pre_Connection_JITMs extends TestCase {
16
17
	/**
18
	 * Tests the Jetpack_Pre_Connection_JITMs::add_pre_connection_jitms method with different
19
	 * published post counts.
20
	 *
21
	 * @param int $posts_count          The number of published posts.
22
	 * @param int $expected_jitms_count The expected number of JITMs that should be returned by
23
	 *                                  Jetpack_Pre_Connection_JITMs::add_pre_connection_jitms.
24
	 *
25
	 * @dataProvider data_provider_test_add_pre_connection_jitms
26
	 */
27
	public function test_add_pre_connection_jitms( $posts_count, $expected_jitms_count ) {
28
		add_filter(
29
			'wp_count_posts',
30
			function ( $counts ) use ( $posts_count ) {
31
				$counts->publish = $posts_count;
32
				return $counts;
33
			}
34
		);
35
36
		$jitms    = new Jetpack_Pre_Connection_JITMs();
37
		$messages = $jitms->add_pre_connection_jitms( array() );
38
		$this->assertCount( $expected_jitms_count, $messages );
39
	}
40
41
	/**
42
	 * Data provider for the test_add_pre_connection_jitms test method.
43
	 *
44
	 * Jetpack has three pre-connection JITMs. One JITM, jpsetup-posts, is only displayed
45
	 * when the number of published posts is greater than or equal to five.
46
	 *
47
	 * @return array An array of test data.
48
	 */
49
	public function data_provider_test_add_pre_connection_jitms() {
50
		return array(
51
			'0 posts' => array( 0, 2 ),
52
			'4 posts' => array( 4, 2 ),
53
			'5 posts' => array( 5, 3 ),
54
			'6 posts' => array( 6, 3 ),
55
		);
56
	}
57
58
	/**
59
	 * Verify that the pre-connection JITM button link ends with the expected query.
60
	 */
61
	public function test_add_pre_connection_jitms_button_link() {
62
		$jitms    = new Jetpack_Pre_Connection_JITMs();
63
		$messages = $jitms->add_pre_connection_jitms( array() );
64
65
		$query = 'admin.php?page=jetpack&#/setup';
66
67
		// Verify that the `jpsetup-upload` JITM is in the list of JITMs.
68
		$index = array_search( 'jpsetup-upload', array_column( $messages, 'id' ), true );
69
		$this->assertNotFalse( $index );
70
71
		$this->assertSame( $query, substr( $messages[ $index ]['button_link'], -strlen( $query ) ) );
72
	}
73
74
	/**
75
	 * Tests the add_pre_connection_jitms method when the input to the method is
76
	 * an array containing a single JITM. The three Jetpack pre-connection JITMs
77
	 * and the additional test JITM should be returned.
78
	 */
79
	public function test_add_pre_connection_jitms_existing_jitms() {
80
		$test_jitm = array(
81
			array(
82
				'id'             => 'test-jitm',
83
				'message_path'   => '/wp:plugins:admin_notices/',
84
				'message'        => __( 'A test message.', 'jetpack' ),
85
				'description'    => __( 'A test description.', 'jetpack' ),
86
				'button_link'    => 'a/test/url',
87
				'button_caption' => __( 'Test button text', 'jetpack' ),
88
			),
89
		);
90
91
		add_filter(
92
			'wp_count_posts',
93
			function ( $counts ) {
94
				$counts->publish = 7;
95
				return $counts;
96
			}
97
		);
98
99
		$jitms    = new Jetpack_Pre_Connection_JITMs();
100
		$messages = $jitms->add_pre_connection_jitms( $test_jitm );
101
102
		$this->assertCount( 4, $messages );
103
	}
104
105
	/**
106
	 * Tests the add_pre_connection_jitms method when the input to the method is
107
	 * not an array. The three Jetpack pre-connection JITMs should
108
	 * be returned.
109
	 */
110
	public function test_add_pre_connection_jitms_not_an_array() {
111
		add_filter(
112
			'wp_count_posts',
113
			function ( $counts ) {
114
				$counts->publish = 7;
115
				return $counts;
116
			}
117
		);
118
119
		$jitms    = new Jetpack_Pre_Connection_JITMs();
120
		$messages = $jitms->add_pre_connection_jitms( 'a test string' );
0 ignored issues
show
Documentation introduced by
'a test string' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
122
		$this->assertCount( 3, $messages );
123
	}
124
}
125