aboutsummaryrefslogtreecommitdiff
blob: b7b5b0836044df74d8ecbd178407de31bb074c51 (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
<?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.
*
*/

/**
 * @group functional
 */
class phpbb_functional_plupload_test extends phpbb_functional_test_case
{
	const CHUNKS = 4;
	private $path;

	protected function set_extension_group_permission($val)
	{
		$db = $this->get_db();
		$query = "
			UPDATE phpbb_extension_groups
			SET allow_in_pm = '$val'
			WHERE group_name = 'IMAGES'
		";
		$db->sql_query($query);
	}

	public function setUp(): void
	{
		parent::setUp();
		$this->purge_cache();
		$this->set_extension_group_permission(1);
		$this->path = __DIR__ . '/fixtures/files/';
		$this->add_lang('posting');
		$this->login();
	}

	public function tearDown(): void
	{
		$this->set_extension_group_permission(0);
		$iterator = new DirectoryIterator(__DIR__ . '/../../phpBB/files/');
		foreach ($iterator as $fileinfo)
		{
			if (
				$fileinfo->isDot()
				|| $fileinfo->isDir()
				|| $fileinfo->getFilename() === 'index.htm'
				|| $fileinfo->getFilename() === '.htaccess'
			)
			{
				continue;
			}

			unlink($fileinfo->getPathname());
		}
	}

	public function get_urls()
	{
		return array(
			array('posting.php?mode=reply&f=2&t=1'),
			array('ucp.php?i=pm&mode=compose'),
		);
	}

	/**
	 * @dataProvider get_urls
	 */
	public function test_chunked_upload($url)
	{
		$chunk_size = ceil(filesize($this->path . 'valid.jpg') / self::CHUNKS);
		$handle = fopen($this->path . 'valid.jpg', 'rb');

		$crawler = self::$client->request('POST', $url . '&sid=' . $this->sid);

		$file_form_data = $this->get_hidden_fields($crawler, $url);

		for ($i = 0; $i < self::CHUNKS; $i++)
		{
			$chunk = fread($handle, $chunk_size);
			file_put_contents($this-> path . 'chunk', $chunk);

			$file = array(
				'tmp_name' => $this->path . 'chunk',
				'name' => 'blob',
				'type' => 'application/octet-stream',
				'size' => strlen($chunk),
				'error' => UPLOAD_ERR_OK,
			);

			self::$client->setServerParameter('HTTP_X_PHPBB_USING_PLUPLOAD', '1');

			$crawler = self::$client->request(
				'POST',
				$url . '&sid=' . $this->sid,
				array_merge(array(
					'chunk' => $i,
					'chunks' => self::CHUNKS,
					'name' => md5('valid') . '.jpg',
					'real_filename' => 'valid.jpg',
					'add_file' => $this->lang('ADD_FILE'),
				), $file_form_data),
				array('fileupload' => $file),
				array('X-PHPBB-USING-PLUPLOAD' => '1')
			);

			if ($i < self::CHUNKS - 1)
			{
				$this->assertContains('{"jsonrpc":"2.0","id":"id","result":null}', self::get_content());
			}
			else
			{
				$response = json_decode(self::get_content(), true);
				$this->assertEquals('valid.jpg', $response['data'][0]['real_filename']);
			}

			unlink($this->path . 'chunk');
		}

		fclose($handle);
	}

	/**
	 * @dataProvider get_urls
	 */
	public function test_normal_upload($url)
	{
		$file = array(
			'tmp_name' => $this->path . 'valid.jpg',
			'name' => 'valid.jpg',
			'type' => 'image/jpeg',
			'size' => filesize($this->path . 'valid.jpg'),
			'error' => UPLOAD_ERR_OK,
		);

		$file_form_data = $this->get_hidden_fields(null, $url);

		self::$client->setServerParameter('HTTP_X_PHPBB_USING_PLUPLOAD', '1');
		self::$client->request(
			'POST',
			$url . '&sid=' . $this->sid,
			array_merge(array(
				'chunk' => '0',
				'chunks' => '1',
				'name' => md5('valid') . '.jpg',
				'real_filename' => 'valid.jpg',
				'add_file' => $this->lang('ADD_FILE'),
			), $file_form_data),
			array('fileupload' => $file)
		);

		$response = json_decode(self::get_content(), true);
		$this->assertEquals('valid.jpg', $response['data'][0]['real_filename']);
	}
}