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 | |
Andrey Andreev | 53d5ee6 | 2014-02-17 16:57:48 +0200 | [diff] [blame] | 34 | $file = vfsStream::newFile('my_file.txt', 0777) |
| 35 | ->withContent($content) |
| 36 | ->lastModified(time() - 86400) |
| 37 | ->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 38 | |
| 39 | $this->assertEquals('777', octal_permissions($file->getPermissions())); |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 42 | // -------------------------------------------------------------------- |
| 43 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 44 | /** |
| 45 | * More tests should happen here, since I'm not hitting the whole function. |
| 46 | */ |
| 47 | public function test_symbolic_permissions() |
| 48 | { |
| 49 | $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] | 50 | |
Andrey Andreev | 53d5ee6 | 2014-02-17 16:57:48 +0200 | [diff] [blame] | 51 | $file = vfsStream::newFile('my_file.txt', 0777) |
| 52 | ->withContent($content) |
| 53 | ->lastModified(time() - 86400) |
| 54 | ->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 55 | |
| 56 | $this->assertEquals('urwxrwxrwx', symbolic_permissions($file->getPermissions())); |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 59 | // -------------------------------------------------------------------- |
| 60 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 61 | public function test_get_mime_by_extension() |
| 62 | { |
| 63 | $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] | 64 | |
Andrey Andreev | 53d5ee6 | 2014-02-17 16:57:48 +0200 | [diff] [blame] | 65 | $file = vfsStream::newFile('my_file.txt', 0777) |
| 66 | ->withContent($content) |
| 67 | ->lastModified(time() - 86400) |
| 68 | ->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 69 | |
| 70 | $this->assertEquals('text/plain', get_mime_by_extension(vfsStream::url('my_file.txt'))); |
| 71 | |
| 72 | // Test a mime with an array, such as png |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 73 | $file = vfsStream::newFile('foo.png')->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 74 | |
| 75 | $this->assertEquals('image/png', get_mime_by_extension(vfsStream::url('foo.png'))); |
| 76 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 77 | // Test a file not in the mimes array |
| 78 | $file = vfsStream::newFile('foo.blarfengar')->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 79 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 80 | $this->assertFalse(get_mime_by_extension(vfsStream::url('foo.blarfengar'))); |
| 81 | } |
| 82 | |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 83 | // -------------------------------------------------------------------- |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 84 | |
| 85 | public function test_get_file_info() |
| 86 | { |
| 87 | // Test Bad File |
| 88 | $this->assertFalse(get_file_info('i_am_bad_boo')); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 89 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 90 | // Test the rest |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 91 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 92 | // First pass in an array |
| 93 | $vals = array( |
| 94 | 'name', 'server_path', 'size', 'date', |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 95 | 'readable', 'writable', 'executable', 'fileperms' |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 96 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 97 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 98 | $this->_test_get_file_info($vals); |
| 99 | |
| 100 | // Test passing in vals as a string. |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 101 | $this->_test_get_file_info(implode(', ', $vals)); |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 102 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 103 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 104 | private function _test_get_file_info($vals) |
| 105 | { |
| 106 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 107 | $last_modified = time() - 86400; |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 108 | |
Andrey Andreev | 53d5ee6 | 2014-02-17 16:57:48 +0200 | [diff] [blame] | 109 | $file = vfsStream::newFile('my_file.txt', 0777) |
| 110 | ->withContent($content) |
| 111 | ->lastModified($last_modified) |
| 112 | ->at($this->_test_dir); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 113 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 114 | $ret_values = array( |
Andrey Andreev | 53d5ee6 | 2014-02-17 16:57:48 +0200 | [diff] [blame] | 115 | 'name' => 'my_file.txt', |
| 116 | 'server_path' => 'vfs://my_file.txt', |
| 117 | 'size' => 57, |
| 118 | 'date' => $last_modified, |
| 119 | 'readable' => TRUE, |
| 120 | 'writable' => TRUE, |
| 121 | 'executable' => TRUE, |
| 122 | 'fileperms' => 33279 |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 123 | ); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 124 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 125 | $info = get_file_info(vfsStream::url('my_file.txt'), $vals); |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 126 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 127 | foreach ($info as $k => $v) |
| 128 | { |
| 129 | $this->assertEquals($ret_values[$k], $v); |
| 130 | } |
| 131 | } |
Andrey Andreev | 99b782d | 2012-06-09 22:24:46 +0300 | [diff] [blame] | 132 | |
| 133 | // -------------------------------------------------------------------- |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 134 | |
Andrey Andreev | 53d5ee6 | 2014-02-17 16:57:48 +0200 | [diff] [blame] | 135 | public function test_write_file() |
| 136 | { |
| 137 | $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] | 138 | |
Andrey Andreev | 53d5ee6 | 2014-02-17 16:57:48 +0200 | [diff] [blame] | 139 | $file = vfsStream::newFile('write.txt', 0777) |
| 140 | ->withContent('') |
| 141 | ->lastModified(time() - 86400) |
| 142 | ->at($this->_test_dir); |
| 143 | |
| 144 | $this->assertTrue(write_file(vfsStream::url('write.txt'), $content)); |
| 145 | } |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 146 | |
Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 147 | } |