Completed
Push — fix/connection-package-tests ( 1d867d )
by Marin
181:56 queued 174:49
created

ManagerTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Connection;
4
5
use phpmock\functions\FunctionProvider;
6
use phpmock\MockBuilder;
7
use PHPUnit\Framework\TestCase;
8
9
class ManagerTest extends TestCase {
10
	public function setUp() {
11
		parent::setUp();
12
13
		$this->mock = $this->getMockBuilder( 'stdClass' )
14
						   ->setMethods( [ 'get_option', 'update_option', 'get_raw_option' ] )
15
						   ->getMock();
16
17
		$this->generator = $this->getMockBuilder( 'stdClass' )
18
								->setMethods( [ 'generate' ] )
19
								->getMock();
20
21
		$this->manager = new Manager();
22
23
		$this->mock_filters( array(
24
			array( 'jetpack_connection_option_manager', false, $this->mock ),
25
			array( 'jetpack_connection_secret_generator', 'wp_generate_password', array( $this->generator, 'generate' ) ),
26
		) );
27
	}
28
29
	public function tearDown() {
30
		parent::tearDown();
31
		$this->clear_mock_filters();
32
	}
33
34
	function test_class_implements_interface() {
35
		$manager = new Manager();
36
		$this->assertInstanceOf( 'Automattic\Jetpack\Connection\Manager_Interface', $manager );
37
	}
38
39
	function test_generate_secrets() {
40
		$this->generator->expects( $this->exactly( 2 ) )
41
						->method( 'generate' )
42
						->will( $this->returnValue( 'topsecretstring' ) );
43
44
		$this->mock->expects( $this->once() )
45
				   ->method( 'update_option' )
46
				   ->with(
47
					   $this->equalTo( Manager::SECRETS_OPTION_NAME ),
48
					   $this->equalTo( array(
49
						   'jetpack_name_1' => array(
50
							   'secret_1' => 'topsecretstring',
51
							   'secret_2' => 'topsecretstring',
52
							   'exp' => time() + 600
53
						   )
54
					   ) )
55
				   );
56
57
		$secrets = $this->manager->generate_secrets( 'name', 1, 600 );
58
59
		$this->assertEquals( 'topsecretstring', $secrets['secret_1'] );
60
		$this->assertEquals( 'topsecretstring', $secrets['secret_2'] );
61
	}
62
63
	function test_get_secrets_not_found() {
64
		$this->mock->expects( $this->once() )
65
				   ->method( 'get_option' )
66
				   ->with(
67
					   $this->equalTo( Manager::SECRETS_OPTION_NAME ),
68
					   $this->anything()
69
				   );
70
71
		$this->assertEquals(
72
			Manager::SECRETS_MISSING,
73
			$this->manager->get_secrets( 'name', 1, 600 )
74
		);
75
	}
76
77
	/**
78
	 * @dataProvider secrets_value_provider
79
	 */
80
	function test_get_secrets_expired( $name, $user_id, $expires, $values ) {
81
		$this->mock->expects( $this->exactly( 2 ) )
82
				   ->method( 'get_option' )
83
				   ->with(
84
					   $this->equalTo( Manager::SECRETS_OPTION_NAME ),
85
					   $this->anything()
86
				   )
87
				   ->will(
88
					   $this->returnValue( array(
89
						   'jetpack_' . $name . '_' . $user_id => array_merge(
90
							   $values,
91
92
							   // Expired secret, should be removed on access.
93
							   array( 'exp' => 0 )
94
						   )
95
					   ) )
96
				   );
97
98
		$this->mock->expects( $this->once() )
99
				   ->method( 'update_option' )
100
				   ->with(
101
					   $this->equalTo( Manager::SECRETS_OPTION_NAME ),
102
					   $this->equalTo( array() )
103
				   );
104
105
		$this->assertEquals(
106
			Manager::SECRETS_EXPIRED,
107
			$this->manager->get_secrets( $name, $user_id, $expires )
108
		);
109
	}
110
111
	/**
112
	 * @dataProvider secrets_value_provider
113
	 */
114
	function test_get_secrets( $name, $user_id, $expires, $values ) {
115
		$this->mock->expects( $this->once() )
116
				   ->method( 'get_option' )
117
				   ->with(
118
					   $this->equalTo( Manager::SECRETS_OPTION_NAME ),
119
					   $this->anything()
120
				   )
121
				   ->will(
122
					   $this->returnValue( array(
123
						   'jetpack_' . $name . '_' . $user_id => array_merge(
124
							   $values,
125
126
							   // Making sure the secret is still active.
127
							   array( 'exp' => $values['exp'] + time() )
128
						   )
129
					   ) )
130
				   );
131
132
		$this->assertEquals(
133
			$values['secret_1'],
134
			$this->manager->get_secrets( $name, $user_id, $expires )['secret_1']
135
		);
136
	}
137
138
	/**
139
	 * Provides values for secrets to test.
140
	 */
141
	function secrets_value_provider() {
142
		return [
143
			[
144
				'action_name',
145
				123,
146
				3600,
147
				[
148
					'secret_1' => 'secret1',
149
					'secret_2' => 'secret2',
150
					'exp'      => 600
151
				]
152
			],
153
			[
154
				'action_name_2',
155
				1234,
156
				36000,
157
				[
158
					'secret_1' => 'secret1withsomewords',
159
					'secret_2' => 'secret2mithosemthingelse',
160
					'exp'      => 36000
161
				]
162
			]
163
		];
164
	}
165
166
	protected function mock_filters( $filters ) {
167
		$this->mocked_filters = $filters;
168
169
		$builder = new MockBuilder();
170
		$builder->setNamespace( __NAMESPACE__ )
171
			->setName( 'apply_filters' )
172
			->setFunction(
173
				function () {
174
					$current_args = func_get_args();
175
					foreach ( $this->mocked_filters as $filter ) {
176
						if ( array_slice( $filter, 0, -1 ) === $current_args ) {
177
							return array_pop( $filter );
178
						}
179
					}
180
				}
181
			);
182
183
		$this->apply_filters_mock = $builder->build();
184
		$this->apply_filters_mock->enable();
185
	}
186
187
	protected function clear_mock_filters() {
188
		$this->apply_filters_mock->disable();
189
		unset( $this->mocked_filters );
190
	}
191
}
192