1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack; |
4
|
|
|
|
5
|
|
|
use Automattic\Jetpack\Roles; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use phpmock\Mock; |
8
|
|
|
use phpmock\MockBuilder; |
9
|
|
|
|
10
|
|
|
class Test_Roles extends TestCase { |
11
|
|
|
/** |
12
|
|
|
* Test setup. |
13
|
|
|
*/ |
14
|
|
|
public function setUp() { |
15
|
|
|
$this->roles = new Roles(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Test teardown. |
20
|
|
|
*/ |
21
|
|
|
public function tearDown() { |
22
|
|
|
Mock::disableAll(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @covers Automattic\Jetpack\Roles::translate_current_user_to_role |
27
|
|
|
*/ |
28
|
|
|
public function test_current_user_to_role_with_role() { |
29
|
|
|
$this->mock_function( 'current_user_can', true, 'administrator' ); |
30
|
|
|
|
31
|
|
|
$this->assertEquals( 'administrator', $this->roles->translate_current_user_to_role() ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @covers Automattic\Jetpack\Roles::translate_current_user_to_role |
36
|
|
|
*/ |
37
|
|
|
public function test_current_user_to_role_with_capability() { |
38
|
|
|
$this->mock_function( 'current_user_can', true, 'edit_others_posts' ); |
39
|
|
|
|
40
|
|
|
$this->assertEquals( 'editor', $this->roles->translate_current_user_to_role() ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @covers Automattic\Jetpack\Roles::translate_current_user_to_role |
45
|
|
|
*/ |
46
|
|
|
public function test_current_user_to_role_with_no_match() { |
47
|
|
|
$this->mock_function( 'current_user_can', false ); |
48
|
|
|
|
49
|
|
|
$this->assertEquals( false, $this->roles->translate_current_user_to_role() ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @covers Automattic\Jetpack\Roles::translate_user_to_role |
54
|
|
|
*/ |
55
|
|
|
public function test_user_to_role_with_role() { |
56
|
|
|
$user_mock = $this->getMockBuilder( 'WP_User' )->getMock(); |
57
|
|
|
$this->mock_function( 'user_can', true, $user_mock, 'administrator' ); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( 'administrator', $this->roles->translate_user_to_role( $user_mock ) ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @covers Automattic\Jetpack\Roles::translate_user_to_role |
64
|
|
|
*/ |
65
|
|
|
public function test_user_to_role_with_capability() { |
66
|
|
|
$user_mock = $this->getMockBuilder( 'WP_User' )->getMock(); |
67
|
|
|
$this->mock_function( 'user_can', true, $user_mock, 'edit_others_posts' ); |
68
|
|
|
|
69
|
|
|
$this->assertEquals( 'editor', $this->roles->translate_user_to_role( $user_mock ) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @covers Automattic\Jetpack\Roles::translate_user_to_role |
74
|
|
|
*/ |
75
|
|
|
public function test_user_to_role_with_no_match() { |
76
|
|
|
$user_mock = $this->getMockBuilder( 'WP_User' )->getMock(); |
77
|
|
|
$this->mock_function( 'user_can', false ); |
78
|
|
|
|
79
|
|
|
$this->assertEquals( false, $this->roles->translate_user_to_role( $user_mock ) ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @covers Automattic\Jetpack\Roles::translate_role_to_cap |
84
|
|
|
*/ |
85
|
|
|
public function test_role_to_cap_existing_role() { |
86
|
|
|
$this->assertEquals( 'edit_others_posts', $this->roles->translate_role_to_cap( 'editor' ) ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @covers Automattic\Jetpack\Roles::translate_role_to_cap |
91
|
|
|
*/ |
92
|
|
|
public function test_role_to_cap_non_existing_role() { |
93
|
|
|
$this->assertEquals( false, $this->roles->translate_role_to_cap( 'follower' ) ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Mock a global function and make it return a certain value. |
98
|
|
|
* Optionally can limit the mock to invocations with certain arguments. |
99
|
|
|
* |
100
|
|
|
* @param string $function_name Name of the function. |
101
|
|
|
* @param mixed $return_value Return value of the function. |
102
|
|
|
* @param mixed $arg_1_value Value of the first argument value we expect. |
103
|
|
|
* @param mixed $arg_2_value Value of the second argument value we expect. |
104
|
|
|
* @return phpmock\Mock The mock object. |
105
|
|
|
*/ |
106
|
|
|
protected function mock_function( $function_name, $return_value = null, $arg_1_value = null, $arg_2_value = null ) { |
107
|
|
|
$builder = new MockBuilder(); |
108
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
109
|
|
|
->setName( $function_name ) |
110
|
|
|
->setFunction( function( $arg_1, $arg_2 = null ) use ( &$return_value, &$arg_1_value, &$arg_2_value ) { |
111
|
|
|
// Return the value if we don't care about arguments. |
112
|
|
|
if ( is_null( $arg_1 ) && is_null( $arg_2 ) ) { |
113
|
|
|
return $return_value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Return the value if we don't care about the second argument, but the first one matches. |
117
|
|
|
if ( is_null( $arg_2 ) && $arg_1_value === $arg_1 ) { |
118
|
|
|
return $return_value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Return the value if both arguments match. |
122
|
|
|
if ( $arg_1_value === $arg_1 && $arg_2_value === $arg_2 ) { |
123
|
|
|
return $return_value; |
124
|
|
|
} |
125
|
|
|
} ); |
126
|
|
|
return $builder->build()->enable(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|