Completed
Push — try/capabilities ( 672273...ec87c0 )
by
unknown
15:57 queued 09:35
created

Test_Jetpack_Capabilities::test_get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 16
loc 16
rs 9.7333
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 extends \WP_UnitTestCase {
9
	var $builder;
10
	var $current_product_slug;
11
12
	public function setUp() {
13
		$this->builder = new Capabilities\Builder();
14
		$this->setUserRole( 'editor' );
15
	}
16
17
	public function tearDown() {
18
		\Mockery::close();
19
	}
20
21 View Code Duplication
	public function test_get() {
22
23
		$capability = $this->builder
24
			->create( 'jetpack.backup.restore' )
25
			->require_wp_role( 'administrator' )
26
			->require_wp_capability( 'administrator' )
27
			->get();
28
29
		// no admin privilege
30
		$this->assertFalse( $capability->check()->granted() );
31
32
		$this->setUserRole( 'administrator' );
33
34
		// has admin privilege
35
		$this->assertTrue( $capability->check()->granted() );
36
	}
37
38
	public function test_capability_has_details() {
39
		$capability = $this->builder
40
			->create( 'jetpack.backup.restore' )
41
			->require_wp_role( 'administrator' )
42
			->require_wp_capability( 'administrator' )
43
			->get();
44
45
		// response should have a "granted" method
46
		$this->assertFalse( $capability->check()->granted() );
47
	}
48
49 View Code Duplication
	public function test_jetpack_plan_rule() {
50
		$capability = $this->builder
51
			->create( 'jetpack.backup.restore' )
52
			->require_minimum_jetpack_plan( 'a_nice_plan' )
53
			->get();
54
55
		// expected plan
56
		$this->mockJetpackPlan( 'a_nice_plan' );
57
58
		$this->assertTrue( $capability->check()->granted() );
59
60
		// unexpected plan
61
		$this->mockJetpackPlan( 'some_other_plan' );
62
63
		$this->assertFalse( $capability->check()->granted() );
64
	}
65
66
	public function test_builder_registers_capability() {
67
		$capability = $this->builder
68
			->create( 'jetpack.test' )
69
			->register()
70
			->get();
71
72
		$this->assertSame( $capability, \Automattic\Jetpack\Capabilities::get( 'jetpack.test' ) );
73
	}
74
75
	/**
76
	 * Utility functions
77
	 */
78
	private function setUserRole( $role ) {
79
		$user = wp_get_current_user(); // new \WP_User( $user_id );
80
		$user->set_role( $role );
81
	}
82
83
	private function mockJetpackPlan( $product_slug ) {
84
		$this->current_product_slug = $product_slug;
85
86
		$mockPlan = \Mockery::mock('alias:Jetpack_Plan');
87
88
		// mock the static method Jetpack_Plan::get and return the instance prop
89
		$mockPlan
90
			->shouldReceive('get')
91
			->andReturnUsing( function() {
92
				return [ 'product_slug' => $this->current_product_slug ];
93
			} );
94
	}
95
}
96