summaryrefslogtreecommitdiffstats
path: root/application/test/tests/test_service_files_valid_id.php
blob: 24886be43bb0ce48d3ed070e92b4011d3eae692c (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
<?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_valid_id extends \test\Test {
	private $model;
	private $filedata;
	private $config;

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

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

	}

	public function init()
	{
		$this->model = \Mockery::mock("Mfile");
		$this->model->shouldReceive("delete_id")->never()->byDefault();
		$this->model->shouldReceive("delete_data_id")->never()->byDefault();
		$this->model->shouldReceive("file")->with("file-hash-1-1")->andReturn("/invalid/path/file-1")->byDefault();
		$this->model->shouldReceive("filemtime")->with("/invalid/path/file-1")->andReturn(500)->byDefault();
		$this->model->shouldReceive("filesize")->with("/invalid/path/file-1")->andReturn(50*1024)->byDefault();
		$this->model->shouldReceive("file_exists")->with("/invalid/path/file-1")->andReturn(true)->byDefault();

		$this->filedata = array(
			"data_id" => "file-hash-1-1",
			"hash" => "file-hash-1",
			"id" => "file-id-1",
			"user" => 2,
			"date" => 500,
		);

		$this->config = array(
			"upload_max_age" => 20,
			"sess_expiration" => 10,
			"small_upload_size" => 10*1024,
		);
	}

	public function cleanup()
	{
		\Mockery::close();
	}

	public function test_valid_id_keepNormalUpload()
	{
		$ret = \service\files::valid_id($this->filedata, $this->config, $this->model, 505);
		$this->t->is($ret, true, "normal case should be valid");
	}

	public function test_valid_id_keepSmallUpload()
	{
		$this->model->shouldReceive("filesize")->with("/invalid/path/file-1")->once()->andReturn(50);

		$ret = \service\files::valid_id($this->filedata, $this->config, $this->model, 550);
		$this->t->is($ret, true, "file is old, but small and should be kept");
	}

	public function test_valid_id_removeOldFile()
	{
		$this->model->shouldReceive("delete_data_id")->with("file-hash-1-1")->once();

		$ret = \service\files::valid_id($this->filedata, $this->config, $this->model, 550);
		$this->t->is($ret, false, "file is old and should be removed");
	}

	public function test_valid_id_removeOldUpload()
	{
		$this->model->shouldReceive("delete_id")->with("file-id-1")->once();
		$this->model->shouldReceive("filemtime")->with("/invalid/path/file-1")->once()->andReturn(540);

		$ret = \service\files::valid_id($this->filedata, $this->config, $this->model, 550);
		$this->t->is($ret, false, "upload is old and should be removed");
	}

	public function test_valid_id_keepNormalUnownedFile()
	{
		$this->filedata["user"] = 0;

		$ret = \service\files::valid_id($this->filedata, $this->config, $this->model, 505);
		$this->t->is($ret, true, "upload is unowned and should be kept");
	}

	public function test_valid_id_removeOldUnownedFile()
	{
		$this->model->shouldReceive("delete_id")->with("file-id-1")->once();
		$this->filedata["user"] = 0;

		$ret = \service\files::valid_id($this->filedata, $this->config, $this->model, 515);
		$this->t->is($ret, false, "upload is old, unowned and should be removed");
	}

	public function test_valid_id_removeMissingFile()
	{
		$this->model->shouldReceive("file_exists")->with("/invalid/path/file-1")->once()->andReturn(false);
		$this->model->shouldReceive("delete_data_id")->with("file-hash-1-1")->once();

		$ret = \service\files::valid_id($this->filedata, $this->config, $this->model, 505);
		$this->t->is($ret, false, "missing file should be removed");
	}

}