Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_functional_plupload_test
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 6
182
0.00% covered (danger)
0.00%
0 / 1
 set_extension_group_permission
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 setUp
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 tearDown
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
 get_urls
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 test_chunked_upload
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
12
 test_normal_upload
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14/**
15 * @group functional
16 */
17class phpbb_functional_plupload_test extends phpbb_functional_test_case
18{
19    const CHUNKS = 4;
20    private $path;
21
22    protected function set_extension_group_permission($val)
23    {
24        $query = "
25            UPDATE phpbb_extension_groups
26            SET allow_in_pm = '$val'
27            WHERE group_name = 'IMAGES'
28        ";
29        $this->db->sql_query($query);
30    }
31
32    protected function setUp(): void
33    {
34        parent::setUp();
35        $this->purge_cache();
36        $this->set_extension_group_permission(1);
37        $this->path = __DIR__ . '/fixtures/files/';
38        $this->add_lang('posting');
39        $this->login();
40    }
41
42    protected function tearDown(): void
43    {
44        $this->set_extension_group_permission(0);
45        $iterator = new DirectoryIterator(__DIR__ . '/../../phpBB/files/');
46        foreach ($iterator as $fileinfo)
47        {
48            if (
49                $fileinfo->isDot()
50                || $fileinfo->isDir()
51                || $fileinfo->getFilename() === 'index.htm'
52                || $fileinfo->getFilename() === '.htaccess'
53            )
54            {
55                continue;
56            }
57
58            unlink($fileinfo->getPathname());
59        }
60    }
61
62    public static function get_urls()
63    {
64        return array(
65            array('posting.php?mode=reply&t=1'),
66            array('ucp.php?i=pm&mode=compose'),
67        );
68    }
69
70    /**
71     * @dataProvider get_urls
72     */
73    public function test_chunked_upload($url)
74    {
75        $chunk_size = ceil(filesize($this->path . 'valid.jpg') / self::CHUNKS);
76        $handle = fopen($this->path . 'valid.jpg', 'rb');
77
78        $crawler = self::$client->request('POST', $url . '&sid=' . $this->sid);
79
80        $file_form_data = $this->get_hidden_fields($crawler, $url);
81
82        for ($i = 0; $i < self::CHUNKS; $i++)
83        {
84            $chunk = fread($handle, $chunk_size);
85            file_put_contents($this-> path . 'chunk', $chunk);
86
87            $file = array(
88                'tmp_name' => $this->path . 'chunk',
89                'name' => 'blob',
90                'type' => 'application/octet-stream',
91                'size' => strlen($chunk),
92                'error' => UPLOAD_ERR_OK,
93            );
94
95            self::$client->setServerParameter('HTTP_X_PHPBB_USING_PLUPLOAD', '1');
96
97            $crawler = self::$client->request(
98                'POST',
99                $url . '&sid=' . $this->sid,
100                array_merge(array(
101                    'chunk' => $i,
102                    'chunks' => self::CHUNKS,
103                    'name' => md5('valid') . '.jpg',
104                    'real_filename' => 'valid.jpg',
105                    'add_file' => $this->lang('ADD_FILE'),
106                ), $file_form_data),
107                array('fileupload' => $file),
108                array('X-PHPBB-USING-PLUPLOAD' => '1')
109            );
110
111            if ($i < self::CHUNKS - 1)
112            {
113                $this->assertStringContainsString('{"jsonrpc":"2.0","id":"id","result":null}', self::get_content());
114            }
115            else
116            {
117                $response = json_decode(self::get_content(), true);
118                $this->assertEquals('valid.jpg', $response['data'][0]['real_filename']);
119            }
120
121            unlink($this->path . 'chunk');
122        }
123
124        fclose($handle);
125    }
126
127    /**
128     * @dataProvider get_urls
129     */
130    public function test_normal_upload($url)
131    {
132        $file = array(
133            'tmp_name' => $this->path . 'valid.jpg',
134            'name' => 'valid.jpg',
135            'type' => 'image/jpeg',
136            'size' => filesize($this->path . 'valid.jpg'),
137            'error' => UPLOAD_ERR_OK,
138        );
139
140        $file_form_data = $this->get_hidden_fields(null, $url);
141
142        self::$client->setServerParameter('HTTP_X_PHPBB_USING_PLUPLOAD', '1');
143        self::$client->request(
144            'POST',
145            $url . '&sid=' . $this->sid,
146            array_merge(array(
147                'chunk' => '0',
148                'chunks' => '1',
149                'name' => md5('valid') . '.jpg',
150                'real_filename' => 'valid.jpg',
151                'add_file' => $this->lang('ADD_FILE'),
152            ), $file_form_data),
153            array('fileupload' => $file)
154        );
155
156        $response = json_decode(self::get_content(), true);
157        $this->assertEquals('valid.jpg', $response['data'][0]['real_filename']);
158    }
159}