(NFC) (dev/core#878) Simplify copyright header (Civi/*)
[civicrm-core.git] / tests / phpunit / CRM / Utils / ZipTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
2fe49090 4| CiviCRM version 5 |
b6708aeb 5+--------------------------------------------------------------------+
f299f7db 6| Copyright CiviCRM LLC (c) 2004-2020 |
b6708aeb 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+--------------------------------------------------------------------+
e70a7fc0 26 */
e9479dcf
EM
27
28/**
29 * Class CRM_Utils_ZipTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035 32class CRM_Utils_ZipTest extends CiviUnitTestCase {
6a488035 33
00be9182 34 public function setUp() {
6a488035
TO
35 parent::setUp();
36 $this->file = FALSE;
37 }
b6708aeb 38
00be9182 39 public function tearDown() {
6a488035
TO
40 parent::tearDown();
41 if ($this->file) {
42 unlink($this->file);
43 }
44 }
45
00be9182 46 public function testFindBaseDirName_normal() {
6a488035 47 $this->_doFindBaseDirName('author-com.example.foo-random/',
9099cab3
CW
48 ['author-com.example.foo-random'],
49 ['author-com.example.foo-random/README.txt' => 'hello']
6a488035
TO
50 );
51 }
52
00be9182 53 public function testFindBaseDirName_0() {
6a488035 54 $this->_doFindBaseDirName('0/',
9099cab3
CW
55 ['0'],
56 []
6a488035
TO
57 );
58 }
b6708aeb 59
00be9182 60 public function testFindBaseDirName_plainfile() {
6a488035 61 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
62 [],
63 ['README.txt' => 'hello']
6a488035
TO
64 );
65 }
66
00be9182 67 public function testFindBaseDirName_twodir() {
6a488035 68 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
69 ['dir-1', 'dir-2'],
70 ['dir-1/README.txt' => 'hello']
6a488035
TO
71 );
72 }
73
00be9182 74 public function testFindBaseDirName_dirfile() {
6a488035 75 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
76 ['dir-1'],
77 ['dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra']
6a488035
TO
78 );
79 }
80
00be9182 81 public function testFindBaseDirName_dot() {
6a488035 82 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
83 ['.'],
84 ['./README.txt' => 'hello']
6a488035
TO
85 );
86 }
87
00be9182 88 public function testFindBaseDirName_dots() {
6a488035 89 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
90 ['..'],
91 ['../README.txt' => 'hello']
6a488035
TO
92 );
93 }
94
00be9182 95 public function testFindBaseDirName_weird() {
6a488035 96 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
97 ['foo/../'],
98 ['foo/../README.txt' => 'hello']
6a488035
TO
99 );
100 }
101
00be9182 102 public function testGuessBaseDir_normal() {
6a488035 103 $this->_doGuessBaseDir('author-com.example.foo-random',
9099cab3
CW
104 ['author-com.example.foo-random'],
105 ['author-com.example.foo-random/README.txt' => 'hello'],
6a488035
TO
106 'com.example.foo'
107 );
108 }
109
00be9182 110 public function testGuessBaseDir_MACOSX() {
6a488035 111 $this->_doGuessBaseDir('com.example.foo',
9099cab3
CW
112 ['com.example.foo', '__MACOSX'],
113 ['author-com.example.foo-random/README.txt' => 'hello', '__MACOSX/foo' => 'bar'],
6a488035
TO
114 'com.example.foo'
115 );
116 }
117
00be9182 118 public function testGuessBaseDir_0() {
6a488035 119 $this->_doGuessBaseDir('0',
9099cab3
CW
120 ['0'],
121 [],
6a488035
TO
122 'com.example.foo'
123 );
124 }
b6708aeb 125
00be9182 126 public function testGuessBaseDir_plainfile() {
6a488035 127 $this->_doGuessBaseDir(FALSE,
9099cab3
CW
128 [],
129 ['README.txt' => 'hello'],
6a488035
TO
130 'com.example.foo'
131 );
132 }
133
fe482240 134 public function testGuessBaseDirTwoDir() {
6a488035 135 $this->_doGuessBaseDir(FALSE,
9099cab3
CW
136 ['dir-1', 'dir-2'],
137 ['dir-1/README.txt' => 'hello'],
6a488035
TO
138 'com.example.foo'
139 );
140 }
b6708aeb 141
fe482240 142 public function testGuessBaseDirWeird() {
6a488035 143 $this->_doGuessBaseDir(FALSE,
9099cab3
CW
144 ['foo/../'],
145 ['foo/../README.txt' => 'hello'],
6a488035
TO
146 'com.example.foo'
147 );
148 }
b6708aeb 149
4cbe18b8 150 /**
100fef9d 151 * @param string $expectedBaseDirName
4cbe18b8
EM
152 * @param $dirs
153 * @param $files
154 */
00be9182 155 public function _doFindBaseDirName($expectedBaseDirName, $dirs, $files) {
6a488035
TO
156 $this->file = tempnam(sys_get_temp_dir(), 'testzip-');
157 $this->assertTrue(CRM_Utils_Zip::createTestZip($this->file, $dirs, $files));
b6708aeb 158
6a488035
TO
159 $zip = new ZipArchive();
160 $this->assertTrue($zip->open($this->file));
161 $this->assertEquals($expectedBaseDirName, CRM_Utils_Zip::findBaseDirName($zip));
162 }
b6708aeb 163
4cbe18b8
EM
164 /**
165 * @param $expectedResult
166 * @param $dirs
167 * @param $files
168 * @param $expectedKey
169 */
00be9182 170 public function _doGuessBaseDir($expectedResult, $dirs, $files, $expectedKey) {
6a488035
TO
171 $this->file = tempnam(sys_get_temp_dir(), 'testzip-');
172 $this->assertTrue(CRM_Utils_Zip::createTestZip($this->file, $dirs, $files));
b6708aeb 173
6a488035
TO
174 $zip = new ZipArchive();
175 $this->assertTrue($zip->open($this->file));
176 $this->assertEquals($expectedResult, CRM_Utils_Zip::guessBaseDir($zip, $expectedKey));
177 }
96025800 178
6a488035 179}