merge in 5.25
[civicrm-core.git] / tests / phpunit / CRM / Utils / ZipTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
7d61e75f
TO
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 +--------------------------------------------------------------------+
e70a7fc0 10 */
e9479dcf
EM
11
12/**
13 * Class CRM_Utils_ZipTest
acb109b7 14 * @group headless
e9479dcf 15 */
6a488035 16class CRM_Utils_ZipTest extends CiviUnitTestCase {
6a488035 17
00be9182 18 public function setUp() {
6a488035
TO
19 parent::setUp();
20 $this->file = FALSE;
21 }
b6708aeb 22
00be9182 23 public function tearDown() {
6a488035
TO
24 parent::tearDown();
25 if ($this->file) {
26 unlink($this->file);
27 }
28 }
29
00be9182 30 public function testFindBaseDirName_normal() {
6a488035 31 $this->_doFindBaseDirName('author-com.example.foo-random/',
9099cab3
CW
32 ['author-com.example.foo-random'],
33 ['author-com.example.foo-random/README.txt' => 'hello']
6a488035
TO
34 );
35 }
36
00be9182 37 public function testFindBaseDirName_0() {
6a488035 38 $this->_doFindBaseDirName('0/',
9099cab3
CW
39 ['0'],
40 []
6a488035
TO
41 );
42 }
b6708aeb 43
00be9182 44 public function testFindBaseDirName_plainfile() {
6a488035 45 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
46 [],
47 ['README.txt' => 'hello']
6a488035
TO
48 );
49 }
50
00be9182 51 public function testFindBaseDirName_twodir() {
6a488035 52 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
53 ['dir-1', 'dir-2'],
54 ['dir-1/README.txt' => 'hello']
6a488035
TO
55 );
56 }
57
00be9182 58 public function testFindBaseDirName_dirfile() {
6a488035 59 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
60 ['dir-1'],
61 ['dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra']
6a488035
TO
62 );
63 }
64
00be9182 65 public function testFindBaseDirName_dot() {
6a488035 66 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
67 ['.'],
68 ['./README.txt' => 'hello']
6a488035
TO
69 );
70 }
71
00be9182 72 public function testFindBaseDirName_dots() {
6a488035 73 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
74 ['..'],
75 ['../README.txt' => 'hello']
6a488035
TO
76 );
77 }
78
00be9182 79 public function testFindBaseDirName_weird() {
6a488035 80 $this->_doFindBaseDirName(FALSE,
9099cab3
CW
81 ['foo/../'],
82 ['foo/../README.txt' => 'hello']
6a488035
TO
83 );
84 }
85
00be9182 86 public function testGuessBaseDir_normal() {
6a488035 87 $this->_doGuessBaseDir('author-com.example.foo-random',
9099cab3
CW
88 ['author-com.example.foo-random'],
89 ['author-com.example.foo-random/README.txt' => 'hello'],
6a488035
TO
90 'com.example.foo'
91 );
92 }
93
00be9182 94 public function testGuessBaseDir_MACOSX() {
6a488035 95 $this->_doGuessBaseDir('com.example.foo',
9099cab3
CW
96 ['com.example.foo', '__MACOSX'],
97 ['author-com.example.foo-random/README.txt' => 'hello', '__MACOSX/foo' => 'bar'],
6a488035
TO
98 'com.example.foo'
99 );
100 }
101
00be9182 102 public function testGuessBaseDir_0() {
6a488035 103 $this->_doGuessBaseDir('0',
9099cab3
CW
104 ['0'],
105 [],
6a488035
TO
106 'com.example.foo'
107 );
108 }
b6708aeb 109
00be9182 110 public function testGuessBaseDir_plainfile() {
6a488035 111 $this->_doGuessBaseDir(FALSE,
9099cab3
CW
112 [],
113 ['README.txt' => 'hello'],
6a488035
TO
114 'com.example.foo'
115 );
116 }
117
fe482240 118 public function testGuessBaseDirTwoDir() {
6a488035 119 $this->_doGuessBaseDir(FALSE,
9099cab3
CW
120 ['dir-1', 'dir-2'],
121 ['dir-1/README.txt' => 'hello'],
6a488035
TO
122 'com.example.foo'
123 );
124 }
b6708aeb 125
fe482240 126 public function testGuessBaseDirWeird() {
6a488035 127 $this->_doGuessBaseDir(FALSE,
9099cab3
CW
128 ['foo/../'],
129 ['foo/../README.txt' => 'hello'],
6a488035
TO
130 'com.example.foo'
131 );
132 }
b6708aeb 133
4cbe18b8 134 /**
100fef9d 135 * @param string $expectedBaseDirName
4cbe18b8
EM
136 * @param $dirs
137 * @param $files
138 */
00be9182 139 public function _doFindBaseDirName($expectedBaseDirName, $dirs, $files) {
6a488035
TO
140 $this->file = tempnam(sys_get_temp_dir(), 'testzip-');
141 $this->assertTrue(CRM_Utils_Zip::createTestZip($this->file, $dirs, $files));
b6708aeb 142
6a488035
TO
143 $zip = new ZipArchive();
144 $this->assertTrue($zip->open($this->file));
145 $this->assertEquals($expectedBaseDirName, CRM_Utils_Zip::findBaseDirName($zip));
146 }
b6708aeb 147
4cbe18b8
EM
148 /**
149 * @param $expectedResult
150 * @param $dirs
151 * @param $files
152 * @param $expectedKey
153 */
00be9182 154 public function _doGuessBaseDir($expectedResult, $dirs, $files, $expectedKey) {
6a488035
TO
155 $this->file = tempnam(sys_get_temp_dir(), 'testzip-');
156 $this->assertTrue(CRM_Utils_Zip::createTestZip($this->file, $dirs, $files));
b6708aeb 157
6a488035
TO
158 $zip = new ZipArchive();
159 $this->assertTrue($zip->open($this->file));
160 $this->assertEquals($expectedResult, CRM_Utils_Zip::guessBaseDir($zip, $expectedKey));
161 }
96025800 162
6a488035 163}