Merge pull request #15794 from KarinG/master
[civicrm-core.git] / tests / phpunit / CRM / Utils / ZipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Utils_ZipTest
14 * @group headless
15 */
16 class CRM_Utils_ZipTest extends CiviUnitTestCase {
17
18 public function setUp() {
19 parent::setUp();
20 $this->file = FALSE;
21 }
22
23 public function tearDown() {
24 parent::tearDown();
25 if ($this->file) {
26 unlink($this->file);
27 }
28 }
29
30 public function testFindBaseDirName_normal() {
31 $this->_doFindBaseDirName('author-com.example.foo-random/',
32 ['author-com.example.foo-random'],
33 ['author-com.example.foo-random/README.txt' => 'hello']
34 );
35 }
36
37 public function testFindBaseDirName_0() {
38 $this->_doFindBaseDirName('0/',
39 ['0'],
40 []
41 );
42 }
43
44 public function testFindBaseDirName_plainfile() {
45 $this->_doFindBaseDirName(FALSE,
46 [],
47 ['README.txt' => 'hello']
48 );
49 }
50
51 public function testFindBaseDirName_twodir() {
52 $this->_doFindBaseDirName(FALSE,
53 ['dir-1', 'dir-2'],
54 ['dir-1/README.txt' => 'hello']
55 );
56 }
57
58 public function testFindBaseDirName_dirfile() {
59 $this->_doFindBaseDirName(FALSE,
60 ['dir-1'],
61 ['dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra']
62 );
63 }
64
65 public function testFindBaseDirName_dot() {
66 $this->_doFindBaseDirName(FALSE,
67 ['.'],
68 ['./README.txt' => 'hello']
69 );
70 }
71
72 public function testFindBaseDirName_dots() {
73 $this->_doFindBaseDirName(FALSE,
74 ['..'],
75 ['../README.txt' => 'hello']
76 );
77 }
78
79 public function testFindBaseDirName_weird() {
80 $this->_doFindBaseDirName(FALSE,
81 ['foo/../'],
82 ['foo/../README.txt' => 'hello']
83 );
84 }
85
86 public function testGuessBaseDir_normal() {
87 $this->_doGuessBaseDir('author-com.example.foo-random',
88 ['author-com.example.foo-random'],
89 ['author-com.example.foo-random/README.txt' => 'hello'],
90 'com.example.foo'
91 );
92 }
93
94 public function testGuessBaseDir_MACOSX() {
95 $this->_doGuessBaseDir('com.example.foo',
96 ['com.example.foo', '__MACOSX'],
97 ['author-com.example.foo-random/README.txt' => 'hello', '__MACOSX/foo' => 'bar'],
98 'com.example.foo'
99 );
100 }
101
102 public function testGuessBaseDir_0() {
103 $this->_doGuessBaseDir('0',
104 ['0'],
105 [],
106 'com.example.foo'
107 );
108 }
109
110 public function testGuessBaseDir_plainfile() {
111 $this->_doGuessBaseDir(FALSE,
112 [],
113 ['README.txt' => 'hello'],
114 'com.example.foo'
115 );
116 }
117
118 public function testGuessBaseDirTwoDir() {
119 $this->_doGuessBaseDir(FALSE,
120 ['dir-1', 'dir-2'],
121 ['dir-1/README.txt' => 'hello'],
122 'com.example.foo'
123 );
124 }
125
126 public function testGuessBaseDirWeird() {
127 $this->_doGuessBaseDir(FALSE,
128 ['foo/../'],
129 ['foo/../README.txt' => 'hello'],
130 'com.example.foo'
131 );
132 }
133
134 /**
135 * @param string $expectedBaseDirName
136 * @param $dirs
137 * @param $files
138 */
139 public function _doFindBaseDirName($expectedBaseDirName, $dirs, $files) {
140 $this->file = tempnam(sys_get_temp_dir(), 'testzip-');
141 $this->assertTrue(CRM_Utils_Zip::createTestZip($this->file, $dirs, $files));
142
143 $zip = new ZipArchive();
144 $this->assertTrue($zip->open($this->file));
145 $this->assertEquals($expectedBaseDirName, CRM_Utils_Zip::findBaseDirName($zip));
146 }
147
148 /**
149 * @param $expectedResult
150 * @param $dirs
151 * @param $files
152 * @param $expectedKey
153 */
154 public function _doGuessBaseDir($expectedResult, $dirs, $files, $expectedKey) {
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($expectedResult, CRM_Utils_Zip::guessBaseDir($zip, $expectedKey));
161 }
162
163 }