Completed
Push — try/e2e-check-if-chrome-instal... ( 24dbcf...296824 )
by Yaroslav
23:03 queued 13:54
created

Test_Plugins_Handler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 218
Duplicated Lines 10.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 22
loc 218
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A test_gets_active_plugins() 0 26 1
A test_gets_active_plugins_when_multisite() 0 35 1
A test_gets_active_plugins_records_unknown_plugins() 0 24 1
A test_gets_active_plugins_ignores_unknown_plugins_when_including_latest() 0 22 1
A test_gets_cached_plugins() 12 12 1
A test_updates_cache_writes_plugins() 10 10 1
A test_detects_when_plugins_change() 0 13 1
A set_up() 0 9 1
A tear_down() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php // phpcs:ignore WordPress.Files.FileName
2
3
/**
4
 * Plugins handler test suite.
5
 *
6
 * @package automattic/jetpack-autoloader
7
 */
8
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Test suite class for the autoloader's plugin handler.
13
 *
14
 * @runTestsInSeparateProcesses Ensure that each test has no previously autoloaded files.
15
 * @preserveGlobalState disabled
16
 */
17
class Test_Plugins_Handler extends TestCase {
18
19
	/**
20
	 * A dependency mock for the handler.
21
	 *
22
	 * @var Plugin_Locator|\PHPUnit\Framework\MockObject\MockObject
23
	 */
24
	private $plugin_locator;
25
26
	/**
27
	 * A dependency mock for the handler.
28
	 *
29
	 * @var Path_Processor|\PHPUnit\Framework\MockObject\MockObject
30
	 */
31
	private $path_processor;
32
33
	/**
34
	 * The class under test.
35
	 *
36
	 * @var Plugins_Handler
37
	 */
38
	private $plugins_handler;
39
40
	/**
41
	 * Setup runs before each test.
42
	 *
43
	 * @before
44
	 */
45
	public function set_up() {
46
		$this->plugin_locator  = $this->getMockBuilder( Plugin_Locator::class )
47
			->disableOriginalConstructor()
48
			->getMock();
49
		$this->path_processor  = $this->getMockBuilder( Path_Processor::class )
50
			->disableOriginalConstructor()
51
			->getMock();
52
		$this->plugins_handler = new Plugins_Handler( $this->plugin_locator, $this->path_processor );
53
	}
54
55
	/**
56
	 * Teardown runs after each test.
57
	 *
58
	 * @after
59
	 */
60
	public function tear_down() {
61
		cleanup_test_wordpress_data();
62
	}
63
64
	/**
65
	 * Tests that all active plugins are found.
66
	 */
67
	public function test_gets_active_plugins() {
68
		global $jetpack_autoloader_activating_plugins_paths;
69
		$jetpack_autoloader_activating_plugins_paths = array( TEST_DATA_PATH . '/plugins/plugin_activating' );
70
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
			->method( 'find_using_option' )
72
			->with( 'active_plugins', false )
73
			->willReturn( array( TEST_DATA_PATH . '/plugins/dummy_current' ) );
74
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
			->method( 'find_activating_this_request' )
76
			->willReturn( array( TEST_DATA_PATH . '/plugins/dummy_dev' ) );
77
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
			->method( 'find_current_plugin' )
79
			->willReturn( TEST_DATA_PATH . '/plugins/dummy_newer' );
80
81
		$plugin_paths = $this->plugins_handler->get_active_plugins();
82
83
		$this->assertEquals(
84
			array(
85
				TEST_DATA_PATH . '/plugins/plugin_activating',
86
				TEST_DATA_PATH . '/plugins/dummy_current',
87
				TEST_DATA_PATH . '/plugins/dummy_dev',
88
				TEST_DATA_PATH . '/plugins/dummy_newer',
89
			),
90
			$plugin_paths
91
		);
92
	}
93
94
	/**
95
	 * Tests that all active plugins are found when the site is multisite.
96
	 */
97
	public function test_gets_active_plugins_when_multisite() {
98
		set_test_is_multisite( true );
99
100
		global $jetpack_autoloader_activating_plugins_paths;
101
		$jetpack_autoloader_activating_plugins_paths = array( TEST_DATA_PATH . '/plugins/plugin_activating' );
102
		$this->plugin_locator->expects( $this->exactly( 2 ) )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
			->method( 'find_using_option' )
104
			->withConsecutive(
105
				array( 'active_plugins', false ),
106
				array( 'active_sitewide_plugins', true )
107
			)
108
			->willReturnOnConsecutiveCalls(
109
				array( TEST_DATA_PATH . '/plugins/dummy_current' ),
110
				array( TEST_DATA_PATH . '/plugins/dummy_newer' )
111
			);
112
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
			->method( 'find_activating_this_request' )
114
			->willReturn( array( TEST_DATA_PATH . '/plugins/dummy_dev' ) );
115
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
			->method( 'find_current_plugin' )
117
			->willReturn( TEST_DATA_PATH . '/plugins' );
118
119
		$plugin_paths = $this->plugins_handler->get_active_plugins();
120
121
		$this->assertEquals(
122
			array(
123
				TEST_DATA_PATH . '/plugins/plugin_activating',
124
				TEST_DATA_PATH . '/plugins/dummy_current',
125
				TEST_DATA_PATH . '/plugins/dummy_newer',
126
				TEST_DATA_PATH . '/plugins/dummy_dev',
127
				TEST_DATA_PATH . '/plugins',
128
			),
129
			$plugin_paths
130
		);
131
	}
132
133
	/**
134
	 * Tests that the current plugin is recorded as unknown when it isn't found as an active plugin.
135
	 */
136
	public function test_gets_active_plugins_records_unknown_plugins() {
137
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
			->method( 'find_using_option' )
139
			->with( 'active_plugins', false )
140
			->willReturn( array() );
141
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
			->method( 'find_activating_this_request' )
143
			->willReturn( array() );
144
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
			->method( 'find_current_plugin' )
146
			->willReturn( TEST_DATA_PATH . '/plugins/dummy_newer' );
147
148
		$plugin_paths = $this->plugins_handler->get_active_plugins();
149
150
		$this->assertEquals(
151
			array(
152
				TEST_DATA_PATH . '/plugins/dummy_newer',
153
			),
154
			$plugin_paths
155
		);
156
157
		global $jetpack_autoloader_activating_plugins_paths;
158
		$this->assertContains( TEST_DATA_PATH . '/plugins/dummy_newer', $jetpack_autoloader_activating_plugins_paths );
159
	}
160
161
	/**
162
	 * Tests that the current plugin is ignored when it isn't an active plugin but the
163
	 * autoloader isn't being included from a plugin file.
164
	 */
165
	public function test_gets_active_plugins_ignores_unknown_plugins_when_including_latest() {
166
		global $jetpack_autoloader_including_latest;
167
		$jetpack_autoloader_including_latest = true;
168
169
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
170
			->method( 'find_using_option' )
171
			->with( 'active_plugins', false )
172
			->willReturn( array() );
173
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
174
			->method( 'find_activating_this_request' )
175
			->willReturn( array() );
176
		$this->plugin_locator->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Plugin_Locator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
			->method( 'find_current_plugin' )
178
			->willReturn( TEST_DATA_PATH . '/plugins/dummy_newer' );
179
180
		$plugin_paths = $this->plugins_handler->get_active_plugins();
181
182
		$this->assertEmpty( $plugin_paths );
183
184
		global $jetpack_autoloader_activating_plugins_paths;
185
		$this->assertEmpty( $jetpack_autoloader_activating_plugins_paths );
186
	}
187
188
	/**
189
	 * Tests that the plugins in the cache are loaded.
190
	 */
191 View Code Duplication
	public function test_gets_cached_plugins() {
192
		set_transient( Plugins_Handler::TRANSIENT_KEY, array( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' ) );
193
194
		$this->path_processor->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
			->method( 'untokenize_path_constants' )
196
			->with( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' )
197
			->willReturn( TEST_DATA_PATH . '/plugins/dummy_newer' );
198
199
		$plugin_paths = $this->plugins_handler->get_cached_plugins();
200
201
		$this->assertEquals( array( TEST_DATA_PATH . '/plugins/dummy_newer' ), $plugin_paths );
202
	}
203
204
	/**
205
	 * Tests that the plugins are updated when they have changed.
206
	 */
207 View Code Duplication
	public function test_updates_cache_writes_plugins() {
208
		$this->path_processor->expects( $this->once() )
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Path_Processor>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
209
			->method( 'tokenize_path_constants' )
210
			->with( TEST_DATA_PATH . '/plugins/dummy_newer' )
211
			->willReturn( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' );
212
213
		$this->plugins_handler->cache_plugins( array( TEST_DATA_PATH . '/plugins/dummy_newer' ) );
214
215
		$this->assertEquals( array( '{{WP_PLUGIN_PATH}}/plugins/dummy_newer' ), get_transient( Plugins_Handler::TRANSIENT_KEY ) );
216
	}
217
218
	/**
219
	 * Tests that the handler indicate whether or not the plugins have changed from the global cached list.
220
	 */
221
	public function test_detects_when_plugins_change() {
222
		global $jetpack_autoloader_cached_plugin_paths;
223
224
		$plugins = array();
225
		$this->assertFalse( $this->plugins_handler->have_plugins_changed( $plugins ) );
226
		$this->assertSame( $plugins, $jetpack_autoloader_cached_plugin_paths );
227
		$this->assertFalse( $this->plugins_handler->have_plugins_changed( $plugins ) );
228
229
		$plugins = array( TEST_DATA_PATH . '/plugins/dummy_newer' );
230
		$this->assertTrue( $this->plugins_handler->have_plugins_changed( $plugins ) );
231
		$this->assertSame( $plugins, $jetpack_autoloader_cached_plugin_paths );
232
		$this->assertFalse( $this->plugins_handler->have_plugins_changed( $plugins ) );
233
	}
234
}
235