Completed
Push — e2e/e2e-runner-groups ( 261a6b...698261 )
by
unknown
65:29 queued 54:32
created

Test_Pre_Connection_JITM::set_up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php  // phpcs:disable
2
3
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\JITMS\Pre_Connection_JITM;
6
use Brain\Monkey;
7
use Brain\Monkey\Filters;
8
use Brain\Monkey\Functions;
9
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
10
use PHPUnit\Framework\TestCase;
11
12
class Test_Pre_Connection_JITM extends TestCase {
13
	use MockeryPHPUnitIntegration;
14
15
	/**
16
	 * An array containing a test pre-connection JITM.
17
	 *
18
	 * @var array
19
	 */
20
	private $test_jitms;
21
22
	/**
23
	 * The Pre_Connection_JITM instance.
24
	 *
25
	 * @var Pre_Connection_JITM
26
	 */
27
	private $jitm_instance;
28
29
	/**
30
	 * Set up.
31
	 *
32
	 * @before
33
	 */
34
	public function set_up() {
35
		Monkey\setUp();
36
37
		Functions\when( 'get_current_screen' )->justReturn( new \stdClass() );
38
		Functions\when( 'site_url' )->justReturn( 'unit-test' );
39
		Functions\when( 'wp_get_environment_type' )->justReturn( '' );
40
		Functions\when( 'get_option' )->justReturn( '' );
41
		Functions\when( '__' )->returnArg();
42
43
		$this->test_jitms = array(
44
			array(
45
				'id'             => 'test-jitm',
46
				'message_path'   => '/wp:plugins:admin_notices/',
47
				'message'        => __( 'A test message.', 'jetpack' ),
48
				'description'    => __( 'A test description.', 'jetpack' ),
49
				'button_link'    => 'a/test/url',
50
				'button_caption' => __( 'Test button text', 'jetpack' ),
51
			),
52
		);
53
54
		$this->jitm_instance = new Pre_Connection_JITM();
55
	}
56
57
	/**
58
	 * Tear down.
59
	 *
60
	 * @after
61
	 */
62
	public function tear_down() {
63
		Monkey\tearDown();
64
	}
65
66
	/**
67
	 * The pre-connection JITMs are disabled when the current user does not have the 'install_plugins' capability.
68
	 */
69 View Code Duplication
	public function test_get_messages_user_cannot_install_plugins() {
70
		Functions\expect( 'current_user_can' )
71
			->once()
72
			->andReturn( false );
73
74
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
75
			->atMost()
76
			->once()
77
			->with( array() )
78
			->andReturn( $this->test_jitms );
79
80
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
81
	}
82
83
	/**
84
	 * The pre-connection JITMs are empty by default. The default value of the 'jetpack_pre_connection_jitms' filter is
85
	 * an empty array.
86
	 */
87 View Code Duplication
	public function test_get_messages_jitms_filter_default() {
88
		Functions\expect( 'current_user_can' )
89
			->atMost()
90
			->once()
91
			->andReturn( true );
92
93
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
94
			->once()
95
			->with( array() );
96
97
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
98
	}
99
100
	/**
101
	 * The Pre_Connection_JITM::get_messages method returns an empty array when the the 'jetpack_pre_connection_jitms' filter
102
	 * returns anything other than an array.
103
	 */
104 View Code Duplication
	public function test_get_messages_filter_returns_string() {
105
		Functions\expect( 'current_user_can' )
106
			->atMost()
107
			->once()
108
			->andReturn( true );
109
110
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
111
			->once()
112
			->with( array() )
113
			->andReturn( 'a string intead of an array' );
114
115
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
116
	}
117
118
	/**
119
	 * The pre-connection JITMs are added using the `jetpack_pre_connection_jitms` filter.
120
	 */
121 View Code Duplication
	public function test_get_messages_return_message() {
122
		$this->set_user_cap_conditions();
123
124
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
125
			->once()
126
			->with( array() )
127
			->andReturn( $this->test_jitms );
128
129
		$messages = $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false );
130
		$this->assertSame( $this->test_jitms[0]['id'], $messages[0]->id );
131
	}
132
133
	/**
134
	 * A pre-connection JITM is only displayed if its message_path value matches the message path
135
	 * passed to Pre_Connection_JITM::get_messages. In this test, the test JITM's path does not match the
136
	 * tested path.
137
	 */
138
	public function test_get_messages_unmatched_message_path() {
139
		$this->set_user_cap_conditions();
140
141
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
142
			->once()
143
			->with( array() )
144
			->andReturn( $this->test_jitms );
145
146
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:edit-comments:admin_notices/', '', false ) );
147
	}
148
149
	/**
150
	 * The pre-connection JITM is not displayed if the message array is missing a required key. In this test, the JITM is
151
	 * missing the message_path key.
152
	 */
153 View Code Duplication
	public function test_get_messages_missing_key() {
154
		$this->set_user_cap_conditions();
155
156
		unset( $this->test_jitms[0]['message_path'] );
157
158
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
159
			->once()
160
			->with( array() )
161
			->andReturn( $this->test_jitms );
162
163
		$this->assertEmpty( $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false ) );
164
	}
165
166
	/**
167
	 * A pre-connection JITM is displayed if it has unexpected keys.
168
	 */
169
	public function test_get_messages_extra_key() {
170
		$this->set_user_cap_conditions();
171
172
		$this->test_jitms[0]['extra_key'] = 'extra jitm key';
173
174
		Filters\expectApplied( 'jetpack_pre_connection_jitms' )
175
			->once()
176
			->with( array() )
177
			->andReturn( $this->test_jitms );
178
179
		$messages = $this->jitm_instance->get_messages( '/wp:plugins:admin_notices/', '', false );
180
		$this->assertSame( $this->test_jitms[0]['id'], $messages[0]->id );
181
	}
182
183
	private function set_user_cap_conditions() {
184
		Functions\expect( 'current_user_can' )
185
			->once()
186
			->andReturn( true );
187
	}
188
}
189