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
|
<?php
/**
* Copyright (c) 2013 Georg Ehrke <georg@ownCloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test;
class Preview extends \PHPUnit_Framework_TestCase {
public function testIsPreviewDeleted() {
$user = $this->initFS();
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$samplefile = '/'.$user.'/files/test.txt';
$rootView->file_put_contents($samplefile, 'dummy file data');
$x = 50;
$y = 50;
$preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y);
$preview->getPreview();
$fileinfo = $rootView->getFileInfo($samplefile);
$fileid = $fileinfo['fileid'];
$thumbcachefile = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $x . '-' . $y . '.png';
$this->assertEquals($rootView->file_exists($thumbcachefile), true);
$preview->deletePreview();
$this->assertEquals($rootView->file_exists($thumbcachefile), false);
}
public function testAreAllPreviewsDeleted() {
$user = $this->initFS();
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$samplefile = '/'.$user.'/files/test.txt';
$rootView->file_put_contents($samplefile, 'dummy file data');
$x = 50;
$y = 50;
$preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y);
$preview->getPreview();
$fileinfo = $rootView->getFileInfo($samplefile);
$fileid = $fileinfo['fileid'];
$thumbcachefolder = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/';
$this->assertEquals($rootView->is_dir($thumbcachefolder), true);
$preview->deleteAllPreviews();
$this->assertEquals($rootView->is_dir($thumbcachefolder), false);
}
public function testIsMaxSizeWorking() {
$user = $this->initFS();
$maxX = 250;
$maxY = 250;
\OC_Config::setValue('preview_max_x', $maxX);
\OC_Config::setValue('preview_max_y', $maxY);
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$samplefile = '/'.$user.'/files/test.txt';
$rootView->file_put_contents($samplefile, 'dummy file data');
$preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000);
$image = $preview->getPreview();
$this->assertEquals($image->width(), $maxX);
$this->assertEquals($image->height(), $maxY);
}
public function txtBlacklist() {
$txt = 'random text file';
$ics = file_get_contents(__DIR__ . '/../data/testcal.ics');
$vcf = file_get_contents(__DIR__ . '/../data/testcontact.vcf');
return array(
array('txt', $txt, false),
array('ics', $ics, true),
array('vcf', $vcf, true),
);
}
/**
* @dataProvider txtBlacklist
*/
public function testIsTransparent($extension, $data, $expectedResult) {
$user = $this->initFS();
$rootView = new \OC\Files\View('');
$rootView->mkdir('/'.$user);
$rootView->mkdir('/'.$user.'/files');
$x = 32;
$y = 32;
$sample = '/'.$user.'/files/test.'.$extension;
$rootView->file_put_contents($sample, $data);
$preview = new \OC\Preview($user, 'files/', 'test.'.$extension, $x, $y);
$image = $preview->getPreview();
$resource = $image->resource();
//http://stackoverflow.com/questions/5702953/imagecolorat-and-transparency
$colorIndex = imagecolorat($resource, 1, 1);
$colorInfo = imagecolorsforindex($resource, $colorIndex);
$this->assertEquals(
$expectedResult,
$colorInfo['alpha'] === 127,
'Failed asserting that only previews for text files are transparent.'
);
}
private function initFS() {
// create a new user with his own filesystem view
// this gets called by each test in this test class
$user=uniqid();
\OC_User::setUserId($user);
\OC\Files\Filesystem::init($user, '/'.$user.'/files');
\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
return $user;
}
}
|