summaryrefslogtreecommitdiffstats
path: root/tests/lib/preview.php
blob: 20e4209dedfcbb1da9223d328347528b7c04bec2 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?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 TestCase {

	const TEST_PREVIEW_USER1 = "test-preview-user1";

	/**
	 * @var \OC\Files\View
	 */
	private $rootView;

	/** @var \OC\Files\Storage\Storage */
	private $originalStorage;

	protected function setUp() {
		parent::setUp();

		// FIXME: use proper tearDown with $this->loginAsUser() and $this->logout()
		// (would currently break the tests for some reason)
		$this->originalStorage = \OC\Files\Filesystem::getStorage('/');

		// create a new user with his own filesystem view
		// this gets called by each test in this test class
		$backend = new \OC_User_Dummy();
		\OC_User::useBackend($backend);
		$backend->createUser(self::TEST_PREVIEW_USER1, self::TEST_PREVIEW_USER1);
		$user = \OC::$server->getUserManager()->get(self::TEST_PREVIEW_USER1);
		\OC::$server->getUserSession()->setUser($user);
		\OC\Files\Filesystem::init(self::TEST_PREVIEW_USER1, '/' . self::TEST_PREVIEW_USER1 . '/files');

		\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');

		$this->rootView = new \OC\Files\View('');
		$this->rootView->mkdir('/'.self::TEST_PREVIEW_USER1);
		$this->rootView->mkdir('/'.self::TEST_PREVIEW_USER1.'/files');
	}

	protected function tearDown() {
		\OC\Files\Filesystem::clearMounts();
		\OC\Files\Filesystem::mount($this->originalStorage, array(), '/');

		parent::tearDown();
	}

	public function testIsMaxSizeWorking() {
		// Max size from config
		$maxX = 1024;
		$maxY = 1024;

		\OC::$server->getConfig()->setSystemValue('preview_max_x', $maxX);
		\OC::$server->getConfig()->setSystemValue('preview_max_y', $maxY);

		// Sample is 1680x1050 JPEG
		$sampleFile = '/' . self::TEST_PREVIEW_USER1 . '/files/testimage.jpg';
		$this->rootView->file_put_contents($sampleFile, file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
		$fileInfo = $this->rootView->getFileInfo($sampleFile);
		$fileId = $fileInfo['fileid'];

		$largeX = 1920;
		$largeY = 1080;
		$preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', $largeX, $largeY);

		$this->assertEquals($preview->isFileValid(), true);

		// There should be no cached copy
		$isCached = $preview->isCached($fileId);

		$this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '-max.png', $isCached);
		$this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);
		$this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $largeX . '-' . $largeY . '.png', $isCached);

		// The returned preview should be of max size
		$image = $preview->getPreview();

		$this->assertEquals($image->width(), $maxX);
		$this->assertEquals($image->height(), $maxY);

		// The max thumbnail should be created
		$maxThumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '-max.png';

		$this->assertEquals($this->rootView->file_exists($maxThumbCacheFile), true);

		// A preview of the asked size should not have been created
		$thumbCacheFile = \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $largeX . '-' . $largeY . '.png';

		$this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);

		// 2nd request should indicate that we have a cached copy of max dimension
		$isCached = $preview->isCached($fileId);
		$this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);

		// Smaller previews should be based on the cached max preview
		$smallX = 50;
		$smallY = 50;
		$preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', $smallX, $smallY);
		$isCached = $preview->isCached($fileId);

		$this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);

		// A small preview should be created
		$image = $preview->getPreview();
		$this->assertEquals($image->width(), $smallX);
		$this->assertEquals($image->height(), $smallY);

		// The cache should contain the small preview
		$thumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $smallX . '-' . $smallY . '.png';

		$this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);

		// 2nd request should indicate that we have a cached copy of the exact dimension
		$isCached = $preview->isCached($fileId);

		$this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $smallX . '-' . $smallY . '.png', $isCached);
	}

	public function testIsPreviewDeleted() {

		$sampleFile = '/'.self::TEST_PREVIEW_USER1.'/files/test.txt';

		$this->rootView->file_put_contents($sampleFile, 'dummy file data');
		
		$x = 50;
		$y = 50;

		$preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.txt', $x, $y);
		$preview->getPreview();

		$fileInfo = $this->rootView->getFileInfo($sampleFile);
		$fileId = $fileInfo['fileid'];

		$thumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
		
		$this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);

		$preview->deletePreview();

		$this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
	}

	public function testAreAllPreviewsDeleted() {

		$sampleFile = '/'.self::TEST_PREVIEW_USER1.'/files/test.txt';

		$this->rootView->file_put_contents($sampleFile, 'dummy file data');
		
		$x = 50;
		$y = 50;

		$preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.txt', $x, $y);
		$preview->getPreview();

		$fileInfo = $this->rootView->getFileInfo($sampleFile);
		$fileId = $fileInfo['fileid'];
		
		$thumbCacheFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/';
		
		$this->assertEquals($this->rootView->is_dir($thumbCacheFolder), true);

		$preview->deleteAllPreviews();

		$this->assertEquals($this->rootView->is_dir($thumbCacheFolder), false);
	}

	public function txtBlacklist() {
		$txt = 'random text file';

		return array(
			array('txt', $txt, false),
		);
	}

	/**
	 * @dataProvider txtBlacklist
	 */
	public function testIsTransparent($extension, $data, $expectedResult) {

		$x = 32;
		$y = 32;

		$sample = '/'.self::TEST_PREVIEW_USER1.'/files/test.'.$extension;
		$this->rootView->file_put_contents($sample, $data);
		$preview = new \OC\Preview(self::TEST_PREVIEW_USER1, '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.'
		);
	}

	public function testCreationFromCached() {

		$sampleFile = '/'.self::TEST_PREVIEW_USER1.'/files/test.txt';

		$this->rootView->file_put_contents($sampleFile, 'dummy file data');

		// create base preview
		$x = 150;
		$y = 150;

		$preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.txt', $x, $y);
		$preview->getPreview();

		$fileInfo = $this->rootView->getFileInfo($sampleFile);
		$fileId = $fileInfo['fileid'];

		$thumbCacheFile = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';

		$this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);


		// create smaller previews
		$preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'test.txt', 50, 50);
		$isCached = $preview->isCached($fileId);

		$this->assertEquals(self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
	}

	/*
	public function testScalingUp() {

		$sampleFile = '/'.$this->user.'/files/test.txt';

		$this->rootView->file_put_contents($sampleFile, 'dummy file data');

		// create base preview
		$x = 150;
		$y = 150;

		$preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
		$preview->getPreview();

		$fileInfo = $this->rootView->getFileInfo($sampleFile);
		$fileId = $fileInfo['fileid'];

		$thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';

		$this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);


		// create bigger previews - with scale up
		$preview = new \OC\Preview($this->user, 'files/', 'test.txt', 250, 250);
		$isCached = $preview->isCached($fileId);

		$this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
	}
	*/
}