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