Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.39% covered (warning)
80.39%
41 / 51
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_files_upload_test
80.39% covered (warning)
80.39%
41 / 51
83.33% covered (warning)
83.33%
5 / 6
7.37
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
2
 test_reset_vars
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 test_set_disallowed_content
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 test_is_valid
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 data_internal_error
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 test_assign_internal_error
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
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
14class phpbb_files_upload_test extends phpbb_test_case
15{
16    /** @var \phpbb\filesystem\filesystem */
17    private $filesystem;
18
19    /** @var \Symfony\Component\DependencyInjection\ContainerInterface */
20    protected $container;
21
22    /** @var \phpbb\files\factory */
23    protected $factory;
24
25    /** @var \bantu\IniGetWrapper\IniGetWrapper */
26    protected $php_ini;
27
28    /** @var \phpbb\language\language */
29    protected $language;
30
31    /** @var \phpbb\request\request_interface */
32    protected $request;
33
34    protected function setUp(): void
35    {
36        // Global $config required by unique_id
37        global $config, $phpbb_root_path, $phpEx;
38
39        if (!is_array($config))
40        {
41            $config = array();
42        }
43
44        $config['rand_seed'] = '';
45        $config['rand_seed_last_update'] = time() + 600;
46
47        $this->request = $this->createMock('\phpbb\request\request');
48
49        $this->filesystem = new \phpbb\filesystem\filesystem();
50        $this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
51        $this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper;
52
53        $this->container = new phpbb_mock_container_builder();
54        $this->container->set('files.filespec', new \phpbb\files\filespec(
55            $this->filesystem,
56            $this->language,
57            $this->php_ini,
58            new \FastImageSize\FastImageSize(),
59            $phpbb_root_path,
60            new \phpbb\mimetype\guesser(array(
61                'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(),
62            ))));
63        $this->factory = new \phpbb\files\factory($this->container);
64    }
65
66    public function test_reset_vars()
67    {
68        $upload = new \phpbb\files\upload($this->factory, $this->language, $this->php_ini, $this->request);
69        $upload->set_max_filesize(500);
70        $this->assertEquals(500, $upload->max_filesize);
71        $upload->reset_vars();
72        $this->assertEquals(0, $upload->max_filesize);
73    }
74
75    public function test_set_disallowed_content()
76    {
77        $upload = new \phpbb\files\upload($this->factory, $this->language, $this->php_ini, $this->request);
78        $disallowed_content = new ReflectionProperty($upload, 'disallowed_content');
79
80        $upload->set_disallowed_content(array('foo'));
81        $this->assertEquals(array('foo'), $disallowed_content->getValue($upload));
82        $upload->set_disallowed_content(array('foo', 'bar', 'meh'));
83        $this->assertEquals(array('foo', 'bar', 'meh'), $disallowed_content->getValue($upload));
84        $upload->set_disallowed_content(false);
85        $this->assertEquals(array('foo', 'bar', 'meh'), $disallowed_content->getValue($upload));
86        $this->assertINstanceOf('\phpbb\files\upload', $upload->set_disallowed_content(array()));
87        $this->assertEquals(array(), $disallowed_content->getValue($upload));
88        $upload->reset_vars();
89        $this->assertEquals(array(), $disallowed_content->getValue($upload));
90    }
91
92    public function test_is_valid()
93    {
94        $upload = new \phpbb\files\upload($this->factory, $this->language, $this->php_ini, $this->request);
95        $this->assertFalse($upload->is_valid('foobar'));
96    }
97
98    public static function data_internal_error()
99    {
100        return array(
101            array(UPLOAD_ERR_INI_SIZE, 'PHP_SIZE_OVERRUN'),
102            array(UPLOAD_ERR_FORM_SIZE, 'WRONG_FILESIZE'),
103            array(UPLOAD_ERR_PARTIAL, 'PARTIAL_UPLOAD'),
104            array(UPLOAD_ERR_NO_FILE, 'NOT_UPLOADED'),
105            array(UPLOAD_ERR_NO_TMP_DIR, 'NO_TEMP_DIR'),
106            array(UPLOAD_ERR_CANT_WRITE, 'NO_TEMP_DIR'),
107            array(UPLOAD_ERR_EXTENSION, 'PHP_UPLOAD_STOPPED'),
108            array(9, false),
109        );
110    }
111
112    /**
113     * @dataProvider data_internal_error
114     */
115    public function test_assign_internal_error($error_code, $expected)
116    {
117        $upload = new \phpbb\files\upload($this->factory, $this->language, $this->php_ini, $this->request);
118        $this->assertSame($expected, $upload->assign_internal_error($error_code));
119    }
120}