summaryrefslogtreecommitdiffstats
path: root/application/test/tests/test_service_files.php
blob: 401ddbe862af84ff0f70da87b05e50943f092a64 (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
<?php
/*
 * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace test\tests;

class test_service_files extends \test\Test {

	public function __construct()
	{
		parent::__construct();

		$CI =& get_instance();
		$CI->load->model("muser");
		$CI->load->model("mfile");

	}

	public function test_verify_uploaded_files_noFiles()
	{
		$a = array();
		try {
			\service\files::verify_uploaded_files($a);
			$this->t->fail("verify should error");
		} catch (\exceptions\UserInputException $e) {
			$this->t->is($e->get_error_id(), "file/no-file", "verify should error");
		}
	}

	public function test_verify_uploaded_files_normal()
	{
		$CI =& get_instance();
		$a = array(
			array(
				"name" => "foobar.txt",
				"type" => "text/plain",
				"tmp_name" => NULL,
				"error" => UPLOAD_ERR_OK,
				"size" => 1,
				"formfield" => "file[1]",
			)
		);

		\service\files::verify_uploaded_files($a);
		$this->t->pass("verify should work");
	}

	public function test_verify_uploaded_files_uploadError()
	{
		$CI =& get_instance();
		$a = array(
			array(
				"name" => "foobar.txt",
				"type" => "text/plain",
				"tmp_name" => NULL,
				"error" => UPLOAD_ERR_NO_FILE,
				"size" => 1,
				"formfield" => "file[1]",
			)
		);

		try {
			\service\files::verify_uploaded_files($a);
			$this->t->fail("verify should error");
		} catch (\exceptions\UserInputException $e) {
			$data = $e->get_data();
			$this->t->is($e->get_error_id(), "file/upload-verify", "verify should error");
			$this->t->is_deeply(array(
				'file[1]' => array(
					'filename' => 'foobar.txt',
					'formfield' => 'file[1]',
					'message' => 'No file was uploaded',
				),
			), $data, "expected data in exception");
		}
	}

	public function test_ellipsize()
	{
		$a1 = "abc";
		$a2 = "abc\nabc";
		$a3 = "abc\nabc\nabc";
		$a4 = "abc\nabc\nabc\nabc";

		$this->t->is(\service\files::ellipsize($a1, 1, strlen($a1)),
			$a1, "Trim 1 line to 1, no change");

		$this->t->is(\service\files::ellipsize($a3, 3, strlen($a3)),
			$a3, "Trim 3 lines to 3, no change");

		$this->t->is(\service\files::ellipsize($a3, 5, strlen($a3)),
			$a3, "Trim 3 lines to 5, no change");

		$this->t->is(\service\files::ellipsize($a2, 1, strlen($a2)),
			"$a1\n...", "Trim 2 lines to 1, drop one line");

		$this->t->is(\service\files::ellipsize($a3, 2, strlen($a3)),
			"$a2\n...", "Trim 3 lines to 2, drop one line");

		$this->t->is(\service\files::ellipsize($a4, 2, strlen($a4)),
			"$a2\n...", "Trim 4 lines to 2, drop 2 lines");

		$this->t->is(\service\files::ellipsize($a3, 3, strlen($a3) + 1),
			"$a2\n...", "Last line incomplete, drop one line");

		$this->t->is(\service\files::ellipsize($a1, 5, strlen($a1) + 1),
			"$a1 ...", "Single line incomplete, only add dots");
	}


}