blob: c318175951126cd27171a54c132a38cb9203c82f [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
Andrey Andreev53d5ee62014-02-17 16:57:48 +020034 $file = vfsStream::newFile('my_file.txt', 0777)
35 ->withContent($content)
36 ->lastModified(time() - 86400)
37 ->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +030038
39 $this->assertEquals('777', octal_permissions($file->getPermissions()));
Greg Akerd92277d2011-04-22 12:56:22 -050040 }
41
Andrey Andreev99b782d2012-06-09 22:24:46 +030042 // --------------------------------------------------------------------
43
Greg Akerd92277d2011-04-22 12:56:22 -050044 /**
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 Andreev99b782d2012-06-09 22:24:46 +030050
Andrey Andreev53d5ee62014-02-17 16:57:48 +020051 $file = vfsStream::newFile('my_file.txt', 0777)
52 ->withContent($content)
53 ->lastModified(time() - 86400)
54 ->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +030055
56 $this->assertEquals('urwxrwxrwx', symbolic_permissions($file->getPermissions()));
Greg Akerd92277d2011-04-22 12:56:22 -050057 }
58
Andrey Andreev99b782d2012-06-09 22:24:46 +030059 // --------------------------------------------------------------------
60
Greg Akerd92277d2011-04-22 12:56:22 -050061 public function test_get_mime_by_extension()
62 {
63 $content = 'Jack and Jill went up the mountain to fight a billy goat.';
Andrey Andreev99b782d2012-06-09 22:24:46 +030064
Andrey Andreev53d5ee62014-02-17 16:57:48 +020065 $file = vfsStream::newFile('my_file.txt', 0777)
66 ->withContent($content)
67 ->lastModified(time() - 86400)
68 ->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +030069
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 Akerd92277d2011-04-22 12:56:22 -050073 $file = vfsStream::newFile('foo.png')->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +030074
75 $this->assertEquals('image/png', get_mime_by_extension(vfsStream::url('foo.png')));
76
Greg Akerd92277d2011-04-22 12:56:22 -050077 // Test a file not in the mimes array
78 $file = vfsStream::newFile('foo.blarfengar')->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +030079
Greg Akerd92277d2011-04-22 12:56:22 -050080 $this->assertFalse(get_mime_by_extension(vfsStream::url('foo.blarfengar')));
81 }
82
Andrey Andreev99b782d2012-06-09 22:24:46 +030083 // --------------------------------------------------------------------
Greg Akerd92277d2011-04-22 12:56:22 -050084
85 public function test_get_file_info()
86 {
87 // Test Bad File
88 $this->assertFalse(get_file_info('i_am_bad_boo'));
Andrey Andreev99b782d2012-06-09 22:24:46 +030089
Greg Akerd92277d2011-04-22 12:56:22 -050090 // Test the rest
Andrey Andreev99b782d2012-06-09 22:24:46 +030091
Greg Akerd92277d2011-04-22 12:56:22 -050092 // First pass in an array
93 $vals = array(
94 'name', 'server_path', 'size', 'date',
Andrey Andreev99b782d2012-06-09 22:24:46 +030095 'readable', 'writable', 'executable', 'fileperms'
Greg Akerd92277d2011-04-22 12:56:22 -050096 );
Andrey Andreev99b782d2012-06-09 22:24:46 +030097
Greg Akerd92277d2011-04-22 12:56:22 -050098 $this->_test_get_file_info($vals);
99
100 // Test passing in vals as a string.
Andrey Andreev99b782d2012-06-09 22:24:46 +0300101 $this->_test_get_file_info(implode(', ', $vals));
Greg Akerd92277d2011-04-22 12:56:22 -0500102 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300103
Greg Akerd92277d2011-04-22 12:56:22 -0500104 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 Andreev99b782d2012-06-09 22:24:46 +0300108
Andrey Andreev53d5ee62014-02-17 16:57:48 +0200109 $file = vfsStream::newFile('my_file.txt', 0777)
110 ->withContent($content)
111 ->lastModified($last_modified)
112 ->at($this->_test_dir);
Andrey Andreev99b782d2012-06-09 22:24:46 +0300113
Greg Akerd92277d2011-04-22 12:56:22 -0500114 $ret_values = array(
Andrey Andreev53d5ee62014-02-17 16:57:48 +0200115 '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 Akerd92277d2011-04-22 12:56:22 -0500123 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300124
Greg Akerd92277d2011-04-22 12:56:22 -0500125 $info = get_file_info(vfsStream::url('my_file.txt'), $vals);
Andrey Andreev99b782d2012-06-09 22:24:46 +0300126
Greg Akerd92277d2011-04-22 12:56:22 -0500127 foreach ($info as $k => $v)
128 {
129 $this->assertEquals($ret_values[$k], $v);
130 }
131 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300132
133 // --------------------------------------------------------------------
Greg Akerd92277d2011-04-22 12:56:22 -0500134
Andrey Andreev53d5ee62014-02-17 16:57:48 +0200135 public function test_write_file()
136 {
137 $content = 'Jack and Jill went up the mountain to fight a billy goat.';
Andrey Andreev99b782d2012-06-09 22:24:46 +0300138
Andrey Andreev53d5ee62014-02-17 16:57:48 +0200139 $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 Akerd92277d2011-04-22 12:56:22 -0500146
Greg Akerd92277d2011-04-22 12:56:22 -0500147}