Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 3 | class File_helper_Test extends CI_TestCase { |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 4 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 5 | public function set_up() |
| 6 | { |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 7 | $this->helper('file'); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 8 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 9 | vfsStreamWrapper::register(); |
| 10 | vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir')); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 11 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 12 | $this->_test_dir = vfsStreamWrapper::getRoot(); |
| 13 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 14 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 15 | // -------------------------------------------------------------------- |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 16 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 17 | public function test_read_file() |
| 18 | { |
| 19 | $this->assertFalse(read_file('does_not_exist')); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 20 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 21 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 22 | |
| 23 | $file = vfsStream::newFile('my_file.txt')->withContent($content)->at($this->_test_dir); |
| 24 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 25 | $this->assertEquals($content, read_file(vfsStream::url('my_file.txt'))); |
| 26 | } |
| 27 | |
| 28 | // -------------------------------------------------------------------- |
| 29 | |
| 30 | public function test_octal_permissions() |
| 31 | { |
| 32 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 33 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 34 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 35 | ->lastModified(time() - 86400) |
| 36 | ->at($this->_test_dir); |
| 37 | |
| 38 | $this->assertEquals('777', octal_permissions($file->getPermissions())); |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 41 | // -------------------------------------------------------------------- |
| 42 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 43 | /** |
| 44 | * More tests should happen here, since I'm not hitting the whole function. |
| 45 | */ |
| 46 | public function test_symbolic_permissions() |
| 47 | { |
| 48 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 49 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 50 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 51 | ->lastModified(time() - 86400) |
| 52 | ->at($this->_test_dir); |
| 53 | |
| 54 | $this->assertEquals('urwxrwxrwx', symbolic_permissions($file->getPermissions())); |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 57 | // -------------------------------------------------------------------- |
| 58 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 59 | public function test_get_mime_by_extension() |
| 60 | { |
| 61 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 62 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 63 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 64 | ->lastModified(time() - 86400) |
| 65 | ->at($this->_test_dir); |
| 66 | |
| 67 | $this->assertEquals('text/plain', get_mime_by_extension(vfsStream::url('my_file.txt'))); |
| 68 | |
| 69 | // Test a mime with an array, such as png |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 70 | $file = vfsStream::newFile('foo.png')->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 71 | |
| 72 | $this->assertEquals('image/png', get_mime_by_extension(vfsStream::url('foo.png'))); |
| 73 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 74 | // Test a file not in the mimes array |
| 75 | $file = vfsStream::newFile('foo.blarfengar')->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 76 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 77 | $this->assertFalse(get_mime_by_extension(vfsStream::url('foo.blarfengar'))); |
| 78 | } |
| 79 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 80 | // -------------------------------------------------------------------- |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 81 | |
| 82 | public function test_get_file_info() |
| 83 | { |
| 84 | // Test Bad File |
| 85 | $this->assertFalse(get_file_info('i_am_bad_boo')); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 86 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 87 | // Test the rest |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 88 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 89 | // First pass in an array |
| 90 | $vals = array( |
| 91 | 'name', 'server_path', 'size', 'date', |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 92 | 'readable', 'writable', 'executable', 'fileperms' |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 93 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 94 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 95 | $this->_test_get_file_info($vals); |
| 96 | |
| 97 | // Test passing in vals as a string. |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 98 | $this->_test_get_file_info(implode(', ', $vals)); |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 99 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 100 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 101 | private function _test_get_file_info($vals) |
| 102 | { |
| 103 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 104 | $last_modified = time() - 86400; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 105 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 106 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 107 | ->lastModified($last_modified) |
| 108 | ->at($this->_test_dir); |
| 109 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 110 | $ret_values = array( |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 111 | 'name' => 'my_file.txt', |
| 112 | 'server_path' => 'vfs://my_file.txt', |
| 113 | 'size' => 57, |
| 114 | 'date' => $last_modified, |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 115 | 'readable' => TRUE, |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 116 | 'writable' => TRUE, |
| 117 | 'executable' => TRUE, |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 118 | 'fileperms' => 33279 |
| 119 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 120 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 121 | $info = get_file_info(vfsStream::url('my_file.txt'), $vals); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 122 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 123 | foreach ($info as $k => $v) |
| 124 | { |
| 125 | $this->assertEquals($ret_values[$k], $v); |
| 126 | } |
| 127 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 128 | |
| 129 | // -------------------------------------------------------------------- |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 130 | |
| 131 | // Skipping for now, as it's not implemented in vfsStreamWrapper |
| 132 | // flock(): vfsStreamWrapper::stream_lock is not implemented! |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 133 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 134 | // public function test_write_file() |
| 135 | // { |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 136 | // if ( ! defined('FOPEN_WRITE_CREATE_DESTRUCTIVE')) |
| 137 | // { |
| 138 | // define('FOPEN_WRITE_CREATE_DESTRUCTIVE', 'wb'); |
| 139 | // } |
| 140 | // |
| 141 | // $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 142 | // |
| 143 | // $file = vfsStream::newFile('write.txt', 0777)->withContent('') |
| 144 | // ->lastModified(time() - 86400) |
| 145 | // ->at($this->_test_dir); |
| 146 | // |
| 147 | // $this->assertTrue(write_file(vfsStream::url('write.txt'), $content)); |
| 148 | // |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 149 | // } |
| 150 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 151 | // -------------------------------------------------------------------- |
| 152 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 153 | } |