Completed
Push — update/show-recurring-payments... ( 106a5e...b030b6 )
by
unknown
415:44 queued 407:29
created

Test_Roles::test_role_to_cap_existing_role()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Tests the Roles package/
4
 *
5
 * @package automattic/jetpack-roles
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Brain\Monkey;
11
use Brain\Monkey\Functions;
12
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
13
use PHPUnit\Framework\TestCase;
14
15
/**
16
 * Class Test_Roles
17
 *
18
 * @package Automattic\Jetpack
19
 */
20
class Test_Roles extends TestCase {
21
	use MockeryPHPUnitIntegration;
22
23
	/**
24
	 * Test setup.
25
	 *
26
	 * @before
27
	 */
28
	public function set_up() {
29
		Monkey\setUp();
30
		$this->roles = new Roles();
31
	}
32
33
	/**
34
	 * Test teardown.
35
	 *
36
	 * @after
37
	 */
38
	public function tear_down() {
39
		Monkey\tearDown();
40
	}
41
42
	/**
43
	 * Tests the current user by role.
44
	 *
45
	 * @covers Automattic\Jetpack\Roles::translate_current_user_to_role
46
	 */
47
	public function test_current_user_to_role_with_role() {
48
		Functions\when( 'current_user_can' )->alias(
49
			function ( $cap ) {
50
				return 'administrator' === $cap;
51
			}
52
		);
53
54
		$this->assertEquals( 'administrator', $this->roles->translate_current_user_to_role() );
55
	}
56
57
	/**
58
	 * Tests the current user by capability.
59
	 *
60
	 * @covers Automattic\Jetpack\Roles::translate_current_user_to_role
61
	 */
62
	public function test_current_user_to_role_with_capability() {
63
		Functions\when( 'current_user_can' )->alias(
64
			function ( $cap ) {
65
				return 'edit_others_posts' === $cap;
66
			}
67
		);
68
69
		$this->assertTrue( current_user_can( 'edit_others_posts' ) );
70
		$this->assertFalse( current_user_can( 'foobar' ) );
71
		$this->assertFalse( current_user_can( 'administrator' ) );
72
73
		$this->assertEquals( 'editor', $this->roles->translate_current_user_to_role() );
74
	}
75
76
	/**
77
	 * Test current user with no match.
78
	 *
79
	 * @covers Automattic\Jetpack\Roles::translate_current_user_to_role
80
	 */
81
	public function test_current_user_to_role_with_no_match() {
82
		Functions\when( 'current_user_can' )->justReturn( false );
83
84
		$this->assertFalse( $this->roles->translate_current_user_to_role() );
85
	}
86
87
	/**
88
	 * Test translating an user to a role by role.
89
	 *
90
	 * @covers Automattic\Jetpack\Roles::translate_user_to_role
91
	 */
92 View Code Duplication
	public function test_user_to_role_with_role() {
93
		$user_mock = $this->getMockBuilder( 'WP_User' )->getMock();
94
		Functions\when( 'user_can' )->alias(
95
			function ( $user, $cap ) use ( $user_mock ) {
96
				return $user_mock === $user && 'administrator' === $cap;
97
			}
98
		);
99
100
		$this->assertEquals( 'administrator', $this->roles->translate_user_to_role( $user_mock ) );
101
	}
102
103
	/**
104
	 * Test translating an user to a role by capablity.
105
	 *
106
	 * @covers Automattic\Jetpack\Roles::translate_user_to_role
107
	 */
108 View Code Duplication
	public function test_user_to_role_with_capability() {
109
		$user_mock = $this->getMockBuilder( 'WP_User' )->getMock();
110
		Functions\when( 'user_can' )->alias(
111
			function ( $user, $cap ) use ( $user_mock ) {
112
				return $user_mock === $user && 'edit_others_posts' === $cap;
113
			}
114
		);
115
116
		$this->assertEquals( 'editor', $this->roles->translate_user_to_role( $user_mock ) );
117
	}
118
119
	/**
120
	 * Test translating an user to a role with no match.
121
	 *
122
	 * @covers Automattic\Jetpack\Roles::translate_user_to_role
123
	 */
124
	public function test_user_to_role_with_no_match() {
125
		$user_mock = $this->getMockBuilder( 'WP_User' )->getMock();
126
		Functions\when( 'user_can' )->justReturn( false );
127
128
		$this->assertFalse( $this->roles->translate_user_to_role( $user_mock ) );
129
	}
130
131
	/**
132
	 * Test translating a role to a cap with an existing role.
133
	 *
134
	 * @covers Automattic\Jetpack\Roles::translate_role_to_cap
135
	 */
136
	public function test_role_to_cap_existing_role() {
137
		$this->assertEquals( 'edit_others_posts', $this->roles->translate_role_to_cap( 'editor' ) );
138
	}
139
140
	/**
141
	 * Test translating a role to a cap with a non-existing role.
142
	 *
143
	 * @covers Automattic\Jetpack\Roles::translate_role_to_cap
144
	 */
145
	public function test_role_to_cap_non_existing_role() {
146
		$this->assertFalse( $this->roles->translate_role_to_cap( 'follower' ) );
147
	}
148
149
}
150