Completed
Push — add/sync-partial-sync-checksum... ( 93f4e2...c9ed3f )
by
unknown
253:26 queued 243:12
created

Test_Roles::test_user_to_role_with_role()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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