Open Journal Systems  3.3.0
testutils.php
1 <?php
2 
3 require_once 'lib/bagit_utils.php';
4 
5 class BagItUtilsTest extends PHPUnit_Framework_TestCase
6 {
7 
8  public function testFilterArrayMatches()
9  {
10  $input = array(
11  'abcde',
12  'bcdef',
13  'cdefg',
14  'defgh',
15  'efghi',
16  'fghij'
17  );
18 
19  $this->assertEquals(1, count(filterArrayMatches('/a/', $input)));
20 
21  $e = filterArrayMatches('/.*e.*/', $input);
22  $this->assertEquals(5, count($e));
23  $this->assertEquals('abcde', $e[0][0]);
24  $this->assertEquals('bcdef', $e[1][0]);
25  $this->assertEquals('cdefg', $e[2][0]);
26  $this->assertEquals('defgh', $e[3][0]);
27  $this->assertEquals('efghi', $e[4][0]);
28  }
29 
30  public function testFilterArrayMatchesFail()
31  {
32  $input = array(
33  'abcde',
34  'bcdef',
35  'cdefg',
36  'defgh',
37  'efghi',
38  'fghij'
39  );
40 
41  $this->assertEquals(0, count(filterArrayMatches('/z/', $input)));
42  }
43 
44  public function testEndsWithTrue()
45  {
46  $this->assertTrue(endsWith("Scholars' Lab", 'b'));
47  $this->assertTrue(endsWith("Scholars' Lab", 'ab'));
48  $this->assertTrue(endsWith("Scholars' Lab", 'Lab'));
49  }
50 
51  public function testEndsWithFalse()
52  {
53  $this->assertFalse(endsWith("Scholars' Lab", 'z'));
54  }
55 
56  private function _testRls($dirnames) {
57  $files = array();
58  foreach ($dirnames as $dirname) {
59  foreach (scandir($dirname) as $filename) {
60  if ($filename[0] != '.' && is_file("$dirname/$filename")) {
61  array_push($files, "$dirname/$filename");
62  }
63  }
64  }
65  sort($files);
66 
67  $lsout = rls($dirnames[0]);
68  sort($lsout);
69 
70  $this->assertEquals(count($files), count($lsout));
71 
72  for ($i=0; $i<count($files); $i++) {
73  $this->assertEquals($files[$i], $lsout[$i]);
74  }
75  }
76 
77  public function testRlsShallow()
78  {
79  $dirname = __DIR__ . '/../lib';
80  $this->_testRls(array($dirname));
81  }
82 
83  public function testRlsDeep()
84  {
85  $dirname = __DIR__;
86  $this->_testRls(
87  array($dirname, "$dirname/TestBag", "$dirname/TestBag/data",
88  "$dirname/TestBag/data/imgs")
89  );
90  }
91 
92  public function testRrmdirShallow()
93  {
94  $tmpdir = tmpdir();
95 
96  mkdir($tmpdir);
97  touch("$tmpdir/a");
98  touch("$tmpdir/b");
99  touch("$tmpdir/c");
100 
101  $this->assertFileExists("$tmpdir/a");
102 
103  rrmdir($tmpdir);
104 
105  $this->assertFalse(file_exists($tmpdir));
106  $this->assertFalse(file_exists("$tmpdir/a"));
107  $this->assertFalse(file_exists("$tmpdir/b"));
108  $this->assertFalse(file_exists("$tmpdir/c"));
109  }
110 
111  public function testRrmdirDeep()
112  {
113  $tmpdir = tmpdir();
114 
115  mkdir($tmpdir);
116  mkdir("$tmpdir/sub");
117  touch("$tmpdir/sub/a");
118  touch("$tmpdir/sub/b");
119  touch("$tmpdir/sub/c");
120 
121  $this->assertFileExists("$tmpdir/sub/c");
122 
123  rrmdir($tmpdir);
124 
125  $this->assertFalse(file_exists($tmpdir));
126  $this->assertFalse(file_exists("$tmpdir/sub"));
127  $this->assertFalse(file_exists("$tmpdir/sub/a"));
128  $this->assertFalse(file_exists("$tmpdir/sub/b"));
129  $this->assertFalse(file_exists("$tmpdir/sub/c"));
130  }
131 
132  public function testRrmdirFile()
133  {
134  $tmpdir = tmpdir();
135  touch($tmpdir);
136 
137  $this->assertFileExists($tmpdir);
138  rrmdir($tmpdir);
139  $this->assertFileExists($tmpdir);
140  }
141 
142  public function testTmpdir()
143  {
144  $tmpdir = tmpdir();
145  $this->assertFalse(file_exists($tmpdir));
146  $this->assertTrue(strpos($tmpdir, sys_get_temp_dir()) !== false);
147  }
148 
149  public function testTmpdirPrefix()
150  {
151  $tmpdir = tmpdir('test_');
152  $this->assertStringStartsWith('test_', basename($tmpdir));
153  }
154 
155  public function testSeenAtKeyIntegerKey()
156  {
157  $data = array(
158  array('a', 'b', 'c'),
159  array('d', 'e', 'f'),
160  array('g', 'h', 'i')
161  );
162 
163  $this->assertTrue(seenAtKey($data, 0, 'a'));
164  $this->assertTrue(seenAtKey($data, 1, 'e'));
165  $this->assertTrue(seenAtKey($data, 2, 'i'));
166  }
167 
168  public function testSeenAtKeyStringKey()
169  {
170  $data = array(
171  array('a' => 1, 'z' => 2),
172  array('a' => 3, 'z' => 4),
173  array('a' => 5, 'z' => 6),
174  array('a' => 7, 'z' => 8)
175  );
176 
177  $this->assertTrue(seenAtKey($data, 'a', 1));
178  $this->assertTrue(seenAtKey($data, 'z', 4));
179  $this->assertTrue(seenAtKey($data, 'a', 5));
180  $this->assertTrue(seenAtKey($data, 'z', 8));
181  }
182 
183  public function testSeenAtKeyFail()
184  {
185  $data = array(
186  array('a' => 1, 'z' => 2),
187  array('a' => 3, 'z' => 4),
188  array('a' => 5, 'z' => 6),
189  array('a' => 7, 'z' => 8)
190  );
191 
192  $this->assertFalse(seenAtKey($data, 'a', 2));
193  $this->assertFalse(seenAtKey($data, 'z', 5));
194  $this->assertFalse(seenAtKey($data, 'a', 6));
195  $this->assertFalse(seenAtKey($data, 'z', 9));
196  $this->assertFalse(seenAtKey($data, 'm', 13));
197  }
198 
199  public function testSaveUrl()
200  {
201  $tmpdir = tmpdir();
202  mkdir($tmpdir);
203 
204  saveUrl('http://www.google.com', "$tmpdir/google.html");
205 
206  $this->assertFileExists("$tmpdir/google.html");
207  $this->assertContains(
208  'html',
209  strtolower(file_get_contents("$tmpdir/google.html"))
210  );
211  }
212 
213  public function testFindFirstExistingPass()
214  {
215  $tmpdir = tmpdir();
216  mkdir($tmpdir);
217 
218  touch("$tmpdir/c");
219 
220  $this->assertEquals(
221  "$tmpdir/c",
222  findFirstExisting(array("$tmpdir/a", "$tmpdir/b", "$tmpdir/c"))
223  );
224  }
225 
226  public function testFindFirstExistingFail()
227  {
228  $tmpdir = tmpdir();
229  mkdir($tmpdir);
230 
231  touch("$tmpdir/c");
232 
233  $this->assertNull(
234  findFirstExisting(array("$tmpdir/a", "$tmpdir/b", "$tmpdir/d"))
235  );
236  }
237 
239  {
240  $tmpdir = tmpdir();
241  mkdir($tmpdir);
242 
243  touch("$tmpdir/c");
244 
245  $this->assertEquals(
246  "$tmpdir/default",
247  findFirstExisting(array("$tmpdir/a", "$tmpdir/b", "$tmpdir/d"),
248  "$tmpdir/default")
249  );
250  }
251 
252  public function testReadFileText()
253  {
254  $this->assertEquals(
255  "BagIt-Version: 0.96\n" .
256  "Tag-File-Character-Encoding: UTF-8\n",
257  readFileText(__DIR__ . '/TestBag/bagit.txt', 'UTF-8')
258  );
259  }
260 
261  public function testReadLines()
262  {
263  $lines = readLines(__DIR__ . '/TestBag/bagit.txt', 'UTF-8');
264  $this->assertEquals(2, count($lines));
265  $this->assertEquals("BagIt-Version: 0.96", $lines[0]);
266  $this->assertEquals("Tag-File-Character-Encoding: UTF-8", $lines[1]);
267  }
268 
269  public function testWriteFileText()
270  {
271  $tmpfile = tmpdir();
272 
273  writeFileText(
274  $tmpfile,
275  'UTF-8',
276  "This is some text.\nYep, it sure is.\n"
277  );
278 
279  $this->assertEquals(
280  "This is some text.\nYep, it sure is.\n",
281  file_get_contents($tmpfile)
282  );
283  }
284 
286  {
287  $this->assertEquals(
288  "this_contained_significant_whitespace_at_one_time",
289  BagIt_sanitizeFileName("this contained\tsignificant\t" .
290  "whitespace at one time")
291  );
292  }
293 
295  {
296  $this->assertEquals(
297  'thisthatwow',
298  BagIt_sanitizeFileName("this&that###wow!!!!~~~???")
299  );
300  }
301 
303  {
304  $this->assertStringStartsWith('nul_', BagIt_sanitizeFileName('NUL'));
305  $this->assertStringStartsWith('aux_', BagIt_sanitizeFileName('AUX'));
306  $this->assertStringStartsWith('com3_', BagIt_sanitizeFileName('COM3'));
307  $this->assertStringStartsWith('lpt6_', BagIt_sanitizeFileName('LPT6'));
308  }
309 
310  public function testBagIt_sanitizeFileName()
311  {
312  $this->assertEquals(
313  'this-is-ok.txt',
314  BagIt_sanitizeFileName('this-is-ok.txt')
315  );
316  }
317 
318  public function testBagIt_readBagItFile()
319  {
320  $filename = __DIR__ . '/TestBag/bagit.txt';
321  list($versions, $encoding, $errors) = BagIt_readBagItFile($filename);
322 
323  $this->assertEquals(2, count($versions));
324  $this->assertEquals(0, $versions['major']);
325  $this->assertEquals(96, $versions['minor']);
326  $this->assertEquals('UTF-8', $encoding);
327  $this->assertEquals(0, count($errors));
328  }
329 
331  {
332  $tmpfile = tmpdir('bagit_');
333  file_put_contents(
334  $tmpfile,
335  "Tag-File-Character-Encoding: ISO-8859-1\n"
336  );
337 
338  list($versions, $encoding, $errors) = BagIt_readBagItFile($tmpfile);
339  $this->assertNull($versions);
340  $this->assertEquals('ISO-8859-1', $encoding);
341  $this->assertEquals(1, count($errors));
342  $this->assertEquals('bagit', $errors[0][0]);
343  $this->assertEquals(
344  'Error reading version information from bagit.txt file.',
345  $errors[0][1]
346  );
347  }
348 
350  {
351  $tmpfile = tmpdir('bagit_');
352  file_put_contents(
353  $tmpfile,
354  "BagIt-Version: 0.96\n"
355  );
356 
357  list($versions, $encoding, $errors) = BagIt_readBagItFile($tmpfile);
358  $this->assertEquals(2, count($versions));
359  $this->assertEquals(0, $versions['major']);
360  $this->assertEquals(96, $versions['minor']);
361 
362  // I'm not entirely sure that this is the behavior I want here.
363  // I think maybe it should set the default (UTF-8) and signal an
364  // error.
365  $this->assertNull($encoding);
366  $this->assertEquals(0, count($errors));
367  }
368 
370  {
371  $filename = __DIR__ . '/doesn-not-exist';
372  list($versions, $encoding, $errors) = BagIt_readBagItFile($filename);
373 
374  $this->assertEquals(2, count($versions));
375  $this->assertEquals(0, $versions['major']);
376  $this->assertEquals(96, $versions['minor']);
377  $this->assertEquals('UTF-8', $encoding);
378  $this->assertEquals(0, count($errors));
379  }
380 
382  {
383  $data =
384  "BagIt-Version: 0.96\n" .
385  "Tag-File-Character-Encoding: UTF-8\n";
386  $versions = BagIt_parseVersionString($data);
387 
388  $this->assertEquals(2, count($versions));
389  $this->assertEquals(0, $versions['major']);
390  $this->assertEquals(96, $versions['minor']);
391  }
392 
394  {
395  $data =
396  "BagIt-Versions: 0.96\n" .
397  "Tag-File-Character-Encoding: UTF-8\n";
398  $versions = BagIt_parseVersionString($data);
399 
400  $this->assertNull($versions);
401  }
402 
404  {
405  $data =
406  "BagIt-Version: 0.96\n" .
407  "Tag-File-Character-Encoding: UTF-8\n";
408  $encoding = BagIt_parseEncodingString($data);
409  $this->assertEquals('UTF-8', $encoding);
410  }
411 
413  {
414  $data =
415  "BagIt-Version: 0.96\n" .
416  "Tag-File-Character-encoding: UTF-8\n";
417  $encoding = BagIt_parseEncodingString($data);
418  $this->assertNull($encoding);
419  }
420 
421  private function _clearTagManifest() {
422  // Other tests add a tagmanifest-sha1.txt, which isn't in the
423  // archives, at the end of the list. Rm it.
424  $rmfile = __DIR__ . '/TestBag/tagmanifest-sha1.txt';
425  if (file_exists($rmfile)) {
426  unlink($rmfile);
427  }
428  }
429 
430  public function testBagIt_uncompressBagZip()
431  {
432  $zipfile = __DIR__ . '/TestBag.zip';
433  $output = BagIt_uncompressBag($zipfile);
434 
435  $this->assertFileExists($output);
436  $this->assertTrue(strpos($output, sys_get_temp_dir()) !== false);
437 
438  $this->_clearTagManifest();
439 
440  $bagFiles = rls(__DIR__ . '/TestBag');
441  sort($bagFiles);
442  $outFiles = rls($output);
443  sort($outFiles);
444 
445  $this->assertEquals(count($bagFiles), count($outFiles));
446  for ($i=0; $i<count($outFiles); $i++) {
447  $this->assertEquals(
448  basename($bagFiles[$i]),
449  basename($outFiles[$i])
450  );
451  }
452  }
453 
454  public function testBagIt_uncompressBagTar()
455  {
456  $tarfile = __DIR__ . '/TestBag.tgz';
457  $output = BagIt_uncompressBag($tarfile);
458 
459  $this->assertFileExists($output);
460  $this->assertTrue(strpos($output, sys_get_temp_dir()) !== false);
461 
462  $this->_clearTagManifest();
463 
464  $bagFiles = rls(__DIR__ . '/TestBag');
465  sort($bagFiles);
466  $outFiles = rls($output);
467  sort($outFiles);
468 
469  $this->assertEquals(count($bagFiles), count($outFiles));
470  for ($i=0; $i<count($outFiles); $i++) {
471  $this->assertEquals(
472  basename($bagFiles[$i]),
473  basename($outFiles[$i])
474  );
475  }
476  }
477 
482  {
483  BagIt_uncompressBag(__DIR__ . '/TestBag');
484  }
485 
486  /* TODO: Fix these so that they're testing correctly.
487  public function testBagIt_compressBagZip()
488  {
489  $this->_clearTagManifest();
490 
491  $output = tmpdir() . '.zip';
492  BagIt_compressBag(__DIR__ . '/TestBag', $output, 'zip');
493 
494  $this->assertFileEquals(__DIR__ . '/TestBag.zip', $output);
495  }
496 
497  public function testBagIt_compressBagTar()
498  {
499  $this->_clearTagManifest();
500 
501  $output = tmpdir() . '.tgz';
502  BagIt_compressBag(__DIR__ . '/TestBag', $output, 'tgz');
503 
504  $this->assertFileEquals(__DIR__ . '/TestBag.tgz', $output);
505  }
506  */
507 
509  {
510  $errors = array();
511  $this->assertTrue(BagIt_validateExists(__FILE__, $errors));
512  $this->assertEquals(0, count($errors));
513  }
514 
516  {
517  $errors = array();
518  $this->assertFalse(
519  BagIt_validateExists(__DIR__ . '/not-here', $errors)
520  );
521  $this->assertEquals(1, count($errors));
522  $this->assertEquals('not-here', $errors[0][0]);
523  $this->assertEquals('not-here does not exist.', $errors[0][1]);
524  }
525 
527  {
528  $lines = array(
529  'some: here',
530  '',
531  'other: there'
532  );
533 
534  $info = BagIt_parseBagInfo($lines);
535  $this->assertEquals('here', $info['some']);
536  $this->assertEquals('there', $info['other']);
537  }
538 
540  {
541  $lines = array(
542  'some: here',
543  ' and there',
544  'other: there',
545  "\tand here"
546  );
547 
548  $info = BagIt_parseBagInfo($lines);
549  $this->assertEquals('here and there', $info['some']);
550  $this->assertEquals('there and here', $info['other']);
551  }
552 
554  {
555  $lines = array(
556  'some: here',
557  'other: there'
558  );
559 
560  $info = BagIt_parseBagInfo($lines);
561  $this->assertEquals('here', $info['some']);
562  $this->assertEquals('there', $info['other']);
563  }
564 
565 }
566 
567 ?>
BagItUtilsTest\testBagIt_uncompressBagTar
testBagIt_uncompressBagTar()
Definition: testutils.php:454
BagItUtilsTest\testBagIt_parseBaseInfoContinued
testBagIt_parseBaseInfoContinued()
Definition: testutils.php:539
BagItUtilsTest\testBagIt_readBagItFileNoVersion
testBagIt_readBagItFileNoVersion()
Definition: testutils.php:330
BagItUtilsTest\testBagIt_parseVersionStringPass
testBagIt_parseVersionStringPass()
Definition: testutils.php:381
BagItUtilsTest\testBagIt_validateExistsPass
testBagIt_validateExistsPass()
Definition: testutils.php:508
BagItUtilsTest\testEndsWithTrue
testEndsWithTrue()
Definition: testutils.php:44
BagItUtilsTest\testBagIt_parseVersionStringFail
testBagIt_parseVersionStringFail()
Definition: testutils.php:393
BagItUtilsTest\testRlsShallow
testRlsShallow()
Definition: testutils.php:77
BagItUtilsTest\testFindFirstExistingDefault
testFindFirstExistingDefault()
Definition: testutils.php:238
BagItUtilsTest\testRrmdirFile
testRrmdirFile()
Definition: testutils.php:132
BagItUtilsTest\testBagIt_uncompressBagError
testBagIt_uncompressBagError()
Definition: testutils.php:481
BagItUtilsTest\testBagIt_validateExistsFail
testBagIt_validateExistsFail()
Definition: testutils.php:515
BagItUtilsTest\testSaveUrl
testSaveUrl()
Definition: testutils.php:199
BagItUtilsTest\testRlsDeep
testRlsDeep()
Definition: testutils.php:83
BagItUtilsTest\testTmpdir
testTmpdir()
Definition: testutils.php:142
BagItUtilsTest\testFindFirstExistingFail
testFindFirstExistingFail()
Definition: testutils.php:226
BagItUtilsTest\testFindFirstExistingPass
testFindFirstExistingPass()
Definition: testutils.php:213
BagItUtilsTest\testRrmdirDeep
testRrmdirDeep()
Definition: testutils.php:111
BagItUtilsTest\testSeenAtKeyIntegerKey
testSeenAtKeyIntegerKey()
Definition: testutils.php:155
BagItUtilsTest\testFilterArrayMatches
testFilterArrayMatches()
Definition: testutils.php:8
BagItUtilsTest\testBagIt_sanitizeFileName
testBagIt_sanitizeFileName()
Definition: testutils.php:310
BagItUtilsTest\testBagIt_sanitizeFileNameDevs
testBagIt_sanitizeFileNameDevs()
Definition: testutils.php:302
BagItUtilsTest\testBagIt_parseBaseInfoStandard
testBagIt_parseBaseInfoStandard()
Definition: testutils.php:553
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
BagItUtilsTest\testBagIt_parseBaseInfoEmptyLine
testBagIt_parseBaseInfoEmptyLine()
Definition: testutils.php:526
BagItUtilsTest\testReadLines
testReadLines()
Definition: testutils.php:261
BagItUtilsTest
Definition: testutils.php:5
BagItUtilsTest\testBagIt_parseEncodingStringPass
testBagIt_parseEncodingStringPass()
Definition: testutils.php:403
BagItUtilsTest\testBagIt_sanitizeFileNameWhiteSpace
testBagIt_sanitizeFileNameWhiteSpace()
Definition: testutils.php:285
BagItUtilsTest\testWriteFileText
testWriteFileText()
Definition: testutils.php:269
BagItUtilsTest\testBagIt_readBagItFile
testBagIt_readBagItFile()
Definition: testutils.php:318
BagItUtilsTest\testBagIt_parseEncodingStringFail
testBagIt_parseEncodingStringFail()
Definition: testutils.php:412
BagItUtilsTest\testTmpdirPrefix
testTmpdirPrefix()
Definition: testutils.php:149
BagItUtilsTest\testSeenAtKeyFail
testSeenAtKeyFail()
Definition: testutils.php:183
BagItUtilsTest\testRrmdirShallow
testRrmdirShallow()
Definition: testutils.php:92
BagItUtilsTest\testBagIt_uncompressBagZip
testBagIt_uncompressBagZip()
Definition: testutils.php:430
BagItUtilsTest\testBagIt_sanitizeFileNameRemove
testBagIt_sanitizeFileNameRemove()
Definition: testutils.php:294
BagItUtilsTest\testReadFileText
testReadFileText()
Definition: testutils.php:252
BagItUtilsTest\testFilterArrayMatchesFail
testFilterArrayMatchesFail()
Definition: testutils.php:30
BagItUtilsTest\testSeenAtKeyStringKey
testSeenAtKeyStringKey()
Definition: testutils.php:168
BagItUtilsTest\testEndsWithFalse
testEndsWithFalse()
Definition: testutils.php:51
BagItUtilsTest\testBagIt_readBagItFileMissing
testBagIt_readBagItFileMissing()
Definition: testutils.php:369
BagItUtilsTest\testBagIt_readBagItFileNoEncoding
testBagIt_readBagItFileNoEncoding()
Definition: testutils.php:349