Completed
Push — try/capabilities ( 7f65a9...672273 )
by
unknown
19:16 queued 12:21
created

Test_Jetpack_Capabilities::mockJetpackPlan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
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_capability() {
22
23
		$capability = $this->builder
24
			->create_capability( 'jetpack.backup.restore' )
25
			->require_wp_role( 'administrator' )
26
			->require_wp_capability( 'administrator' )
27
			->get_capability();
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_capability( 'jetpack.backup.restore' )
41
			->require_wp_role( 'administrator' )
42
			->require_wp_capability( 'administrator' )
43
			->get_capability();
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_capability( 'jetpack.backup.restore' )
52
			->require_minimum_jetpack_plan( 'a_nice_plan' )
53
			->get_capability();
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
	/**
67
	 * Utility functions
68
	 */
69
	private function setUserRole( $role ) {
70
		$user = wp_get_current_user(); // new \WP_User( $user_id );
71
		$user->set_role( $role );
72
	}
73
74
	private function mockJetpackPlan( $product_slug ) {
75
		$this->current_product_slug = $product_slug;
76
77
		$mockPlan = \Mockery::mock('alias:Jetpack_Plan');
78
79
		// mock the static method Jetpack_Plan::get and return the instance prop
80
		$mockPlan
81
			->shouldReceive('get')
82
			->andReturnUsing( function() {
83
				return [ 'product_slug' => $this->current_product_slug ];
84
			} );
85
	}
86
}
87