CRM-13460 - Add is_numeric checks and test coverage
[civicrm-core.git] / tests / phpunit / CRM / Utils / ZipTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Utils_ZipTest extends CiviUnitTestCase {
4 function get_info() {
5 return array(
6 'name' => 'Zip Test',
7 'description' => 'Test Zip Functions',
8 'group' => 'CiviCRM BAO Tests',
9 );
10 }
11
12 function setUp() {
13 parent::setUp();
14 $this->file = FALSE;
15 }
16
17 function tearDown() {
18 parent::tearDown();
19 if ($this->file) {
20 unlink($this->file);
21 }
22 }
23
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')
28 );
29 }
30
31 function testFindBaseDirName_0() {
32 $this->_doFindBaseDirName('0/',
33 array('0'),
34 array()
35 );
36 }
37
38 function testFindBaseDirName_plainfile() {
39 $this->_doFindBaseDirName(FALSE,
40 array(),
41 array('README.txt' => 'hello')
42 );
43 }
44
45 function testFindBaseDirName_twodir() {
46 $this->_doFindBaseDirName(FALSE,
47 array('dir-1', 'dir-2'),
48 array('dir-1/README.txt' => 'hello')
49 );
50 }
51
52 function testFindBaseDirName_dirfile() {
53 $this->_doFindBaseDirName(FALSE,
54 array('dir-1'),
55 array('dir-1/README.txt' => 'hello', 'MANIFEST.MF' => 'extra')
56 );
57 }
58
59 function testFindBaseDirName_dot() {
60 $this->_doFindBaseDirName(FALSE,
61 array('.'),
62 array('./README.txt' => 'hello')
63 );
64 }
65
66 function testFindBaseDirName_dots() {
67 $this->_doFindBaseDirName(FALSE,
68 array('..'),
69 array('../README.txt' => 'hello')
70 );
71 }
72
73 function testFindBaseDirName_weird() {
74 $this->_doFindBaseDirName(FALSE,
75 array('foo/../'),
76 array('foo/../README.txt' => 'hello')
77 );
78 }
79
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'),
84 'com.example.foo'
85 );
86 }
87
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'),
92 'com.example.foo'
93 );
94 }
95
96 function testGuessBaseDir_0() {
97 $this->_doGuessBaseDir('0',
98 array('0'),
99 array(),
100 'com.example.foo'
101 );
102 }
103
104 function testGuessBaseDir_plainfile() {
105 $this->_doGuessBaseDir(FALSE,
106 array(),
107 array('README.txt' => 'hello'),
108 'com.example.foo'
109 );
110 }
111
112 function testGuessBaseDir_twodir() {
113 $this->_doGuessBaseDir(FALSE,
114 array('dir-1', 'dir-2'),
115 array('dir-1/README.txt' => 'hello'),
116 'com.example.foo'
117 );
118 }
119
120 function testGuessBaseDir_weird() {
121 $this->_doGuessBaseDir(FALSE,
122 array('foo/../'),
123 array('foo/../README.txt' => 'hello'),
124 'com.example.foo'
125 );
126 }
127
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));
131
132 $zip = new ZipArchive();
133 $this->assertTrue($zip->open($this->file));
134 $this->assertEquals($expectedBaseDirName, CRM_Utils_Zip::findBaseDirName($zip));
135 }
136
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));
140
141 $zip = new ZipArchive();
142 $this->assertTrue($zip->open($this->file));
143 $this->assertEquals($expectedResult, CRM_Utils_Zip::guessBaseDir($zip, $expectedKey));
144 }
145 }