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