aboutsummaryrefslogtreecommitdiffstats
path: root/tests/extension/finder_test.php
blob: 0cf268d32b29f07dff2134f07b63789dcaa3f38c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';

class phpbb_extension_finder_test extends phpbb_test_case
{
	protected $extension_manager;
	protected $finder;

	public function setUp()
	{
		$this->extension_manager = new phpbb_mock_extension_manager(
			dirname(__FILE__) . '/',
			array(
				'vendor2/foo' => array(
					'ext_name' => 'vendor2/foo',
					'ext_active' => '1',
					'ext_path' => 'ext/vendor2/foo/',
				),
				'vendor3/bar' => array(
					'ext_name' => 'vendor3/bar',
					'ext_active' => '1',
					'ext_path' => 'ext/vendor3/bar/',
				),
			));

		$this->finder = $this->extension_manager->get_finder();
	}

	public function test_suffix_get_classes()
	{
		$classes = $this->finder
			->core_path('phpbb/default/')
			->extension_suffix('_class')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'\phpbb\default\implementation',
				'\vendor2\foo\a_class',
				'\vendor2\foo\b_class',
				'\vendor3\bar\my\hidden_class',
			),
			$classes
		);
	}

	public function test_get_directories()
	{
		$dirs = $this->finder
			->directory('/type')
			->get_directories();

		sort($dirs);
		$this->assertEquals(array(
			dirname(__FILE__) . '/ext/vendor2/foo/type/',
		), $dirs);
	}

	public function test_prefix_get_directories()
	{
		$dirs = $this->finder
			->prefix('ty')
			->get_directories();

		sort($dirs);
		$this->assertEquals(array(
			dirname(__FILE__) . '/ext/vendor2/foo/sub/type/',
			dirname(__FILE__) . '/ext/vendor2/foo/type/',
			dirname(__FILE__) . '/ext/vendor2/foo/typewrong/',
		), $dirs);
	}

	public function test_prefix_get_classes()
	{
		$classes = $this->finder
			->core_path('phpbb/default/')
			->extension_prefix('hidden_')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'\phpbb\default\implementation',
				'\vendor3\bar\my\hidden_class',
			),
			$classes
		);
	}

	public function test_directory_get_classes()
	{
		$classes = $this->finder
			->core_path('phpbb/default/')
			->extension_directory('type')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'\phpbb\default\implementation',
				'\vendor2\foo\sub\type\alternative',
				'\vendor2\foo\type\alternative',
			),
			$classes
		);
	}

	public function test_absolute_directory_get_classes()
	{
		$classes = $this->finder
			->directory('/type/')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'\vendor2\foo\type\alternative',
			),
			$classes
		);
	}

	public function test_non_absolute_directory_get_classes()
	{
		$classes = $this->finder
			->directory('type/')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'\vendor2\foo\sub\type\alternative',
				'\vendor2\foo\type\alternative',
			),
			$classes
		);
	}

	public function test_sub_directory_get_classes()
	{
		$classes = $this->finder
			->directory('/sub/type')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'\vendor2\foo\sub\type\alternative',
			),
			$classes
		);
	}

	public function test_uncleansub_directory_get_classes()
	{
		$classes = $this->finder
			->directory('/sub/../sub/type')
			->get_classes();

		sort($classes);
		$this->assertEquals(
			array(
				'\vendor2\foo\sub\type\alternative',
			),
			$classes
		);
	}

	public function test_find_from_extension()
	{
		$files = $this->finder
			->extension_directory('/type')
			->find_from_extension('vendor2/foo', dirname(__FILE__) . '/ext/vendor2/foo/');
		$classes = $this->finder->get_classes_from_files($files);

		sort($classes);
		$this->assertEquals(
			array(
				'\vendor2\foo\type\alternative',
				'\vendor2\foo\type\dummy\empty',
			),
			$classes
		);
	}

	/**
	* These do not work because of changes with PHPBB3-11386
	* They do not seem neccessary to me, so I am commenting them out for now
	public function test_get_classes_create_cache()
	{
		$cache = new phpbb_mock_cache;
		$finder = new \phpbb\extension\finder(new \phpbb\filesystem(), dirname(__FILE__) . '/', $cache, 'php', '_custom_cache_name');
		$finder->set_extensions($this->extension_manager->all_enabled());
		$files = $finder->suffix('_class.php')->get_files();

		$expected_files = array(
			'ext/bar/my/hidden_class.php' => 'bar',
			'ext/foo/a_class.php' => 'foo',
			'ext/foo/b_class.php' => 'foo',
		);

		$query = array(
			'core_path' => false,
			'core_suffix' => '_class.php',
			'core_prefix' => false,
			'core_directory' => false,
			'extension_suffix' => '_class.php',
			'extension_prefix' => false,
			'extension_directory' => false,
			'is_dir' => false,
		);

		$cache->checkAssociativeVar($this, '_custom_cache_name', array(
			md5(serialize($query)) => $expected_files,
		), false);
	}

	public function test_cached_get_files()
	{
		$query = array(
			'core_path' => 'phpbb/foo',
			'core_suffix' => false,
			'core_prefix' => false,
			'core_directory' => 'bar',
			'extension_suffix' => false,
			'extension_prefix' => false,
			'extension_directory' => false,
			'is_dir' => false,
		);

		$finder = new \phpbb\extension\finder(
			new \phpbb\filesystem(),
			dirname(__FILE__) . '/',
			new phpbb_mock_cache(array(
				'_ext_finder' => array(
					md5(serialize($query)) => array('file_name' => 'extension'),
				),
			))
		);
		$finder->set_extensions($this->extension_manager->all_enabled());

		$classes = $finder
			->core_path($query['core_path'])
			->core_directory($query['core_directory'])
			->get_files();

		sort($classes);
		$this->assertEquals(
			array(dirname(__FILE__) . '/file_name'),
			$classes
		);
	}
	*/
}