1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack; |
4
|
|
|
|
5
|
|
|
use Automattic\Jetpack\Capabilities; |
6
|
|
|
use phpmock\functions\FunctionProvider; |
7
|
|
|
use phpmock\Mock; |
8
|
|
|
use phpmock\MockBuilder; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class Test_Jetpack_Capabilities extends \WP_UnitTestCase { |
12
|
|
|
public function setUp() { |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function tearDown() { |
16
|
|
|
Mock::disableAll(); |
17
|
|
|
\Mockery::close(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function test_get_capability() { |
21
|
|
|
// Capabilities::register( new Capability( 'jetpack.backup.restore' ) ); |
22
|
|
|
|
23
|
|
|
$builder = new Capabilities\Builder(); |
24
|
|
|
|
25
|
|
|
$builder->create( 'jetpack.backup.restore' ) |
|
|
|
|
26
|
|
|
->require_wp_role( 'administrator' ) |
27
|
|
|
->require_wp_capability( 'administrator' ) |
28
|
|
|
->require_minimum_jetpack_plan( JETPACK_BUSINESS_PLAN_SLUG ); |
29
|
|
|
|
30
|
|
|
$this->setUserRole( 'administrator' ); |
31
|
|
|
|
32
|
|
|
$cap = Capabilities::get( 'jetpack.backup.restore' ); |
33
|
|
|
$this->assertTrue( $cap->available ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// public function test_jitm_disabled_by_filter() { |
37
|
|
|
// $this->mock_filters( array( |
38
|
|
|
// array( 'jetpack_just_in_time_msgs', false, false ), |
39
|
|
|
// ) ); |
40
|
|
|
|
41
|
|
|
// $jitm = new JITM(); |
42
|
|
|
// $this->assertFalse( $jitm->register() ); |
43
|
|
|
|
44
|
|
|
// $this->clear_mock_filters(); |
45
|
|
|
// } |
46
|
|
|
|
47
|
|
|
// public function test_jitm_enabled_by_default() { |
48
|
|
|
// $this->mock_filters( array( |
49
|
|
|
// array( 'jetpack_just_in_time_msgs', false, true ), |
50
|
|
|
// ) ); |
51
|
|
|
|
52
|
|
|
// $jitm = new JITM(); |
53
|
|
|
// $this->assertTrue( $jitm->register() ); |
54
|
|
|
|
55
|
|
|
// $this->clear_mock_filters(); |
56
|
|
|
// } |
57
|
|
|
|
58
|
|
|
// /** |
59
|
|
|
// * This is an example of a test which uses Mockery to tests a class static method. |
60
|
|
|
// * |
61
|
|
|
// * It requires the runInSeparateProcess tag so that the class isn't already autoloaded. |
62
|
|
|
// * |
63
|
|
|
// * @runInSeparateProcess |
64
|
|
|
// */ |
65
|
|
|
// public function test_prepare_jitms_enqueues_assets() { |
66
|
|
|
// $mockAssets = \Mockery::mock('alias:Automattic\Jetpack\Assets'); |
67
|
|
|
|
68
|
|
|
// // mock the static method and return a dummy value |
69
|
|
|
// $mockAssets |
70
|
|
|
// ->shouldReceive('get_file_url_for_environment') |
71
|
|
|
// ->andReturn('the_file_url'); |
72
|
|
|
|
73
|
|
|
// $jitm = new JITM(); |
74
|
|
|
// $screen = (object) array( 'id' => 'jetpack_foo' ); // fake screen object |
75
|
|
|
// $jitm->prepare_jitms( $screen ); |
76
|
|
|
|
77
|
|
|
// // this should enqueue a jetpack-jitm-new script |
78
|
|
|
// do_action( 'admin_enqueue_scripts' ); |
79
|
|
|
|
80
|
|
|
// // assert our script was enqueued with the right value |
81
|
|
|
// $script = $this->get_enqueued_script( 'jetpack-jitm-new' ); |
82
|
|
|
|
83
|
|
|
// $this->assertEquals( 'the_file_url', $script['src'] ); |
84
|
|
|
// } |
85
|
|
|
|
86
|
|
|
// /* |
87
|
|
|
// public function test_prepare_jitms_does_not_show_on_some_screens() { |
88
|
|
|
// $jitm = new JITM(); |
89
|
|
|
// $screen = new \stdClass(); |
90
|
|
|
// $screen->id = 'jetpack_page_stats'; |
91
|
|
|
// $jitm->prepare_jitms( $screen ); |
92
|
|
|
// } |
93
|
|
|
// */ |
94
|
|
|
|
95
|
|
|
// protected function mock_filters( $filters ) { |
96
|
|
|
// $this->mocked_filters = $filters; |
97
|
|
|
// $builder = new MockBuilder(); |
98
|
|
|
// $builder->setNamespace( __NAMESPACE__ ) |
99
|
|
|
// ->setName( 'apply_filters' ) |
100
|
|
|
// ->setFunction( |
101
|
|
|
// function() { |
102
|
|
|
// $current_args = func_get_args(); |
103
|
|
|
// foreach ( $this->mocked_filters as $filter ) { |
104
|
|
|
// if ( array_slice( $filter, 0, -1 ) === $current_args ) { |
105
|
|
|
// return array_pop( $filter ); |
106
|
|
|
// } |
107
|
|
|
// } |
108
|
|
|
// } |
109
|
|
|
// ); |
110
|
|
|
// $this->apply_filters_mock = $builder->build(); |
111
|
|
|
// $this->apply_filters_mock->enable(); |
112
|
|
|
// } |
113
|
|
|
|
114
|
|
|
// protected function clear_mock_filters() { |
115
|
|
|
// $this->apply_filters_mock->disable(); |
116
|
|
|
// unset( $this->mocked_filters ); |
117
|
|
|
// } |
118
|
|
|
|
119
|
|
|
// protected function mock_add_action() { |
120
|
|
|
// $builder = new MockBuilder(); |
121
|
|
|
// $builder->setNamespace( __NAMESPACE__ ) |
122
|
|
|
// ->setName( 'add_action' ) |
123
|
|
|
// ->setFunction( function( $name, $callable ) { |
124
|
|
|
// global $actions; |
125
|
|
|
|
126
|
|
|
// if ( is_null( $actions ) ) { |
127
|
|
|
// $actions = array(); |
128
|
|
|
// } |
129
|
|
|
|
130
|
|
|
// // don't worry about precedence for now |
131
|
|
|
// if ( ! isset( $actions[$name] ) ) { |
132
|
|
|
// $actions[$name] = array(); |
133
|
|
|
// } |
134
|
|
|
|
135
|
|
|
// $actions[$name][] = $callable; |
136
|
|
|
// } ); |
137
|
|
|
// $builder->build()->enable(); |
138
|
|
|
// } |
139
|
|
|
|
140
|
|
|
// protected function mock_do_action() { |
141
|
|
|
// $builder = new MockBuilder(); |
142
|
|
|
// $builder->setNamespace( __NAMESPACE__ ) |
143
|
|
|
// ->setName( 'do_action' ) |
144
|
|
|
// ->setFunction( function() { |
145
|
|
|
// global $actions; |
146
|
|
|
// $args = func_get_args(); |
147
|
|
|
// $name = array_shift( $args ); |
148
|
|
|
|
149
|
|
|
// if ( is_null( $actions ) ) { |
150
|
|
|
// $actions = array(); |
151
|
|
|
// } |
152
|
|
|
|
153
|
|
|
// // don't worry about precedence for now |
154
|
|
|
// if ( ! isset( $actions[$name] ) ) { |
155
|
|
|
// $actions[$name] = array(); |
156
|
|
|
// } |
157
|
|
|
|
158
|
|
|
// foreach( $actions[$name] as $callable ) { |
159
|
|
|
// call_user_func_array( $callable, $args ); |
160
|
|
|
// } |
161
|
|
|
// } ); |
162
|
|
|
// $builder->build()->enable(); |
163
|
|
|
// } |
164
|
|
|
|
165
|
|
|
// protected function mock_wp_enqueue_script() { |
166
|
|
|
// $builder = new MockBuilder(); |
167
|
|
|
// $builder->setNamespace( __NAMESPACE__ ) |
168
|
|
|
// ->setName( 'wp_enqueue_script' ) |
169
|
|
|
// ->setFunction( function( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
170
|
|
|
// global $wp_scripts; |
171
|
|
|
|
172
|
|
|
// if ( is_null( $wp_scripts ) ) { |
173
|
|
|
// $wp_scripts = array(); |
174
|
|
|
// } |
175
|
|
|
|
176
|
|
|
// $wp_scripts[$handle] = compact( 'src', 'deps', 'ver', 'in_footer' ); |
177
|
|
|
// } ); |
178
|
|
|
// $builder->build()->enable(); |
179
|
|
|
// } |
180
|
|
|
|
181
|
|
|
// protected function get_enqueued_script( $handle ) { |
182
|
|
|
// global $wp_scripts; |
183
|
|
|
// return isset( $wp_scripts[$handle] ) ? $wp_scripts[$handle] : null; |
184
|
|
|
// } |
185
|
|
|
|
186
|
|
|
// protected function clear_added_actions() { |
187
|
|
|
// global $actions; |
188
|
|
|
// $actions = array(); |
189
|
|
|
// } |
190
|
|
|
|
191
|
|
|
// protected function clear_enqueued_scripts() { |
192
|
|
|
// global $wp_scripts; |
193
|
|
|
// $wp_scripts = array(); |
194
|
|
|
// } |
195
|
|
|
|
196
|
|
|
// protected function mock_empty_function( $name ) { |
197
|
|
|
// $builder = new MockBuilder(); |
198
|
|
|
// $builder->setNamespace( __NAMESPACE__ ) |
199
|
|
|
// ->setName( $name ) |
200
|
|
|
// ->setFunction( function() use ( $name ) { |
201
|
|
|
// // echo "Called $name with " . print_r( func_get_args(),1 ) . "\n"; |
202
|
|
|
// } ); |
203
|
|
|
// $builder->build()->enable(); |
204
|
|
|
// } |
205
|
|
|
} |
206
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.