Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.04% covered (success)
91.04%
61 / 67
81.82% covered (warning)
81.82%
9 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_compress_test
91.04% covered (success)
91.04%
61 / 67
81.82% covered (warning)
81.82%
9 / 11
18.23
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 check_extensions
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 tearDown
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 clear_dir
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 archive_files
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 valid_extraction
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 tar_archive_list
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 test_extract_tar
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 test_extract_zip
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 test_compress_tar
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 test_compress_zip
100.00% covered (success)
100.00%
9 / 9
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
14require_once __DIR__ . '/../../phpBB/includes/functions_admin.php';
15require_once __DIR__ . '/../../phpBB/includes/functions_compress.php';
16
17class phpbb_compress_test extends phpbb_test_case
18{
19    const EXTRACT_DIR = '/extract/';
20    const ARCHIVE_DIR = '/archive/';
21
22    private $path;
23
24    protected $filelist = array(
25        '1.txt',
26        'dir/2.txt',
27        'dir/3.txt',
28        'dir/subdir/4.txt',
29    );
30
31    protected $conflicts = array(
32        '1_1.txt',
33        '1_2.txt',
34        'dir/2_1.txt',
35    );
36
37    protected function setUp(): void
38    {
39        // Required for compress::add_file
40        global $phpbb_root_path;
41        $phpbb_root_path = '';
42
43        $this->path = __DIR__ . '/fixtures/';
44    }
45
46    protected function check_extensions($extensions)
47    {
48        foreach ($extensions as $extension)
49        {
50            if (!@extension_loaded($extension))
51            {
52                $this->markTestSkipped("$extension extension is not loaded");
53            }
54        }
55    }
56
57    protected function tearDown(): void
58    {
59        foreach (array(__DIR__ . self::EXTRACT_DIR, __DIR__ . self::ARCHIVE_DIR) as $dir)
60        {
61            $this->clear_dir($dir);
62        }
63    }
64
65    protected function clear_dir($dir)
66    {
67        $iterator = new DirectoryIterator($dir);
68        foreach ($iterator as $fileinfo)
69        {
70            $name = $fileinfo->getFilename();
71            $path = $fileinfo->getPathname();
72
73            if ($name[0] !== '.')
74            {
75                if ($fileinfo->isDir())
76                {
77                    $this->clear_dir($path);
78                    rmdir($path);
79                }
80                else
81                {
82                    unlink($path);
83                }
84            }
85        }
86    }
87
88    protected function archive_files($compress)
89    {
90        $compress->add_file($this->path . '1.txt', $this->path);
91        $compress->add_file(
92            'tests/compress/fixtures/dir/',
93            'tests/compress/fixtures/',
94            '',
95            // The comma here is not an error, this is a comma-separated list
96            'subdir/4.txt,3.txt'
97        );
98        $compress->add_custom_file($this->path . 'dir/3.txt', 'dir/3.txt');
99        $compress->add_data(file_get_contents($this->path . 'dir/subdir/4.txt'), 'dir/subdir/4.txt');
100
101        // Add multiples of the same file to check conflicts are handled
102        $compress->add_file($this->path . '1.txt', $this->path);
103        $compress->add_file($this->path . '1.txt', $this->path);
104        $compress->add_file($this->path . 'dir/2.txt', $this->path);
105    }
106
107    protected function valid_extraction($extra = array())
108    {
109        $filelist = array_merge($this->filelist, $extra);
110
111        foreach ($filelist as $filename)
112        {
113            $path = __DIR__ . self::EXTRACT_DIR . $filename;
114            $this->assertTrue(file_exists($path));
115
116            // Check the file's contents is correct
117            $contents = explode('_', basename($filename, '.txt'));
118            $contents = $contents[0];
119            $this->assertEquals($contents . "\n", file_get_contents($path));
120        }
121    }
122
123    public static function tar_archive_list()
124    {
125        return array(
126            array('archive.tar', '.tar', array()),
127            array('archive.tar.gz', '.tar.gz', array('zlib')),
128            array('archive.tar.bz2', '.tar.bz2', array('bz2')),
129        );
130    }
131
132    /**
133     * @dataProvider tar_archive_list
134     */
135    public function test_extract_tar($filename, $type, $extensions)
136    {
137        $this->check_extensions($extensions);
138        $compress = new compress_tar('r', $this->path . $filename);
139        $compress->extract('tests/compress/' . self::EXTRACT_DIR);
140        $this->valid_extraction();
141    }
142
143    public function test_extract_zip()
144    {
145        $compress = new compress_zip('r', $this->path . 'archive.zip');
146        $compress->extract('tests/compress/' . self::EXTRACT_DIR);
147        $this->valid_extraction();
148    }
149
150    /**
151     * @depends test_extract_tar
152     * @dataProvider tar_archive_list
153     */
154    public function test_compress_tar($filename, $type, $extensions)
155    {
156        $this->check_extensions($extensions);
157
158        $tar = __DIR__ . self::ARCHIVE_DIR . $filename;
159        $compress = new compress_tar('w', $tar);
160        $this->archive_files($compress);
161        $compress->close();
162        $this->assertTrue(file_exists($tar));
163
164        $compress->mode = 'r';
165        $compress->open();
166        $compress->extract('tests/compress/' . self::EXTRACT_DIR);
167        $this->valid_extraction($this->conflicts);
168    }
169
170    /**
171     * @depends test_extract_zip
172     */
173    public function test_compress_zip()
174    {
175        $this->check_extensions(array('zlib'));
176
177        $zip =  __DIR__ . self::ARCHIVE_DIR . 'archive.zip';
178        $compress = new compress_zip('w', $zip);
179        $this->archive_files($compress);
180        $compress->close();
181        $this->assertTrue(file_exists($zip));
182
183        $compress = new compress_zip('r', $zip);
184        $compress->extract('tests/compress/' . self::EXTRACT_DIR);
185        $this->valid_extraction($this->conflicts);
186    }
187}