summaryrefslogtreecommitdiffstats
path: root/.drone.yml
Commit message (Expand)AuthorAgeFilesLines
* Extract acceptance tests for tags to their own feature fileDaniel Calviño Sánchez2018-10-291-0/+9
* Merge pull request #11769 from nextcloud/3rdparty/remove/random_compatRoeland Jago Douma2018-10-111-3/+3
|\
| * Remove random_compatJoas Schilling2018-10-111-3/+3
* | Add 7.3 CI to droneRoeland Jago Douma2018-10-111-0/+53
|/
* Ignore the RiakCacheRoeland Jago Douma2018-10-101-1/+1
* Add syntax ci job for php7.2Roeland Jago Douma2018-10-101-0/+9
* Add drone stepRoeland Jago Douma2018-09-281-0/+9
* extend integration tests with users with numerical IDsArthur Schiwon2018-09-281-1/+13
* Add drone supportRoeland Jago Douma2018-09-271-0/+8
* use a dummy database name for the default postgres database nameRobin Appelman2018-09-171-1/+3
* simplify tests and move some out to a new feature as we need a pristineArthur Schiwon2018-08-091-0/+12
* behat integration tests for LDAP, basic setupArthur Schiwon2018-08-091-0/+22
* Use correct domain for ci-assetsJulius Härtl2018-07-291-1/+1
* Update object storageMorris Jobke2018-07-291-1/+1
* Frontend regression testing with puppeteerJulius Härtl2018-07-291-0/+48
* Add integration tests for searching files by commentsDaniel Calviño Sánchez2018-07-251-0/+10
* use older image for disabled redis testsArthur Schiwon2018-06-301-1/+1
* Applied patch for CI to work per https://github.com/nextcloud/server/pull/976...Johannes Ernst2018-06-291-5/+5
* fix build stepArthur Schiwon2018-06-291-1/+1
* Config set and droneJohn Molakvoæ (skjnldsv)2018-06-251-0/+8
* Add vue build for oauth2 app to droneJulius Härtl2018-06-201-0/+8
* Run vue builds in parallelMorris Jobke2018-06-191-3/+4
* Fixed testsJohn Molakvoæ (skjnldsv)2018-06-121-1/+1
* Add basic acceptance tests for apps managementJulius Härtl2018-06-091-0/+9
* Bump updatenotificationJohn Molakvoæ (skjnldsv)2018-06-071-2/+9
* Add vue apps build testsJohn Molakvoæ (skjnldsv)2018-06-071-0/+8
* add azure unit tests with azuriteRobin Appelman2018-06-041-0/+9
* Fix drone mysqlmb4 testsJohn Molakvoæ (skjnldsv)2018-05-301-1/+1
* Add feature to droneJohn Molakvoæ (skjnldsv)2018-05-161-0/+9
* Bump integration test imageRoeland Jago Douma2018-05-141-29/+29
* Merge pull request #9145 from nextcloud/use-authorized-github-checksMorris Jobke2018-04-111-0/+1
|\
| * Add auth token to github requestsMorris Jobke2018-04-111-0/+1
* | Disable unreliable swift testsMorris Jobke2018-04-111-6/+6
|/
* Merge pull request #9127 from nextcloud/make-possible-to-run-acceptance-tests...Morris Jobke2018-04-091-6/+6
|\
| * Use Docker image for acceptance testsDaniel Calviño Sánchez2018-04-091-6/+6
* | Do not run download scenarios tagged as "large" in DroneDaniel Calviño Sánchez2018-04-061-1/+1
* | Add integration tests for downloading basic zip filesDaniel Calviño Sánchez2018-04-061-0/+10
|/
* Header acceptance featuresJohn Molakvoæ (skjnldsv)2018-03-091-0/+9
* Add v3.0 auth runRoeland Jago Douma2018-02-261-0/+4
* Use a proper wait for create of the containerRoeland Jago Douma2018-02-191-2/+1
* Fix typo in build matrix of .drone.ymlDaniel Calviño Sánchez2018-02-161-1/+1
* Add acceptance tests for comments to DroneDaniel Calviño Sánchez2018-02-151-0/+9
* add ci for swift primary storageRobin Appelman2018-02-081-0/+11
* Bump php7.2 imageRoeland Jago Douma2018-02-051-5/+5
* Use 7.2 imageRoeland Jago Douma2018-02-041-1/+1
* Use a phan version instead of masterRoeland Jago Douma2018-02-041-1/+1
* Update memcached imageMorris Jobke2018-01-251-1/+1
* Bump docker containers usedRoeland Jago Douma2018-01-241-26/+26
* Raise PHP version number to PHP 7.0Morris Jobke2018-01-121-89/+0
* Some tests for the remote cloud apiRobin Appelman2017-12-081-0/+10
id='n599' href='#n599'>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 964 965
<?php
/**
 * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
 *
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 * @author Bjoern Schiessle <bjoern@schiessle.org>
 * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
 * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
 * @author Joas Schilling <coding@schilljs.com>
 * @author Julius Haertl <jus@bitgrid.net>
 * @author Julius Härtl <jus@bitgrid.net>
 * @author Lukas Reschke <lukas@statuscode.ch>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\Theming\Tests\Controller;

use OC\Files\AppData\Factory;
use OC\L10N\L10N;
use OC\Template\SCSSCacher;
use OCA\Theming\Controller\ThemingController;
use OCA\Theming\ImageManager;
use OCA\Theming\ThemingDefaults;
use OCA\Theming\Util;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ITempManager;
use OCP\IURLGenerator;
use Test\TestCase;

class ThemingControllerTest extends TestCase {
	/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
	private $request;
	/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
	private $config;
	/** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
	private $themingDefaults;
	/** @var Util */
	private $util;
	/** @var \OCP\AppFramework\Utility\ITimeFactory */
	private $timeFactory;
	/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
	private $l10n;
	/** @var ThemingController */
	private $themingController;
	/** @var ITempManager */
	private $tempManager;
	/** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
	private $appManager;
	/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
	private $appData;
	/** @var ImageManager|\PHPUnit_Framework_MockObject_MockObject */
	private $imageManager;
	/** @var SCSSCacher */
	private $scssCacher;
	/** @var IURLGenerator */
	private $urlGenerator;

	protected function setUp(): void {
		$this->request = $this->createMock(IRequest::class);
		$this->config = $this->createMock(IConfig::class);
		$this->themingDefaults = $this->createMock(ThemingDefaults::class);
		$this->l10n = $this->createMock(L10N::class);
		$this->appData = $this->createMock(IAppData::class);
		$this->appManager = $this->createMock(IAppManager::class);
		$this->util = new Util($this->config, $this->appManager, $this->appData);
		$this->tempManager = \OC::$server->getTempManager();
		$this->scssCacher = $this->createMock(SCSSCacher::class);
		$this->urlGenerator = $this->createMock(IURLGenerator::class);
		$this->imageManager = $this->createMock(ImageManager::class);

		$this->timeFactory = $this->createMock(ITimeFactory::class);
		$this->timeFactory->expects($this->any())
			->method('getTime')
			->willReturn(123);

		$this->overwriteService(ITimeFactory::class, $this->timeFactory);

		$this->themingController = new ThemingController(
			'theming',
			$this->request,
			$this->config,
			$this->themingDefaults,
			$this->util,
			$this->l10n,
			$this->tempManager,
			$this->appData,
			$this->scssCacher,
			$this->urlGenerator,
			$this->appManager,
			$this->imageManager
		);

		parent::setUp();
	}

	public function dataUpdateStylesheetSuccess() {
		return [
			['name', str_repeat('a', 250), 'Saved'],
			['url', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
			['slogan', str_repeat('a', 500), 'Saved'],
			['color', '#0082c9', 'Saved'],
			['color', '#0082C9', 'Saved'],
			['color', '#0082C9', 'Saved'],
			['imprintUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
			['privacyUrl', 'https://nextcloud.com/' . str_repeat('a', 478), 'Saved'],
		];
	}

	/**
	 * @dataProvider dataUpdateStylesheetSuccess
	 *
	 * @param string $setting
	 * @param string $value
	 * @param string $message
	 */
	public function testUpdateStylesheetSuccess($setting, $value, $message) {
		$this->themingDefaults
			->expects($this->once())
			->method('set')
			->with($setting, $value);
		$this->l10n
			->expects($this->once())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));
		$this->scssCacher
			->expects($this->once())
			->method('getCachedSCSS')
			->with('core', '/core/css/css-variables.scss')
			->willReturn('/core/css/someHash-css-variables.scss');
		$this->urlGenerator
			->expects($this->once())
			->method('linkTo')
			->with('', '/core/css/someHash-css-variables.scss')
			->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => $message,
						'serverCssUrl' => '/nextcloudWebroot/core/css/someHash-css-variables.scss',
					],
				'status' => 'success',
			]
		);
		$this->assertEquals($expected, $this->themingController->updateStylesheet($setting, $value));
	}

	public function dataUpdateStylesheetError() {
		return [
			['name', str_repeat('a', 251), 'The given name is too long'],
			['url', 'http://example.com/' . str_repeat('a', 501), 'The given web address is too long'],
			['url', str_repeat('a', 501), 'The given web address is not a valid URL'],
			['url', 'javascript:alert(1)', 'The given web address is not a valid URL'],
			['slogan', str_repeat('a', 501), 'The given slogan is too long'],
			['color', '0082C9', 'The given color is invalid'],
			['color', '#0082Z9', 'The given color is invalid'],
			['color', 'Nextcloud', 'The given color is invalid'],
			['imprintUrl', '0082C9', 'The given legal notice address is not a valid URL'],
			['imprintUrl', '0082C9', 'The given legal notice address is not a valid URL'],
			['imprintUrl', 'javascript:foo', 'The given legal notice address is not a valid URL'],
			['privacyUrl', '#0082Z9', 'The given privacy policy address is not a valid URL'],
		];
	}

	/**
	 * @dataProvider dataUpdateStylesheetError
	 *
	 * @param string $setting
	 * @param string $value
	 * @param string $message
	 */
	public function testUpdateStylesheetError($setting, $value, $message) {
		$this->themingDefaults
			->expects($this->never())
			->method('set')
			->with($setting, $value);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => $message,
					],
				'status' => 'error',
			],
			Http::STATUS_BAD_REQUEST
		);
		$this->assertEquals($expected, $this->themingController->updateStylesheet($setting, $value));
	}

	public function testUpdateLogoNoData() {
		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('logo');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn(null);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => 'No file uploaded',
					],
				'status' => 'failure',
			],
			Http::STATUS_UNPROCESSABLE_ENTITY
		);

		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	/**
	 * Checks that trying to upload an SVG favicon without imagemagick
	 * results in an unsupported media type response.
	 *
	 * @test
	 * @return void
	 */
	public function testUploadSVGFaviconWithoutImagemagick() {
		$this->imageManager
			->method('shouldReplaceIcons')
			->willReturn(false);

		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('favicon');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn([
				'tmp_name' => __DIR__  . '/../../../../tests/data/testimagelarge.svg',
				'type' => 'image/svg',
				'name' => 'testimagelarge.svg',
				'error' => 0,
			]);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData
			->expects($this->once())
			->method('getFolder')
			->with('images')
			->willReturn($folder);

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => 'Unsupported image type',
					],
				'status' => 'failure'
			],
			Http::STATUS_UNPROCESSABLE_ENTITY
		);

		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	public function testUpdateLogoInvalidMimeType() {
		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('logo');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn([
				'tmp_name' => __DIR__  . '/../../../../tests/data/lorem.txt',
				'type' => 'application/pdf',
				'name' => 'logo.pdf',
				'error' => 0,
			]);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData
			->expects($this->once())
			->method('getFolder')
			->with('images')
			->willReturn($folder);

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => 'Unsupported image type',
					],
				'status' => 'failure'
			],
			Http::STATUS_UNPROCESSABLE_ENTITY
		);

		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	public function dataUpdateImages() {
		return [
			['image/jpeg', false],
			['image/jpeg', true],
			['image/gif'],
			['image/png'],
			['image/svg+xml'],
			['image/svg']
		];
	}

	/** @dataProvider dataUpdateImages */
	public function testUpdateLogoNormalLogoUpload($mimeType, $folderExists=true) {
		$tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . '/logo.svg';
		$destination = \OC::$server->getTempManager()->getTemporaryFolder();

		touch($tmpLogo);
		copy(__DIR__ . '/../../../../tests/data/testimage.png', $tmpLogo);
		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('logo');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn([
				'tmp_name' => $tmpLogo,
				'type' => $mimeType,
				'name' => 'logo.svg',
				'error' => 0,
			]);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));


		$file = $this->createMock(ISimpleFile::class);
		$folder = $this->createMock(ISimpleFolder::class);
		if($folderExists) {
			$this->appData
				->expects($this->once())
				->method('getFolder')
				->with('images')
				->willReturn($folder);
		} else {
			$this->appData
				->expects($this->at(0))
				->method('getFolder')
				->with('images')
				->willThrowException(new NotFoundException());
			$this->appData
				->expects($this->at(1))
				->method('newFolder')
				->with('images')
				->willReturn($folder);
		}
		$folder->expects($this->once())
			->method('newFile')
			->with('logo')
			->willReturn($file);
		$this->urlGenerator->expects($this->once())
			->method('linkTo')
			->willReturn('serverCss');
		$this->imageManager->expects($this->once())
			->method('getImageUrl')
			->with('logo')
			->willReturn('imageUrl');
		$expected = new DataResponse(
			[
				'data' =>
					[
						'name' => 'logo.svg',
						'message' => 'Saved',
						'url' => 'imageUrl',
						'serverCssUrl' => 'serverCss'
					],
				'status' => 'success'
			]
		);

		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	/** @dataProvider dataUpdateImages */
	public function testUpdateLogoLoginScreenUpload($folderExists) {
		$tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . 'logo.png';

		touch($tmpLogo);
		copy(__DIR__ . '/../../../../tests/data/desktopapp.png', $tmpLogo);
		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('background');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn([
				'tmp_name' => $tmpLogo,
				'type' => 'image/svg+xml',
				'name' => 'logo.svg',
				'error' => 0,
			]);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$file = $this->createMock(ISimpleFile::class);
		$folder = $this->createMock(ISimpleFolder::class);
		if($folderExists) {
			$this->appData
				->expects($this->once())
				->method('getFolder')
				->with('images')
				->willReturn($folder);
		} else {
			$this->appData
				->expects($this->at(0))
				->method('getFolder')
				->with('images')
				->willThrowException(new NotFoundException());
			$this->appData
				->expects($this->at(1))
				->method('newFolder')
				->with('images')
				->willReturn($folder);
		}
		$folder->expects($this->once())
			->method('newFile')
			->with('background')
			->willReturn($file);

		$this->urlGenerator->expects($this->once())
			->method('linkTo')
			->willReturn('serverCss');
		$this->imageManager->expects($this->once())
			->method('getImageUrl')
			->with('background')
			->willReturn('imageUrl');
		$expected = new DataResponse(
			[
				'data' =>
					[
						'name' => 'logo.svg',
						'message' => 'Saved',
						'url' => 'imageUrl',
						'serverCssUrl' => 'serverCss'
					],
				'status' => 'success'
			]
		);
		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	public function testUpdateLogoLoginScreenUploadWithInvalidImage() {
		$tmpLogo = \OC::$server->getTempManager()->getTemporaryFolder() . '/logo.svg';

		touch($tmpLogo);
		file_put_contents($tmpLogo, file_get_contents(__DIR__  . '/../../../../tests/data/data.zip'));
		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('logo');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn([
				'tmp_name' => $tmpLogo,
				'type' => 'foobar',
				'name' => 'logo.svg',
				'error' => 0,
			]);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData
			->expects($this->once())
			->method('getFolder')
			->with('images')
			->willReturn($folder);

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => 'Unsupported image type',
					],
				'status' => 'failure'
			],
			Http::STATUS_UNPROCESSABLE_ENTITY
		);
		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	public function dataPhpUploadErrors() {
		return [
			[UPLOAD_ERR_INI_SIZE, 'The uploaded file exceeds the upload_max_filesize directive in php.ini'],
			[UPLOAD_ERR_FORM_SIZE, 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'],
			[UPLOAD_ERR_PARTIAL, 'The file was only partially uploaded'],
			[UPLOAD_ERR_NO_FILE, 'No file was uploaded'],
			[UPLOAD_ERR_NO_TMP_DIR, 'Missing a temporary folder'],
			[UPLOAD_ERR_CANT_WRITE, 'Could not write file to disk'],
			[UPLOAD_ERR_EXTENSION, 'A PHP extension stopped the file upload'],
		];
	}

	/**
	 * @dataProvider dataPhpUploadErrors
	 */
	public function testUpdateLogoLoginScreenUploadWithInvalidImageUpload($error, $expectedErrorMessage) {
		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('background');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn([
				'tmp_name' => '',
				'type' => 'image/svg+xml',
				'name' => 'logo.svg',
				'error' => $error,
			]);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => $expectedErrorMessage,
					],
				'status' => 'failure'
			],
			Http::STATUS_UNPROCESSABLE_ENTITY
		);
		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	/**
	 * @dataProvider dataPhpUploadErrors
	 */
	public function testUpdateLogoUploadWithInvalidImageUpload($error, $expectedErrorMessage) {
		$this->request
			->expects($this->at(0))
			->method('getParam')
			->with('key')
			->willReturn('background');
		$this->request
			->expects($this->at(1))
			->method('getUploadedFile')
			->with('image')
			->willReturn([
				'tmp_name' => '',
				'type' => 'text/svg',
				'name' => 'logo.svg',
				'error' => $error,
			]);
		$this->l10n
			->expects($this->any())
			->method('t')
			->will($this->returnCallback(function($str) {
				return $str;
			}));

		$expected = new DataResponse(
			[
				'data' =>
					[
						'message' => $expectedErrorMessage
					],
				'status' => 'failure'
			],
			Http::STATUS_UNPROCESSABLE_ENTITY
		);
		$this->assertEquals($expected, $this->themingController->uploadImage());
	}

	public function testUndo() {
		$this->l10n
			->expects($this->once())
			->method('t')
			->with('Saved')
			->willReturn('Saved');
		$this->themingDefaults
			->expects($this->once())
			->method('undo')
			->with('MySetting')
			->willReturn('MyValue');
		$this->scssCacher
			->expects($this->once())
			->method('getCachedSCSS')
			->with('core', '/core/css/css-variables.scss')
			->willReturn('/core/css/someHash-css-variables.scss');
		$this->urlGenerator
			->expects($this->once())
			->method('linkTo')
			->with('', '/core/css/someHash-css-variables.scss')
			->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');

		$expected = new DataResponse(
			[
				'data' =>
					[
						'value' => 'MyValue',
						'message' => 'Saved',
						'serverCssUrl' => '/nextcloudWebroot/core/css/someHash-css-variables.scss',
					],
				'status' => 'success'
			]
		);
		$this->assertEquals($expected, $this->themingController->undo('MySetting'));
	}

	public function dataUndoDelete() {
		return [
			[ 'backgroundMime', 'background' ],
			[ 'logoMime', 'logo' ]
		];
	}

	/** @dataProvider dataUndoDelete */
	public function testUndoDelete($value, $filename) {
		$this->l10n
			->expects($this->once())
			->method('t')
			->with('Saved')
			->willReturn('Saved');
		$this->themingDefaults
			->expects($this->once())
			->method('undo')
			->with($value)
			->willReturn($value);
		$this->scssCacher
			->expects($this->once())
			->method('getCachedSCSS')
			->with('core', '/core/css/css-variables.scss')
			->willReturn('/core/css/someHash-css-variables.scss');
		$this->urlGenerator
			->expects($this->once())
			->method('linkTo')
			->with('', '/core/css/someHash-css-variables.scss')
			->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');
		$this->imageManager->expects($this->once())
			->method('delete')
			->with($filename);

		$expected = new DataResponse(
			[
				'data' =>
					[
						'value' => $value,
						'message' => 'Saved',
						'serverCssUrl' => '/nextcloudWebroot/core/css/someHash-css-variables.scss',
					],
				'status' => 'success'
			]
		);
		$this->assertEquals($expected, $this->themingController->undo($value));
	}



	public function testGetLogoNotExistent() {
		$this->imageManager->method('getImage')
			->with($this->equalTo('logo'))
			->willThrowException(new NotFoundException());

		$expected = new Http\NotFoundResponse();
		$this->assertEquals($expected, $this->themingController->getImage('logo'));
	}

	public function testGetLogo() {
		$file = $this->createMock(ISimpleFile::class);
		$this->imageManager->expects($this->once())
			->method('getImage')
			->willReturn($file);
		$this->config
			->expects($this->any())
			->method('getAppValue')
			->with('theming', 'logoMime', '')
			->willReturn('text/svg');

		@$expected = new Http\FileDisplayResponse($file);
		$expected->cacheFor(3600);
		$expected->addHeader('Content-Type', 'text/svg');
		$expected->addHeader('Content-Disposition', 'attachment; filename="logo"');
		$csp = new Http\ContentSecurityPolicy();
		$csp->allowInlineStyle();
		$expected->setContentSecurityPolicy($csp);
		@$this->assertEquals($expected, $this->themingController->getImage('logo'));
	}


	public function testGetLoginBackgroundNotExistent() {
		$this->imageManager->method('getImage')
			->with($this->equalTo('background'))
			->willThrowException(new NotFoundException());
		$expected = new Http\NotFoundResponse();
		$this->assertEquals($expected, $this->themingController->getImage('background'));
	}

	public function testGetLoginBackground() {
		$file = $this->createMock(ISimpleFile::class);
		$this->imageManager->expects($this->once())
			->method('getImage')
			->willReturn($file);

		$this->config
			->expects($this->any())
			->method('getAppValue')
			->with('theming', 'backgroundMime', '')
			->willReturn('image/png');

		@$expected = new Http\FileDisplayResponse($file);
		$expected->cacheFor(3600);
		$expected->addHeader('Content-Type', 'image/png');
		$expected->addHeader('Content-Disposition', 'attachment; filename="background"');
		$csp = new Http\ContentSecurityPolicy();
		$csp->allowInlineStyle();
		$expected->setContentSecurityPolicy($csp);
		@$this->assertEquals($expected, $this->themingController->getImage('background'));
	}


	public function testGetStylesheet() {
		$this->appManager->expects($this->once())->method('getAppPath')->with('theming')->willReturn(\OC::$SERVERROOT . '/theming');
		$file = $this->createMock(ISimpleFile::class);
		$file->expects($this->any())->method('getName')->willReturn('theming.css');
		$file->expects($this->any())->method('getContent')->willReturn('compiled');
		$this->scssCacher->expects($this->once())->method('process')->willReturn(true);
		$this->scssCacher->expects($this->once())->method('getCachedCSS')->willReturn($file);

		$response = new Http\FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
		$response->cacheFor(86400);

		$actual = $this->themingController->getStylesheet();
		$this->assertEquals($response, $actual);
	}

	public function testGetStylesheetFails() {
		$this->appManager->expects($this->once())->method('getAppPath')->with('theming')->willReturn(\OC::$SERVERROOT . '/theming');
		$file = $this->createMock(ISimpleFile::class);
		$file->expects($this->any())->method('getName')->willReturn('theming.css');
		$file->expects($this->any())->method('getContent')->willReturn('compiled');
		$this->scssCacher->expects($this->once())->method('process')->willReturn(true);
		$this->scssCacher->expects($this->once())->method('getCachedCSS')->willThrowException(new NotFoundException());
		$response = new Http\NotFoundResponse();

		$actual = $this->themingController->getStylesheet();
		$this->assertEquals($response, $actual);
	}

	public function testGetStylesheetOutsideServerroot() {
		$this->appManager->expects($this->once())->method('getAppPath')->with('theming')->willReturn('/outside/serverroot/theming');
		$file = $this->createMock(ISimpleFile::class);
		$file->expects($this->any())->method('getName')->willReturn('theming.css');
		$file->expects($this->any())->method('getContent')->willReturn('compiled');
		$this->scssCacher->expects($this->once())->method('process')->with('/outside/serverroot/theming', 'css/theming.scss', 'theming')->willReturn(true);
		$this->scssCacher->expects($this->once())->method('getCachedCSS')->willReturn($file);

		$response = new Http\FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
		$response->cacheFor(86400);

		$actual = $this->themingController->getStylesheet();
		$this->assertEquals($response, $actual);
	}

	public function testGetJavascript() {
		$this->themingDefaults
			->expects($this->at(0))
			->method('getName')
			->willReturn("");
		$this->themingDefaults
			->expects($this->at(1))
			->method('getBaseUrl')
			->willReturn("");
		$this->themingDefaults
			->expects($this->at(2))
			->method('getSlogan')
			->willReturn("");
		$this->themingDefaults
			->expects($this->at(3))
			->method('getColorPrimary')
			->willReturn("#000");


		$expectedResponse = '(function() {
	OCA.Theming = {
		name: "",
		url: "",
		slogan: "",
		color: "#000",
		imprintUrl: null,
		privacyUrl: null,
		inverted: false,
		cacheBuster: null
	};
})();';
		$expected = new Http\DataDownloadResponse($expectedResponse, 'javascript', 'text/javascript');
		$expected->cacheFor(3600);
		@$this->assertEquals($expected, $this->themingController->getJavascript());
	}
	public function testGetJavascriptInverted() {
		$this->themingDefaults
			->expects($this->at(0))
			->method('getName')
			->willReturn("Nextcloud");
		$this->themingDefaults
			->expects($this->at(1))
			->method('getBaseUrl')
			->willReturn("nextcloudurl");
		$this->themingDefaults
			->expects($this->at(2))
			->method('getSlogan')
			->willReturn("awesome");
		$this->themingDefaults
			->expects($this->any())
			->method('getColorPrimary')
			->willReturn("#ffffff");

		$expectedResponse = '(function() {
	OCA.Theming = {
		name: "Nextcloud",
		url: "nextcloudurl",
		slogan: "awesome",
		color: "#ffffff",
		imprintUrl: null,
		privacyUrl: null,
		inverted: true,
		cacheBuster: null
	};
})();';
		$expected = new Http\DataDownloadResponse($expectedResponse, 'javascript', 'text/javascript');
		$expected->cacheFor(3600);
		@$this->assertEquals($expected, $this->themingController->getJavascript());
	}

	public function testGetManifest() {
		$this->config
			->expects($this->once())
			->method('getAppValue')
			->with('theming', 'cachebuster', '0')
			->willReturn('0');
		$this->themingDefaults
			->expects($this->any())
			->method('getName')
			->willReturn('Nextcloud');
		$this->urlGenerator
			->expects($this->at(0))
			->method('getBaseUrl')
			->willReturn('localhost');
		$this->urlGenerator
			->expects($this->at(1))
			->method('linkToRoute')
			->with('theming.Icon.getTouchIcon', ['app' => 'core'])
			->willReturn('touchicon');
		$this->urlGenerator
			->expects($this->at(2))
			->method('linkToRoute')
			->with('theming.Icon.getFavicon', ['app' => 'core'])
			->willReturn('favicon');
		$response = new Http\JSONResponse([
			'name' => 'Nextcloud',
			'start_url' => 'localhost',
			'icons' =>
				[
					[
						'src' => 'touchicon?v=0',
						'type'=> 'image/png',
						'sizes'=> '128x128'
					],
					[
						'src' => 'favicon?v=0',
						'type' => 'image/svg+xml',
						'sizes' => '16x16'
					]
				],
			'display' => 'standalone'
		]);
		$response->cacheFor(3600);
		$this->assertEquals($response, $this->themingController->getManifest('core'));
	}

}