Merge pull request #12521 from eileenmcnaughton/fix_im_export_test
[civicrm-core.git] / tests / phpunit / CRM / Utils / ZipTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
2fe49090 4| CiviCRM version 5 |
b6708aeb 5+--------------------------------------------------------------------+
8c9251b3 6| Copyright CiviCRM LLC (c) 2004-2018 |
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
TO
47 $this->_doFindBaseDirName('author-com.example.foo-random/',
48 array('author-com.example.foo-random'),
49 array('author-com.example.foo-random/README.txt' => 'hello')
50 );
51 }
52
00be9182 53 public function testFindBaseDirName_0() {
6a488035
TO
54 $this->_doFindBaseDirName('0/',
55 array('0'),
56 array()
57 );
58 }
b6708aeb 59
00be9182 60 public function testFindBaseDirName_plainfile() {
6a488035
TO
61 $this->_doFindBaseDirName(FALSE,
62 array(),
63 array('README.txt' => 'hello')
64 );
65 }
66
00be9182 67 public function testFindBaseDirName_twodir() {
6a488035
TO
68 $this->_doFindBaseDirName(FALSE,
69 array('dir-1', 'dir-2'),
70 array('dir-1/README.txt' => 'hello')
71 );
72 }
73
00be9182 74 public function testFindBaseDirName_dirfile() {
6a488035
TO
75 $this->_doFindBaseDirName(FALSE,
76 array('dir-1'),
77 array('dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra')
78 );
79 }
80
00be9182 81 public function testFindBaseDirName_dot() {
6a488035
TO
82 $this->_doFindBaseDirName(FALSE,
83 array('.'),
84 array('./README.txt' => 'hello')
85 );
86 }
87
00be9182 88 public function testFindBaseDirName_dots() {
6a488035
TO
89 $this->_doFindBaseDirName(FALSE,
90 array('..'),
91 array('../README.txt' => 'hello')
92 );
93 }
94
00be9182 95 public function testFindBaseDirName_weird() {
6a488035
TO
96 $this->_doFindBaseDirName(FALSE,
97 array('foo/../'),
98 array('foo/../README.txt' => 'hello')
99 );
100 }
101
00be9182 102 public function testGuessBaseDir_normal() {
6a488035
TO
103 $this->_doGuessBaseDir('author-com.example.foo-random',
104 array('author-com.example.foo-random'),
105 array('author-com.example.foo-random/README.txt' => 'hello'),
106 'com.example.foo'
107 );
108 }
109
00be9182 110 public function testGuessBaseDir_MACOSX() {
6a488035
TO
111 $this->_doGuessBaseDir('com.example.foo',
112 array('com.example.foo', '__MACOSX'),
113 array('author-com.example.foo-random/README.txt' => 'hello', '__MACOSX/foo' => 'bar'),
114 'com.example.foo'
115 );
116 }
117
00be9182 118 public function testGuessBaseDir_0() {
6a488035
TO
119 $this->_doGuessBaseDir('0',
120 array('0'),
121 array(),
122 'com.example.foo'
123 );
124 }
b6708aeb 125
00be9182 126 public function testGuessBaseDir_plainfile() {
6a488035
TO
127 $this->_doGuessBaseDir(FALSE,
128 array(),
129 array('README.txt' => 'hello'),
130 'com.example.foo'
131 );
132 }
133
fe482240 134 public function testGuessBaseDirTwoDir() {
6a488035
TO
135 $this->_doGuessBaseDir(FALSE,
136 array('dir-1', 'dir-2'),
137 array('dir-1/README.txt' => 'hello'),
138 'com.example.foo'
139 );
140 }
b6708aeb 141
fe482240 142 public function testGuessBaseDirWeird() {
6a488035
TO
143 $this->_doGuessBaseDir(FALSE,
144 array('foo/../'),
145 array('foo/../README.txt' => 'hello'),
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}