3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Class CRM_Utils_ZipTest
16 class CRM_Utils_ZipTest
extends CiviUnitTestCase
{
18 public function setUp() {
23 public function tearDown() {
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']
37 public function testFindBaseDirName_0() {
38 $this->_doFindBaseDirName('0/',
44 public function testFindBaseDirName_plainfile() {
45 $this->_doFindBaseDirName(FALSE,
47 ['README.txt' => 'hello']
51 public function testFindBaseDirName_twodir() {
52 $this->_doFindBaseDirName(FALSE,
54 ['dir-1/README.txt' => 'hello']
58 public function testFindBaseDirName_dirfile() {
59 $this->_doFindBaseDirName(FALSE,
61 ['dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra']
65 public function testFindBaseDirName_dot() {
66 $this->_doFindBaseDirName(FALSE,
68 ['./README.txt' => 'hello']
72 public function testFindBaseDirName_dots() {
73 $this->_doFindBaseDirName(FALSE,
75 ['../README.txt' => 'hello']
79 public function testFindBaseDirName_weird() {
80 $this->_doFindBaseDirName(FALSE,
82 ['foo/../README.txt' => 'hello']
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'],
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'],
102 public function testGuessBaseDir_0() {
103 $this->_doGuessBaseDir('0',
110 public function testGuessBaseDir_plainfile() {
111 $this->_doGuessBaseDir(FALSE,
113 ['README.txt' => 'hello'],
118 public function testGuessBaseDirTwoDir() {
119 $this->_doGuessBaseDir(FALSE,
121 ['dir-1/README.txt' => 'hello'],
126 public function testGuessBaseDirWeird() {
127 $this->_doGuessBaseDir(FALSE,
129 ['foo/../README.txt' => 'hello'],
135 * @param string $expectedBaseDirName
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));
143 $zip = new ZipArchive();
144 $this->assertTrue($zip->open($this->file
));
145 $this->assertEquals($expectedBaseDirName, CRM_Utils_Zip
::findBaseDirName($zip));
149 * @param $expectedResult
152 * @param $expectedKey
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));
158 $zip = new ZipArchive();
159 $this->assertTrue($zip->open($this->file
));
160 $this->assertEquals($expectedResult, CRM_Utils_Zip
::guessBaseDir($zip, $expectedKey));