aboutsummaryrefslogtreecommitdiffstats
path: root/tests/upload/fileupload_test.php
blob: 8b7056e9cf093cad901d291a31079e1ad6731feb (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
<?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 __DIR__ . '/../../phpBB/includes/functions.php';
require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php';
require_once __DIR__ . '/../mock/filespec.php';

class phpbb_fileupload_test extends phpbb_test_case
{
	private $path;

	private $filesystem;

	/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
	protected $container;

	/** @var \phpbb\files\factory */
	protected $factory;

	protected function setUp()
	{
		// Global $config required by unique_id
		// Global $user required by several functions dealing with translations
		// Global $request required by form_upload, local_upload and is_valid
		global $config, $user, $request, $phpbb_filesystem, $phpbb_container;

		if (!is_array($config))
		{
			$config = array();
		}

		$config['rand_seed'] = '';
		$config['rand_seed_last_update'] = time() + 600;

		$user = new phpbb_mock_user();
		$user->lang = new phpbb_mock_lang();

		$request = new phpbb_mock_request();

		$this->filesystem = $phpbb_filesystem = new \phpbb\filesystem\filesystem();

		$this->container = new phpbb_mock_container_builder($this->phpbb_root_path, $this->phpEx);
		$this->container->set('files.filespec', new \phpbb\files\filespec(
			$this->filesystem,
			new \phpbb\mimetype\guesser(array(
				'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(),
			))));
		$this->factory = new \phpbb\files\factory($this->container);

		$this->path = __DIR__ . '/fixture/';
	}

	private function gen_valid_filespec()
	{
		$filespec = new phpbb_mock_filespec();
		$filespec->filesize = 1;
		$filespec->extension = 'jpg';
		$filespec->realname = 'valid';
		$filespec->width = 2;
		$filespec->height = 2;

		return $filespec;
	}

	protected function tearDown()
	{
		// Clear globals
		global $config, $user;
		$config = array();
		$user = null;
	}

	public function test_common_checks_invalid_extension()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(array('png'))
			->set_max_filesize(100);
		$file = $this->gen_valid_filespec();
		$upload->common_checks($file);
		$this->assertEquals('DISALLOWED_EXTENSION', $file->error[0]);
	}

	public function test_common_checks_invalid_filename()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(array('jpg'))
			->set_max_filesize(100);
		$file = $this->gen_valid_filespec();
		$file->realname = 'invalid?';
		$upload->common_checks($file);
		$this->assertEquals('INVALID_FILENAME', $file->error[0]);
	}

	public function test_common_checks_too_large()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(array('jpg'))
			->set_max_filesize(100);
		$file = $this->gen_valid_filespec();
		$file->filesize = 1000;
		$upload->common_checks($file);
		$this->assertEquals('WRONG_FILESIZE', $file->error[0]);
	}

	public function test_common_checks_valid_file()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(array('jpg'))
			->set_max_filesize(1000);
		$file = $this->gen_valid_filespec();
		$upload->common_checks($file);
		$this->assertEquals(0, sizeof($file->error));
	}

	public function test_local_upload()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(array('jpg'))
			->set_max_filesize(1000);

		copy($this->path . 'jpg', $this->path . 'jpg.jpg');
		$file = $upload->local_upload($this->path . 'jpg.jpg');
		$this->assertEquals(0, sizeof($file->error));
		unlink($this->path . 'jpg.jpg');
	}

	public function test_move_existent_file()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(array('jpg'))
			->set_max_filesize(1000);

		copy($this->path . 'jpg', $this->path . 'jpg.jpg');
		$file = $upload->local_upload($this->path . 'jpg.jpg');
		$this->assertEquals(0, sizeof($file->error));
		$this->assertFalse($file->move_file('../tests/upload/fixture'));
		$this->assertFalse($file->file_moved);
		$this->assertEquals(1, sizeof($file->error));
	}

	public function test_move_existent_file_overwrite()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(array('jpg'))
			->set_max_filesize(1000);

		copy($this->path . 'jpg', $this->path . 'jpg.jpg');
		copy($this->path . 'jpg', $this->path . 'copies/jpg.jpg');
		$file = $upload->local_upload($this->path . 'jpg.jpg');
		$this->assertEquals(0, sizeof($file->error));
		$file->move_file('../tests/upload/fixture/copies', true);
		$this->assertEquals(0, sizeof($file->error));
		unlink($this->path . 'copies/jpg.jpg');
	}

	public function test_valid_dimensions()
	{
		$upload = new \phpbb\files\upload($this->filesystem, $this->factory);
		$upload->set_allowed_extensions(false)
			->set_max_filesize(false)
			->set_allowed_dimensions(1, 1, 100, 100);

		$file1 = $this->gen_valid_filespec();
		$file2 = $this->gen_valid_filespec();
		$file2->height = 101;
		$file3 = $this->gen_valid_filespec();
		$file3->width = 0;

		$this->assertTrue($upload->valid_dimensions($file1));
		$this->assertFalse($upload->valid_dimensions($file2));
		$this->assertFalse($upload->valid_dimensions($file3));
	}
}