1
|
|
|
<?php |
2
|
|
|
require_once 'Intraface/functions.php'; |
3
|
|
|
require_once 'Intraface/modules/filemanager/FileHandler.php'; |
4
|
|
|
require_once 'file_functions.php'; |
5
|
|
|
|
6
|
|
|
class FileHandlerTest extends PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
private $file_name = 'tester.jpg'; |
9
|
|
|
|
10
|
|
|
function setUp() |
11
|
|
|
{ |
12
|
|
|
$db = MDB2::singleton(DB_DSN); |
13
|
|
|
$db->query('TRUNCATE file_handler'); |
14
|
|
|
fht_deltree(PATH_UPLOAD . '1'); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function createKernel() |
18
|
|
|
{ |
19
|
|
|
$kernel = new Stub_Kernel; |
20
|
|
|
return $kernel; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
function createFileHandler() |
24
|
|
|
{ |
25
|
|
|
return new FileHandler($this->createKernel()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
function createFile() |
29
|
|
|
{ |
30
|
|
|
$data = array('file_name' => $this->file_name); |
31
|
|
|
$filehandler = $this->createFileHandler(); |
32
|
|
|
$this->assertTrue($filehandler->update($data) > 0); |
33
|
|
|
return $filehandler; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
//////////////////////////////////////////////////////////////// |
37
|
|
|
|
38
|
|
|
function testConstruction() |
39
|
|
|
{ |
40
|
|
|
$filehandler = $this->createFileHandler(); |
41
|
|
|
$this->assertTrue(is_object($filehandler)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function testFactoryReturnsAValidFileHandlerObject() |
45
|
|
|
{ |
46
|
|
|
$fh = $this->createFile(); |
47
|
|
|
$accesskey = $fh->getAccessKey(); |
48
|
|
|
$filehandler = FileHandler::factory($this->createKernel(), $accesskey); |
49
|
|
|
$this->assertTrue(is_object($filehandler)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function testUpdate() |
53
|
|
|
{ |
54
|
|
|
$fh = $this->createFile(); |
55
|
|
|
$this->assertEquals($this->file_name, $fh->get('file_name')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function testDelete() |
59
|
|
|
{ |
60
|
|
|
// @todo how do we test precisely that it is deleted |
61
|
|
|
$fh = $this->createFile(); |
62
|
|
|
$id = $fh->getId(); |
63
|
|
|
|
64
|
|
|
$fh = new FileHandler($this->createKernel(), $id); |
65
|
|
|
$this->assertTrue($fh->delete()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function testUnDelete() |
69
|
|
|
{ |
70
|
|
|
// @todo how do we test precisely that it is undeleted |
71
|
|
|
$fh = $this->createFile(); |
72
|
|
|
$fh->delete(); |
73
|
|
|
$this->assertTrue($fh->undelete()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
function testSave() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$fh = new FileHandler($this->createKernel()); |
79
|
|
|
// first we make a copy of the file as it is moved by upload. |
80
|
|
|
copy(dirname(__FILE__) . '/wideonball.jpg', PATH_UPLOAD.'wideonball.jpg'); |
81
|
|
|
$id = $fh->save(PATH_UPLOAD.'wideonball.jpg', 'Filename'); |
82
|
|
|
$fh->error->view(); |
83
|
|
|
$this->assertTrue($id > 0); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
function testAccessKeyIsValid() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$fh = new FileHandler($this->createKernel()); |
89
|
|
|
// first we make a copy of the file as it is moved by upload. |
90
|
|
|
copy(dirname(__FILE__) . '/wideonball.jpg', PATH_UPLOAD.'wideonball.jpg'); |
91
|
|
|
$id = $fh->save(PATH_UPLOAD.'wideonball.jpg', 'Filename'); |
|
|
|
|
92
|
|
|
$fh->load(); |
93
|
|
|
$this->assertEquals(50, strlen($fh->get('access_key'))); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
function testCreateTemporaryFile() |
97
|
|
|
{ |
98
|
|
|
$fh = new FileHandler($this->createKernel()); |
99
|
|
|
$this->assertEquals('TemporaryFile', get_class($fh->createTemporaryFile())); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.