public function setUp() { $this->tmpdir = tmpdir(); mkdir($this->tmpdir); file_put_contents("{$this->tmpdir}/fetch.txt", "http://www.google.com - data/google/index.html\n" . "http://www.yahoo.com - data/yahoo/index.html\n"); $this->fetch = new BagItFetch("{$this->tmpdir}/fetch.txt"); }
function setupTmpDir($path, $conflevel = -1, $conftext = null) { $tmpdir = tmpdir(); if (count($path) > 0) { $scriptdir = $tmpdir . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $path); mkdir($scriptdir, 0777, true); } else { $scriptdir = $tmpdir; } $scriptfile = $scriptdir . DIRECTORY_SEPARATOR . "dummy.php"; fclose(fopen($scriptfile, "w")); $_SERVER['SCRIPT_FILENAME'] = $scriptfile; if ($conflevel >= 0 && $conflevel <= count($path)) { $path2 = array_slice($path, 0, $conflevel); $confdir = $tmpdir . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $path2) . DIRECTORY_SEPARATOR . FITTEST; mkdir($confdir); if (!is_null($conftext)) { $conffile = $confdir . DIRECTORY_SEPARATOR . CONFFILE; file_put_contents($conffile, $conftext); } } return $tmpdir; }
if (!is_dir($mediaDirectory)) { trigger_error("The provided directory path doesn't exist.", E_USER_WARNING); goto answering; } $databaseFile = realpath($mediaDirectory) . "/metadata.db"; if (file_exists($databaseFile)) { unlink($databaseFile); $htmlMessage = $htmlMessage . "<p/>Deleted previously existing db file at: '" . $databaseFile . "'"; } // We don't have getid3 in WebKit. If not currently present, we // will try to download it now before making use of it. // // getid3 is available at http://getid3.sourceforge.net $getId3DestDir = "getid3"; if (!file_exists($getId3DestDir)) { $tmpDir = tmpdir(FALSE, "php"); if (!is_dir($tmpDir)) { trigger_error("Impossible to create temporal directory.", E_USER_WARNING); goto answering; } $id3DestPath = $tmpDir . "/getid3.zip"; $phpVersion = explode(".", PHP_VERSION); if ($phpVersion[0] > 4 && $phpVersion[1] > 0) { $id3Url = "http://sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.9.7/getID3-1.9.7.zip/download"; } else { $id3Url = "http://sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.7.10/getid3-1.7.10-20090426.zip/download"; } $htmlMessage = $htmlMessage . "<p/>getId3 not in the system.<p/>Downloading from: '" . $id3Url . "'"; $ctxtOpts = array("http" => array("method" => "GET", "timeout" => 60)); $ctxt = stream_context_create($ctxtOpts); $src = fopen($id3Url, "rb", false, $ctxt);
public function testEmptyDirectory() { $tmp = tmpdir(); try { mkdir($tmp); $bag = new BagIt($tmp); $this->assertFileExists("{$tmp}/bagit.txt"); $this->assertFileExists("{$tmp}/manifest-sha1.txt"); $this->assertFileExists("{$tmp}/bag-info.txt"); $this->assertFileExists("{$tmp}/fetch.txt"); $this->assertFileExists("{$tmp}/tagmanifest-sha1.txt"); } catch (Exception $e) { rrmdir($tmp); throw $e; } rrmdir($tmp); }
public function testBagIt_readBagItFileNoEncoding() { $tmpfile = tmpdir('bagit_'); file_put_contents($tmpfile, "BagIt-Version: 0.96\n"); list($versions, $encoding, $errors) = BagIt_readBagItFile($tmpfile); $this->assertEquals(2, count($versions)); $this->assertEquals(0, $versions['major']); $this->assertEquals(96, $versions['minor']); // I'm not entirely sure that this is the behavior I want here. // I think maybe it should set the default (UTF-8) and signal an // error. $this->assertNull($encoding); $this->assertEquals(0, count($errors)); }
/** * This is the use case for consuming a bag from someone else. The user * does these actions: * * <ol> * <li>Open the bag;</li> * <li>Validate the downloaded contents;</li> * <li>Fetch on-line items in the bag;</li> * <li>Validate the bag's contents; and</li> * <li>Copy items from the bag onto the local disk.</li> * </ol> */ public function testBagConsumer() { $srcbag = __DIR__ . '/TestBag.tgz'; // 1. Open the bag; $bag = new BagIt($srcbag); $this->queueRm($bag->bagDirectory); $this->assertTrue($bag->isValid()); $this->assertTrue($bag->isExtended()); $bagInfo = $bag->getBagInfo(); $this->assertEquals('0.96', $bagInfo['version']); $this->assertEquals('UTF-8', $bagInfo['encoding']); $this->assertEquals('sha1', $bagInfo['hash']); $this->assertEquals('sha1', $bag->getHashEncoding()); $this->assertEquals(7, count($bag->getBagContents())); $this->assertEquals(0, count($bag->getBagErrors())); // 2. Validate the downloaded contents; $bag->validate(); $this->assertEquals(0, count($bag->getBagErrors())); // 3. Fetch on-line items in the bag; $bag->fetch->download(); $bag->update(); $bag->validate(); $this->assertEquals(8, count($bag->getBagContents())); // 4. Validate the bag's contents; and $this->assertEquals(0, count($bag->getBagErrors())); // 5. Copy items from the bag onto the local disk. $tmpdir = tmpdir(); mkdir($tmpdir); $this->queueRm($tmpdir); foreach ($bag->getBagContents() as $bagFile) { $basename = basename($bagFile); copy($bagFile, "{$tmpdir}/{$basename}"); } $this->assertEquals(count($bag->getBagContents()) + 2, count(scandir($tmpdir))); $this->assertEquals(count($bag->manifest->getData()) + 2, count(scandir($tmpdir))); }
public function testValidateChecksum() { $tmp = tmpdir(); try { mkdir($tmp); mkdir($tmp . '/data'); file_put_contents("{$tmp}/manifest-sha1.txt", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa data/missing.txt\n"); touch("{$tmp}/data/missing.txt"); $manifest = new BagItManifest("{$tmp}/manifest-sha1.txt", "{$tmp}/"); $errors = array(); $this->assertFalse($manifest->validate($errors)); $this->assertTrue(seenAtKey($errors, 0, 'data/missing.txt')); $this->assertTrue(seenAtKey($errors, 1, 'Checksum mismatch.')); } catch (Exception $e) { rrmdir($tmp); throw $e; } rrmdir($tmp); }
// $dir = sys_get_temp_dir(); // // echo $dir . PHP_EOL; // $tmpfname = tempnam($dir, "HEXLET"); // // $temp = tmpfile(); // try { // fwrite($temp, "my data"); // fseek($temp, 0); // echo fread($temp, 1024); // } finally { // fclose($temp); // } // $temp = new \SplTempFileObject(); function tmpdir($func) { $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(); mkdir($dir); try { return $func($dir); } finally { rmdir($dir); } } $dirname = tmpdir(function ($dir) { return tempnam($dir, 'hexlet123'); }); if (file_exists($dirname)) { echo $dirname . " exists\n"; } else { echo "failed"; }