Completed
Push — add/masterbar--my-home ( 5c5e9c...8732fd )
by
unknown
10:54 queued 04:17
created

ManagerTest::test_api_url_uses_constants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ManagerTest::test_is_active_when_not_connected() 0 7 1
1
<?php
2
3
namespace Automattic\Jetpack\Connection;
4
5
use phpmock\Mock;
6
use phpmock\MockBuilder;
7
use PHPUnit\Framework\TestCase;
8
9
use Automattic\Jetpack\Constants;
10
11
class ManagerTest extends TestCase {
12
13
	protected $arguments_stack = [];
14
15
	public function setUp() {
16
		$this->manager = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' )
17
		                      ->setMethods( [ 'get_access_token' ] )
18
		                      ->getMock();
19
20
		$builder = new MockBuilder();
21
		$builder->setNamespace( __NAMESPACE__ )
22
				->setName( 'apply_filters' )
23
				->setFunction(
24
					function( $filter_name, $return_value ) {
25
						return $return_value;
26
					}
27
				);
28
29
		$this->apply_filters = $builder->build();
30
31
		$builder = new MockBuilder();
32
		$builder->setNamespace( __NAMESPACE__ )
33
				->setName( 'wp_redirect' )
34
				->setFunction(
35
					function( $url ) {
36
						$this->arguments_stack['wp_redirect'] [] = [ $url ];
37
						return true;
38
					}
39
				);
40
41
		$this->wp_redirect = $builder->build();
42
	}
43
44
	public function tearDown() {
45
		unset( $this->manager );
46
		Constants::clear_constants();
47
		Mock::disableAll();
48
	}
49
50
	/**
51
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
52
	 */
53
	public function test_is_active_when_connected() {
54
		$access_token = (object) [
55
			'secret'           => 'abcd1234',
56
			'external_user_id' => 1,
57
		];
58
		$this->manager->expects( $this->once() )
59
		              ->method( 'get_access_token' )
60
		              ->will( $this->returnValue( $access_token ) );
61
62
		$this->assertTrue( $this->manager->is_active() );
63
	}
64
65
	/**
66
	 * @covers Automattic\Jetpack\Connection\Manager::is_active
67
	 */
68
	public function test_is_active_when_not_connected() {
69
		$this->manager->expects( $this->once() )
70
		              ->method( 'get_access_token' )
71
		              ->will( $this->returnValue( false ) );
72
73
		$this->assertFalse( $this->manager->is_active() );
74
	}
75
76
	public function test_api_url_defaults() {
77
		$this->apply_filters->enable();
78
79
		$this->assertEquals(
80
			'https://jetpack.wordpress.com/jetpack.something/1/',
81
			$this->manager->api_url( 'something' )
82
		);
83
		$this->assertEquals(
84
			'https://jetpack.wordpress.com/jetpack.another_thing/1/',
85
			$this->manager->api_url( 'another_thing/' )
86
		);
87
	}
88
89
	/**
90
	 * Testing the ability of the api_url method to follow set constants and filters.
91
	 *
92
	 * @covers Automattic\Jetpack\Connection\Manager::api_url
93
	 */
94
	public function test_api_url_uses_constants_and_filters() {
95
		$this->apply_filters->enable();
96
97
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/base.' );
98
		$this->assertEquals(
99
			'https://example.com/api/base.something/1/',
100
			$this->manager->api_url( 'something' )
101
		);
102
103
		Constants::set_constant( 'JETPACK__API_BASE', 'https://example.com/api/another.' );
104
		Constants::set_constant( 'JETPACK__API_VERSION', '99' );
105
		$this->assertEquals(
106
			'https://example.com/api/another.something/99/',
107
			$this->manager->api_url( 'something' )
108
		);
109
110
		$this->apply_filters->disable();
111
112
		// Getting a new special mock just for this occasion.
113
		$builder = new MockBuilder();
114
		$builder->setNamespace( __NAMESPACE__ )
115
				->setName( 'apply_filters' )
116
				->setFunction(
117
					function( $filter_name, $return_value ) {
118
						$this->arguments_stack[ $filter_name ] [] = func_get_args();
119
						return 'completely overwrite';
120
					}
121
				);
122
123
		$builder->build()->enable();
124
125
		$this->assertEquals(
126
			'completely overwrite',
127
			$this->manager->api_url( 'something' )
128
		);
129
130
		// The jetpack_api_url argument stack should not be empty, making sure the filter was
131
		// called with a proper name and arguments.
132
		$call_arguments = array_pop( $this->arguments_stack['jetpack_api_url'] );
133
		$this->assertEquals( 'something', $call_arguments[2] );
134
		$this->assertEquals(
135
			Constants::get_constant( 'JETPACK__API_BASE' ),
136
			$call_arguments[3]
137
		);
138
		$this->assertEquals(
139
			'/' . Constants::get_constant( 'JETPACK__API_VERSION' ) . '/',
140
			$call_arguments[4]
141
		);
142
	}
143
144
	/**
145
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
146
	 */
147
	public function test_is_user_connected_with_default_user_id_logged_out() {
148
		$this->mock_function( 'get_current_user_id', 0 );
149
150
		$this->assertFalse( $this->manager->is_user_connected() );
151
	}
152
153
	/**
154
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
155
	 */
156
	public function test_is_user_connected_with_false_user_id_logged_out() {
157
		$this->mock_function( 'get_current_user_id', 0 );
158
159
		$this->assertFalse( $this->manager->is_user_connected( false ) );
160
	}
161
162
	/**
163
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
164
	 */
165
	public function test_is_user_connected_with_user_id_logged_out_not_connected() {
166
		$this->mock_function( 'absint', 1 );
167
		$this->manager->expects( $this->once() )
168
		              ->method( 'get_access_token' )
169
		              ->will( $this->returnValue( false ) );
170
171
		$this->assertFalse( $this->manager->is_user_connected( 1 ) );
172
	}
173
174
175
	/**
176
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
177
	 */
178 View Code Duplication
	public function test_is_user_connected_with_default_user_id_logged_in() {
179
		$this->mock_function( 'get_current_user_id', 1 );
180
		$access_token = (object) [
181
			'secret'           => 'abcd1234',
182
			'external_user_id' => 1,
183
		];
184
		$this->manager->expects( $this->once() )
185
		              ->method( 'get_access_token' )
186
		              ->will( $this->returnValue( $access_token ) );
187
188
		$this->assertTrue( $this->manager->is_user_connected() );
189
	}
190
191
	/**
192
	 * @covers Automattic\Jetpack\Connection\Manager::is_user_connected
193
	 */
194 View Code Duplication
	public function test_is_user_connected_with_user_id_logged_in() {
195
		$this->mock_function( 'absint', 1 );
196
		$access_token = (object) [
197
			'secret'           => 'abcd1234',
198
			'external_user_id' => 1,
199
		];
200
		$this->manager->expects( $this->once() )
201
		              ->method( 'get_access_token' )
202
		              ->will( $this->returnValue( $access_token ) );
203
204
		$this->assertTrue( $this->manager->is_user_connected( 1 ) );
205
	}
206
207
	/**
208
	 * Mock a global function and make it return a certain value.
209
	 *
210
	 * @param string $function_name Name of the function.
211
	 * @param mixed  $return_value  Return value of the function.
212
	 * @return phpmock\Mock The mock object.
213
	 */
214 View Code Duplication
	protected function mock_function( $function_name, $return_value = null ) {
215
		$builder = new MockBuilder();
216
		$builder->setNamespace( __NAMESPACE__ )
217
			->setName( $function_name )
218
			->setFunction( function() use ( &$return_value ) {
219
				return $return_value;
220
			} );
221
		return $builder->build()->enable();
222
	}
223
}
224