aboutsummaryrefslogtreecommitdiffstats
path: root/.github/workflows/block-outdated-3rdparty.yml
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2024-04-05 14:53:30 +0200
committerJoas Schilling <coding@schilljs.com>2024-04-05 16:12:54 +0200
commitabf95a4cb20c9f346aa2e243be1fa2cd92121b28 (patch)
tree75c20a5842781e3bb1b23d552653a1e273591a3b /.github/workflows/block-outdated-3rdparty.yml
parent4178d5c0c9327b5fd3fc61f07551fe72205a8844 (diff)
downloadnextcloud-server-abf95a4cb20c9f346aa2e243be1fa2cd92121b28.tar.gz
nextcloud-server-abf95a4cb20c9f346aa2e243be1fa2cd92121b28.zip
fix(3rdparty): Add a CI job to check 3rdparty integrity
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to '.github/workflows/block-outdated-3rdparty.yml')
-rw-r--r--.github/workflows/block-outdated-3rdparty.yml53
1 files changed, 53 insertions, 0 deletions
diff --git a/.github/workflows/block-outdated-3rdparty.yml b/.github/workflows/block-outdated-3rdparty.yml
new file mode 100644
index 00000000000..e43981afac0
--- /dev/null
+++ b/.github/workflows/block-outdated-3rdparty.yml
@@ -0,0 +1,53 @@
+name: Block merging with outdated 3rdparty/
+
+on:
+ pull_request:
+ types: [opened, ready_for_review, reopened, synchronize]
+
+permissions:
+ contents: read
+
+concurrency:
+ group: block-outdated-3rdparty-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
+
+jobs:
+ block-outdated-3rdparty:
+ name: Block merging with outdated 3rdparty/
+
+ runs-on: ubuntu-latest-low
+
+ steps:
+ - name: Check requirement
+ uses: dorny/paths-filter@4512585405083f25c027a35db413c2b3b9006d50 # v2.11.1
+ id: changes
+ continue-on-error: true
+ with:
+ filters: |
+ src:
+ - '3rdparty'
+ - 'version.php'
+
+ - name: Checkout
+ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
+
+ - name: 3rdparty commit hash on current branch
+ id: actual
+ run: |
+ echo "commit=$(git submodule status | grep ' 3rdparty' | egrep -o '[a-f0-9]{40}')" >> "$GITHUB_OUTPUT"
+
+ - name: Last 3rdparty commit on target branch
+ id: target
+ run: |
+ echo "commit=$(git ls-remote https://github.com/nextcloud/3rdparty ${{ github.base_ref }} | awk '{ print $1}')" >> "$GITHUB_OUTPUT"
+
+ - name: Compare if 3rdparty commits are different
+ run: |
+ echo '3rdparty/ seems to not point to the last commit of the dedicated branch:'
+ echo "Branch has: ${{ steps.actual.outputs.commit }}"
+ echo "${{ github.base_ref }} has: ${{ steps.target.outputs.commit }}"
+
+ - name: Fail if 3rdparty commits are different
+ if: ${{ steps.changes.outputs.src != 'false' && steps.actual.outputs.commit != steps.target.outputs.commit }}
+ run: |
+ exit 1
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
<?php
/**
 * @author Georg Ehrke <georg@owncloud.com>
 * @author Olivier Paroz <owncloud@interfasys.ch>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace Test;

use OC\Files\FileInfo;
use OC\Files\Storage\Temporary;
use OC\Files\View;
use Test\Traits\MountProviderTrait;
use Test\Traits\UserTrait;

/**
 * Class Preview
 *
 * @group DB
 *
 * @package Test
 */
class Preview extends TestCase {
	use UserTrait;
	use MountProviderTrait;

	const TEST_PREVIEW_USER1 = "test-preview-user1";

	/** @var \OC\Files\View */
	private $rootView;
	/**
	 * Note that using 756 with an image with a ratio of 1.6 brings interesting rounding issues
	 *
	 * @var int maximum width allowed for a preview
	 * */
	private $configMaxWidth = 756;
	/** @var int maximum height allowed for a preview */
	private $configMaxHeight = 756;
	private $keepAspect;
	private $scalingUp;

	private $samples = [];
	private $sampleFileId;
	private $sampleFilename;
	private $sampleWidth;
	private $sampleHeight;
	private $maxScaleFactor;
	/** @var int width of the max preview */
	private $maxPreviewWidth;
	/** @var int height of the max preview */
	private $maxPreviewHeight;
	/** @var int height of the max preview, which is the same as the one of the original image */
	private $maxPreviewRatio;
	private $cachedBigger = [];

	/**
	 * Make sure your configuration file doesn't contain any additional providers
	 */
	protected function setUp() {
		parent::setUp();

		$this->createUser(self::TEST_PREVIEW_USER1, self::TEST_PREVIEW_USER1);
		$this->loginAsUser(self::TEST_PREVIEW_USER1);

		$storage = new \OC\Files\Storage\Temporary([]);
		\OC\Files\Filesystem::mount($storage, [], '/' . self::TEST_PREVIEW_USER1 . '/');

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

		// We simulate the max dimension set in the config
		\OC::$server->getConfig()
			->setSystemValue('preview_max_x', $this->configMaxWidth);
		\OC::$server->getConfig()
			->setSystemValue('preview_max_y', $this->configMaxHeight);
		// Used to test upscaling
		$this->maxScaleFactor = 2;
		\OC::$server->getConfig()
			->setSystemValue('preview_max_scale_factor', $this->maxScaleFactor);

		// We need to enable the providers we're going to use in the tests
		$providers = [
			'OC\\Preview\\JPEG',
			'OC\\Preview\\PNG',
			'OC\\Preview\\GIF',
			'OC\\Preview\\TXT',
			'OC\\Preview\\Postscript'
		];
		\OC::$server->getConfig()
			->setSystemValue('enabledPreviewProviders', $providers);

		// Sample is 1680x1050 JPEG
		$this->prepareSample('testimage.jpg', 1680, 1050);
		// Sample is 2400x1707 EPS
		$this->prepareSample('testimage.eps', 2400, 1707);
		// Sample is 1200x450 PNG
		$this->prepareSample('testimage-wide.png', 1200, 450);
		// Sample is 64x64 GIF
		$this->prepareSample('testimage.gif', 64, 64);
	}

	protected function tearDown() {
		$this->logout();

		parent::tearDown();
	}

	/**
	 * Tests if a preview can be deleted
	 */
	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);
		/** @var int $fileId */
		$fileId = $fileInfo['fileid'];
		$thumbCacheFile = $this->buildCachePath($fileId, $x, $y, true);

		$this->assertSame(
			true, $this->rootView->file_exists($thumbCacheFile), "$thumbCacheFile \n"
		);

		$preview->deletePreview();

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

	/**
	 * Tests if all previews can be deleted
	 *
	 * We test this first to make sure we'll be able to cleanup after each preview generating test
	 */
	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);
		/** @var int $fileId */
		$fileId = $fileInfo['fileid'];

		$thumbCacheFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER .
			'/' . $fileId . '/';

		$this->assertSame(true, $this->rootView->is_dir($thumbCacheFolder), "$thumbCacheFolder \n");

		$preview->deleteAllPreviews();

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

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

		return [
			['txt', $txt, false],
		];
	}

	/**
	 * @dataProvider txtBlacklist
	 *
	 * @param $extension
	 * @param $data
	 * @param $expectedResult
	 */
	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->assertSame(
			$expectedResult,
			$colorInfo['alpha'] === 127,
			'Failed asserting that only previews for text files are transparent.'
		);
	}

	/**
	 * Tests if unsupported previews return an empty object
	 */
	public function testUnsupportedPreviewsReturnEmptyObject() {
		$width = 400;
		$height = 200;

		// Previews for odt files are not enabled
		$imgData = file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.odt');
		$imgPath = '/' . self::TEST_PREVIEW_USER1 . '/files/testimage.odt';
		$this->rootView->file_put_contents($imgPath, $imgData);

		$preview =
			new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.odt', $width, $height);
		$preview->getPreview();
		$image = $preview->getPreview();

		$this->assertSame(false, $image->valid());
	}

	/**
	 * We generate the data to use as it makes it easier to adjust in case we need to test
	 * something different
	 *
	 * @return array
	 */
	public static function dimensionsDataProvider() {
		$data = [];
		$samples = [
			[200, 800],
			[200, 800],
			[50, 400],
			[4, 60],
		];
		$keepAspect = false;
		$scalingUp = false;

		for ($a = 0; $a < sizeof($samples); $a++) {
			for ($b = 0; $b < 2; $b++) {
				for ($c = 0; $c < 2; $c++) {
					for ($d = 0; $d < 4; $d++) {
						$coordinates = [
							[
								-rand($samples[$a][0], $samples[$a][1]),
								-rand($samples[$a][0], $samples[$a][1])
							],
							[
								rand($samples[$a][0], $samples[$a][1]),
								rand($samples[$a][0], $samples[$a][1])
							],
							[
								-rand($samples[$a][0], $samples[$a][1]),
								rand($samples[$a][0], $samples[$a][1])
							],
							[
								rand($samples[$a][0], $samples[$a][1]),
								-rand($samples[$a][0], $samples[$a][1])
							]
						];
						$row = [$a];
						$row[] = $coordinates[$d][0];
						$row[] = $coordinates[$d][1];
						$row[] = $keepAspect;
						$row[] = $scalingUp;
						$data[] = $row;
					}
					$scalingUp = !$scalingUp;
				}
				$keepAspect = !$keepAspect;
			}
		}

		return $data;
	}

	/**
	 * Tests if a preview of max dimensions gets created
	 *
	 * @requires extension imagick
	 * @dataProvider dimensionsDataProvider
	 *
	 * @param int $sampleId
	 * @param int $widthAdjustment
	 * @param int $heightAdjustment
	 * @param bool $keepAspect
	 * @param bool $scalingUp
	 */
	public function testCreateMaxAndNormalPreviewsAtFirstRequest(
		$sampleId, $widthAdjustment, $heightAdjustment, $keepAspect = false, $scalingUp = false
	) {
		//$this->markTestSkipped('Not testing this at this time');

		// Get the right sample for the experiment
		$this->getSample($sampleId);
		$sampleWidth = $this->sampleWidth;
		$sampleHeight = $this->sampleHeight;
		$sampleFileId = $this->sampleFileId;

		// Adjust the requested size so that we trigger various test cases
		$previewWidth = $sampleWidth + $widthAdjustment;
		$previewHeight = $sampleHeight + $heightAdjustment;
		$this->keepAspect = $keepAspect;
		$this->scalingUp = $scalingUp;

		// Generates the max preview
		$preview = $this->createPreview($previewWidth, $previewHeight);

		// There should be no cached thumbnails
		$thumbnailFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER .
			'/' . $sampleFileId;
		$this->assertSame(false, $this->rootView->is_dir($thumbnailFolder));

		$image = $preview->getPreview();
		$this->assertNotSame(false, $image);

		$maxThumbCacheFile = $this->buildCachePath(
			$sampleFileId, $this->maxPreviewWidth, $this->maxPreviewHeight, true, '-max'
		);

		$this->assertSame(
			true, $this->rootView->file_exists($maxThumbCacheFile), "$maxThumbCacheFile \n"
		);

		// We check the dimensions of the file we've just stored
		$maxPreview = imagecreatefromstring($this->rootView->file_get_contents($maxThumbCacheFile));

		$this->assertEquals($this->maxPreviewWidth, imagesx($maxPreview));
		$this->assertEquals($this->maxPreviewHeight, imagesy($maxPreview));

		// A thumbnail of the asked dimensions should also have been created (within the constraints of the max preview)
		list($limitedPreviewWidth, $limitedPreviewHeight) =
			$this->simulatePreviewDimensions($previewWidth, $previewHeight);

		$actualWidth = $image->width();
		$actualHeight = $image->height();

		$this->assertEquals(
			(int)$limitedPreviewWidth, $image->width(), "$actualWidth x $actualHeight \n"
		);
		$this->assertEquals((int)$limitedPreviewHeight, $image->height());

		// And it should be cached
		$this->checkCache($sampleFileId, $limitedPreviewWidth, $limitedPreviewHeight);

		$preview->deleteAllPreviews();
	}

	/**
	 * Tests if the second preview will be based off the cached max preview
	 *
	 * @requires extension imagick
	 * @dataProvider dimensionsDataProvider
	 *
	 * @param int $sampleId
	 * @param int $widthAdjustment
	 * @param int $heightAdjustment
	 * @param bool $keepAspect
	 * @param bool $scalingUp
	 */
	public function testSecondPreviewsGetCachedMax(
		$sampleId, $widthAdjustment, $heightAdjustment, $keepAspect = false, $scalingUp = false
	) {
		//$this->markTestSkipped('Not testing this at this time');

		$this->getSample($sampleId);
		$sampleWidth = $this->sampleWidth;
		$sampleHeight = $this->sampleHeight;
		$sampleFileId = $this->sampleFileId;

		//Creates the Max preview which will be used in the rest of the test
		$this->createMaxPreview();

		// Adjust the requested size so that we trigger various test cases
		$previewWidth = $sampleWidth + $widthAdjustment;
		$previewHeight = $sampleHeight + $heightAdjustment;
		$this->keepAspect = $keepAspect;
		$this->scalingUp = $scalingUp;

		$preview = $this->createPreview($previewWidth, $previewHeight);

		// A cache query should return the thumbnail of max dimension
		$isCached = $preview->isCached($sampleFileId);
		$cachedMaxPreview = $this->buildCachePath(
			$sampleFileId, $this->maxPreviewWidth, $this->maxPreviewHeight, false, '-max'
		);
		$this->assertSame($cachedMaxPreview, $isCached);
	}

	/**
	 * Make sure that the max preview can never be deleted
	 *
	 * For this test to work, the preview we generate first has to be the size of max preview
	 */
	public function testMaxPreviewCannotBeDeleted() {
		//$this->markTestSkipped('Not testing this at this time');

		$this->keepAspect = true;
		$this->getSample(0);
		$fileId = $this->sampleFileId;

		//Creates the Max preview which we will try to delete
		$preview = $this->createMaxPreview();

		// We try to deleted the preview
		$preview->deletePreview();
		$this->assertNotSame(false, $preview->isCached($fileId));

		$preview->deleteAllPreviews();
	}

	public static function aspectDataProvider() {
		$data = [];
		$samples = 4;
		$keepAspect = false;
		$scalingUp = false;
		for ($a = 0; $a < $samples; $a++) {
			for ($b = 0; $b < 2; $b++) {
				for ($c = 0; $c < 2; $c++) {
					$row = [$a];
					$row[] = $keepAspect;
					$row[] = $scalingUp;
					$data[] = $row;
					$scalingUp = !$scalingUp;
				}
				$keepAspect = !$keepAspect;
			}
		}

		return $data;
	}

	/**
	 * We ask for a preview larger than what is set in the configuration,
	 * so we should be getting either the max preview or a preview the size
	 * of the dimensions set in the config
	 *
	 * @requires extension imagick
	 * @dataProvider aspectDataProvider
	 *
	 * @param int $sampleId
	 * @param bool $keepAspect
	 * @param bool $scalingUp
	 */
	public function testDoNotCreatePreviewsLargerThanConfigMax(
		$sampleId, $keepAspect = false, $scalingUp = false
	) {
		//$this->markTestSkipped('Not testing this at this time');

		$this->getSample($sampleId);

		//Creates the Max preview which will be used in the rest of the test
		$this->createMaxPreview();

		// Now we will create the real preview
		$previewWidth = 4000;
		$previewHeight = 4000;
		$this->keepAspect = $keepAspect;
		$this->scalingUp = $scalingUp;

		// Tries to create the very large preview
		$preview = $this->createPreview($previewWidth, $previewHeight);

		$image = $preview->getPreview();
		$this->assertNotSame(false, $image);

		list($expectedWidth, $expectedHeight) =
			$this->simulatePreviewDimensions($previewWidth, $previewHeight);
		$this->assertEquals($expectedWidth, $image->width());
		$this->assertEquals($expectedHeight, $image->height());

		// A preview of the asked size should not have been created since it's larger that our max dimensions
		$postfix = $this->getThumbnailPostfix($previewWidth, $previewHeight);
		$thumbCacheFile = $this->buildCachePath(
			$this->sampleFileId, $previewWidth, $previewHeight, false, $postfix
		);
		$this->assertSame(
			false, $this->rootView->file_exists($thumbCacheFile), "$thumbCacheFile \n"
		);

		$preview->deleteAllPreviews();
	}

	/**
	 * Makes sure we're getting the proper cached thumbnail
	 *
	 * When we start by generating a preview which keeps the aspect ratio
	 * 200-125-with-aspect
	 * 300-300    ✓
	 *
	 * When we start by generating a preview of exact dimensions
	 * 200-200    ✓
	 * 300-188-with-aspect
	 *
	 * @requires extension imagick
	 * @dataProvider aspectDataProvider
	 *
	 * @param int $sampleId
	 * @param bool $keepAspect
	 * @param bool $scalingUp
	 */
	public function testIsBiggerWithAspectRatioCached(
		$sampleId, $keepAspect = false, $scalingUp = false
	) {
		//$this->markTestSkipped('Not testing this at this time');

		$previewWidth = 400;
		$previewHeight = 400;
		$this->getSample($sampleId);
		$fileId = $this->sampleFileId;
		$this->keepAspect = $keepAspect;
		$this->scalingUp = $scalingUp;

		// Caching the max preview in our preview array for the test
		$this->cachedBigger[] = $this->buildCachePath(
			$fileId, $this->maxPreviewWidth, $this->maxPreviewHeight, false, '-max'
		);

		$this->getSmallerThanMaxPreview($fileId, $previewWidth, $previewHeight);
		// We switch the aspect ratio, to generate a thumbnail we should not be picked up
		$this->keepAspect = !$keepAspect;
		$this->getSmallerThanMaxPreview($fileId, $previewWidth + 100, $previewHeight + 100);

		// Small thumbnails are always cropped
		$this->keepAspect = false;
		// Smaller previews should be based on the previous, larger preview, with the correct aspect ratio
		$this->createThumbnailFromBiggerCachedPreview($fileId, 32, 32);

		// 2nd cache query should indicate that we have a cached copy of the exact dimension
		$this->getCachedSmallThumbnail($fileId, 32, 32);

		// We create a preview in order to be able to delete the cache
		$preview = $this->createPreview(rand(), rand());
		$preview->deleteAllPreviews();
		$this->cachedBigger = [];
	}

	/**
	 * Initialises the preview
	 *
	 * @param int $width
	 * @param int $height
	 *
	 * @return \OC\Preview
	 */
	private function createPreview($width, $height) {
		$preview = new \OC\Preview(
			self::TEST_PREVIEW_USER1, 'files/', $this->sampleFilename, $width,
			$height
		);

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

		$preview->setKeepAspect($this->keepAspect);
		$preview->setScalingup($this->scalingUp);

		return $preview;
	}

	/**
	 * Creates the Max preview which will be used in the rest of the test
	 *
	 * @return \OC\Preview
	 */
	private function createMaxPreview() {
		$this->keepAspect = true;
		$preview = $this->createPreview($this->maxPreviewWidth, $this->maxPreviewHeight);
		$preview->getPreview();

		return $preview;
	}

	/**
	 * Makes sure the preview which was just created has been saved to disk
	 *
	 * @param int $fileId
	 * @param int $previewWidth
	 * @param int $previewHeight
	 */
	private function checkCache($fileId, $previewWidth, $previewHeight) {
		$postfix = $this->getThumbnailPostfix($previewWidth, $previewHeight);

		$thumbCacheFile = $this->buildCachePath(
			$fileId, $previewWidth, $previewHeight, true, $postfix
		);

		$this->assertSame(
			true, $this->rootView->file_exists($thumbCacheFile), "$thumbCacheFile \n"
		);
	}

	/**
	 * Computes special filename postfixes
	 *
	 * @param int $width
	 * @param int $height
	 *
	 * @return string
	 */
	private function getThumbnailPostfix($width, $height) {
		// Need to take care of special postfix added to the dimensions
		$postfix = '';
		$isMaxPreview = ($width === $this->maxPreviewWidth
			&& $height === $this->maxPreviewHeight) ? true : false;
		if ($isMaxPreview) {
			$postfix = '-max';
		}
		if ($this->keepAspect && !$isMaxPreview) {
			$postfix = '-with-aspect';
		}

		return $postfix;
	}

	private function getSmallerThanMaxPreview($fileId, $previewWidth, $previewHeight) {
		$preview = $this->createPreview($previewWidth, $previewHeight);

		$image = $preview->getPreview();
		$this->assertNotSame(false, $image);

		// A thumbnail of the asked dimensions should also have been created (within the constraints of the max preview)
		list($limitedPreviewWidth, $limitedPreviewHeight) =
			$this->simulatePreviewDimensions($previewWidth, $previewHeight);

		$this->assertEquals($limitedPreviewWidth, $image->width());
		$this->assertEquals($limitedPreviewHeight, $image->height());

		// And it should be cached
		$this->checkCache($fileId, $limitedPreviewWidth, $limitedPreviewHeight);

		$this->cachedBigger[] = $preview->isCached($fileId);
	}

	private function createThumbnailFromBiggerCachedPreview($fileId, $width, $height) {
		$preview = $this->createPreview($width, $height);

		// A cache query should return a thumbnail of slightly larger dimensions
		// and with the proper aspect ratio
		$isCached = $preview->isCached($fileId);
		$expectedCachedBigger = $this->getExpectedCachedBigger();

		$this->assertSame($expectedCachedBigger, $isCached);

		$image = $preview->getPreview();
		$this->assertNotSame(false, $image);
	}

	/**
	 * Picks the bigger cached preview with the correct aspect ratio or the max preview if it's
	 * smaller than that
	 *
	 * For non-upscaled images, we pick the only picture without aspect ratio
	 *
	 * @return string
	 */
	private function getExpectedCachedBigger() {
		$foundPreview = null;
		$foundWidth = null;
		$foundHeight = null;
		$maxPreview = null;
		$maxWidth = null;
		$maxHeight = null;

		foreach ($this->cachedBigger as $cached) {
			$size = explode('-', basename($cached));
			$width = (int)$size[0];
			$height = (int)$size[1];

			if (strpos($cached, 'max')) {
				$maxWidth = $width;
				$maxHeight = $height;
				$maxPreview = $cached;
				continue;
			}

			// We pick the larger preview with no aspect ratio
			if (!strpos($cached, 'aspect') && !strpos($cached, 'max')) {
				$foundPreview = $cached;
				$foundWidth = $width;
				$foundHeight = $height;
			}
		}
		if ($foundWidth > $maxWidth && $foundHeight > $maxHeight) {
			$foundPreview = $maxPreview;
		}

		return $foundPreview;
	}

	/**
	 * A small thumbnail of exact dimensions should be in the cache
	 *
	 * @param int $fileId
	 * @param int $width
	 * @param int $height
	 */
	private function getCachedSmallThumbnail($fileId, $width, $height) {
		$preview = $this->createPreview($width, $height);

		$isCached = $preview->isCached($fileId);
		$thumbCacheFile = $this->buildCachePath($fileId, $width, $height);

		$this->assertSame($thumbCacheFile, $isCached, "$thumbCacheFile \n");
	}

	/**
	 * Builds the complete path to a cached thumbnail starting from the user folder
	 *
	 * @param int $fileId
	 * @param int $width
	 * @param int $height
	 * @param bool $user
	 * @param string $postfix
	 *
	 * @return string
	 */
	private function buildCachePath($fileId, $width, $height, $user = false, $postfix = '') {
		$userPath = '';
		if ($user) {
			$userPath = '/' . self::TEST_PREVIEW_USER1 . '/';
		}

		return $userPath . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId
		. '/' . $width . '-' . $height . $postfix . '.png';
	}

	/**
	 * Stores the sample in the filesystem and stores it in the $samples array
	 *
	 * @param string $fileName
	 * @param int $sampleWidth
	 * @param int $sampleHeight
	 */
	private function prepareSample($fileName, $sampleWidth, $sampleHeight) {
		$imgData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $fileName);
		$imgPath = '/' . self::TEST_PREVIEW_USER1 . '/files/' . $fileName;
		$this->rootView->file_put_contents($imgPath, $imgData);
		$fileInfo = $this->rootView->getFileInfo($imgPath);

		list($maxPreviewWidth, $maxPreviewHeight) =
			$this->setMaxPreview($sampleWidth, $sampleHeight);

		$this->samples[] =
			[
				'sampleFileId' => $fileInfo['fileid'],
				'sampleFileName' => $fileName,
				'sampleWidth' => $sampleWidth,
				'sampleHeight' => $sampleHeight,
				'maxPreviewWidth' => $maxPreviewWidth,
				'maxPreviewHeight' => $maxPreviewHeight
			];
	}

	/**
	 * Sets the variables used to define the boundaries which need to be respected when using a
	 * specific sample
	 *
	 * @param $sampleId
	 */
	private function getSample($sampleId) {
		// Corrects a rounding difference when using the EPS (Imagick converted) sample
		$filename = $this->samples[$sampleId]['sampleFileName'];
		$splitFileName = pathinfo($filename);
		$extension = $splitFileName['extension'];
		$correction = ($extension === 'eps') ? 1 : 0;
		$maxPreviewHeight = $this->samples[$sampleId]['maxPreviewHeight'];
		$maxPreviewHeight = $maxPreviewHeight - $correction;

		$this->sampleFileId = $this->samples[$sampleId]['sampleFileId'];
		$this->sampleFilename = $this->samples[$sampleId]['sampleFileName'];
		$this->sampleWidth = $this->samples[$sampleId]['sampleWidth'];
		$this->sampleHeight = $this->samples[$sampleId]['sampleHeight'];
		$this->maxPreviewWidth = $this->samples[$sampleId]['maxPreviewWidth'];
		$this->maxPreviewHeight = $maxPreviewHeight;
		$ratio = $this->maxPreviewWidth / $this->maxPreviewHeight;
		$this->maxPreviewRatio = $ratio;
	}

	/**
	 * Defines the size of the max preview
	 *
	 * @fixme the Imagick previews don't have the exact same size on disk as they're calculated here
	 *
	 * @param int $sampleWidth
	 * @param int $sampleHeight
	 *
	 * @return array
	 */
	private function setMaxPreview($sampleWidth, $sampleHeight) {
		// Max previews are never scaled up
		$this->scalingUp = false;
		// Max previews always keep the aspect ratio
		$this->keepAspect = true;
		// We set this variable in order to be able to calculate the max preview with the proper aspect ratio
		$this->maxPreviewRatio = $sampleWidth / $sampleHeight;
		$maxPreviewWidth = min($sampleWidth, $this->configMaxWidth);
		$maxPreviewHeight = min($sampleHeight, $this->configMaxHeight);
		list($maxPreviewWidth, $maxPreviewHeight) =
			$this->applyAspectRatio($maxPreviewWidth, $maxPreviewHeight);

		return [$maxPreviewWidth, $maxPreviewHeight];
	}

	/**
	 * Calculates the expected dimensions of the preview to be able to assess if we've got the
	 * right result
	 *
	 * @param int $askedWidth
	 * @param int $askedHeight
	 *
	 * @return array
	 */
	private function simulatePreviewDimensions($askedWidth, $askedHeight) {
		$askedWidth = min($askedWidth, $this->configMaxWidth);
		$askedHeight = min($askedHeight, $this->configMaxHeight);

		if ($this->keepAspect) {
			// Defines the box in which the preview has to fit
			$scaleFactor = $this->scalingUp ? $this->maxScaleFactor : 1;
			$newPreviewWidth = min($askedWidth, $this->maxPreviewWidth * $scaleFactor);
			$newPreviewHeight = min($askedHeight, $this->maxPreviewHeight * $scaleFactor);
			list($newPreviewWidth, $newPreviewHeight) =
				$this->applyAspectRatio($newPreviewWidth, $newPreviewHeight);
		} else {
			list($newPreviewWidth, $newPreviewHeight) =
				$this->fixSize($askedWidth, $askedHeight);
		}

		return [(int)$newPreviewWidth, (int)$newPreviewHeight];
	}

	/**
	 * Resizes the boundaries to match the aspect ratio
	 *
	 * @param int $askedWidth
	 * @param int $askedHeight
	 *
	 * @return \int[]
	 */
	private function applyAspectRatio($askedWidth, $askedHeight) {
		$originalRatio = $this->maxPreviewRatio;
		if ($askedWidth / $originalRatio < $askedHeight) {
			$askedHeight = round($askedWidth / $originalRatio);
		} else {
			$askedWidth = round($askedHeight * $originalRatio);
		}

		return [(int)$askedWidth, (int)$askedHeight];
	}

	/**
	 * Clips or stretches the dimensions so that they fit in the boundaries
	 *
	 * @param int $askedWidth
	 * @param int $askedHeight
	 *
	 * @return array
	 */
	private function fixSize($askedWidth, $askedHeight) {
		if ($this->scalingUp) {
			$askedWidth = min($this->configMaxWidth, $askedWidth);
			$askedHeight = min($this->configMaxHeight, $askedHeight);
		}

		return [(int)$askedWidth, (int)$askedHeight];
	}

	public function testKeepAspectRatio() {
		$originalWidth = 1680;
		$originalHeight = 1050;
		$originalAspectRation = $originalWidth / $originalHeight;

		$preview = new \OC\Preview(
			self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg',
			150,
			150
		);
		$preview->setKeepAspect(true);
		$image = $preview->getPreview();

		$aspectRatio = $image->width() / $image->height();
		$this->assertEquals(round($originalAspectRation, 2), round($aspectRatio, 2));

		$this->assertLessThanOrEqual(150, $image->width());
		$this->assertLessThanOrEqual(150, $image->height());
	}

	public function testKeepAspectRatioCover() {
		$originalWidth = 1680;
		$originalHeight = 1050;
		$originalAspectRation = $originalWidth / $originalHeight;

		$preview = new \OC\Preview(
			self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg',
			150,
			150
		);
		$preview->setKeepAspect(true);
		$preview->setMode(\OC\Preview::MODE_COVER);
		$image = $preview->getPreview();

		$aspectRatio = $image->width() / $image->height();
		$this->assertEquals(round($originalAspectRation, 2), round($aspectRatio, 2));

		$this->assertGreaterThanOrEqual(150, $image->width());
		$this->assertGreaterThanOrEqual(150, $image->height());
	}

	public function testSetFileWithInfo() {
		$info = new FileInfo('/foo', null, '/foo', ['mimetype' => 'foo/bar'], null);
		$preview = new \OC\Preview();
		$preview->setFile('/foo', $info);
		$this->assertEquals($info, $this->invokePrivate($preview, 'getFileInfo'));
	}

	public function testIsCached() {
		$sourceFile = __DIR__ . '/../data/testimage.png';
		$userId = $this->getUniqueID();
		$this->createUser($userId, 'pass');

		$storage = new Temporary();
		$storage->mkdir('files');
		$this->registerMount($userId, $storage, '/' . $userId);

		\OC_Util::tearDownFS();
		\OC_Util::setupFS($userId);
		$preview = new \OC\Preview($userId, 'files');
		$view = new View('/' . $userId . '/files');
		$view->file_put_contents('test.png', file_get_contents($sourceFile));
		$info = $view->getFileInfo('test.png');
		$preview->setFile('test.png', $info);

		$preview->setMaxX(64);
		$preview->setMaxY(64);

		$this->assertFalse($preview->isCached($info->getId()));

		$preview->getPreview();

		$this->assertEquals('thumbnails/' . $info->getId() . '/64-64.png', $preview->isCached($info->getId()));
	}
}