Open Journal Systems  3.3.0
testusecases.php
1 <?php
2 
3 require_once 'lib/bagit.php';
4 
8 class BagPhpUseCaseTest extends PHPUnit_Framework_TestCase
9 {
10  var $to_rm;
11 
12  private function queueRm($dirname)
13  {
14  array_push($this->to_rm, $dirname);
15  }
16 
17  public function setUp()
18  {
19  $this->to_rm = array();
20  }
21 
22  public function tearDown()
23  {
24  foreach ($this->to_rm as $dirname)
25  {
26  rrmdir($dirname);
27  }
28  }
29 
42  public function testBagProducer()
43  {
44  $tmpdir = tmpdir();
45  mkdir($tmpdir);
46  $this->queueRm($tmpdir);
47 
48  $tmpbag = "$tmpdir/BagProducer";
49 
50  // 1. Create a new bag;
51  $bag = new BagIt($tmpbag);
52 
53  $this->assertTrue($bag->isValid());
54  $this->assertTrue($bag->isExtended());
55 
56  $bagInfo = $bag->getBagInfo();
57  $this->assertEquals('0.96', $bagInfo['version']);
58  $this->assertEquals('UTF-8', $bagInfo['encoding']);
59  $this->assertEquals('sha1', $bagInfo['hash']);
60 
61  $this->assertEquals("$tmpbag/data", $bag->getDataDirectory());
62  $this->assertEquals('sha1', $bag->getHashEncoding());
63  $this->assertEquals(0, count($bag->getBagContents()));
64  $this->assertEquals(0, count($bag->getBagErrors()));
65 
66  // 2. Add files to the bag;
67  $srcdir = __DIR__ . '/TestBag/data';
68  $bag->addFile("$srcdir/README.txt", 'data/README.txt');
69  $bag->addFile("$srcdir/imgs/uvalib.png", 'data/payloads/uvalib.png');
70  // This needs to add data/ to the beginning of the file.
71  $bag->addFile(
72  "$srcdir/imgs/fibtriangle-110x110.jpg",
73  'payloads/fibtri.jpg'
74  );
75 
76  // 3. Add fetch entries;
77  $bag->fetch->add('http://www.scholarslab.org/', 'data/index.html');
78 
79  // 4. Update the bag; and
80  $bag->update();
81 
82  // 5. Package the bag.
83  $pkgfile = "$tmpdir/BagProducer.tgz";
84  $bag->package($pkgfile);
85 
86  // Finally, we need to validate the contents of the package.
87  $dest = new BagIt($pkgfile);
88  $this->queueRm($dest->bagDirectory);
89 
90  // First, verify that the data files are correct.
91  $this->assertEquals(
92  "BagIt-Version: 0.96\n" .
93  "Tag-File-Character-Encoding: UTF-8\n",
94  file_get_contents($dest->bagitFile)
95  );
96 
97  // Second, verify that everything was uncompressed OK.
98  $dest->validate();
99  $this->assertEquals(0, count($dest->bagErrors));
100 
101  // Now, check that the file was fetched.
102  $dest->fetch->download();
103  $this->assertFileExists("{$dest->bagDirectory}/data/index.html");
104 
105  // Also, check that fibtri.jpg was added in the data/ directory.
106  $this->assertFalse(
107  file_exists("{$dest->bagDirectory}/payloads/fibtri.jpg")
108  );
109  $this->assertFileExists("{$dest->bagDirectory}/data/payloads/fibtri.jpg");
110 
111  }
112 
125  public function testBagConsumer()
126  {
127  $srcbag = __DIR__ . '/TestBag.tgz';
128 
129  // 1. Open the bag;
130  $bag = new BagIt($srcbag);
131  $this->queueRm($bag->bagDirectory);
132 
133  $this->assertTrue($bag->isValid());
134  $this->assertTrue($bag->isExtended());
135 
136  $bagInfo = $bag->getBagInfo();
137  $this->assertEquals('0.96', $bagInfo['version']);
138  $this->assertEquals('UTF-8', $bagInfo['encoding']);
139  $this->assertEquals('sha1', $bagInfo['hash']);
140 
141  $this->assertEquals('sha1', $bag->getHashEncoding());
142  $this->assertEquals(7, count($bag->getBagContents()));
143  $this->assertEquals(0, count($bag->getBagErrors()));
144 
145  // 2. Validate the downloaded contents;
146  $bag->validate();
147  $this->assertEquals(0, count($bag->getBagErrors()));
148 
149  // 3. Fetch on-line items in the bag;
150  $bag->fetch->download();
151  $bag->update();
152  $bag->validate();
153  $this->assertEquals(8, count($bag->getBagContents()));
154 
155  // 4. Validate the bag's contents; and
156  $this->assertEquals(0, count($bag->getBagErrors()));
157 
158  // 5. Copy items from the bag onto the local disk.
159  $tmpdir = tmpdir();
160  mkdir($tmpdir);
161  $this->queueRm($tmpdir);
162 
163  foreach ($bag->getBagContents() as $bagFile)
164  {
165  $basename = basename($bagFile);
166  copy($bagFile, "$tmpdir/$basename");
167  }
168 
169  $this->assertEquals(
170  count($bag->getBagContents()) + 2,
171  count(scandir($tmpdir))
172  );
173  $this->assertEquals(
174  count($bag->manifest->getData()) + 2,
175  count(scandir($tmpdir))
176  );
177 
178  }
179 
180 }
181 
182 ?>
BagPhpUseCaseTest
Definition: testusecases.php:8
BagPhpUseCaseTest\testBagProducer
testBagProducer()
Definition: testusecases.php:42
BagPhpUseCaseTest\tearDown
tearDown()
Definition: testusecases.php:22
BagPhpUseCaseTest\testBagConsumer
testBagConsumer()
Definition: testusecases.php:125
BagPhpUseCaseTest\$to_rm
$to_rm
Definition: testusecases.php:10
BagIt
Definition: bagit.php:69
BagPhpUseCaseTest\setUp
setUp()
Definition: testusecases.php:17