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