2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Utils_ZipTest
extends CiviUnitTestCase
{
7 'description' => 'Test Zip Functions',
8 'group' => 'CiviCRM BAO Tests',
24 function testFindBaseDirName_normal() {
25 $this->_doFindBaseDirName('author-com.example.foo-random/',
26 array('author-com.example.foo-random'),
27 array('author-com.example.foo-random/README.txt' => 'hello')
31 function testFindBaseDirName_0() {
32 $this->_doFindBaseDirName('0/',
38 function testFindBaseDirName_plainfile() {
39 $this->_doFindBaseDirName(FALSE,
41 array('README.txt' => 'hello')
45 function testFindBaseDirName_twodir() {
46 $this->_doFindBaseDirName(FALSE,
47 array('dir-1', 'dir-2'),
48 array('dir-1/README.txt' => 'hello')
52 function testFindBaseDirName_dirfile() {
53 $this->_doFindBaseDirName(FALSE,
55 array('dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra')
59 function testFindBaseDirName_dot() {
60 $this->_doFindBaseDirName(FALSE,
62 array('./README.txt' => 'hello')
66 function testFindBaseDirName_dots() {
67 $this->_doFindBaseDirName(FALSE,
69 array('../README.txt' => 'hello')
73 function testFindBaseDirName_weird() {
74 $this->_doFindBaseDirName(FALSE,
76 array('foo/../README.txt' => 'hello')
80 function testGuessBaseDir_normal() {
81 $this->_doGuessBaseDir('author-com.example.foo-random',
82 array('author-com.example.foo-random'),
83 array('author-com.example.foo-random/README.txt' => 'hello'),
88 function testGuessBaseDir_MACOSX() {
89 $this->_doGuessBaseDir('com.example.foo',
90 array('com.example.foo', '__MACOSX'),
91 array('author-com.example.foo-random/README.txt' => 'hello', '__MACOSX/foo' => 'bar'),
96 function testGuessBaseDir_0() {
97 $this->_doGuessBaseDir('0',
104 function testGuessBaseDir_plainfile() {
105 $this->_doGuessBaseDir(FALSE,
107 array('README.txt' => 'hello'),
112 function testGuessBaseDir_twodir() {
113 $this->_doGuessBaseDir(FALSE,
114 array('dir-1', 'dir-2'),
115 array('dir-1/README.txt' => 'hello'),
120 function testGuessBaseDir_weird() {
121 $this->_doGuessBaseDir(FALSE,
123 array('foo/../README.txt' => 'hello'),
128 function _doFindBaseDirName($expectedBaseDirName, $dirs, $files) {
129 $this->file
= tempnam(sys_get_temp_dir(), 'testzip-');
130 $this->assertTrue(CRM_Utils_Zip
::createTestZip($this->file
, $dirs, $files));
132 $zip = new ZipArchive();
133 $this->assertTrue($zip->open($this->file
));
134 $this->assertEquals($expectedBaseDirName, CRM_Utils_Zip
::findBaseDirName($zip));
137 function _doGuessBaseDir($expectedResult, $dirs, $files, $expectedKey) {
138 $this->file
= tempnam(sys_get_temp_dir(), 'testzip-');
139 $this->assertTrue(CRM_Utils_Zip
::createTestZip($this->file
, $dirs, $files));
141 $zip = new ZipArchive();
142 $this->assertTrue($zip->open($this->file
));
143 $this->assertEquals($expectedResult, CRM_Utils_Zip
::guessBaseDir($zip, $expectedKey));