Merge pull request #14925 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Utils / ZipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Utils_ZipTest
30 * @group headless
31 */
32 class CRM_Utils_ZipTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 $this->file = FALSE;
37 }
38
39 public function tearDown() {
40 parent::tearDown();
41 if ($this->file) {
42 unlink($this->file);
43 }
44 }
45
46 public function testFindBaseDirName_normal() {
47 $this->_doFindBaseDirName('author-com.example.foo-random/',
48 ['author-com.example.foo-random'],
49 ['author-com.example.foo-random/README.txt' => 'hello']
50 );
51 }
52
53 public function testFindBaseDirName_0() {
54 $this->_doFindBaseDirName('0/',
55 ['0'],
56 []
57 );
58 }
59
60 public function testFindBaseDirName_plainfile() {
61 $this->_doFindBaseDirName(FALSE,
62 [],
63 ['README.txt' => 'hello']
64 );
65 }
66
67 public function testFindBaseDirName_twodir() {
68 $this->_doFindBaseDirName(FALSE,
69 ['dir-1', 'dir-2'],
70 ['dir-1/README.txt' => 'hello']
71 );
72 }
73
74 public function testFindBaseDirName_dirfile() {
75 $this->_doFindBaseDirName(FALSE,
76 ['dir-1'],
77 ['dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra']
78 );
79 }
80
81 public function testFindBaseDirName_dot() {
82 $this->_doFindBaseDirName(FALSE,
83 ['.'],
84 ['./README.txt' => 'hello']
85 );
86 }
87
88 public function testFindBaseDirName_dots() {
89 $this->_doFindBaseDirName(FALSE,
90 ['..'],
91 ['../README.txt' => 'hello']
92 );
93 }
94
95 public function testFindBaseDirName_weird() {
96 $this->_doFindBaseDirName(FALSE,
97 ['foo/../'],
98 ['foo/../README.txt' => 'hello']
99 );
100 }
101
102 public function testGuessBaseDir_normal() {
103 $this->_doGuessBaseDir('author-com.example.foo-random',
104 ['author-com.example.foo-random'],
105 ['author-com.example.foo-random/README.txt' => 'hello'],
106 'com.example.foo'
107 );
108 }
109
110 public function testGuessBaseDir_MACOSX() {
111 $this->_doGuessBaseDir('com.example.foo',
112 ['com.example.foo', '__MACOSX'],
113 ['author-com.example.foo-random/README.txt' => 'hello', '__MACOSX/foo' => 'bar'],
114 'com.example.foo'
115 );
116 }
117
118 public function testGuessBaseDir_0() {
119 $this->_doGuessBaseDir('0',
120 ['0'],
121 [],
122 'com.example.foo'
123 );
124 }
125
126 public function testGuessBaseDir_plainfile() {
127 $this->_doGuessBaseDir(FALSE,
128 [],
129 ['README.txt' => 'hello'],
130 'com.example.foo'
131 );
132 }
133
134 public function testGuessBaseDirTwoDir() {
135 $this->_doGuessBaseDir(FALSE,
136 ['dir-1', 'dir-2'],
137 ['dir-1/README.txt' => 'hello'],
138 'com.example.foo'
139 );
140 }
141
142 public function testGuessBaseDirWeird() {
143 $this->_doGuessBaseDir(FALSE,
144 ['foo/../'],
145 ['foo/../README.txt' => 'hello'],
146 'com.example.foo'
147 );
148 }
149
150 /**
151 * @param string $expectedBaseDirName
152 * @param $dirs
153 * @param $files
154 */
155 public function _doFindBaseDirName($expectedBaseDirName, $dirs, $files) {
156 $this->file = tempnam(sys_get_temp_dir(), 'testzip-');
157 $this->assertTrue(CRM_Utils_Zip::createTestZip($this->file, $dirs, $files));
158
159 $zip = new ZipArchive();
160 $this->assertTrue($zip->open($this->file));
161 $this->assertEquals($expectedBaseDirName, CRM_Utils_Zip::findBaseDirName($zip));
162 }
163
164 /**
165 * @param $expectedResult
166 * @param $dirs
167 * @param $files
168 * @param $expectedKey
169 */
170 public function _doGuessBaseDir($expectedResult, $dirs, $files, $expectedKey) {
171 $this->file = tempnam(sys_get_temp_dir(), 'testzip-');
172 $this->assertTrue(CRM_Utils_Zip::createTestZip($this->file, $dirs, $files));
173
174 $zip = new ZipArchive();
175 $this->assertTrue($zip->open($this->file));
176 $this->assertEquals($expectedResult, CRM_Utils_Zip::guessBaseDir($zip, $expectedKey));
177 }
178
179 }