Completed
Push — try/capabilities ( ec87c0...074fe2 )
by
unknown
06:15
created

Test_Jetpack_Capabilities_Base::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\Capabilities;
6
use phpmock\functions\FunctionProvider;
7
8
class Test_Jetpack_Capabilities_Base extends \WP_UnitTestCase {
9
	var $builder;
10
	var $current_product_slug;
11
12
	public function setUp() {
13
		\Automattic\Jetpack\Capabilities::clear();
14
		$this->builder = new Capabilities\Builder();
15
		$this->setUserRole( 'editor' );
16
	}
17
18
	public function tearDown() {
19
		\Mockery::close();
20
	}
21
22
	/**
23
	 * Utility functions
24
	 */
25
	protected function setUserRole( $role ) {
26
		$user = wp_get_current_user();
27
		$user->set_role( $role );
28
	}
29
30
	protected function addUserCapability( $cap ) {
31
		$user = wp_get_current_user();
32
		$user->add_cap( $cap );
33
	}
34
}
35
36
/**
37
 * Test registering and getting capabilities
38
 */
39
class Test_Jetpack_Capabilities_Global extends Test_Jetpack_Capabilities_Base {
40
	public function test_register_capability() {
41
		$cap = new Capabilities\Capability( 'foo' );
42
		\Automattic\Jetpack\Capabilities::register( $cap );
43
44
		$this->assertSame( $cap, \Automattic\Jetpack\Capabilities::get( 'foo' ) );
45
	}
46
47
	public function test_map_meta_cap_wraps_jetpack_capabilities() {
48
		// let's create a capability we don't comply with... yet
49
		$capability = $this->builder
50
			->create( 'jetpack.backup.restore' )
51
			->require_wp_role( 'administrator' )
52
			->register()->get();
53
54
		// quick assertion to make sure it's false
55
		$this->assertFalse( $capability->check()->granted() );
56
57
		// oh look! it's part of WP's caps now
58
		$this->assertFalse( current_user_can( 'jetpack.backup.restore' ) );
59
60
		// now let's comply
61
		$this->setUserRole( 'administrator' );
62
63
		// has admin privilege
64
		$this->assertTrue( current_user_can( 'jetpack.backup.restore' ) );
65
	}
66
}
67
68
class Test_Jetpack_Capabilities_Jetpack_Plan extends Test_Jetpack_Capabilities_Base {
69
	public function test_jetpack_plan_rule() {
70
		$capability = $this->builder
71
			->create( 'jetpack.backup.restore' )
72
			->require_minimum_jetpack_plan( 'a_nice_plan' )
73
			->get();
74
75
		// expected plan
76
		$this->mockJetpackPlan( 'a_nice_plan' );
77
78
		$this->assertTrue( $capability->check()->granted() );
79
80
		// unexpected plan
81
		$this->mockJetpackPlan( 'some_other_plan' );
82
83
		$this->assertFalse( $capability->check()->granted() );
84
	}
85
86
	private function mockJetpackPlan( $product_slug ) {
87
		$this->current_product_slug = $product_slug;
88
89
		$mockPlan = \Mockery::mock('alias:Jetpack_Plan');
90
91
		// mock the static method Jetpack_Plan::get and return the instance prop
92
		$mockPlan
93
			->shouldReceive('get')
94
			->andReturnUsing( function() {
95
				return [ 'product_slug' => $this->current_product_slug ];
96
			} );
97
	}
98
}
99
100 View Code Duplication
class Test_Jetpack_Capabilities_WP_Role extends Test_Jetpack_Capabilities_Base {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
	public function test_check_role() {
102
		$capability = $this->builder
103
			->create( 'jetpack.backup.restore' )
104
			->require_wp_role( 'administrator' )
105
			->get();
106
107
		// no admin privilege
108
		$this->assertFalse( $capability->check()->granted() );
109
110
		$this->setUserRole( 'administrator' );
111
112
		// has admin privilege
113
		$this->assertTrue( $capability->check()->granted() );
114
	}
115
}
116
117 View Code Duplication
class Test_Jetpack_Capabilities_WP_Capability extends Test_Jetpack_Capabilities_Base {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
	public function test_check_capability() {
119
		$capability = $this->builder
120
			->create( 'jetpack.backup.restore' )
121
			->require_wp_capability( 'do_a_thing' )
122
			->get();
123
124
		// no admin privilege
125
		$this->assertFalse( $capability->check()->granted() );
126
127
		$this->addUserCapability( 'do_a_thing' );
128
129
		// has admin privilege
130
		$this->assertTrue( $capability->check()->granted() );
131
	}
132
}
133
134
class Test_Jetpack_Capabilities_Builder extends Test_Jetpack_Capabilities_Base {
135
	public function test_builder_registers_capability() {
136
		$capability = $this->builder
137
			->create( 'jetpack.test' )
138
			->register()
139
			->get();
140
141
		$this->assertSame( $capability, \Automattic\Jetpack\Capabilities::get( 'jetpack.test' ) );
142
	}
143
}