1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* Path processor test suite. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-autoloader |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Test suite class for the Autoloader part that handles processing paths. |
12
|
|
|
*/ |
13
|
|
|
class Test_Path_Processor extends TestCase { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The path processor we're testing. |
17
|
|
|
* |
18
|
|
|
* @var Path_Processor |
19
|
|
|
*/ |
20
|
|
|
private $processor; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Setup runs before each test. |
24
|
|
|
* |
25
|
|
|
* @before |
26
|
|
|
*/ |
27
|
|
|
public function set_up() { |
28
|
|
|
$this->processor = new Path_Processor(); |
29
|
|
|
|
30
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
31
|
|
|
@symlink( WP_PLUGIN_DIR . '/dummy_current', WP_PLUGIN_DIR . '/dummy_symlink' ); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Teardown runs after each test. |
36
|
|
|
* |
37
|
|
|
* @after |
38
|
|
|
*/ |
39
|
|
|
public function tear_down() { |
40
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
41
|
|
|
@unlink( WP_PLUGIN_DIR . '/dummy_symlink' ); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Tests that the processor is able to successfully tokenize and untokenize paths. |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function test_handles_path_tokenization_and_untokenization() { |
48
|
|
|
$path = $this->processor->tokenize_path_constants( WP_PLUGIN_DIR . '/dummy_current' ); |
49
|
|
|
|
50
|
|
|
$this->assertEquals( '{{WP_PLUGIN_DIR}}/dummy_current', $path ); |
51
|
|
|
|
52
|
|
|
$path = $this->processor->untokenize_path_constants( $path ); |
53
|
|
|
|
54
|
|
|
$this->assertEquals( WP_PLUGIN_DIR . '/dummy_current', $path ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Tests that the processor is able to successfully tokenize and untokenize paths on Windows. |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function test_handles_path_tokenization_and_untokenization_with_windows_paths() { |
61
|
|
|
$path = $this->processor->tokenize_path_constants( WP_PLUGIN_DIR . '/dummy_current' ); |
62
|
|
|
|
63
|
|
|
$this->assertEquals( '{{WP_PLUGIN_DIR}}/dummy_current', $path ); |
64
|
|
|
|
65
|
|
|
$path = $this->processor->untokenize_path_constants( $path ); |
66
|
|
|
|
67
|
|
|
$this->assertEquals( WP_PLUGIN_DIR . '/dummy_current', $path ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Tests that the processor resolves symlinks when untokenizing. |
72
|
|
|
*/ |
73
|
|
|
public function test_path_untokenization_resolves_symlinks() { |
74
|
|
|
$path = $this->processor->untokenize_path_constants( '{{WP_PLUGIN_DIR}}/dummy_symlink' ); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( WP_PLUGIN_DIR . '/dummy_current', $path ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests that find_directory_with_autoloader does not work on non-PHP files. |
81
|
|
|
*/ |
82
|
|
|
public function test_does_not_find_directory_for_non_php_files() { |
83
|
|
|
$path = $this->processor->find_directory_with_autoloader( |
84
|
|
|
'dummy_current/dummy-file.test', |
85
|
|
|
array( TEST_DATA_PATH . '/plugins' ) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->assertFalse( $path ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Tests that find_directory_with_autoloader does not work for files that don't have the autoloader. |
93
|
|
|
*/ |
94
|
|
|
public function test_does_not_find_directory_for_not_autoloaded_plugin() { |
95
|
|
|
$path = $this->processor->find_directory_with_autoloader( |
96
|
|
|
'file-plugin.php', |
97
|
|
|
array( TEST_DATA_PATH . '/plugins' ) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->assertFalse( $path ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Tests that find_directory_with_autoloader finds directories for plugins that have the autoloader. |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function test_finds_directory_for_autoloaded_plugin() { |
107
|
|
|
$path = $this->processor->find_directory_with_autoloader( |
108
|
|
|
'dummy_current/dummy_current.php', |
109
|
|
|
array( TEST_DATA_PATH . '/plugins' ) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current', $path ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Tests that find_directory_with_autoloader finds directories using Windows paths. |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function test_finds_directory_for_autoloaded_plugin_with_windows_paths() { |
119
|
|
|
$path = $this->processor->find_directory_with_autoloader( |
120
|
|
|
'dummy_current\dummy_current.php', |
121
|
|
|
array( WP_PLUGIN_DIR ) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current', $path ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Tests that find_directory_with_autoloader finds the realpath of directories that use symlinks. |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function test_finds_directory_realpath_for_symlinked_plugin() { |
131
|
|
|
$path = $this->processor->find_directory_with_autoloader( |
132
|
|
|
'dummy_symlink\dummy_current.php', |
133
|
|
|
array( WP_PLUGIN_DIR ) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$this->assertEquals( TEST_DATA_PATH . '/plugins/dummy_current', $path ); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: