blob: 9b03da9d7cc3661519d01eda5fd93aaeb68d2ca0 [file] [log] [blame]
Greg Akerd92277d2011-04-22 12:56:22 -05001<?php
2
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07003class File_helper_Test extends CI_TestCase {
Greg Akerd92277d2011-04-22 12:56:22 -05004
Greg Akerd92277d2011-04-22 12:56:22 -05005 public function set_up()
6 {
Taufan Adityae1dc9ea2012-03-28 16:49:49 +07007 $this->helper('file');
Andrey Andreev99b782d2012-06-09 22:24:46 +03008
Greg Akerd92277d2011-04-22 12:56:22 -05009 vfsStreamWrapper::register();
10 vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir'));
Andrey Andreev99b782d2012-06-09 22:24:46 +030011
Greg Akerd92277d2011-04-22 12:56:22 -050012 $this->_test_dir = vfsStreamWrapper::getRoot();
13 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030014
Greg Akerd92277d2011-04-22 12:56:22 -050015 // --------------------------------------------------------------------
Andrey Andreev99b782d2012-06-09 22:24:46 +030016
Greg Akerd92277d2011-04-22 12:56:22 -050017 public function test_read_file()
18 {
19 $this->assertFalse(read_file('does_not_exist'));
Andrey Andreev99b782d2012-06-09 22:24:46 +030020
Greg Akerd92277d2011-04-22 12:56:22 -050021 $content = 'Jack and Jill went up the mountain to fight a billy goat.';
Andrey Andreev99b782d2012-06-09 22:24:46 +030022
23 $file = vfsStream::newFile('my_file.txt')->withContent($content)->at($this->_test_dir);
24
Greg Akerd92277d2011-04-22 12:56:22 -050025 $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 Andreev99b782d2012-06-09 22:24:46 +030033
Greg Akerd92277d2011-04-22 12:56:22 -050034 $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content)
Andrey Andreev99b782d2012-06-09 22:24:46 +030035 ->lastModified(time() - 86400)
36 ->at($this->_test_dir);
37
38 $this->assertEquals('777', octal_permissions($file->getPermissions()));
Greg Akerd92277d2011-04-22 12:56:22 -050039 }
40
Andrey Andreev99b782d2012-06-09 22:24:46 +030041 // --------------------------------------------------------------------
42
Greg Akerd92277d2011-04-22 12:56:22 -050043 /**
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 Andreev99b782d2012-06-09 22:24:46 +030049
Greg Akerd92277d2011-04-22 12:56:22 -050050 $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content)
Andrey Andreev99b782d2012-06-09 22:24:46 +030051 ->lastModified(time() - 86400)
52 ->at($this->_test_dir);
53
54 $this->assertEquals('urwxrwxrwx', symbolic_permissions($file->getPermissions()));
Greg Akerd92277d2011-04-22 12:56:22 -050055 }
56
Andrey Andreev99b782d2012-06-09 22:24:46 +030057 // --------------------------------------------------------------------
58
Greg Akerd92277d2011-04-22 12:56:22 -050059 public function test_get_mime_by_extension()
60 {
61 $content = 'Jack and Jill went up the mountain to fight a billy goat.';
Andrey Andreev99b782d2012-06-09 22:24:46 +030062
Greg Akerd92277d2011-04-22 12:56:22 -050063 $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content)
Andrey Andreev99b782d2012-06-09 22:24:46 +030064 ->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 Akerd92277d2011-04-22 12:56:22 -050070 $file = vfsStream::newFile('foo.png')->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +030071
72 $this->assertEquals('image/png', get_mime_by_extension(vfsStream::url('foo.png')));
73
Greg Akerd92277d2011-04-22 12:56:22 -050074 // Test a file not in the mimes array
75 $file = vfsStream::newFile('foo.blarfengar')->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +030076
Greg Akerd92277d2011-04-22 12:56:22 -050077 $this->assertFalse(get_mime_by_extension(vfsStream::url('foo.blarfengar')));
78 }
79
Andrey Andreev99b782d2012-06-09 22:24:46 +030080 // --------------------------------------------------------------------
Greg Akerd92277d2011-04-22 12:56:22 -050081
82 public function test_get_file_info()
83 {
84 // Test Bad File
85 $this->assertFalse(get_file_info('i_am_bad_boo'));
Andrey Andreev99b782d2012-06-09 22:24:46 +030086
Greg Akerd92277d2011-04-22 12:56:22 -050087 // Test the rest
Andrey Andreev99b782d2012-06-09 22:24:46 +030088
Greg Akerd92277d2011-04-22 12:56:22 -050089 // First pass in an array
90 $vals = array(
91 'name', 'server_path', 'size', 'date',
Andrey Andreev99b782d2012-06-09 22:24:46 +030092 'readable', 'writable', 'executable', 'fileperms'
Greg Akerd92277d2011-04-22 12:56:22 -050093 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030094
Greg Akerd92277d2011-04-22 12:56:22 -050095 $this->_test_get_file_info($vals);
96
97 // Test passing in vals as a string.
Andrey Andreev99b782d2012-06-09 22:24:46 +030098 $this->_test_get_file_info(implode(', ', $vals));
Greg Akerd92277d2011-04-22 12:56:22 -050099 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300100
Greg Akerd92277d2011-04-22 12:56:22 -0500101 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 Andreev99b782d2012-06-09 22:24:46 +0300105
Greg Akerd92277d2011-04-22 12:56:22 -0500106 $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content)
Andrey Andreev99b782d2012-06-09 22:24:46 +0300107 ->lastModified($last_modified)
108 ->at($this->_test_dir);
109
Greg Akerd92277d2011-04-22 12:56:22 -0500110 $ret_values = array(
Andrey Andreev99b782d2012-06-09 22:24:46 +0300111 'name' => 'my_file.txt',
112 'server_path' => 'vfs://my_file.txt',
113 'size' => 57,
114 'date' => $last_modified,
Greg Akerd92277d2011-04-22 12:56:22 -0500115 'readable' => TRUE,
Andrey Andreev99b782d2012-06-09 22:24:46 +0300116 'writable' => TRUE,
117 'executable' => TRUE,
Greg Akerd92277d2011-04-22 12:56:22 -0500118 'fileperms' => 33279
119 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300120
Greg Akerd92277d2011-04-22 12:56:22 -0500121 $info = get_file_info(vfsStream::url('my_file.txt'), $vals);
Andrey Andreev99b782d2012-06-09 22:24:46 +0300122
Greg Akerd92277d2011-04-22 12:56:22 -0500123 foreach ($info as $k => $v)
124 {
125 $this->assertEquals($ret_values[$k], $v);
126 }
127 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300128
129 // --------------------------------------------------------------------
Greg Akerd92277d2011-04-22 12:56:22 -0500130
131 // Skipping for now, as it's not implemented in vfsStreamWrapper
132 // flock(): vfsStreamWrapper::stream_lock is not implemented!
Andrey Andreev99b782d2012-06-09 22:24:46 +0300133
Greg Akerd92277d2011-04-22 12:56:22 -0500134 // public function test_write_file()
135 // {
Andrey Andreev99b782d2012-06-09 22:24:46 +0300136 // 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 Akerd92277d2011-04-22 12:56:22 -0500149 // }
150
Andrey Andreev99b782d2012-06-09 22:24:46 +0300151 // --------------------------------------------------------------------
152
Greg Akerd92277d2011-04-22 12:56:22 -0500153}