summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Upload_test.php
blob: 021af28a70e52eaaf843fcf77648c540284f68ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php

class Upload_test extends CI_TestCase {

	function set_up()
	{
		$obj = new stdClass;
		$obj->upload = new Mock_Libraries_Upload();
		$obj->security = new Mock_Core_Security();
		$obj->lang = new Mock_Core_Lang();

		$this->ci_instance($obj);
		$this->upload = $obj->upload;

		vfsStreamWrapper::register();
		vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir'));

		$this->_test_dir = vfsStreamWrapper::getRoot();
	}

	function test_do_upload()
	{
		$this->markTestIncomplete('TODO');
	}

	function test_data()
	{
		$data = array(
				'file_name'		=> 'hello.txt',
				'file_type'		=> 'text/plain',
				'file_path'		=> '/tmp/',
				'full_path'		=> '/tmp/hello.txt',
				'raw_name'		=> 'hello',
				'orig_name'		=> 'hello.txt',
				'client_name'		=> '',
				'file_ext'		=> '.txt',
				'file_size'		=> 100,
				'is_image'		=> FALSE,
				'image_width'		=> '',
				'image_height'		=> '',
				'image_type'		=> '',
				'image_size_str'	=> ''
			);

		$this->upload->set_upload_path('/tmp/');

		foreach ($data as $k => $v)
		{
			$this->upload->{$k}	= $v;
		}

		$this->assertEquals('hello.txt', $this->upload->data('file_name'));
		$this->assertEquals($data, $this->upload->data());
	}

	function test_set_upload_path()
	{
		$this->upload->set_upload_path('/tmp/');
		$this->assertEquals('/tmp/', $this->upload->upload_path);

		$this->upload->set_upload_path('/tmp');
		$this->assertEquals('/tmp/', $this->upload->upload_path);
	}

	function test_set_filename()
	{
		$this->markTestIncomplete('TODO');
	}

	function test_set_max_filesize()
	{
		$this->upload->set_max_filesize(100);
		$this->assertEquals(100, $this->upload->max_size);
	}	

	function test_set_max_filename()
	{
		$this->upload->set_max_filename(100);
		$this->assertEquals(100, $this->upload->max_filename);
	}

	function test_set_max_width()
	{
		$this->upload->set_max_width(100);
		$this->assertEquals(100, $this->upload->max_width);
	}	

	function test_set_max_height()
	{
		$this->upload->set_max_height(100);
		$this->assertEquals(100, $this->upload->max_height);
	}

	function test_set_allowed_types()
	{
		$this->upload->set_allowed_types('*');
		$this->assertEquals('*', $this->upload->allowed_types);

		$this->upload->set_allowed_types('foo|bar');
		$this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
	}

	function test_set_image_properties()
	{
		$this->markTestIncomplete('TODO');
	}

	function test_set_xss_clean()
	{
		$this->upload->set_xss_clean(TRUE);
		$this->assertTrue($this->upload->xss_clean);

		$this->upload->set_xss_clean(FALSE);
		$this->assertFalse($this->upload->xss_clean);
	}

	function test_is_image()
	{
		$this->upload->file_type = 'image/x-png';
		$this->assertTrue($this->upload->is_image());

		$this->upload->file_type = 'text/plain';
		$this->assertFalse($this->upload->is_image());
	}

	function test_is_allowed_filetype()
	{
		$this->upload->allowed_types = array('html');

		$this->upload->file_ext = '.txt';
		$this->upload->file_type = 'text/plain';
		$this->assertFalse($this->upload->is_allowed_filetype(FALSE));
		$this->assertFalse($this->upload->is_allowed_filetype(TRUE));

		$this->upload->file_ext = '.html';
		$this->upload->file_type = 'text/html';
		$this->assertTrue($this->upload->is_allowed_filetype(FALSE));
		$this->assertTrue($this->upload->is_allowed_filetype(TRUE));

		$this->markTestIncomplete('Image tests');
	}

	function test_is_allowed_filesize()
	{
		$this->upload->max_size = 100;
		$this->upload->file_size = 99;

		$this->assertTrue($this->upload->is_allowed_filesize());

		$this->upload->file_size = 101;
		$this->assertFalse($this->upload->is_allowed_filesize());
	}

	function test_is_allowed_dimensions()
	{
		$this->markTestIncomplete('TODO');
	}

	function test_validate_upload_path()
	{
		$this->upload->upload_path = '';
		$this->assertFalse($this->upload->validate_upload_path());

		$this->upload->upload_path = vfsStream::url('testDir');
		$this->assertTrue($this->upload->validate_upload_path());
	}

	function test_get_extension()
	{
		$this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
		$this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
		$this->assertEquals('', $this->upload->get_extension('hello'));
	}

	function test_clean_file_name()
	{
		$this->assertEquals('hello.txt', $this->upload->clean_file_name('hello.txt'));
		$this->assertEquals('hello.txt', $this->upload->clean_file_name('%253chell>o.txt'));
	}

	function test_limit_filename_length()
	{
		$this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
		$this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
	}

	function test_do_xss_clean()
	{
		$file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir);
		$file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir);
		$file3 = vfsStream::newFile('file3.txt')->withContent('<script type="text/javascript">alert("Boo! said the billy goat")</script>')->at($this->_test_dir);

		$this->upload->file_temp = vfsStream::url('file1.txt');
		$this->assertTrue($this->upload->do_xss_clean());

		$this->upload->file_temp = vfsStream::url('file2.txt');
		$this->assertFalse($this->upload->do_xss_clean());

		$this->upload->file_temp = vfsStream::url('file3.txt');
		$this->assertFalse($this->upload->do_xss_clean());
	}

	function test_set_error()
	{
		$errors = array(
			'An error!'
		);

		$another = 'Another error!';

		$this->upload->set_error($errors);
		$this->assertEquals($errors, $this->upload->error_msg);

		$errors[] = $another;
		$this->upload->set_error($another);
		$this->assertEquals($errors, $this->upload->error_msg);
	}

	function test_display_errors()
	{
		$this->upload->error_msg[] = 'Error test';
		$this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
	}

	function test_mimes_types()
	{
		$this->assertEquals('text/plain', $this->upload->mimes_types('txt'));
		$this->assertFalse($this->upload->mimes_types('foobar'));
	}

}