<commit_before><?php
/**
 * @copyright Copyright (c) 2017 <NAME> <<EMAIL>>
 *
 * @author <NAME> <<EMAIL>>
 *
 * @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 Test\Template;

use OC\Files\AppData\Factory;
use OC\Template\SCSSCacher;
use OCA\Theming\ThemingDefaults;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICache;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IURLGenerator;

class SCSSCacherTest extends \Test\TestCase {
	/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
	protected $logger;
	/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
	protected $appData;
	/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
	protected $urlGenerator;
	/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
	protected $config;
	/** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
	protected $themingDefaults;
	/** @var SCSSCacher */
	protected $scssCacher;
	/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
	protected $depsCache;

	protected function setUp() {
		parent::setUp();
		$this->logger = $this->createMock(ILogger::class);
		$this->appData = $this->createMock(IAppData::class);
		/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
		$factory = $this->createMock(Factory::class);
		$factory->method('get')->with('css')->willReturn($this->appData);
		$this->urlGenerator = $this->createMock(IURLGenerator::class);
		$this->config = $this->createMock(IConfig::class);
		$this->depsCache = $this->createMock(ICache::class);
		$this->themingDefaults = $this->createMock(ThemingDefaults::class);
		$this->scssCacher = new SCSSCacher(
			$this->logger,
			$factory,
			$this->urlGenerator,
			$this->config,
			$this->themingDefaults,
			\OC::$SERVERROOT,
			$this->depsCache
		);
		$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);

		$this->urlGenerator->expects($this->any())
			->method('getBaseUrl')
			->willReturn('http://localhost/nextcloud');
	}

	public function testProcessUncachedFileNoAppDataFolder() {
		$folder = $this->createMock(ISimpleFolder::class);
		$file = $this->createMock(ISimpleFile::class);
		$file->expects($this->any())->method('getSize')->willReturn(1);

		$this->appData->expects($this->once())->method('getFolder')->with('core')->willThrowException(new NotFoundException());
		$this->appData->expects($this->once())->method('newFolder')->with('core')->willReturn($folder);

		$fileDeps = $this->createMock(ISimpleFile::class);
		$gzfile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';

		$folder->method('getFile')
			->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
				if ($path === $filePrefix.'styles.css') {
					return $file;
				} else if ($path === $filePrefix.'styles.css.deps') {
					throw new NotFoundException();
				} else if ($path === $filePrefix.'styles.css.gzip') {
					return $gzfile;
				} else {
					$this->fail();
				}
			}));
		$folder->expects($this->once())
			->method('newFile')
			->with($filePrefix.'styles.css.deps')
			->willReturn($fileDeps);

		$this->urlGenerator->expects($this->once())
			->method('getBaseUrl')
			->willReturn('http://localhost/nextcloud');

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testProcessUncachedFile() {
		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
		$file = $this->createMock(ISimpleFile::class);
		$file->expects($this->any())->method('getSize')->willReturn(1);
		$fileDeps = $this->createMock(ISimpleFile::class);
		$gzfile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';

		$folder->method('getFile')
			->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
				if ($path === $filePrefix.'styles.css') {
					return $file;
				} else if ($path === $filePrefix.'styles.css.deps') {
					throw new NotFoundException();
				} else if ($path === $filePrefix.'styles.css.gzip') {
					return $gzfile;
				}else {
					$this->fail();
				}
			}));
		$folder->expects($this->once())
			->method('newFile')
			->with($filePrefix.'styles.css.deps')
			->willReturn($fileDeps);

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testProcessCachedFile() {
		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
		$file = $this->createMock(ISimpleFile::class);
		$fileDeps = $this->createMock(ISimpleFile::class);
		$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
		$gzFile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';

		$folder->method('getFile')
			->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
				if ($name === $filePrefix.'styles.css') {
					return $file;
				} else if ($name === $filePrefix.'styles.css.deps') {
					return $fileDeps;
				} else if ($name === $filePrefix.'styles.css.gzip') {
					return $gzFile;
				}
				$this->fail();
			}));

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testProcessCachedFileMemcache() {
		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData->expects($this->once())
			->method('getFolder')
			->with('core')
			->willReturn($folder);
		$folder->method('getName')
			->willReturn('core');

		$file = $this->createMock(ISimpleFile::class);

		$fileDeps = $this->createMock(ISimpleFile::class);
		$fileDeps->expects($this->any())->method('getSize')->willReturn(1);

		$gzFile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
		$folder->method('getFile')
			->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
				if ($name === $filePrefix.'styles.css') {
					return $file;
				} else if ($name === $filePrefix.'styles.css.deps') {
					return $fileDeps;
				} else if ($name === $filePrefix.'styles.css.gzip') {
					return $gzFile;
				}
				$this->fail();
			}));

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testIsCachedNoFile() {
		$fileNameCSS = "styles.css";
		$folder = $this->createMock(ISimpleFolder::class);

		$folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willThrowException(new NotFoundException());
		$actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
		$this->assertFalse($actual);<commit_msg>Fix tests<commit_after><?php
/**
 * @copyright Copyright (c) 2017 <NAME> <<EMAIL>>
 *
 * @author <NAME> <<EMAIL>>
 *
 * @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 Test\Template;

use OC\Files\AppData\Factory;
use OC\Template\SCSSCacher;
use OCA\Theming\ThemingDefaults;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICache;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IURLGenerator;

class SCSSCacherTest extends \Test\TestCase {
	/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
	protected $logger;
	/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
	protected $appData;
	/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
	protected $urlGenerator;
	/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
	protected $config;
	/** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
	protected $themingDefaults;
	/** @var SCSSCacher */
	protected $scssCacher;
	/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
	protected $depsCache;

	protected function setUp() {
		parent::setUp();
		$this->logger = $this->createMock(ILogger::class);
		$this->appData = $this->createMock(IAppData::class);
		/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
		$factory = $this->createMock(Factory::class);
		$factory->method('get')->with('css')->willReturn($this->appData);
		$this->urlGenerator = $this->createMock(IURLGenerator::class);
		$this->config = $this->createMock(IConfig::class);
		$this->depsCache = $this->createMock(ICache::class);
		$this->themingDefaults = $this->createMock(ThemingDefaults::class);
		$this->scssCacher = new SCSSCacher(
			$this->logger,
			$factory,
			$this->urlGenerator,
			$this->config,
			$this->themingDefaults,
			\OC::$SERVERROOT,
			$this->depsCache
		);
		$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);

		$this->urlGenerator->expects($this->any())
			->method('getBaseUrl')
			->willReturn('http://localhost/nextcloud');
	}

	public function testProcessUncachedFileNoAppDataFolder() {
		$folder = $this->createMock(ISimpleFolder::class);
		$file = $this->createMock(ISimpleFile::class);
		$file->expects($this->any())->method('getSize')->willReturn(1);

		$this->appData->expects($this->once())->method('getFolder')->with('core')->willThrowException(new NotFoundException());
		$this->appData->expects($this->once())->method('newFolder')->with('core')->willReturn($folder);
		$this->appData->method('getDirectoryListing')->willReturn([]);

		$fileDeps = $this->createMock(ISimpleFile::class);
		$gzfile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';

		$folder->method('getFile')
			->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
				if ($path === $filePrefix.'styles.css') {
					return $file;
				} else if ($path === $filePrefix.'styles.css.deps') {
					throw new NotFoundException();
				} else if ($path === $filePrefix.'styles.css.gzip') {
					return $gzfile;
				} else {
					$this->fail();
				}
			}));
		$folder->expects($this->once())
			->method('newFile')
			->with($filePrefix.'styles.css.deps')
			->willReturn($fileDeps);

		$this->urlGenerator->expects($this->once())
			->method('getBaseUrl')
			->willReturn('http://localhost/nextcloud');

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testProcessUncachedFile() {
		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
		$this->appData->method('getDirectoryListing')->willReturn([]);
		$file = $this->createMock(ISimpleFile::class);
		$file->expects($this->any())->method('getSize')->willReturn(1);
		$fileDeps = $this->createMock(ISimpleFile::class);
		$gzfile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';

		$folder->method('getFile')
			->will($this->returnCallback(function($path) use ($file, $gzfile, $filePrefix) {
				if ($path === $filePrefix.'styles.css') {
					return $file;
				} else if ($path === $filePrefix.'styles.css.deps') {
					throw new NotFoundException();
				} else if ($path === $filePrefix.'styles.css.gzip') {
					return $gzfile;
				}else {
					$this->fail();
				}
			}));
		$folder->expects($this->once())
			->method('newFile')
			->with($filePrefix.'styles.css.deps')
			->willReturn($fileDeps);

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testProcessCachedFile() {
		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData->expects($this->once())->method('getFolder')->with('core')->willReturn($folder);
		$this->appData->method('getDirectoryListing')->willReturn([]);
		$file = $this->createMock(ISimpleFile::class);
		$fileDeps = $this->createMock(ISimpleFile::class);
		$fileDeps->expects($this->any())->method('getSize')->willReturn(1);
		$gzFile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';

		$folder->method('getFile')
			->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
				if ($name === $filePrefix.'styles.css') {
					return $file;
				} else if ($name === $filePrefix.'styles.css.deps') {
					return $fileDeps;
				} else if ($name === $filePrefix.'styles.css.gzip') {
					return $gzFile;
				}
				$this->fail();
			}));

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testProcessCachedFileMemcache() {
		$folder = $this->createMock(ISimpleFolder::class);
		$this->appData->expects($this->once())
			->method('getFolder')
			->with('core')
			->willReturn($folder);
		$folder->method('getName')
			->willReturn('core');
		$this->appData->method('getDirectoryListing')->willReturn([]);

		$file = $this->createMock(ISimpleFile::class);

		$fileDeps = $this->createMock(ISimpleFile::class);
		$fileDeps->expects($this->any())->method('getSize')->willReturn(1);

		$gzFile = $this->createMock(ISimpleFile::class);
		$filePrefix = substr(md5('http://localhost/nextcloud'), 0, 8) . '-';
		$folder->method('getFile')
			->will($this->returnCallback(function($name) use ($file, $fileDeps, $gzFile, $filePrefix) {
				if ($name === $filePrefix.'styles.css') {
					return $file;
				} else if ($name === $filePrefix.'styles.css.deps') {
					return $fileDeps;
				} else if ($name === $filePrefix.'styles.css.gzip') {
					return $gzFile;
				}
				$this->fail();
			}));

		$actual = $this->scssCacher->process(\OC::$SERVERROOT, '/core/css/styles.scss', 'core');
		$this->assertTrue($actual);
	}

	public function testIsCachedNoFile() {
		$fileNameCSS = "styles.css";
		$folder = $this->createMock(ISimpleFolder::class);

		$folder->expects($this->at(0))->method('getFile')->with($fileNameCSS)->willThrowException(new NotFoundException());
		$actual = self::invokePrivate($this->scssCacher, 'isCached', [$fileNameCSS, $folder]);
		$this->assertFalse($actual);
{'fraction_non_alphanumeric': 0.1957624968282162, 'fraction_numerical': 0.0029180411063181935, 'mean_word_length': 4.935617469879518, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 325, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3853644', 'n_tokens_mistral': 5742, 'n_tokens_neox': 5181, 'n_words': 876}